blob: 6d327b7aa813a4ef69263a473b6881204fd78004 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/string.h>
83#include <linux/mm.h>
84#include <linux/socket.h>
85#include <linux/sockios.h>
86#include <linux/errno.h>
87#include <linux/interrupt.h>
88#include <linux/if_ether.h>
89#include <linux/netdevice.h>
90#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070091#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/skbuff.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070093#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040094#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020095#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -080097#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700101#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200103#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000105#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <linux/highmem.h>
107#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <linux/netpoll.h>
110#include <linux/rcupdate.h>
111#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500114#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700115#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700116#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700117#include <linux/ctype.h>
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700118#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700119#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700120#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700121#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700122#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700123#include <linux/ipv6.h>
124#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700125#include <linux/jhash.h>
126#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700127#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900128#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900129#include <trace/events/skb.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700130#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000131#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100132#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300133#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700134#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100135#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400136#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800137#include <linux/hrtimer.h>
Daniel Borkmann357b6cc2020-03-18 10:33:22 +0100138#include <linux/netfilter_ingress.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530139#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200140#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200141#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200142#include <linux/net_namespace.h>
Paolo Abeniaaa5d902018-12-14 11:51:58 +0100143#include <linux/indirect_call_wrapper.h>
Jiri Pirkoaf3836d2019-03-28 13:56:37 +0100144#include <net/devlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700146#include "net-sysfs.h"
147
Herbert Xud565b0a2008-12-15 23:38:52 -0800148#define MAX_GRO_SKBS 8
149
Herbert Xu5d38a072009-01-04 16:13:40 -0800150/* This should be increased if a protocol with a bigger head is added. */
151#define GRO_MAX_HEAD (MAX_HEADER + 128)
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000154static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000155struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
156struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000157static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000159static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700160static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700161 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000162static int call_netdevice_notifiers_extack(unsigned long val,
163 struct net_device *dev,
164 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200165static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700168 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * semaphore.
170 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800171 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700174 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * actual updates. This allows pure readers to access the list even
176 * while a writer is preparing to update it.
177 *
178 * To put it another way, dev_base_lock is held for writing only to
179 * protect against pure readers; the rtnl semaphore provides the
180 * protection against other writers.
181 *
182 * See, for example usages, register_netdevice() and
183 * unregister_netdevice(), which must be called with the rtnl
184 * semaphore held.
185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187EXPORT_SYMBOL(dev_base_lock);
188
Florian Westphal6c557002017-10-02 23:50:05 +0200189static DEFINE_MUTEX(ifalias_mutex);
190
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300191/* protects napi_hash addition/deletion and napi_gen_id */
192static DEFINE_SPINLOCK(napi_hash_lock);
193
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800194static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800195static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300196
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200197static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000198
Thomas Graf4e985ad2011-06-21 03:11:20 +0000199static inline void dev_base_seq_inc(struct net *net)
200{
tcharding643aa9c2017-02-09 17:56:05 +1100201 while (++net->dev_base_seq == 0)
202 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000203}
204
Eric W. Biederman881d9662007-09-17 11:56:21 -0700205static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700207 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000208
stephen hemminger08e98972009-11-10 07:20:34 +0000209 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Eric W. Biederman881d9662007-09-17 11:56:21 -0700212static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700214 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000217static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000218{
219#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000220 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000221#endif
222}
223
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000224static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000225{
226#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000227 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000228#endif
229}
230
Jiri Pirkoff927412019-09-30 11:48:15 +0200231static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
232 const char *name)
233{
234 struct netdev_name_node *name_node;
235
236 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
237 if (!name_node)
238 return NULL;
239 INIT_HLIST_NODE(&name_node->hlist);
240 name_node->dev = dev;
241 name_node->name = name;
242 return name_node;
243}
244
245static struct netdev_name_node *
246netdev_name_node_head_alloc(struct net_device *dev)
247{
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200248 struct netdev_name_node *name_node;
249
250 name_node = netdev_name_node_alloc(dev, dev->name);
251 if (!name_node)
252 return NULL;
253 INIT_LIST_HEAD(&name_node->list);
254 return name_node;
Jiri Pirkoff927412019-09-30 11:48:15 +0200255}
256
257static void netdev_name_node_free(struct netdev_name_node *name_node)
258{
259 kfree(name_node);
260}
261
262static void netdev_name_node_add(struct net *net,
263 struct netdev_name_node *name_node)
264{
265 hlist_add_head_rcu(&name_node->hlist,
266 dev_name_hash(net, name_node->name));
267}
268
269static void netdev_name_node_del(struct netdev_name_node *name_node)
270{
271 hlist_del_rcu(&name_node->hlist);
272}
273
274static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
275 const char *name)
276{
277 struct hlist_head *head = dev_name_hash(net, name);
278 struct netdev_name_node *name_node;
279
280 hlist_for_each_entry(name_node, head, hlist)
281 if (!strcmp(name_node->name, name))
282 return name_node;
283 return NULL;
284}
285
286static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
287 const char *name)
288{
289 struct hlist_head *head = dev_name_hash(net, name);
290 struct netdev_name_node *name_node;
291
292 hlist_for_each_entry_rcu(name_node, head, hlist)
293 if (!strcmp(name_node->name, name))
294 return name_node;
295 return NULL;
296}
297
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200298int netdev_name_node_alt_create(struct net_device *dev, const char *name)
299{
300 struct netdev_name_node *name_node;
301 struct net *net = dev_net(dev);
302
303 name_node = netdev_name_node_lookup(net, name);
304 if (name_node)
305 return -EEXIST;
306 name_node = netdev_name_node_alloc(dev, name);
307 if (!name_node)
308 return -ENOMEM;
309 netdev_name_node_add(net, name_node);
310 /* The node that holds dev->name acts as a head of per-device list. */
311 list_add_tail(&name_node->list, &dev->name_node->list);
312
313 return 0;
314}
315EXPORT_SYMBOL(netdev_name_node_alt_create);
316
317static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
318{
319 list_del(&name_node->list);
320 netdev_name_node_del(name_node);
321 kfree(name_node->name);
322 netdev_name_node_free(name_node);
323}
324
325int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
326{
327 struct netdev_name_node *name_node;
328 struct net *net = dev_net(dev);
329
330 name_node = netdev_name_node_lookup(net, name);
331 if (!name_node)
332 return -ENOENT;
Eric Dumazete08ad802020-02-14 07:53:53 -0800333 /* lookup might have found our primary name or a name belonging
334 * to another device.
335 */
336 if (name_node == dev->name_node || name_node->dev != dev)
337 return -EINVAL;
338
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200339 __netdev_name_node_alt_destroy(name_node);
340
341 return 0;
342}
343EXPORT_SYMBOL(netdev_name_node_alt_destroy);
344
345static void netdev_name_node_alt_flush(struct net_device *dev)
346{
347 struct netdev_name_node *name_node, *tmp;
348
349 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
350 __netdev_name_node_alt_destroy(name_node);
351}
352
Eric W. Biedermance286d32007-09-12 13:53:49 +0200353/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000354static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200355{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900356 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200357
358 ASSERT_RTNL();
359
360 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800361 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Jiri Pirkoff927412019-09-30 11:48:15 +0200362 netdev_name_node_add(net, dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000363 hlist_add_head_rcu(&dev->index_hlist,
364 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200365 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000366
367 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200368}
369
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000370/* Device list removal
371 * caller must respect a RCU grace period before freeing/reusing dev
372 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200373static void unlist_netdevice(struct net_device *dev)
374{
375 ASSERT_RTNL();
376
377 /* Unlink dev from the device chain */
378 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800379 list_del_rcu(&dev->dev_list);
Jiri Pirkoff927412019-09-30 11:48:15 +0200380 netdev_name_node_del(dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000381 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200382 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000383
384 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * Our notifier list
389 */
390
Alan Sternf07d5b92006-05-09 15:23:03 -0700391static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/*
394 * Device drivers call our routines to queue packets here. We empty the
395 * queue in the local softnet handler.
396 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700397
Eric Dumazet9958da02010-04-17 04:17:02 +0000398DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700399EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100402 *
403 * Protocol management and registration routines
404 *
405 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * Add a protocol ID to the list. Now that the input handler is
410 * smarter we can dispense with all the messy stuff that used to be
411 * here.
412 *
413 * BEWARE!!! Protocol handlers, mangling input packets,
414 * MUST BE last in hash buckets and checking protocol handlers
415 * MUST start from promiscuous ptype_all chain in net_bh.
416 * It is true now, do not change it.
417 * Explanation follows: if protocol handler, mangling packet, will
418 * be the first on list, it is not able to sense, that packet
419 * is cloned and should be copied-on-write, so that it will
420 * change it and subsequent readers will get broken packet.
421 * --ANK (980803)
422 */
423
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000424static inline struct list_head *ptype_head(const struct packet_type *pt)
425{
426 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800427 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000428 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800429 return pt->dev ? &pt->dev->ptype_specific :
430 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/**
434 * dev_add_pack - add packet handler
435 * @pt: packet type declaration
436 *
437 * Add a protocol handler to the networking stack. The passed &packet_type
438 * is linked into kernel lists and may not be freed until it has been
439 * removed from the kernel lists.
440 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900441 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * guarantee all CPU's that are in middle of receiving packets
443 * will see the new packet type (until the next received packet).
444 */
445
446void dev_add_pack(struct packet_type *pt)
447{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000448 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000450 spin_lock(&ptype_lock);
451 list_add_rcu(&pt->list, head);
452 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700454EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/**
457 * __dev_remove_pack - remove packet handler
458 * @pt: packet type declaration
459 *
460 * Remove a protocol handler that was previously added to the kernel
461 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
462 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900463 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
465 * The packet type might still be in use by receivers
466 * and must not be freed until after all the CPU's have gone
467 * through a quiescent state.
468 */
469void __dev_remove_pack(struct packet_type *pt)
470{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000471 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct packet_type *pt1;
473
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000474 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 list_for_each_entry(pt1, head, list) {
477 if (pt == pt1) {
478 list_del_rcu(&pt->list);
479 goto out;
480 }
481 }
482
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000483 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000485 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700487EXPORT_SYMBOL(__dev_remove_pack);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/**
490 * dev_remove_pack - remove packet handler
491 * @pt: packet type declaration
492 *
493 * Remove a protocol handler that was previously added to the kernel
494 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
495 * from the kernel lists and can be freed or reused once this function
496 * returns.
497 *
498 * This call sleeps to guarantee that no CPU is looking at the packet
499 * type after return.
500 */
501void dev_remove_pack(struct packet_type *pt)
502{
503 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 synchronize_net();
506}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700507EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Vlad Yasevich62532da2012-11-15 08:49:10 +0000509
510/**
511 * dev_add_offload - register offload handlers
512 * @po: protocol offload declaration
513 *
514 * Add protocol offload handlers to the networking stack. The passed
515 * &proto_offload is linked into kernel lists and may not be freed until
516 * it has been removed from the kernel lists.
517 *
518 * This call does not sleep therefore it can not
519 * guarantee all CPU's that are in middle of receiving packets
520 * will see the new offload handlers (until the next received packet).
521 */
522void dev_add_offload(struct packet_offload *po)
523{
David S. Millerbdef7de2015-06-01 14:56:09 -0700524 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000525
526 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700527 list_for_each_entry(elem, &offload_base, list) {
528 if (po->priority < elem->priority)
529 break;
530 }
531 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000532 spin_unlock(&offload_lock);
533}
534EXPORT_SYMBOL(dev_add_offload);
535
536/**
537 * __dev_remove_offload - remove offload handler
538 * @po: packet offload declaration
539 *
540 * Remove a protocol offload handler that was previously added to the
541 * kernel offload handlers by dev_add_offload(). The passed &offload_type
542 * is removed from the kernel lists and can be freed or reused once this
543 * function returns.
544 *
545 * The packet type might still be in use by receivers
546 * and must not be freed until after all the CPU's have gone
547 * through a quiescent state.
548 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800549static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000550{
551 struct list_head *head = &offload_base;
552 struct packet_offload *po1;
553
Eric Dumazetc53aa502012-11-16 08:08:23 +0000554 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000555
556 list_for_each_entry(po1, head, list) {
557 if (po == po1) {
558 list_del_rcu(&po->list);
559 goto out;
560 }
561 }
562
563 pr_warn("dev_remove_offload: %p not found\n", po);
564out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000565 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000566}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000567
568/**
569 * dev_remove_offload - remove packet offload handler
570 * @po: packet offload declaration
571 *
572 * Remove a packet offload handler that was previously added to the kernel
573 * offload handlers by dev_add_offload(). The passed &offload_type is
574 * removed from the kernel lists and can be freed or reused once this
575 * function returns.
576 *
577 * This call sleeps to guarantee that no CPU is looking at the packet
578 * type after return.
579 */
580void dev_remove_offload(struct packet_offload *po)
581{
582 __dev_remove_offload(po);
583
584 synchronize_net();
585}
586EXPORT_SYMBOL(dev_remove_offload);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100589 *
590 * Device Boot-time Settings Routines
591 *
592 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/* Boot time configuration table */
595static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
596
597/**
598 * netdev_boot_setup_add - add new setup entry
599 * @name: name of the device
600 * @map: configured settings for the device
601 *
602 * Adds new setup entry to the dev_boot_setup list. The function
603 * returns 0 on error and 1 on success. This is a generic routine to
604 * all netdevices.
605 */
606static int netdev_boot_setup_add(char *name, struct ifmap *map)
607{
608 struct netdev_boot_setup *s;
609 int i;
610
611 s = dev_boot_setup;
612 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
613 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
614 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff92008-07-01 19:57:19 -0700615 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 memcpy(&s[i].map, map, sizeof(s[i].map));
617 break;
618 }
619 }
620
621 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
622}
623
624/**
tcharding722c9a02017-02-09 17:56:04 +1100625 * netdev_boot_setup_check - check boot time settings
626 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *
tcharding722c9a02017-02-09 17:56:04 +1100628 * Check boot time settings for the device.
629 * The found settings are set for the device to be used
630 * later in the device probing.
631 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
633int netdev_boot_setup_check(struct net_device *dev)
634{
635 struct netdev_boot_setup *s = dev_boot_setup;
636 int i;
637
638 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
639 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff92008-07-01 19:57:19 -0700640 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100641 dev->irq = s[i].map.irq;
642 dev->base_addr = s[i].map.base_addr;
643 dev->mem_start = s[i].map.mem_start;
644 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 1;
646 }
647 }
648 return 0;
649}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700650EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652
653/**
tcharding722c9a02017-02-09 17:56:04 +1100654 * netdev_boot_base - get address from boot time settings
655 * @prefix: prefix for network device
656 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 *
tcharding722c9a02017-02-09 17:56:04 +1100658 * Check boot time settings for the base address of device.
659 * The found settings are set for the device to be used
660 * later in the device probing.
661 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663unsigned long netdev_boot_base(const char *prefix, int unit)
664{
665 const struct netdev_boot_setup *s = dev_boot_setup;
666 char name[IFNAMSIZ];
667 int i;
668
669 sprintf(name, "%s%d", prefix, unit);
670
671 /*
672 * If device already registered then return base of 1
673 * to indicate not to probe for this interface
674 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700675 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 1;
677
678 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
679 if (!strcmp(name, s[i].name))
680 return s[i].map.base_addr;
681 return 0;
682}
683
684/*
685 * Saves at boot time configured settings for any netdevice.
686 */
687int __init netdev_boot_setup(char *str)
688{
689 int ints[5];
690 struct ifmap map;
691
692 str = get_options(str, ARRAY_SIZE(ints), ints);
693 if (!str || !*str)
694 return 0;
695
696 /* Save settings */
697 memset(&map, 0, sizeof(map));
698 if (ints[0] > 0)
699 map.irq = ints[1];
700 if (ints[0] > 1)
701 map.base_addr = ints[2];
702 if (ints[0] > 2)
703 map.mem_start = ints[3];
704 if (ints[0] > 3)
705 map.mem_end = ints[4];
706
707 /* Add new entry to the list */
708 return netdev_boot_setup_add(str, &map);
709}
710
711__setup("netdev=", netdev_boot_setup);
712
713/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100714 *
715 * Device Interface Subroutines
716 *
717 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200720 * dev_get_iflink - get 'iflink' value of a interface
721 * @dev: targeted interface
722 *
723 * Indicates the ifindex the interface is linked to.
724 * Physical interfaces have the same 'ifindex' and 'iflink' values.
725 */
726
727int dev_get_iflink(const struct net_device *dev)
728{
729 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
730 return dev->netdev_ops->ndo_get_iflink(dev);
731
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200732 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200733}
734EXPORT_SYMBOL(dev_get_iflink);
735
736/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700737 * dev_fill_metadata_dst - Retrieve tunnel egress information.
738 * @dev: targeted interface
739 * @skb: The packet.
740 *
741 * For better visibility of tunnel traffic OVS needs to retrieve
742 * egress tunnel information for a packet. Following API allows
743 * user to get this info.
744 */
745int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
746{
747 struct ip_tunnel_info *info;
748
749 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
750 return -EINVAL;
751
752 info = skb_tunnel_info_unclone(skb);
753 if (!info)
754 return -ENOMEM;
755 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
756 return -EINVAL;
757
758 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
759}
760EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
761
762/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700764 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * @name: name to find
766 *
767 * Find an interface by name. Must be called under RTNL semaphore
768 * or @dev_base_lock. If the name is found a pointer to the device
769 * is returned. If the name is not found then %NULL is returned. The
770 * reference counters are not incremented so the caller must be
771 * careful with locks.
772 */
773
Eric W. Biederman881d9662007-09-17 11:56:21 -0700774struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jiri Pirkoff927412019-09-30 11:48:15 +0200776 struct netdev_name_node *node_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Jiri Pirkoff927412019-09-30 11:48:15 +0200778 node_name = netdev_name_node_lookup(net, name);
779 return node_name ? node_name->dev : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700781EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783/**
tcharding722c9a02017-02-09 17:56:04 +1100784 * dev_get_by_name_rcu - find a device by its name
785 * @net: the applicable net namespace
786 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000787 *
tcharding722c9a02017-02-09 17:56:04 +1100788 * Find an interface by name.
789 * If the name is found a pointer to the device is returned.
790 * If the name is not found then %NULL is returned.
791 * The reference counters are not incremented so the caller must be
792 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000793 */
794
795struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
796{
Jiri Pirkoff927412019-09-30 11:48:15 +0200797 struct netdev_name_node *node_name;
Eric Dumazet72c95282009-10-30 07:11:27 +0000798
Jiri Pirkoff927412019-09-30 11:48:15 +0200799 node_name = netdev_name_node_lookup_rcu(net, name);
800 return node_name ? node_name->dev : NULL;
Eric Dumazet72c95282009-10-30 07:11:27 +0000801}
802EXPORT_SYMBOL(dev_get_by_name_rcu);
803
804/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700806 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * @name: name to find
808 *
809 * Find an interface by name. This can be called from any
810 * context and does its own locking. The returned handle has
811 * the usage count incremented and the caller must use dev_put() to
812 * release it when it is no longer needed. %NULL is returned if no
813 * matching device is found.
814 */
815
Eric W. Biederman881d9662007-09-17 11:56:21 -0700816struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 struct net_device *dev;
819
Eric Dumazet72c95282009-10-30 07:11:27 +0000820 rcu_read_lock();
821 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (dev)
823 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000824 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return dev;
826}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700827EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829/**
830 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700831 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * @ifindex: index of device
833 *
834 * Search for an interface by index. Returns %NULL if the device
835 * is not found or a pointer to the device. The device has not
836 * had its reference counter increased so the caller must be careful
837 * about locking. The caller must hold either the RTNL semaphore
838 * or @dev_base_lock.
839 */
840
Eric W. Biederman881d9662007-09-17 11:56:21 -0700841struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700843 struct net_device *dev;
844 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Sasha Levinb67bfe02013-02-27 17:06:00 -0800846 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (dev->ifindex == ifindex)
848 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return NULL;
851}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700852EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000854/**
855 * dev_get_by_index_rcu - find a device by its ifindex
856 * @net: the applicable net namespace
857 * @ifindex: index of device
858 *
859 * Search for an interface by index. Returns %NULL if the device
860 * is not found or a pointer to the device. The device has not
861 * had its reference counter increased so the caller must be careful
862 * about locking. The caller must hold RCU lock.
863 */
864
865struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
866{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000867 struct net_device *dev;
868 struct hlist_head *head = dev_index_hash(net, ifindex);
869
Sasha Levinb67bfe02013-02-27 17:06:00 -0800870 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000871 if (dev->ifindex == ifindex)
872 return dev;
873
874 return NULL;
875}
876EXPORT_SYMBOL(dev_get_by_index_rcu);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879/**
880 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700881 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * @ifindex: index of device
883 *
884 * Search for an interface by index. Returns NULL if the device
885 * is not found or a pointer to the device. The device returned has
886 * had a reference added and the pointer is safe until the user calls
887 * dev_put to indicate they have finished with it.
888 */
889
Eric W. Biederman881d9662007-09-17 11:56:21 -0700890struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct net_device *dev;
893
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000894 rcu_read_lock();
895 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (dev)
897 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000898 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return dev;
900}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700901EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200904 * dev_get_by_napi_id - find a device by napi_id
905 * @napi_id: ID of the NAPI struct
906 *
907 * Search for an interface by NAPI ID. Returns %NULL if the device
908 * is not found or a pointer to the device. The device has not had
909 * its reference counter increased so the caller must be careful
910 * about locking. The caller must hold RCU lock.
911 */
912
913struct net_device *dev_get_by_napi_id(unsigned int napi_id)
914{
915 struct napi_struct *napi;
916
917 WARN_ON_ONCE(!rcu_read_lock_held());
918
919 if (napi_id < MIN_NAPI_ID)
920 return NULL;
921
922 napi = napi_by_id(napi_id);
923
924 return napi ? napi->dev : NULL;
925}
926EXPORT_SYMBOL(dev_get_by_napi_id);
927
928/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200929 * netdev_get_name - get a netdevice name, knowing its ifindex.
930 * @net: network namespace
931 * @name: a pointer to the buffer where the name will be stored.
932 * @ifindex: the ifindex of the interface to get the name from.
933 *
934 * The use of raw_seqcount_begin() and cond_resched() before
935 * retrying is required as we want to give the writers a chance
Thomas Gleixner2da2b322019-10-15 21:18:08 +0200936 * to complete when CONFIG_PREEMPTION is not set.
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200937 */
938int netdev_get_name(struct net *net, char *name, int ifindex)
939{
940 struct net_device *dev;
941 unsigned int seq;
942
943retry:
944 seq = raw_seqcount_begin(&devnet_rename_seq);
945 rcu_read_lock();
946 dev = dev_get_by_index_rcu(net, ifindex);
947 if (!dev) {
948 rcu_read_unlock();
949 return -ENODEV;
950 }
951
952 strcpy(name, dev->name);
953 rcu_read_unlock();
954 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
955 cond_resched();
956 goto retry;
957 }
958
959 return 0;
960}
961
962/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000963 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700964 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * @type: media type of device
966 * @ha: hardware address
967 *
968 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800969 * is not found or a pointer to the device.
970 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000971 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * and the caller must therefore be careful about locking
973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
975
Eric Dumazet941666c2010-12-05 01:23:53 +0000976struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
977 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct net_device *dev;
980
Eric Dumazet941666c2010-12-05 01:23:53 +0000981 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (dev->type == type &&
983 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700984 return dev;
985
986 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Eric Dumazet941666c2010-12-05 01:23:53 +0000988EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300989
Eric W. Biederman881d9662007-09-17 11:56:21 -0700990struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700991{
992 struct net_device *dev;
993
994 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700995 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700996 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700997 return dev;
998
999 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001000}
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001001EXPORT_SYMBOL(__dev_getfirstbyhwtype);
1002
Eric W. Biederman881d9662007-09-17 11:56:21 -07001003struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001005 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001007 rcu_read_lock();
1008 for_each_netdev_rcu(net, dev)
1009 if (dev->type == type) {
1010 dev_hold(dev);
1011 ret = dev;
1012 break;
1013 }
1014 rcu_read_unlock();
1015 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017EXPORT_SYMBOL(dev_getfirstbyhwtype);
1018
1019/**
WANG Cong6c555492014-09-11 15:35:09 -07001020 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001021 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * @if_flags: IFF_* values
1023 * @mask: bitmask of bits in if_flags to check
1024 *
1025 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +00001026 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -07001027 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
1029
WANG Cong6c555492014-09-11 15:35:09 -07001030struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1031 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001033 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
WANG Cong6c555492014-09-11 15:35:09 -07001035 ASSERT_RTNL();
1036
Pavel Emelianov7562f872007-05-03 15:13:45 -07001037 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001038 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001040 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
1042 }
1043 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001044 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
WANG Cong6c555492014-09-11 15:35:09 -07001046EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048/**
1049 * dev_valid_name - check if name is okay for network device
1050 * @name: name string
1051 *
1052 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -07001053 * to allow sysfs to work. We also disallow any kind of
1054 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
David S. Miller95f050b2012-03-06 16:12:15 -05001056bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001058 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001059 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001060 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001061 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001062 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001063 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001064
1065 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001066 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001067 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001068 name++;
1069 }
David S. Miller95f050b2012-03-06 16:12:15 -05001070 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001072EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001075 * __dev_alloc_name - allocate a name for a device
1076 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001078 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
1080 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001081 * id. It scans list of devices to build up a free map, then chooses
1082 * the first empty slot. The caller must hold the dev_base or rtnl lock
1083 * while allocating the name and adding the device in order to avoid
1084 * duplicates.
1085 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1086 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
1088
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001089static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 const char *p;
1093 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001094 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct net_device *d;
1096
Rasmus Villemoes93809102017-11-13 00:15:08 +01001097 if (!dev_valid_name(name))
1098 return -EINVAL;
1099
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001100 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (p) {
1102 /*
1103 * Verify the string as this thing may have come from
1104 * the user. There must be either one "%d" and no other "%"
1105 * characters.
1106 */
1107 if (p[1] != 'd' || strchr(p + 2, '%'))
1108 return -EINVAL;
1109
1110 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001111 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!inuse)
1113 return -ENOMEM;
1114
Eric W. Biederman881d9662007-09-17 11:56:21 -07001115 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!sscanf(d->name, name, &i))
1117 continue;
1118 if (i < 0 || i >= max_netdevices)
1119 continue;
1120
1121 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001122 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!strncmp(buf, d->name, IFNAMSIZ))
1124 set_bit(i, inuse);
1125 }
1126
1127 i = find_first_zero_bit(inuse, max_netdevices);
1128 free_page((unsigned long) inuse);
1129 }
1130
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001131 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001132 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* It is possible to run out of possible slots
1136 * when the name is long and there isn't enough space left
1137 * for the digits, or if all bits are used.
1138 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001139 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001142static int dev_alloc_name_ns(struct net *net,
1143 struct net_device *dev,
1144 const char *name)
1145{
1146 char buf[IFNAMSIZ];
1147 int ret;
1148
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001149 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001150 ret = __dev_alloc_name(net, name, buf);
1151 if (ret >= 0)
1152 strlcpy(dev->name, buf, IFNAMSIZ);
1153 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001156/**
1157 * dev_alloc_name - allocate a name for a device
1158 * @dev: device
1159 * @name: name format string
1160 *
1161 * Passed a format string - eg "lt%d" it will try and find a suitable
1162 * id. It scans list of devices to build up a free map, then chooses
1163 * the first empty slot. The caller must hold the dev_base or rtnl lock
1164 * while allocating the name and adding the device in order to avoid
1165 * duplicates.
1166 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1167 * Returns the number of the unit assigned or a negative errno code.
1168 */
1169
1170int dev_alloc_name(struct net_device *dev, const char *name)
1171{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001172 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001173}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001174EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001175
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001176static int dev_get_valid_name(struct net *net, struct net_device *dev,
1177 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001178{
David S. Miller55a5ec92018-01-02 11:45:07 -05001179 BUG_ON(!net);
1180
1181 if (!dev_valid_name(name))
1182 return -EINVAL;
1183
1184 if (strchr(name, '%'))
1185 return dev_alloc_name_ns(net, dev, name);
1186 else if (__dev_get_by_name(net, name))
1187 return -EEXIST;
1188 else if (dev->name != name)
1189 strlcpy(dev->name, name, IFNAMSIZ);
1190
1191 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194/**
1195 * dev_change_name - change name of a device
1196 * @dev: device
1197 * @newname: name (or format string) must be at least IFNAMSIZ
1198 *
1199 * Change name of a device, can pass format strings "eth%d".
1200 * for wildcarding.
1201 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001202int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Tom Gundersen238fa362014-07-14 16:37:23 +02001204 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001205 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001207 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001208 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001211 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001213 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001214
1215 /* Some auto-enslaved devices e.g. failover slaves are
1216 * special, as userspace might rename the device after
1217 * the interface had been brought up and running since
1218 * the point kernel initiated auto-enslavement. Allow
1219 * live name change even when these slave devices are
1220 * up and running.
1221 *
1222 * Typically, users of these auto-enslaving devices
1223 * don't actually care about slave name change, as
1224 * they are supposed to operate on master interface
1225 * directly.
1226 */
1227 if (dev->flags & IFF_UP &&
1228 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -EBUSY;
1230
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001231 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001232
1233 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001234 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001235 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001236 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001237
Herbert Xufcc5a032007-07-30 17:03:38 -07001238 memcpy(oldname, dev->name, IFNAMSIZ);
1239
Gao feng828de4f2012-09-13 20:58:27 +00001240 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001241 if (err < 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001242 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001243 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001246 if (oldname[0] && !strchr(oldname, '%'))
1247 netdev_info(dev, "renamed from %s\n", oldname);
1248
Tom Gundersen238fa362014-07-14 16:37:23 +02001249 old_assign_type = dev->name_assign_type;
1250 dev->name_assign_type = NET_NAME_RENAMED;
1251
Herbert Xufcc5a032007-07-30 17:03:38 -07001252rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001253 ret = device_rename(&dev->dev, dev->name);
1254 if (ret) {
1255 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001256 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001257 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001258 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001259 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001260
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001261 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001262
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001263 netdev_adjacent_rename_links(dev, oldname);
1264
Herbert Xu7f988ea2007-07-30 16:35:46 -07001265 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001266 netdev_name_node_del(dev->name_node);
Eric Dumazet72c95282009-10-30 07:11:27 +00001267 write_unlock_bh(&dev_base_lock);
1268
1269 synchronize_rcu();
1270
1271 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001272 netdev_name_node_add(net, dev->name_node);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001273 write_unlock_bh(&dev_base_lock);
1274
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001275 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001276 ret = notifier_to_errno(ret);
1277
1278 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001279 /* err >= 0 after dev_alloc_name() or stores the first errno */
1280 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001281 err = ret;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001282 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001283 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001284 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001285 dev->name_assign_type = old_assign_type;
1286 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001287 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001288 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001289 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001290 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001291 }
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 return err;
1295}
1296
1297/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001298 * dev_set_alias - change ifalias of a device
1299 * @dev: device
1300 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001301 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001302 *
1303 * Set ifalias for a device,
1304 */
1305int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1306{
Florian Westphal6c557002017-10-02 23:50:05 +02001307 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001308
1309 if (len >= IFALIASZ)
1310 return -EINVAL;
1311
Florian Westphal6c557002017-10-02 23:50:05 +02001312 if (len) {
1313 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1314 if (!new_alias)
1315 return -ENOMEM;
1316
1317 memcpy(new_alias->ifalias, alias, len);
1318 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001319 }
1320
Florian Westphal6c557002017-10-02 23:50:05 +02001321 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001322 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1323 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001324 mutex_unlock(&ifalias_mutex);
1325
1326 if (new_alias)
1327 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001328
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001329 return len;
1330}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001331EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001332
Florian Westphal6c557002017-10-02 23:50:05 +02001333/**
1334 * dev_get_alias - get ifalias of a device
1335 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001336 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001337 * @len: size of buffer
1338 *
1339 * get ifalias for a device. Caller must make sure dev cannot go
1340 * away, e.g. rcu read lock or own a reference count to device.
1341 */
1342int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1343{
1344 const struct dev_ifalias *alias;
1345 int ret = 0;
1346
1347 rcu_read_lock();
1348 alias = rcu_dereference(dev->ifalias);
1349 if (alias)
1350 ret = snprintf(name, len, "%s", alias->ifalias);
1351 rcu_read_unlock();
1352
1353 return ret;
1354}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001355
1356/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001357 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001358 * @dev: device to cause notification
1359 *
1360 * Called to indicate a device has changed features.
1361 */
1362void netdev_features_change(struct net_device *dev)
1363{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001364 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001365}
1366EXPORT_SYMBOL(netdev_features_change);
1367
1368/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * netdev_state_change - device changes state
1370 * @dev: device to cause notification
1371 *
1372 * Called to indicate a device has changed state. This function calls
1373 * the notifier chains for netdev_chain and sends a NEWLINK message
1374 * to the routing socket.
1375 */
1376void netdev_state_change(struct net_device *dev)
1377{
1378 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001379 struct netdev_notifier_change_info change_info = {
1380 .info.dev = dev,
1381 };
Loic Prylli54951192014-07-01 21:39:43 -07001382
David Ahern51d0c0472017-10-04 17:48:45 -07001383 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001384 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001385 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001388EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Amerigo Wangee89bab2012-08-09 22:14:56 +00001390/**
tcharding722c9a02017-02-09 17:56:04 +11001391 * netdev_notify_peers - notify network peers about existence of @dev
1392 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001393 *
1394 * Generate traffic such that interested network peers are aware of
1395 * @dev, such as by generating a gratuitous ARP. This may be used when
1396 * a device wants to inform the rest of the network about some sort of
1397 * reconfiguration such as a failover event or virtual machine
1398 * migration.
1399 */
1400void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001401{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001402 rtnl_lock();
1403 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001404 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001405 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001406}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001407EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001408
Petr Machata40c900a2018-12-06 17:05:47 +00001409static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001411 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001412 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001414 ASSERT_RTNL();
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!netif_device_present(dev))
1417 return -ENODEV;
1418
Neil Hormanca99ca12013-02-05 08:05:43 +00001419 /* Block netpoll from trying to do any rx path servicing.
1420 * If we don't do this there is a chance ndo_poll_controller
1421 * or ndo_poll may be running while we open the device
1422 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001423 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001424
Petr Machata40c900a2018-12-06 17:05:47 +00001425 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001426 ret = notifier_to_errno(ret);
1427 if (ret)
1428 return ret;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001431
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001432 if (ops->ndo_validate_addr)
1433 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001434
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001435 if (!ret && ops->ndo_open)
1436 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Eric W. Biederman66b55522014-03-27 15:39:03 -07001438 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001439
Jeff Garzikbada3392007-10-23 20:19:37 -07001440 if (ret)
1441 clear_bit(__LINK_STATE_START, &dev->state);
1442 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001444 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001446 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return ret;
1450}
Patrick McHardybd380812010-02-26 06:34:53 +00001451
1452/**
1453 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001454 * @dev: device to open
1455 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001456 *
1457 * Takes a device from down to up state. The device's private open
1458 * function is invoked and then the multicast lists are loaded. Finally
1459 * the device is moved into the up state and a %NETDEV_UP message is
1460 * sent to the netdev notifier chain.
1461 *
1462 * Calling this function on an active interface is a nop. On a failure
1463 * a negative errno code is returned.
1464 */
Petr Machata00f54e62018-12-06 17:05:36 +00001465int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001466{
1467 int ret;
1468
Patrick McHardybd380812010-02-26 06:34:53 +00001469 if (dev->flags & IFF_UP)
1470 return 0;
1471
Petr Machata40c900a2018-12-06 17:05:47 +00001472 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001473 if (ret < 0)
1474 return ret;
1475
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001476 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001477 call_netdevice_notifiers(NETDEV_UP, dev);
1478
1479 return ret;
1480}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001481EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
stephen hemminger7051b882017-07-18 15:59:27 -07001483static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Octavian Purdila44345722010-12-13 12:44:07 +00001485 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001486
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001487 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001488 might_sleep();
1489
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001490 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001491 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001492 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001493
Octavian Purdila44345722010-12-13 12:44:07 +00001494 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Octavian Purdila44345722010-12-13 12:44:07 +00001496 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Octavian Purdila44345722010-12-13 12:44:07 +00001498 /* Synchronize to scheduled poll. We cannot touch poll list, it
1499 * can be even on different cpu. So just clear netif_running().
1500 *
1501 * dev->stop() will invoke napi_disable() on all of it's
1502 * napi_struct instances on this device.
1503 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001504 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Octavian Purdila44345722010-12-13 12:44:07 +00001507 dev_deactivate_many(head);
1508
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001509 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001510 const struct net_device_ops *ops = dev->netdev_ops;
1511
1512 /*
1513 * Call the device specific close. This cannot fail.
1514 * Only if device is UP
1515 *
1516 * We allow it to be called even after a DETACH hot-plug
1517 * event.
1518 */
1519 if (ops->ndo_stop)
1520 ops->ndo_stop(dev);
1521
Octavian Purdila44345722010-12-13 12:44:07 +00001522 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001523 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001524 }
Octavian Purdila44345722010-12-13 12:44:07 +00001525}
1526
stephen hemminger7051b882017-07-18 15:59:27 -07001527static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001528{
1529 LIST_HEAD(single);
1530
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001531 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001532 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001533 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001534}
1535
stephen hemminger7051b882017-07-18 15:59:27 -07001536void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001537{
1538 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001539
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001540 /* Remove the devices that don't need to be closed */
1541 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001542 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001543 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001544
1545 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001546
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001547 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001548 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001549 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001550 if (unlink)
1551 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
David S. Miller99c4a262015-03-18 22:52:33 -04001554EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001555
1556/**
1557 * dev_close - shutdown an interface.
1558 * @dev: device to shutdown
1559 *
1560 * This function moves an active device into down state. A
1561 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1562 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1563 * chain.
1564 */
stephen hemminger7051b882017-07-18 15:59:27 -07001565void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001566{
Eric Dumazete14a5992011-05-10 12:26:06 -07001567 if (dev->flags & IFF_UP) {
1568 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001569
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001570 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001571 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001572 list_del(&single);
1573 }
Patrick McHardybd380812010-02-26 06:34:53 +00001574}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001575EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001578/**
1579 * dev_disable_lro - disable Large Receive Offload on a device
1580 * @dev: device
1581 *
1582 * Disable Large Receive Offload (LRO) on a net device. Must be
1583 * called under RTNL. This is needed if received packets may be
1584 * forwarded to another interface.
1585 */
1586void dev_disable_lro(struct net_device *dev)
1587{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001588 struct net_device *lower_dev;
1589 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001590
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001591 dev->wanted_features &= ~NETIF_F_LRO;
1592 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001593
Michał Mirosław22d59692011-04-21 12:42:15 +00001594 if (unlikely(dev->features & NETIF_F_LRO))
1595 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001596
1597 netdev_for_each_lower_dev(dev, lower_dev, iter)
1598 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001599}
1600EXPORT_SYMBOL(dev_disable_lro);
1601
Michael Chan56f5aa72017-12-16 03:09:41 -05001602/**
1603 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1604 * @dev: device
1605 *
1606 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1607 * called under RTNL. This is needed if Generic XDP is installed on
1608 * the device.
1609 */
1610static void dev_disable_gro_hw(struct net_device *dev)
1611{
1612 dev->wanted_features &= ~NETIF_F_GRO_HW;
1613 netdev_update_features(dev);
1614
1615 if (unlikely(dev->features & NETIF_F_GRO_HW))
1616 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1617}
1618
Kirill Tkhaiede27622018-03-23 19:47:19 +03001619const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1620{
1621#define N(val) \
1622 case NETDEV_##val: \
1623 return "NETDEV_" __stringify(val);
1624 switch (cmd) {
1625 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1626 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1627 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1628 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1629 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1630 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1631 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001632 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1633 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001634 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001635 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001636#undef N
1637 return "UNKNOWN_NETDEV_EVENT";
1638}
1639EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1640
Jiri Pirko351638e2013-05-28 01:30:21 +00001641static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1642 struct net_device *dev)
1643{
David Ahern51d0c0472017-10-04 17:48:45 -07001644 struct netdev_notifier_info info = {
1645 .dev = dev,
1646 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001647
Jiri Pirko351638e2013-05-28 01:30:21 +00001648 return nb->notifier_call(nb, val, &info);
1649}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001650
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001651static int call_netdevice_register_notifiers(struct notifier_block *nb,
1652 struct net_device *dev)
1653{
1654 int err;
1655
1656 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1657 err = notifier_to_errno(err);
1658 if (err)
1659 return err;
1660
1661 if (!(dev->flags & IFF_UP))
1662 return 0;
1663
1664 call_netdevice_notifier(nb, NETDEV_UP, dev);
1665 return 0;
1666}
1667
1668static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1669 struct net_device *dev)
1670{
1671 if (dev->flags & IFF_UP) {
1672 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1673 dev);
1674 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1675 }
1676 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1677}
1678
1679static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1680 struct net *net)
1681{
1682 struct net_device *dev;
1683 int err;
1684
1685 for_each_netdev(net, dev) {
1686 err = call_netdevice_register_notifiers(nb, dev);
1687 if (err)
1688 goto rollback;
1689 }
1690 return 0;
1691
1692rollback:
1693 for_each_netdev_continue_reverse(net, dev)
1694 call_netdevice_unregister_notifiers(nb, dev);
1695 return err;
1696}
1697
1698static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1699 struct net *net)
1700{
1701 struct net_device *dev;
1702
1703 for_each_netdev(net, dev)
1704 call_netdevice_unregister_notifiers(nb, dev);
1705}
1706
Eric W. Biederman881d9662007-09-17 11:56:21 -07001707static int dev_boot_phase = 1;
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/**
tcharding722c9a02017-02-09 17:56:04 +11001710 * register_netdevice_notifier - register a network notifier block
1711 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *
tcharding722c9a02017-02-09 17:56:04 +11001713 * Register a notifier to be called when network device events occur.
1714 * The notifier passed is linked into the kernel structures and must
1715 * not be reused until it has been unregistered. A negative errno code
1716 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 *
tcharding722c9a02017-02-09 17:56:04 +11001718 * When registered all registration and up events are replayed
1719 * to the new notifier to allow device to have a race free
1720 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
1722
1723int register_netdevice_notifier(struct notifier_block *nb)
1724{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001725 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int err;
1727
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001728 /* Close race with setup_net() and cleanup_net() */
1729 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001731 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001732 if (err)
1733 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001734 if (dev_boot_phase)
1735 goto unlock;
1736 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001737 err = call_netdevice_register_net_notifiers(nb, net);
1738 if (err)
1739 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001741
1742unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001744 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001746
1747rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001748 for_each_net_continue_reverse(net)
1749 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001750
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001751 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001752 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001754EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756/**
tcharding722c9a02017-02-09 17:56:04 +11001757 * unregister_netdevice_notifier - unregister a network notifier block
1758 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 *
tcharding722c9a02017-02-09 17:56:04 +11001760 * Unregister a notifier previously registered by
1761 * register_netdevice_notifier(). The notifier is unlinked into the
1762 * kernel structures and may then be reused. A negative errno code
1763 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001764 *
tcharding722c9a02017-02-09 17:56:04 +11001765 * After unregistering unregister and down device events are synthesized
1766 * for all devices on the device list to the removed notifier to remove
1767 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
1769
1770int unregister_netdevice_notifier(struct notifier_block *nb)
1771{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001772 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001773 int err;
1774
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001775 /* Close race with setup_net() and cleanup_net() */
1776 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001777 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001778 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001779 if (err)
1780 goto unlock;
1781
Jiri Pirko48b3a132020-01-25 12:17:06 +01001782 for_each_net(net)
1783 call_netdevice_unregister_net_notifiers(nb, net);
1784
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001785unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001786 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001787 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001790EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jiri Pirko1f637702020-01-25 12:17:07 +01001792static int __register_netdevice_notifier_net(struct net *net,
1793 struct notifier_block *nb,
1794 bool ignore_call_fail)
1795{
1796 int err;
1797
1798 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1799 if (err)
1800 return err;
1801 if (dev_boot_phase)
1802 return 0;
1803
1804 err = call_netdevice_register_net_notifiers(nb, net);
1805 if (err && !ignore_call_fail)
1806 goto chain_unregister;
1807
1808 return 0;
1809
1810chain_unregister:
1811 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1812 return err;
1813}
1814
1815static int __unregister_netdevice_notifier_net(struct net *net,
1816 struct notifier_block *nb)
1817{
1818 int err;
1819
1820 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1821 if (err)
1822 return err;
1823
1824 call_netdevice_unregister_net_notifiers(nb, net);
1825 return 0;
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001829 * register_netdevice_notifier_net - register a per-netns network notifier block
1830 * @net: network namespace
1831 * @nb: notifier
1832 *
1833 * Register a notifier to be called when network device events occur.
1834 * The notifier passed is linked into the kernel structures and must
1835 * not be reused until it has been unregistered. A negative errno code
1836 * is returned on a failure.
1837 *
1838 * When registered all registration and up events are replayed
1839 * to the new notifier to allow device to have a race free
1840 * view of the network device list.
1841 */
1842
1843int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1844{
1845 int err;
1846
1847 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001848 err = __register_netdevice_notifier_net(net, nb, false);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001849 rtnl_unlock();
1850 return err;
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001851}
1852EXPORT_SYMBOL(register_netdevice_notifier_net);
1853
1854/**
1855 * unregister_netdevice_notifier_net - unregister a per-netns
1856 * network notifier block
1857 * @net: network namespace
1858 * @nb: notifier
1859 *
1860 * Unregister a notifier previously registered by
1861 * register_netdevice_notifier(). The notifier is unlinked into the
1862 * kernel structures and may then be reused. A negative errno code
1863 * is returned on a failure.
1864 *
1865 * After unregistering unregister and down device events are synthesized
1866 * for all devices on the device list to the removed notifier to remove
1867 * the need for special case cleanup code.
1868 */
1869
1870int unregister_netdevice_notifier_net(struct net *net,
1871 struct notifier_block *nb)
1872{
1873 int err;
1874
1875 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001876 err = __unregister_netdevice_notifier_net(net, nb);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001877 rtnl_unlock();
1878 return err;
1879}
1880EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1881
Jiri Pirko93642e12020-01-25 12:17:08 +01001882int register_netdevice_notifier_dev_net(struct net_device *dev,
1883 struct notifier_block *nb,
1884 struct netdev_net_notifier *nn)
1885{
1886 int err;
1887
1888 rtnl_lock();
1889 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1890 if (!err) {
1891 nn->nb = nb;
1892 list_add(&nn->list, &dev->net_notifier_list);
1893 }
1894 rtnl_unlock();
1895 return err;
1896}
1897EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1898
1899int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1900 struct notifier_block *nb,
1901 struct netdev_net_notifier *nn)
1902{
1903 int err;
1904
1905 rtnl_lock();
1906 list_del(&nn->list);
1907 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
1908 rtnl_unlock();
1909 return err;
1910}
1911EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1912
1913static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1914 struct net *net)
1915{
1916 struct netdev_net_notifier *nn;
1917
1918 list_for_each_entry(nn, &dev->net_notifier_list, list) {
1919 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
1920 __register_netdevice_notifier_net(net, nn->nb, true);
1921 }
1922}
1923
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001924/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001925 * call_netdevice_notifiers_info - call all network notifier blocks
1926 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00001927 * @info: notifier information data
1928 *
1929 * Call all network notifier blocks. Parameters and return value
1930 * are as for raw_notifier_call_chain().
1931 */
1932
stephen hemminger1d143d92013-12-29 14:01:29 -08001933static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001934 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001935{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001936 struct net *net = dev_net(info->dev);
1937 int ret;
1938
Jiri Pirko351638e2013-05-28 01:30:21 +00001939 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001940
1941 /* Run per-netns notifier block chain first, then run the global one.
1942 * Hopefully, one day, the global one is going to be removed after
1943 * all notifier block registrators get converted to be per-netns.
1944 */
1945 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1946 if (ret & NOTIFY_STOP_MASK)
1947 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00001948 return raw_notifier_call_chain(&netdev_chain, val, info);
1949}
Jiri Pirko351638e2013-05-28 01:30:21 +00001950
Petr Machata26372602018-12-06 17:05:45 +00001951static int call_netdevice_notifiers_extack(unsigned long val,
1952 struct net_device *dev,
1953 struct netlink_ext_ack *extack)
1954{
1955 struct netdev_notifier_info info = {
1956 .dev = dev,
1957 .extack = extack,
1958 };
1959
1960 return call_netdevice_notifiers_info(val, &info);
1961}
1962
Jiri Pirko351638e2013-05-28 01:30:21 +00001963/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 * call_netdevice_notifiers - call all network notifier blocks
1965 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001966 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 *
1968 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001969 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 */
1971
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001972int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Petr Machata26372602018-12-06 17:05:45 +00001974 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001976EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001978/**
1979 * call_netdevice_notifiers_mtu - call all network notifier blocks
1980 * @val: value passed unmodified to notifier function
1981 * @dev: net_device pointer passed unmodified to notifier function
1982 * @arg: additional u32 argument passed to the notifier function
1983 *
1984 * Call all network notifier blocks. Parameters and return value
1985 * are as for raw_notifier_call_chain().
1986 */
1987static int call_netdevice_notifiers_mtu(unsigned long val,
1988 struct net_device *dev, u32 arg)
1989{
1990 struct netdev_notifier_info_ext info = {
1991 .info.dev = dev,
1992 .ext.mtu = arg,
1993 };
1994
1995 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1996
1997 return call_netdevice_notifiers_info(val, &info.info);
1998}
1999
Pablo Neira1cf519002015-05-13 18:19:37 +02002000#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002001static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002002
2003void net_inc_ingress_queue(void)
2004{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002005 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002006}
2007EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2008
2009void net_dec_ingress_queue(void)
2010{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002011 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002012}
2013EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2014#endif
2015
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002016#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002017static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002018
2019void net_inc_egress_queue(void)
2020{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002021 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002022}
2023EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2024
2025void net_dec_egress_queue(void)
2026{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002027 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002028}
2029EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2030#endif
2031
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002032static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002033#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00002034static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08002035static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002036static void netstamp_clear(struct work_struct *work)
2037{
2038 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08002039 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002040
Eric Dumazet13baa002017-03-01 14:28:39 -08002041 wanted = atomic_add_return(deferred, &netstamp_wanted);
2042 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002043 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002044 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002045 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002046}
2047static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00002048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050void net_enable_timestamp(void)
2051{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002052#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002053 int wanted;
2054
2055 while (1) {
2056 wanted = atomic_read(&netstamp_wanted);
2057 if (wanted <= 0)
2058 break;
2059 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2060 return;
2061 }
2062 atomic_inc(&netstamp_needed_deferred);
2063 schedule_work(&netstamp_work);
2064#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002065 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002068EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070void net_disable_timestamp(void)
2071{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002072#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002073 int wanted;
2074
2075 while (1) {
2076 wanted = atomic_read(&netstamp_wanted);
2077 if (wanted <= 1)
2078 break;
2079 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2080 return;
2081 }
2082 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002083 schedule_work(&netstamp_work);
2084#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002085 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002088EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Eric Dumazet3b098e22010-05-15 23:57:10 -07002090static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002092 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002093 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002094 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002097#define net_timestamp_check(COND, SKB) \
2098 if (static_branch_unlikely(&netstamp_needed_key)) { \
2099 if ((COND) && !(SKB)->tstamp) \
2100 __net_timestamp(SKB); \
2101 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002102
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002103bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002104{
2105 unsigned int len;
2106
2107 if (!(dev->flags & IFF_UP))
2108 return false;
2109
2110 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2111 if (skb->len <= len)
2112 return true;
2113
2114 /* if TSO is enabled, we don't care about the length as the packet
2115 * could be forwarded without being segmented before
2116 */
2117 if (skb_is_gso(skb))
2118 return true;
2119
2120 return false;
2121}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002122EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002123
Herbert Xua0265d22014-04-17 13:45:03 +08002124int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2125{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002126 int ret = ____dev_forward_skb(dev, skb);
2127
2128 if (likely(!ret)) {
2129 skb->protocol = eth_type_trans(skb, dev);
2130 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002131 }
2132
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002133 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002134}
2135EXPORT_SYMBOL_GPL(__dev_forward_skb);
2136
Arnd Bergmann44540962009-11-26 06:07:08 +00002137/**
2138 * dev_forward_skb - loopback an skb to another netif
2139 *
2140 * @dev: destination network device
2141 * @skb: buffer to forward
2142 *
2143 * return values:
2144 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002145 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002146 *
2147 * dev_forward_skb can be used for injecting an skb from the
2148 * start_xmit function of one device into the receive queue
2149 * of another device.
2150 *
2151 * The receiving device may be in another namespace, so
2152 * we have to clear all information in the skb that could
2153 * impact namespace isolation.
2154 */
2155int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2156{
Herbert Xua0265d22014-04-17 13:45:03 +08002157 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002158}
2159EXPORT_SYMBOL_GPL(dev_forward_skb);
2160
Changli Gao71d9dec2010-12-15 19:57:25 +00002161static inline int deliver_skb(struct sk_buff *skb,
2162 struct packet_type *pt_prev,
2163 struct net_device *orig_dev)
2164{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002165 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002166 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002167 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002168 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2169}
2170
Salam Noureddine7866a622015-01-27 11:35:48 -08002171static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2172 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002173 struct net_device *orig_dev,
2174 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002175 struct list_head *ptype_list)
2176{
2177 struct packet_type *ptype, *pt_prev = *pt;
2178
2179 list_for_each_entry_rcu(ptype, ptype_list, list) {
2180 if (ptype->type != type)
2181 continue;
2182 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002183 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002184 pt_prev = ptype;
2185 }
2186 *pt = pt_prev;
2187}
2188
Eric Leblondc0de08d2012-08-16 22:02:58 +00002189static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2190{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002191 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002192 return false;
2193
2194 if (ptype->id_match)
2195 return ptype->id_match(ptype, skb->sk);
2196 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2197 return true;
2198
2199 return false;
2200}
2201
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002202/**
2203 * dev_nit_active - return true if any network interface taps are in use
2204 *
2205 * @dev: network device to check for the presence of taps
2206 */
2207bool dev_nit_active(struct net_device *dev)
2208{
2209 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2210}
2211EXPORT_SYMBOL_GPL(dev_nit_active);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213/*
2214 * Support routine. Sends outgoing frames to any network
2215 * taps currently in use.
2216 */
2217
David Ahern74b20582016-05-10 11:19:50 -07002218void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002221 struct sk_buff *skb2 = NULL;
2222 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002223 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002226again:
2227 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002228 if (ptype->ignore_outgoing)
2229 continue;
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Never send packets back to the socket
2232 * they originated from - MvS (miquels@drinkel.ow.org)
2233 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002234 if (skb_loop_sk(ptype, skb))
2235 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002236
Salam Noureddine7866a622015-01-27 11:35:48 -08002237 if (pt_prev) {
2238 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002239 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002240 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002242
2243 /* need to clone skb, done only once */
2244 skb2 = skb_clone(skb, GFP_ATOMIC);
2245 if (!skb2)
2246 goto out_unlock;
2247
2248 net_timestamp_set(skb2);
2249
2250 /* skb->nh should be correctly
2251 * set by sender, so that the second statement is
2252 * just protection against buggy protocols.
2253 */
2254 skb_reset_mac_header(skb2);
2255
2256 if (skb_network_header(skb2) < skb2->data ||
2257 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2258 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2259 ntohs(skb2->protocol),
2260 dev->name);
2261 skb_reset_network_header(skb2);
2262 }
2263
2264 skb2->transport_header = skb2->network_header;
2265 skb2->pkt_type = PACKET_OUTGOING;
2266 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002268
2269 if (ptype_list == &ptype_all) {
2270 ptype_list = &dev->ptype_all;
2271 goto again;
2272 }
2273out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002274 if (pt_prev) {
2275 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2276 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2277 else
2278 kfree_skb(skb2);
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 rcu_read_unlock();
2281}
David Ahern74b20582016-05-10 11:19:50 -07002282EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Ben Hutchings2c530402012-07-10 10:55:09 +00002284/**
2285 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002286 * @dev: Network device
2287 * @txq: number of queues available
2288 *
2289 * If real_num_tx_queues is changed the tc mappings may no longer be
2290 * valid. To resolve this verify the tc mapping remains valid and if
2291 * not NULL the mapping. With no priorities mapping to this
2292 * offset/count pair it will no longer be used. In the worst case TC0
2293 * is invalid nothing can be done so disable priority mappings. If is
2294 * expected that drivers will fix this mapping if they can before
2295 * calling netif_set_real_num_tx_queues.
2296 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002297static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002298{
2299 int i;
2300 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2301
2302 /* If TC0 is invalidated disable TC mapping */
2303 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002304 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002305 dev->num_tc = 0;
2306 return;
2307 }
2308
2309 /* Invalidated prio to tc mappings set to TC0 */
2310 for (i = 1; i < TC_BITMASK + 1; i++) {
2311 int q = netdev_get_prio_tc_map(dev, i);
2312
2313 tc = &dev->tc_to_txq[q];
2314 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002315 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2316 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002317 netdev_set_prio_tc_map(dev, i, 0);
2318 }
2319 }
2320}
2321
Alexander Duyck8d059b02016-10-28 11:43:49 -04002322int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2323{
2324 if (dev->num_tc) {
2325 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2326 int i;
2327
Alexander Duyckffcfe252018-07-09 12:19:38 -04002328 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002329 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2330 if ((txq - tc->offset) < tc->count)
2331 return i;
2332 }
2333
Alexander Duyckffcfe252018-07-09 12:19:38 -04002334 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002335 return -1;
2336 }
2337
2338 return 0;
2339}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002340EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002341
Alexander Duyck537c00d2013-01-10 08:57:02 +00002342#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002343struct static_key xps_needed __read_mostly;
2344EXPORT_SYMBOL(xps_needed);
2345struct static_key xps_rxqs_needed __read_mostly;
2346EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002347static DEFINE_MUTEX(xps_map_mutex);
2348#define xmap_dereference(P) \
2349 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2350
Alexander Duyck6234f872016-10-28 11:46:49 -04002351static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2352 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002353{
2354 struct xps_map *map = NULL;
2355 int pos;
2356
2357 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002358 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002359 if (!map)
2360 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002361
Alexander Duyck6234f872016-10-28 11:46:49 -04002362 for (pos = map->len; pos--;) {
2363 if (map->queues[pos] != index)
2364 continue;
2365
2366 if (map->len > 1) {
2367 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002368 break;
2369 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002370
Amritha Nambiar80d19662018-06-29 21:26:41 -07002371 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002372 kfree_rcu(map, rcu);
2373 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002374 }
2375
Alexander Duyck6234f872016-10-28 11:46:49 -04002376 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002377}
2378
Alexander Duyck6234f872016-10-28 11:46:49 -04002379static bool remove_xps_queue_cpu(struct net_device *dev,
2380 struct xps_dev_maps *dev_maps,
2381 int cpu, u16 offset, u16 count)
2382{
Alexander Duyck184c4492016-10-28 11:50:13 -04002383 int num_tc = dev->num_tc ? : 1;
2384 bool active = false;
2385 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002386
Alexander Duyck184c4492016-10-28 11:50:13 -04002387 for (tci = cpu * num_tc; num_tc--; tci++) {
2388 int i, j;
2389
2390 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002391 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002392 break;
2393 }
2394
2395 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002396 }
2397
Alexander Duyck184c4492016-10-28 11:50:13 -04002398 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002399}
2400
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002401static void reset_xps_maps(struct net_device *dev,
2402 struct xps_dev_maps *dev_maps,
2403 bool is_rxqs_map)
2404{
2405 if (is_rxqs_map) {
2406 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2407 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2408 } else {
2409 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2410 }
2411 static_key_slow_dec_cpuslocked(&xps_needed);
2412 kfree_rcu(dev_maps, rcu);
2413}
2414
Amritha Nambiar80d19662018-06-29 21:26:41 -07002415static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2416 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2417 u16 offset, u16 count, bool is_rxqs_map)
2418{
2419 bool active = false;
2420 int i, j;
2421
2422 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2423 j < nr_ids;)
2424 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2425 count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002426 if (!active)
2427 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002428
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002429 if (!is_rxqs_map) {
2430 for (i = offset + (count - 1); count--; i--) {
2431 netdev_queue_numa_node_write(
2432 netdev_get_tx_queue(dev, i),
2433 NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002434 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002435 }
2436}
2437
Alexander Duyck6234f872016-10-28 11:46:49 -04002438static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2439 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002440{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002441 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002442 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002443 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002444
Amritha Nambiar04157462018-06-29 21:26:46 -07002445 if (!static_key_false(&xps_needed))
2446 return;
2447
Andrei Vagin4d99f662018-08-08 20:07:35 -07002448 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002449 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002450
Amritha Nambiar04157462018-06-29 21:26:46 -07002451 if (static_key_false(&xps_rxqs_needed)) {
2452 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2453 if (dev_maps) {
2454 nr_ids = dev->num_rx_queues;
2455 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2456 offset, count, true);
2457 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002458 }
2459
2460 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002461 if (!dev_maps)
2462 goto out_no_maps;
2463
Amritha Nambiar80d19662018-06-29 21:26:41 -07002464 if (num_possible_cpus() > 1)
2465 possible_mask = cpumask_bits(cpu_possible_mask);
2466 nr_ids = nr_cpu_ids;
2467 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2468 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002469
Alexander Duyck537c00d2013-01-10 08:57:02 +00002470out_no_maps:
2471 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002472 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002473}
2474
Alexander Duyck6234f872016-10-28 11:46:49 -04002475static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2476{
2477 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2478}
2479
Amritha Nambiar80d19662018-06-29 21:26:41 -07002480static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2481 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002482{
2483 struct xps_map *new_map;
2484 int alloc_len = XPS_MIN_MAP_ALLOC;
2485 int i, pos;
2486
2487 for (pos = 0; map && pos < map->len; pos++) {
2488 if (map->queues[pos] != index)
2489 continue;
2490 return map;
2491 }
2492
Amritha Nambiar80d19662018-06-29 21:26:41 -07002493 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002494 if (map) {
2495 if (pos < map->alloc_len)
2496 return map;
2497
2498 alloc_len = map->alloc_len * 2;
2499 }
2500
Amritha Nambiar80d19662018-06-29 21:26:41 -07002501 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2502 * map
2503 */
2504 if (is_rxqs_map)
2505 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2506 else
2507 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2508 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002509 if (!new_map)
2510 return NULL;
2511
2512 for (i = 0; i < pos; i++)
2513 new_map->queues[i] = map->queues[i];
2514 new_map->alloc_len = alloc_len;
2515 new_map->len = pos;
2516
2517 return new_map;
2518}
2519
Andrei Vagin4d99f662018-08-08 20:07:35 -07002520/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002521int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2522 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002523{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002524 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002525 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002526 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002527 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002528 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002529 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002530 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002531
Alexander Duyck184c4492016-10-28 11:50:13 -04002532 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002533 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002534 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002535 if (num_tc < 0)
2536 return -EINVAL;
2537
2538 /* If queue belongs to subordinate dev use its map */
2539 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2540
Alexander Duyck184c4492016-10-28 11:50:13 -04002541 tc = netdev_txq_to_tc(dev, index);
2542 if (tc < 0)
2543 return -EINVAL;
2544 }
2545
Amritha Nambiar80d19662018-06-29 21:26:41 -07002546 mutex_lock(&xps_map_mutex);
2547 if (is_rxqs_map) {
2548 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2549 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2550 nr_ids = dev->num_rx_queues;
2551 } else {
2552 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2553 if (num_possible_cpus() > 1) {
2554 online_mask = cpumask_bits(cpu_online_mask);
2555 possible_mask = cpumask_bits(cpu_possible_mask);
2556 }
2557 dev_maps = xmap_dereference(dev->xps_cpus_map);
2558 nr_ids = nr_cpu_ids;
2559 }
2560
Alexander Duyck184c4492016-10-28 11:50:13 -04002561 if (maps_sz < L1_CACHE_BYTES)
2562 maps_sz = L1_CACHE_BYTES;
2563
Alexander Duyck01c5f862013-01-10 08:57:35 +00002564 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002565 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2566 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002567 if (!new_dev_maps)
2568 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002569 if (!new_dev_maps) {
2570 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002571 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002572 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002573
Amritha Nambiar80d19662018-06-29 21:26:41 -07002574 tci = j * num_tc + tc;
2575 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002576 NULL;
2577
Amritha Nambiar80d19662018-06-29 21:26:41 -07002578 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002579 if (!map)
2580 goto error;
2581
Amritha Nambiar80d19662018-06-29 21:26:41 -07002582 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002583 }
2584
2585 if (!new_dev_maps)
2586 goto out_no_new_maps;
2587
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002588 if (!dev_maps) {
2589 /* Increment static keys at most once per type */
2590 static_key_slow_inc_cpuslocked(&xps_needed);
2591 if (is_rxqs_map)
2592 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2593 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002594
Amritha Nambiar80d19662018-06-29 21:26:41 -07002595 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2596 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002597 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002598 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002599 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002600 map = xmap_dereference(dev_maps->attr_map[tci]);
2601 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002602 }
2603
2604 /* We need to explicitly update tci as prevous loop
2605 * could break out early if dev_maps is NULL.
2606 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002607 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002608
Amritha Nambiar80d19662018-06-29 21:26:41 -07002609 if (netif_attr_test_mask(j, mask, nr_ids) &&
2610 netif_attr_test_online(j, online_mask, nr_ids)) {
2611 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002612 int pos = 0;
2613
Amritha Nambiar80d19662018-06-29 21:26:41 -07002614 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002615 while ((pos < map->len) && (map->queues[pos] != index))
2616 pos++;
2617
2618 if (pos == map->len)
2619 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002620#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002621 if (!is_rxqs_map) {
2622 if (numa_node_id == -2)
2623 numa_node_id = cpu_to_node(j);
2624 else if (numa_node_id != cpu_to_node(j))
2625 numa_node_id = -1;
2626 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002627#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002628 } else if (dev_maps) {
2629 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002630 map = xmap_dereference(dev_maps->attr_map[tci]);
2631 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002632 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002633
Alexander Duyck184c4492016-10-28 11:50:13 -04002634 /* copy maps belonging to foreign traffic classes */
2635 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2636 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002637 map = xmap_dereference(dev_maps->attr_map[tci]);
2638 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002639 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002640 }
2641
Amritha Nambiar80d19662018-06-29 21:26:41 -07002642 if (is_rxqs_map)
2643 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2644 else
2645 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002646
Alexander Duyck537c00d2013-01-10 08:57:02 +00002647 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002648 if (!dev_maps)
2649 goto out_no_old_maps;
2650
Amritha Nambiar80d19662018-06-29 21:26:41 -07002651 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2652 j < nr_ids;) {
2653 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2654 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2655 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002656 if (map && map != new_map)
2657 kfree_rcu(map, rcu);
2658 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002659 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002660
Alexander Duyck184c4492016-10-28 11:50:13 -04002661 kfree_rcu(dev_maps, rcu);
2662
2663out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002664 dev_maps = new_dev_maps;
2665 active = true;
2666
2667out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002668 if (!is_rxqs_map) {
2669 /* update Tx queue numa node */
2670 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2671 (numa_node_id >= 0) ?
2672 numa_node_id : NUMA_NO_NODE);
2673 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002674
Alexander Duyck01c5f862013-01-10 08:57:35 +00002675 if (!dev_maps)
2676 goto out_no_maps;
2677
Amritha Nambiar80d19662018-06-29 21:26:41 -07002678 /* removes tx-queue from unused CPUs/rx-queues */
2679 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2680 j < nr_ids;) {
2681 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002682 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002683 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2684 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002685 active |= remove_xps_queue(dev_maps, tci, index);
2686 for (i = num_tc - tc, tci++; --i; tci++)
2687 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002688 }
2689
2690 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002691 if (!active)
2692 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002693
2694out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002695 mutex_unlock(&xps_map_mutex);
2696
2697 return 0;
2698error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002699 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002700 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2701 j < nr_ids;) {
2702 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2703 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002704 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002705 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002706 NULL;
2707 if (new_map && new_map != map)
2708 kfree(new_map);
2709 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002710 }
2711
Alexander Duyck537c00d2013-01-10 08:57:02 +00002712 mutex_unlock(&xps_map_mutex);
2713
Alexander Duyck537c00d2013-01-10 08:57:02 +00002714 kfree(new_dev_maps);
2715 return -ENOMEM;
2716}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002717EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002718
2719int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2720 u16 index)
2721{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002722 int ret;
2723
2724 cpus_read_lock();
2725 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2726 cpus_read_unlock();
2727
2728 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002729}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002730EXPORT_SYMBOL(netif_set_xps_queue);
2731
2732#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002733static void netdev_unbind_all_sb_channels(struct net_device *dev)
2734{
2735 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2736
2737 /* Unbind any subordinate channels */
2738 while (txq-- != &dev->_tx[0]) {
2739 if (txq->sb_dev)
2740 netdev_unbind_sb_channel(dev, txq->sb_dev);
2741 }
2742}
2743
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002744void netdev_reset_tc(struct net_device *dev)
2745{
Alexander Duyck6234f872016-10-28 11:46:49 -04002746#ifdef CONFIG_XPS
2747 netif_reset_xps_queues_gt(dev, 0);
2748#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002749 netdev_unbind_all_sb_channels(dev);
2750
2751 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002752 dev->num_tc = 0;
2753 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2754 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2755}
2756EXPORT_SYMBOL(netdev_reset_tc);
2757
2758int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2759{
2760 if (tc >= dev->num_tc)
2761 return -EINVAL;
2762
Alexander Duyck6234f872016-10-28 11:46:49 -04002763#ifdef CONFIG_XPS
2764 netif_reset_xps_queues(dev, offset, count);
2765#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002766 dev->tc_to_txq[tc].count = count;
2767 dev->tc_to_txq[tc].offset = offset;
2768 return 0;
2769}
2770EXPORT_SYMBOL(netdev_set_tc_queue);
2771
2772int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2773{
2774 if (num_tc > TC_MAX_QUEUE)
2775 return -EINVAL;
2776
Alexander Duyck6234f872016-10-28 11:46:49 -04002777#ifdef CONFIG_XPS
2778 netif_reset_xps_queues_gt(dev, 0);
2779#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002780 netdev_unbind_all_sb_channels(dev);
2781
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002782 dev->num_tc = num_tc;
2783 return 0;
2784}
2785EXPORT_SYMBOL(netdev_set_num_tc);
2786
Alexander Duyckffcfe252018-07-09 12:19:38 -04002787void netdev_unbind_sb_channel(struct net_device *dev,
2788 struct net_device *sb_dev)
2789{
2790 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2791
2792#ifdef CONFIG_XPS
2793 netif_reset_xps_queues_gt(sb_dev, 0);
2794#endif
2795 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2796 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2797
2798 while (txq-- != &dev->_tx[0]) {
2799 if (txq->sb_dev == sb_dev)
2800 txq->sb_dev = NULL;
2801 }
2802}
2803EXPORT_SYMBOL(netdev_unbind_sb_channel);
2804
2805int netdev_bind_sb_channel_queue(struct net_device *dev,
2806 struct net_device *sb_dev,
2807 u8 tc, u16 count, u16 offset)
2808{
2809 /* Make certain the sb_dev and dev are already configured */
2810 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2811 return -EINVAL;
2812
2813 /* We cannot hand out queues we don't have */
2814 if ((offset + count) > dev->real_num_tx_queues)
2815 return -EINVAL;
2816
2817 /* Record the mapping */
2818 sb_dev->tc_to_txq[tc].count = count;
2819 sb_dev->tc_to_txq[tc].offset = offset;
2820
2821 /* Provide a way for Tx queue to find the tc_to_txq map or
2822 * XPS map for itself.
2823 */
2824 while (count--)
2825 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2826
2827 return 0;
2828}
2829EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2830
2831int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2832{
2833 /* Do not use a multiqueue device to represent a subordinate channel */
2834 if (netif_is_multiqueue(dev))
2835 return -ENODEV;
2836
2837 /* We allow channels 1 - 32767 to be used for subordinate channels.
2838 * Channel 0 is meant to be "native" mode and used only to represent
2839 * the main root device. We allow writing 0 to reset the device back
2840 * to normal mode after being used as a subordinate channel.
2841 */
2842 if (channel > S16_MAX)
2843 return -EINVAL;
2844
2845 dev->num_tc = -channel;
2846
2847 return 0;
2848}
2849EXPORT_SYMBOL(netdev_set_sb_channel);
2850
John Fastabendf0796d52010-07-01 13:21:57 +00002851/*
2852 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002853 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002854 */
Tom Herberte6484932010-10-18 18:04:39 +00002855int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002856{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002857 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002858 int rc;
2859
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002860 disabling = txq < dev->real_num_tx_queues;
2861
Tom Herberte6484932010-10-18 18:04:39 +00002862 if (txq < 1 || txq > dev->num_tx_queues)
2863 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002864
Ben Hutchings5c565802011-02-15 19:39:21 +00002865 if (dev->reg_state == NETREG_REGISTERED ||
2866 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002867 ASSERT_RTNL();
2868
Tom Herbert1d24eb42010-11-21 13:17:27 +00002869 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2870 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002871 if (rc)
2872 return rc;
2873
John Fastabend4f57c082011-01-17 08:06:04 +00002874 if (dev->num_tc)
2875 netif_setup_tc(dev, txq);
2876
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002877 dev->real_num_tx_queues = txq;
2878
2879 if (disabling) {
2880 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002881 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002882#ifdef CONFIG_XPS
2883 netif_reset_xps_queues_gt(dev, txq);
2884#endif
2885 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002886 } else {
2887 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002888 }
Tom Herberte6484932010-10-18 18:04:39 +00002889
Tom Herberte6484932010-10-18 18:04:39 +00002890 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002891}
2892EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002893
Michael Daltona953be52014-01-16 22:23:28 -08002894#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002895/**
2896 * netif_set_real_num_rx_queues - set actual number of RX queues used
2897 * @dev: Network device
2898 * @rxq: Actual number of RX queues
2899 *
2900 * This must be called either with the rtnl_lock held or before
2901 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002902 * negative error code. If called before registration, it always
2903 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002904 */
2905int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2906{
2907 int rc;
2908
Tom Herbertbd25fa72010-10-18 18:00:16 +00002909 if (rxq < 1 || rxq > dev->num_rx_queues)
2910 return -EINVAL;
2911
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002912 if (dev->reg_state == NETREG_REGISTERED) {
2913 ASSERT_RTNL();
2914
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002915 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2916 rxq);
2917 if (rc)
2918 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002919 }
2920
2921 dev->real_num_rx_queues = rxq;
2922 return 0;
2923}
2924EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2925#endif
2926
Ben Hutchings2c530402012-07-10 10:55:09 +00002927/**
2928 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002929 *
2930 * This routine should set an upper limit on the number of RSS queues
2931 * used by default by multiqueue devices.
2932 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002933int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002934{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302935 return is_kdump_kernel() ?
2936 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002937}
2938EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2939
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002940static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002941{
2942 struct softnet_data *sd;
2943 unsigned long flags;
2944
2945 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002946 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002947 q->next_sched = NULL;
2948 *sd->output_queue_tailp = q;
2949 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002950 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2951 local_irq_restore(flags);
2952}
2953
David S. Miller37437bb2008-07-16 02:15:04 -07002954void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002955{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002956 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2957 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002958}
2959EXPORT_SYMBOL(__netif_schedule);
2960
Eric Dumazete6247022013-12-05 04:45:08 -08002961struct dev_kfree_skb_cb {
2962 enum skb_free_reason reason;
2963};
2964
2965static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002966{
Eric Dumazete6247022013-12-05 04:45:08 -08002967 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002968}
Denis Vlasenko56079432006-03-29 15:57:29 -08002969
John Fastabend46e5da40a2014-09-12 20:04:52 -07002970void netif_schedule_queue(struct netdev_queue *txq)
2971{
2972 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03002973 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07002974 struct Qdisc *q = rcu_dereference(txq->qdisc);
2975
2976 __netif_schedule(q);
2977 }
2978 rcu_read_unlock();
2979}
2980EXPORT_SYMBOL(netif_schedule_queue);
2981
John Fastabend46e5da40a2014-09-12 20:04:52 -07002982void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2983{
2984 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2985 struct Qdisc *q;
2986
2987 rcu_read_lock();
2988 q = rcu_dereference(dev_queue->qdisc);
2989 __netif_schedule(q);
2990 rcu_read_unlock();
2991 }
2992}
2993EXPORT_SYMBOL(netif_tx_wake_queue);
2994
Eric Dumazete6247022013-12-05 04:45:08 -08002995void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2996{
2997 unsigned long flags;
2998
Myungho Jung98998862017-04-25 11:58:15 -07002999 if (unlikely(!skb))
3000 return;
3001
Reshetova, Elena63354792017-06-30 13:07:58 +03003002 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08003003 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03003004 refcount_set(&skb->users, 0);
3005 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08003006 return;
3007 }
3008 get_kfree_skb_cb(skb)->reason = reason;
3009 local_irq_save(flags);
3010 skb->next = __this_cpu_read(softnet_data.completion_queue);
3011 __this_cpu_write(softnet_data.completion_queue, skb);
3012 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3013 local_irq_restore(flags);
3014}
3015EXPORT_SYMBOL(__dev_kfree_skb_irq);
3016
3017void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08003018{
3019 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08003020 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08003021 else
3022 dev_kfree_skb(skb);
3023}
Eric Dumazete6247022013-12-05 04:45:08 -08003024EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08003025
3026
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003027/**
3028 * netif_device_detach - mark device as removed
3029 * @dev: network device
3030 *
3031 * Mark device as removed from system and therefore no longer available.
3032 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003033void netif_device_detach(struct net_device *dev)
3034{
3035 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3036 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003037 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003038 }
3039}
3040EXPORT_SYMBOL(netif_device_detach);
3041
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003042/**
3043 * netif_device_attach - mark device as attached
3044 * @dev: network device
3045 *
3046 * Mark device as attached from system and restart if needed.
3047 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003048void netif_device_attach(struct net_device *dev)
3049{
3050 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3051 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003052 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003053 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003054 }
3055}
3056EXPORT_SYMBOL(netif_device_attach);
3057
Jiri Pirko5605c762015-05-12 14:56:12 +02003058/*
3059 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3060 * to be used as a distribution range.
3061 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003062static u16 skb_tx_hash(const struct net_device *dev,
3063 const struct net_device *sb_dev,
3064 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003065{
3066 u32 hash;
3067 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003068 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003069
Alexander Duyckeadec8772018-07-09 12:19:48 -04003070 if (dev->num_tc) {
3071 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3072
3073 qoffset = sb_dev->tc_to_txq[tc].offset;
3074 qcount = sb_dev->tc_to_txq[tc].count;
3075 }
3076
Jiri Pirko5605c762015-05-12 14:56:12 +02003077 if (skb_rx_queue_recorded(skb)) {
3078 hash = skb_get_rx_queue(skb);
Amritha Nambiar6e11d152020-02-24 10:56:00 -08003079 if (hash >= qoffset)
3080 hash -= qoffset;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003081 while (unlikely(hash >= qcount))
3082 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003083 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003084 }
3085
3086 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3087}
Jiri Pirko5605c762015-05-12 14:56:12 +02003088
Ben Hutchings36c92472012-01-17 07:57:56 +00003089static void skb_warn_bad_offload(const struct sk_buff *skb)
3090{
Wei Tang84d15ae2016-06-16 21:17:49 +08003091 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003092 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003093 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003094
Ben Greearc846ad92013-04-19 10:45:52 +00003095 if (!net_ratelimit())
3096 return;
3097
Bjørn Mork88ad4172015-11-16 19:16:40 +01003098 if (dev) {
3099 if (dev->dev.parent)
3100 name = dev_driver_string(dev->dev.parent);
3101 else
3102 name = netdev_name(dev);
3103 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003104 skb_dump(KERN_WARNING, skb, false);
3105 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003106 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003107 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003108}
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110/*
3111 * Invalidate hardware checksum when packet is to be mangled, and
3112 * complete checksum manually on outgoing path.
3113 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003114int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Al Virod3bc23e2006-11-14 21:24:49 -08003116 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003117 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Patrick McHardy84fa7932006-08-29 16:44:56 -07003119 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003120 goto out_set_summed;
3121
3122 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003123 skb_warn_bad_offload(skb);
3124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126
Eric Dumazetcef401d2013-01-25 20:34:37 +00003127 /* Before computing a checksum, we should make sure no frag could
3128 * be modified by an external entity : checksum could be wrong.
3129 */
3130 if (skb_has_shared_frag(skb)) {
3131 ret = __skb_linearize(skb);
3132 if (ret)
3133 goto out;
3134 }
3135
Michał Mirosław55508d62010-12-14 15:24:08 +00003136 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003137 BUG_ON(offset >= skb_headlen(skb));
3138 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3139
3140 offset += skb->csum_offset;
3141 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3142
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003143 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3144 if (ret)
3145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003147 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003148out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003150out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return ret;
3152}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003153EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Davide Carattib72b5bf2017-05-18 15:44:38 +02003155int skb_crc32c_csum_help(struct sk_buff *skb)
3156{
3157 __le32 crc32c_csum;
3158 int ret = 0, offset, start;
3159
3160 if (skb->ip_summed != CHECKSUM_PARTIAL)
3161 goto out;
3162
3163 if (unlikely(skb_is_gso(skb)))
3164 goto out;
3165
3166 /* Before computing a checksum, we should make sure no frag could
3167 * be modified by an external entity : checksum could be wrong.
3168 */
3169 if (unlikely(skb_has_shared_frag(skb))) {
3170 ret = __skb_linearize(skb);
3171 if (ret)
3172 goto out;
3173 }
3174 start = skb_checksum_start_offset(skb);
3175 offset = start + offsetof(struct sctphdr, checksum);
3176 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3177 ret = -EINVAL;
3178 goto out;
3179 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003180
3181 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3182 if (ret)
3183 goto out;
3184
Davide Carattib72b5bf2017-05-18 15:44:38 +02003185 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3186 skb->len - start, ~(__u32)0,
3187 crc32c_csum_stub));
3188 *(__le32 *)(skb->data + offset) = crc32c_csum;
3189 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003190 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003191out:
3192 return ret;
3193}
3194
Vlad Yasevich53d64712014-03-27 17:26:18 -04003195__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003196{
3197 __be16 type = skb->protocol;
3198
Pravin B Shelar19acc322013-05-07 20:41:07 +00003199 /* Tunnel gso handlers can set protocol to ethernet. */
3200 if (type == htons(ETH_P_TEB)) {
3201 struct ethhdr *eth;
3202
3203 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3204 return 0;
3205
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003206 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003207 type = eth->h_proto;
3208 }
3209
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003210 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003211}
3212
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003213/**
3214 * skb_mac_gso_segment - mac layer segmentation handler.
3215 * @skb: buffer to segment
3216 * @features: features for the output path (see dev->features)
3217 */
3218struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3219 netdev_features_t features)
3220{
3221 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3222 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003223 int vlan_depth = skb->mac_len;
3224 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003225
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003226 if (unlikely(!type))
3227 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003228
Vlad Yasevich53d64712014-03-27 17:26:18 -04003229 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003230
3231 rcu_read_lock();
3232 list_for_each_entry_rcu(ptype, &offload_base, list) {
3233 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003234 segs = ptype->callbacks.gso_segment(skb, features);
3235 break;
3236 }
3237 }
3238 rcu_read_unlock();
3239
3240 __skb_push(skb, skb->data - skb_mac_header(skb));
3241
3242 return segs;
3243}
3244EXPORT_SYMBOL(skb_mac_gso_segment);
3245
3246
Cong Wang12b00042013-02-05 16:36:38 +00003247/* openvswitch calls this on rx path, so we need a different check.
3248 */
3249static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3250{
3251 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003252 return skb->ip_summed != CHECKSUM_PARTIAL &&
3253 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003254
3255 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003256}
3257
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003258/**
Cong Wang12b00042013-02-05 16:36:38 +00003259 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003260 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003261 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003262 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003263 *
3264 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003265 *
3266 * It may return NULL if the skb requires no segmentation. This is
3267 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003268 *
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003269 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003270 */
Cong Wang12b00042013-02-05 16:36:38 +00003271struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3272 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003273{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003274 struct sk_buff *segs;
3275
Cong Wang12b00042013-02-05 16:36:38 +00003276 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003277 int err;
3278
Eric Dumazetb2504a52017-01-31 10:20:32 -08003279 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003280 err = skb_cow_head(skb, 0);
3281 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003282 return ERR_PTR(err);
3283 }
3284
Alexander Duyck802ab552016-04-10 21:45:03 -04003285 /* Only report GSO partial support if it will enable us to
3286 * support segmentation on this frame without needing additional
3287 * work.
3288 */
3289 if (features & NETIF_F_GSO_PARTIAL) {
3290 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3291 struct net_device *dev = skb->dev;
3292
3293 partial_features |= dev->features & dev->gso_partial_features;
3294 if (!skb_gso_ok(skb, features | partial_features))
3295 features &= ~NETIF_F_GSO_PARTIAL;
3296 }
3297
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003298 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003299 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3300
Pravin B Shelar68c33162013-02-14 14:02:41 +00003301 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003302 SKB_GSO_CB(skb)->encap_level = 0;
3303
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003304 skb_reset_mac_header(skb);
3305 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003306
Eric Dumazetb2504a52017-01-31 10:20:32 -08003307 segs = skb_mac_gso_segment(skb, features);
3308
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003309 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003310 skb_warn_bad_offload(skb);
3311
3312 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003313}
Cong Wang12b00042013-02-05 16:36:38 +00003314EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003315
Herbert Xufb286bb2005-11-10 13:01:24 -08003316/* Take action when hardware reception checksum errors are detected. */
3317#ifdef CONFIG_BUG
Cong Wang7fe50ac2018-11-12 14:47:18 -08003318void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003319{
3320 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003321 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Willem de Bruijn64131392019-07-07 05:51:55 -04003322 skb_dump(KERN_ERR, skb, true);
Herbert Xufb286bb2005-11-10 13:01:24 -08003323 dump_stack();
3324 }
3325}
3326EXPORT_SYMBOL(netdev_rx_csum_fault);
3327#endif
3328
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003329/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003330static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003332#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 int i;
tchardingf4563a72017-02-09 17:56:07 +11003334
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003335 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003336 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3337 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003338
Ian Campbellea2ab692011-08-22 23:44:58 +00003339 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003340 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003341 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003342 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003343#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return 0;
3345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Simon Horman3b392dd2014-06-04 08:53:17 +09003347/* If MPLS offload request, verify we are testing hardware MPLS features
3348 * instead of standard features for the netdev.
3349 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003350#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003351static netdev_features_t net_mpls_features(struct sk_buff *skb,
3352 netdev_features_t features,
3353 __be16 type)
3354{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003355 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003356 features &= skb->dev->mpls_features;
3357
3358 return features;
3359}
3360#else
3361static netdev_features_t net_mpls_features(struct sk_buff *skb,
3362 netdev_features_t features,
3363 __be16 type)
3364{
3365 return features;
3366}
3367#endif
3368
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003369static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003370 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003371{
Vlad Yasevich53d64712014-03-27 17:26:18 -04003372 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09003373 __be16 type;
3374
3375 type = skb_network_protocol(skb, &tmp);
3376 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003377
Ed Cashinc0d680e2012-09-19 15:49:00 +00003378 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003379 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003380 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003381 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003382 if (illegal_highdma(skb->dev, skb))
3383 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003384
3385 return features;
3386}
3387
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003388netdev_features_t passthru_features_check(struct sk_buff *skb,
3389 struct net_device *dev,
3390 netdev_features_t features)
3391{
3392 return features;
3393}
3394EXPORT_SYMBOL(passthru_features_check);
3395
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003396static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003397 struct net_device *dev,
3398 netdev_features_t features)
3399{
3400 return vlan_features_check(skb, features);
3401}
3402
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003403static netdev_features_t gso_features_check(const struct sk_buff *skb,
3404 struct net_device *dev,
3405 netdev_features_t features)
3406{
3407 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3408
3409 if (gso_segs > dev->gso_max_segs)
3410 return features & ~NETIF_F_GSO_MASK;
3411
Alexander Duyck802ab552016-04-10 21:45:03 -04003412 /* Support for GSO partial features requires software
3413 * intervention before we can actually process the packets
3414 * so we need to strip support for any partial features now
3415 * and we can pull them back in after we have partially
3416 * segmented the frame.
3417 */
3418 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3419 features &= ~dev->gso_partial_features;
3420
3421 /* Make sure to clear the IPv4 ID mangling feature if the
3422 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003423 */
3424 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3425 struct iphdr *iph = skb->encapsulation ?
3426 inner_ip_hdr(skb) : ip_hdr(skb);
3427
3428 if (!(iph->frag_off & htons(IP_DF)))
3429 features &= ~NETIF_F_TSO_MANGLEID;
3430 }
3431
3432 return features;
3433}
3434
Florian Westphalc1e756b2014-05-05 15:00:44 +02003435netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003436{
Jesse Gross5f352272014-12-23 22:37:26 -08003437 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003438 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003439
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003440 if (skb_is_gso(skb))
3441 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003442
Jesse Gross5f352272014-12-23 22:37:26 -08003443 /* If encapsulation offload request, verify we are testing
3444 * hardware encapsulation features instead of standard
3445 * features for the netdev
3446 */
3447 if (skb->encapsulation)
3448 features &= dev->hw_enc_features;
3449
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003450 if (skb_vlan_tagged(skb))
3451 features = netdev_intersect_features(features,
3452 dev->vlan_features |
3453 NETIF_F_HW_VLAN_CTAG_TX |
3454 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003455
Jesse Gross5f352272014-12-23 22:37:26 -08003456 if (dev->netdev_ops->ndo_features_check)
3457 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3458 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003459 else
3460 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003461
Florian Westphalc1e756b2014-05-05 15:00:44 +02003462 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003463}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003464EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003465
David S. Miller2ea25512014-08-29 21:10:01 -07003466static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003467 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003468{
David S. Miller2ea25512014-08-29 21:10:01 -07003469 unsigned int len;
3470 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003471
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003472 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003473 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003474
David S. Miller2ea25512014-08-29 21:10:01 -07003475 len = skb->len;
3476 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003477 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003478 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003479
Patrick McHardy572a9d72009-11-10 06:14:14 +00003480 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003481}
David S. Miller2ea25512014-08-29 21:10:01 -07003482
David S. Miller8dcda222014-09-01 15:06:40 -07003483struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3484 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003485{
3486 struct sk_buff *skb = first;
3487 int rc = NETDEV_TX_OK;
3488
3489 while (skb) {
3490 struct sk_buff *next = skb->next;
3491
David S. Millera8305bf2018-07-29 20:42:53 -07003492 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003493 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003494 if (unlikely(!dev_xmit_complete(rc))) {
3495 skb->next = next;
3496 goto out;
3497 }
3498
3499 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003500 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003501 rc = NETDEV_TX_BUSY;
3502 break;
3503 }
3504 }
3505
3506out:
3507 *ret = rc;
3508 return skb;
3509}
3510
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003511static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3512 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003513{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003514 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003515 !vlan_hw_offload_capable(features, skb->vlan_proto))
3516 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003517 return skb;
3518}
3519
Davide Caratti43c26a12017-05-18 15:44:41 +02003520int skb_csum_hwoffload_help(struct sk_buff *skb,
3521 const netdev_features_t features)
3522{
3523 if (unlikely(skb->csum_not_inet))
3524 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3525 skb_crc32c_csum_help(skb);
3526
3527 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3528}
3529EXPORT_SYMBOL(skb_csum_hwoffload_help);
3530
Steffen Klassertf53c7232017-12-20 10:41:36 +01003531static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003532{
3533 netdev_features_t features;
3534
David S. Millereae3f882014-08-30 15:17:13 -07003535 features = netif_skb_features(skb);
3536 skb = validate_xmit_vlan(skb, features);
3537 if (unlikely(!skb))
3538 goto out_null;
3539
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003540 skb = sk_validate_xmit_skb(skb, dev);
3541 if (unlikely(!skb))
3542 goto out_null;
3543
Johannes Berg8b86a612015-04-17 15:45:04 +02003544 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003545 struct sk_buff *segs;
3546
3547 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003548 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003549 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003550 } else if (segs) {
3551 consume_skb(skb);
3552 skb = segs;
3553 }
David S. Millereae3f882014-08-30 15:17:13 -07003554 } else {
3555 if (skb_needs_linearize(skb, features) &&
3556 __skb_linearize(skb))
3557 goto out_kfree_skb;
3558
3559 /* If packet is not checksummed and device does not
3560 * support checksumming for this protocol, complete
3561 * checksumming here.
3562 */
3563 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3564 if (skb->encapsulation)
3565 skb_set_inner_transport_header(skb,
3566 skb_checksum_start_offset(skb));
3567 else
3568 skb_set_transport_header(skb,
3569 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003570 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003571 goto out_kfree_skb;
3572 }
3573 }
3574
Steffen Klassertf53c7232017-12-20 10:41:36 +01003575 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003576
David S. Millereae3f882014-08-30 15:17:13 -07003577 return skb;
3578
3579out_kfree_skb:
3580 kfree_skb(skb);
3581out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003582 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003583 return NULL;
3584}
3585
Steffen Klassertf53c7232017-12-20 10:41:36 +01003586struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003587{
3588 struct sk_buff *next, *head = NULL, *tail;
3589
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003590 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003591 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003592 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003593
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003594 /* in case skb wont be segmented, point to itself */
3595 skb->prev = skb;
3596
Steffen Klassertf53c7232017-12-20 10:41:36 +01003597 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003598 if (!skb)
3599 continue;
3600
3601 if (!head)
3602 head = skb;
3603 else
3604 tail->next = skb;
3605 /* If skb was segmented, skb->prev points to
3606 * the last segment. If not, it still contains skb.
3607 */
3608 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003609 }
3610 return head;
3611}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003612EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003613
Eric Dumazet1def9232013-01-10 12:36:42 +00003614static void qdisc_pkt_len_init(struct sk_buff *skb)
3615{
3616 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3617
3618 qdisc_skb_cb(skb)->pkt_len = skb->len;
3619
3620 /* To get more precise estimation of bytes sent on wire,
3621 * we add to pkt_len the headers size of all segments
3622 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003623 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003624 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003625 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003626
Eric Dumazet757b8b12013-01-15 21:14:21 -08003627 /* mac layer + network layer */
3628 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3629
3630 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003631 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3632 const struct tcphdr *th;
3633 struct tcphdr _tcphdr;
3634
3635 th = skb_header_pointer(skb, skb_transport_offset(skb),
3636 sizeof(_tcphdr), &_tcphdr);
3637 if (likely(th))
3638 hdr_len += __tcp_hdrlen(th);
3639 } else {
3640 struct udphdr _udphdr;
3641
3642 if (skb_header_pointer(skb, skb_transport_offset(skb),
3643 sizeof(_udphdr), &_udphdr))
3644 hdr_len += sizeof(struct udphdr);
3645 }
Jason Wang15e5a032013-03-25 20:19:59 +00003646
3647 if (shinfo->gso_type & SKB_GSO_DODGY)
3648 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3649 shinfo->gso_size);
3650
3651 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003652 }
3653}
3654
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003655static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3656 struct net_device *dev,
3657 struct netdev_queue *txq)
3658{
3659 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003660 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003661 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003662 int rc;
3663
Eric Dumazeta2da5702011-01-20 03:48:19 +00003664 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003665
3666 if (q->flags & TCQ_F_NOLOCK) {
Paolo Abeni379349e2020-02-18 18:15:44 +01003667 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
3668 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003669
3670 if (unlikely(to_free))
3671 kfree_skb_list(to_free);
3672 return rc;
3673 }
3674
Eric Dumazet79640a42010-06-02 05:09:29 -07003675 /*
3676 * Heuristic to force contended enqueues to serialize on a
3677 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003678 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003679 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003680 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003681 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003682 if (unlikely(contended))
3683 spin_lock(&q->busylock);
3684
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003685 spin_lock(root_lock);
3686 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003687 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003688 rc = NET_XMIT_DROP;
3689 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003690 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003691 /*
3692 * This is a work-conserving queue; there are no old skbs
3693 * waiting to be sent out; and the qdisc is not running -
3694 * xmit the skb directly.
3695 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003696
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003697 qdisc_bstats_update(q, skb);
3698
Eric Dumazet55a93b32014-10-03 15:31:07 -07003699 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003700 if (unlikely(contended)) {
3701 spin_unlock(&q->busylock);
3702 contended = false;
3703 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003704 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003705 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003706
John Fastabend6c148182017-12-07 09:54:06 -08003707 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003708 rc = NET_XMIT_SUCCESS;
3709 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003710 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003711 if (qdisc_run_begin(q)) {
3712 if (unlikely(contended)) {
3713 spin_unlock(&q->busylock);
3714 contended = false;
3715 }
3716 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003717 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003718 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003719 }
3720 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003721 if (unlikely(to_free))
3722 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003723 if (unlikely(contended))
3724 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003725 return rc;
3726}
3727
Daniel Borkmann86f85152013-12-29 17:27:11 +01003728#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003729static void skb_update_prio(struct sk_buff *skb)
3730{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003731 const struct netprio_map *map;
3732 const struct sock *sk;
3733 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003734
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003735 if (skb->priority)
3736 return;
3737 map = rcu_dereference_bh(skb->dev->priomap);
3738 if (!map)
3739 return;
3740 sk = skb_to_full_sk(skb);
3741 if (!sk)
3742 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003743
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003744 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3745
3746 if (prioidx < map->priomap_len)
3747 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003748}
3749#else
3750#define skb_update_prio(skb)
3751#endif
3752
Dave Jonesd29f7492008-07-22 14:09:06 -07003753/**
Michel Machado95603e22012-06-12 10:16:35 +00003754 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003755 * @net: network namespace this loopback is happening in
3756 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003757 * @skb: buffer to transmit
3758 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003759int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003760{
3761 skb_reset_mac_header(skb);
3762 __skb_pull(skb, skb_network_offset(skb));
3763 skb->pkt_type = PACKET_LOOPBACK;
3764 skb->ip_summed = CHECKSUM_UNNECESSARY;
3765 WARN_ON(!skb_dst(skb));
3766 skb_dst_force(skb);
3767 netif_rx_ni(skb);
3768 return 0;
3769}
3770EXPORT_SYMBOL(dev_loopback_xmit);
3771
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003772#ifdef CONFIG_NET_EGRESS
3773static struct sk_buff *
3774sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3775{
Jiri Pirko46209402017-11-03 11:46:25 +01003776 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003777 struct tcf_result cl_res;
3778
Jiri Pirko46209402017-11-03 11:46:25 +01003779 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003780 return skb;
3781
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003782 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Jiri Pirko46209402017-11-03 11:46:25 +01003783 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003784
Jiri Pirko46209402017-11-03 11:46:25 +01003785 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003786 case TC_ACT_OK:
3787 case TC_ACT_RECLASSIFY:
3788 skb->tc_index = TC_H_MIN(cl_res.classid);
3789 break;
3790 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003791 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003792 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003793 kfree_skb(skb);
3794 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003795 case TC_ACT_STOLEN:
3796 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003797 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003798 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003799 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003800 return NULL;
3801 case TC_ACT_REDIRECT:
3802 /* No need to push/pop skb's mac_header here on egress! */
3803 skb_do_redirect(skb);
3804 *ret = NET_XMIT_SUCCESS;
3805 return NULL;
3806 default:
3807 break;
3808 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01003809
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003810 return skb;
3811}
3812#endif /* CONFIG_NET_EGRESS */
3813
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003814#ifdef CONFIG_XPS
3815static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3816 struct xps_dev_maps *dev_maps, unsigned int tci)
3817{
3818 struct xps_map *map;
3819 int queue_index = -1;
3820
3821 if (dev->num_tc) {
3822 tci *= dev->num_tc;
3823 tci += netdev_get_prio_tc_map(dev, skb->priority);
3824 }
3825
3826 map = rcu_dereference(dev_maps->attr_map[tci]);
3827 if (map) {
3828 if (map->len == 1)
3829 queue_index = map->queues[0];
3830 else
3831 queue_index = map->queues[reciprocal_scale(
3832 skb_get_hash(skb), map->len)];
3833 if (unlikely(queue_index >= dev->real_num_tx_queues))
3834 queue_index = -1;
3835 }
3836 return queue_index;
3837}
3838#endif
3839
Alexander Duyckeadec8772018-07-09 12:19:48 -04003840static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3841 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003842{
3843#ifdef CONFIG_XPS
3844 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003845 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003846 int queue_index = -1;
3847
Amritha Nambiar04157462018-06-29 21:26:46 -07003848 if (!static_key_false(&xps_needed))
3849 return -1;
3850
Jiri Pirko638b2a62015-05-12 14:56:13 +02003851 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003852 if (!static_key_false(&xps_rxqs_needed))
3853 goto get_cpus_map;
3854
Alexander Duyckeadec8772018-07-09 12:19:48 -04003855 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003856 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003857 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003858
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003859 if (tci >= 0 && tci < dev->num_rx_queues)
3860 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3861 tci);
3862 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003863
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003864get_cpus_map:
3865 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003866 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003867 if (dev_maps) {
3868 unsigned int tci = skb->sender_cpu - 1;
3869
3870 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3871 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003872 }
3873 }
3874 rcu_read_unlock();
3875
3876 return queue_index;
3877#else
3878 return -1;
3879#endif
3880}
3881
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003882u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003883 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003884{
3885 return 0;
3886}
3887EXPORT_SYMBOL(dev_pick_tx_zero);
3888
3889u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003890 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003891{
3892 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3893}
3894EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3895
Paolo Abenib71b5832019-03-20 11:02:05 +01003896u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3897 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003898{
3899 struct sock *sk = skb->sk;
3900 int queue_index = sk_tx_queue_get(sk);
3901
Alexander Duyckeadec8772018-07-09 12:19:48 -04003902 sb_dev = sb_dev ? : dev;
3903
Jiri Pirko638b2a62015-05-12 14:56:13 +02003904 if (queue_index < 0 || skb->ooo_okay ||
3905 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003906 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003907
Jiri Pirko638b2a62015-05-12 14:56:13 +02003908 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003909 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003910
3911 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003912 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003913 rcu_access_pointer(sk->sk_dst_cache))
3914 sk_tx_queue_set(sk, new_index);
3915
3916 queue_index = new_index;
3917 }
3918
3919 return queue_index;
3920}
Paolo Abenib71b5832019-03-20 11:02:05 +01003921EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003922
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003923struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
3924 struct sk_buff *skb,
3925 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003926{
3927 int queue_index = 0;
3928
3929#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003930 u32 sender_cpu = skb->sender_cpu - 1;
3931
3932 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003933 skb->sender_cpu = raw_smp_processor_id() + 1;
3934#endif
3935
3936 if (dev->real_num_tx_queues != 1) {
3937 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003938
Jiri Pirko638b2a62015-05-12 14:56:13 +02003939 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01003940 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003941 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003942 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003943
Alexander Duyckd5845272017-11-22 10:57:41 -08003944 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003945 }
3946
3947 skb_set_queue_mapping(skb, queue_index);
3948 return netdev_get_tx_queue(dev, queue_index);
3949}
3950
Michel Machado95603e22012-06-12 10:16:35 +00003951/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003952 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003953 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04003954 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003955 *
3956 * Queue a buffer for transmission to a network device. The caller must
3957 * have set the device and priority and built the buffer before calling
3958 * this function. The function can be called from an interrupt.
3959 *
3960 * A negative errno code is returned on a failure. A success does not
3961 * guarantee the frame will be transmitted as it may be dropped due
3962 * to congestion or traffic shaping.
3963 *
3964 * -----------------------------------------------------------------------------------
3965 * I notice this method can also return errors from the queue disciplines,
3966 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3967 * be positive.
3968 *
3969 * Regardless of the return value, the skb is consumed, so it is currently
3970 * difficult to retry a send to this method. (You can bump the ref count
3971 * before sending to hold a reference for retry if you are careful.)
3972 *
3973 * When calling this method, interrupts MUST be enabled. This is because
3974 * the BH enable code must have IRQs enabled so that it will not deadlock.
3975 * --BLG
3976 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003977static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
3979 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003980 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 struct Qdisc *q;
3982 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01003983 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003985 skb_reset_mac_header(skb);
3986
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003987 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3988 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3989
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003990 /* Disable soft irqs for various locks below. Also
3991 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003993 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Neil Horman5bc14212011-11-22 05:10:51 +00003995 skb_update_prio(skb);
3996
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003997 qdisc_pkt_len_init(skb);
3998#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003999 skb->tc_at_ingress = 0;
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004000# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004001 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004002 skb = sch_handle_egress(skb, &rc, dev);
4003 if (!skb)
4004 goto out;
4005 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004006# endif
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004007#endif
Eric Dumazet02875872014-10-05 18:38:35 -07004008 /* If device/qdisc don't need skb->dst, release it right now while
4009 * its hot in this cpu cache.
4010 */
4011 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4012 skb_dst_drop(skb);
4013 else
4014 skb_dst_force(skb);
4015
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004016 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08004017 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07004018
Koki Sanagicf66ba52010-08-23 18:45:02 +09004019 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00004021 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07004022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
4025 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11004026 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
tchardingeb13da12017-02-09 17:56:06 +11004028 * Really, it is unlikely that netif_tx_lock protection is necessary
4029 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4030 * counters.)
4031 * However, it is possible, that they rely on protection
4032 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
tchardingeb13da12017-02-09 17:56:06 +11004034 * Check this and shot the lock. It is not prone from deadlocks.
4035 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 */
4037 if (dev->flags & IFF_UP) {
4038 int cpu = smp_processor_id(); /* ok because BHs are off */
4039
David S. Millerc773e842008-07-08 23:13:53 -07004040 if (txq->xmit_lock_owner != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004041 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004042 goto recursion_alert;
4043
Steffen Klassertf53c7232017-12-20 10:41:36 +01004044 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004045 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004046 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004047
David S. Millerc773e842008-07-08 23:13:53 -07004048 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Tom Herbert734664982011-11-28 16:32:44 +00004050 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004051 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004052 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004053 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004054 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004055 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 goto out;
4057 }
4058 }
David S. Millerc773e842008-07-08 23:13:53 -07004059 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004060 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4061 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 } else {
4063 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004064 * unfortunately
4065 */
4066recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004067 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4068 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070 }
4071
4072 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004073 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Eric Dumazet015f0682014-03-27 08:45:56 -07004075 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004076 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return rc;
4078out:
Herbert Xud4828d82006-06-22 02:28:18 -07004079 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return rc;
4081}
Jason Wangf663dd92014-01-10 16:18:26 +08004082
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004083int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004084{
4085 return __dev_queue_xmit(skb, NULL);
4086}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004087EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Alexander Duyckeadec8772018-07-09 12:19:48 -04004089int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004090{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004091 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004092}
4093EXPORT_SYMBOL(dev_queue_xmit_accel);
4094
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004095int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4096{
4097 struct net_device *dev = skb->dev;
4098 struct sk_buff *orig_skb = skb;
4099 struct netdev_queue *txq;
4100 int ret = NETDEV_TX_BUSY;
4101 bool again = false;
4102
4103 if (unlikely(!netif_running(dev) ||
4104 !netif_carrier_ok(dev)))
4105 goto drop;
4106
4107 skb = validate_xmit_skb_list(skb, dev, &again);
4108 if (skb != orig_skb)
4109 goto drop;
4110
4111 skb_set_queue_mapping(skb, queue_id);
4112 txq = skb_get_tx_queue(dev, skb);
4113
4114 local_bh_disable();
4115
4116 HARD_TX_LOCK(dev, txq, smp_processor_id());
4117 if (!netif_xmit_frozen_or_drv_stopped(txq))
4118 ret = netdev_start_xmit(skb, dev, txq, false);
4119 HARD_TX_UNLOCK(dev, txq);
4120
4121 local_bh_enable();
4122
4123 if (!dev_xmit_complete(ret))
4124 kfree_skb(skb);
4125
4126 return ret;
4127drop:
4128 atomic_long_inc(&dev->tx_dropped);
4129 kfree_skb_list(skb);
4130 return NET_XMIT_DROP;
4131}
4132EXPORT_SYMBOL(dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
tchardingeb13da12017-02-09 17:56:06 +11004134/*************************************************************************
4135 * Receiver routines
4136 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004138int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004139EXPORT_SYMBOL(netdev_max_backlog);
4140
Eric Dumazet3b098e22010-05-15 23:57:10 -07004141int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004142int netdev_budget __read_mostly = 300;
Konstantin Khlebnikova48379802020-04-06 14:39:32 +03004143/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4144unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004145int weight_p __read_mostly = 64; /* old backlog weight */
4146int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4147int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4148int dev_rx_weight __read_mostly = 64;
4149int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004150/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4151int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004153/* Called with irq disabled */
4154static inline void ____napi_schedule(struct softnet_data *sd,
4155 struct napi_struct *napi)
4156{
4157 list_add_tail(&napi->poll_list, &sd->poll_list);
4158 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4159}
4160
Eric Dumazetdf334542010-03-24 19:13:54 +00004161#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004162
4163/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004164struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004165EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004166u32 rps_cpu_mask __read_mostly;
4167EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004168
Eric Dumazetdc053602019-03-22 08:56:38 -07004169struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004170EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004171struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004172EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004173
Ben Hutchingsc4454772011-01-19 11:03:53 +00004174static struct rps_dev_flow *
4175set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4176 struct rps_dev_flow *rflow, u16 next_cpu)
4177{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004178 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004179#ifdef CONFIG_RFS_ACCEL
4180 struct netdev_rx_queue *rxqueue;
4181 struct rps_dev_flow_table *flow_table;
4182 struct rps_dev_flow *old_rflow;
4183 u32 flow_id;
4184 u16 rxq_index;
4185 int rc;
4186
4187 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004188 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4189 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004190 goto out;
4191 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4192 if (rxq_index == skb_get_rx_queue(skb))
4193 goto out;
4194
4195 rxqueue = dev->_rx + rxq_index;
4196 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4197 if (!flow_table)
4198 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004199 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004200 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4201 rxq_index, flow_id);
4202 if (rc < 0)
4203 goto out;
4204 old_rflow = rflow;
4205 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004206 rflow->filter = rc;
4207 if (old_rflow->filter == rflow->filter)
4208 old_rflow->filter = RPS_NO_FILTER;
4209 out:
4210#endif
4211 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004212 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004213 }
4214
Ben Hutchings09994d12011-10-03 04:42:46 +00004215 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004216 return rflow;
4217}
4218
Tom Herbert0a9627f2010-03-16 08:03:29 +00004219/*
4220 * get_rps_cpu is called from netif_receive_skb and returns the target
4221 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004222 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004223 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004224static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4225 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004226{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004227 const struct rps_sock_flow_table *sock_flow_table;
4228 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004229 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004230 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004231 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004232 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004233 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004234
Tom Herbert0a9627f2010-03-16 08:03:29 +00004235 if (skb_rx_queue_recorded(skb)) {
4236 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004237
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004238 if (unlikely(index >= dev->real_num_rx_queues)) {
4239 WARN_ONCE(dev->real_num_rx_queues > 1,
4240 "%s received packet on queue %u, but number "
4241 "of RX queues is %u\n",
4242 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004243 goto done;
4244 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004245 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004246 }
4247
Eric Dumazet567e4b72015-02-06 12:59:01 -08004248 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4249
4250 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4251 map = rcu_dereference(rxqueue->rps_map);
4252 if (!flow_table && !map)
4253 goto done;
4254
Changli Gao2d47b452010-08-17 19:00:56 +00004255 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004256 hash = skb_get_hash(skb);
4257 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004258 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004259
Tom Herbertfec5e652010-04-16 16:01:27 -07004260 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4261 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004262 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004263 u32 next_cpu;
4264 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004265
Eric Dumazet567e4b72015-02-06 12:59:01 -08004266 /* First check into global flow table if there is a match */
4267 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4268 if ((ident ^ hash) & ~rps_cpu_mask)
4269 goto try_rps;
4270
4271 next_cpu = ident & rps_cpu_mask;
4272
4273 /* OK, now we know there is a match,
4274 * we can look at the local (per receive queue) flow table
4275 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004276 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004277 tcpu = rflow->cpu;
4278
Tom Herbertfec5e652010-04-16 16:01:27 -07004279 /*
4280 * If the desired CPU (where last recvmsg was done) is
4281 * different from current CPU (one in the rx-queue flow
4282 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004283 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004284 * - Current CPU is offline.
4285 * - The current CPU's queue tail has advanced beyond the
4286 * last packet that was enqueued using this table entry.
4287 * This guarantees that all previous packets for the flow
4288 * have been dequeued, thus preserving in order delivery.
4289 */
4290 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004291 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004292 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004293 rflow->last_qtail)) >= 0)) {
4294 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004295 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004296 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004297
Eric Dumazeta31196b2015-04-25 09:35:24 -07004298 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004299 *rflowp = rflow;
4300 cpu = tcpu;
4301 goto done;
4302 }
4303 }
4304
Eric Dumazet567e4b72015-02-06 12:59:01 -08004305try_rps:
4306
Tom Herbert0a9627f2010-03-16 08:03:29 +00004307 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004308 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004309 if (cpu_online(tcpu)) {
4310 cpu = tcpu;
4311 goto done;
4312 }
4313 }
4314
4315done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004316 return cpu;
4317}
4318
Ben Hutchingsc4454772011-01-19 11:03:53 +00004319#ifdef CONFIG_RFS_ACCEL
4320
4321/**
4322 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4323 * @dev: Device on which the filter was set
4324 * @rxq_index: RX queue index
4325 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4326 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4327 *
4328 * Drivers that implement ndo_rx_flow_steer() should periodically call
4329 * this function for each installed filter and remove the filters for
4330 * which it returns %true.
4331 */
4332bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4333 u32 flow_id, u16 filter_id)
4334{
4335 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4336 struct rps_dev_flow_table *flow_table;
4337 struct rps_dev_flow *rflow;
4338 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004339 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004340
4341 rcu_read_lock();
4342 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4343 if (flow_table && flow_id <= flow_table->mask) {
4344 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004345 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004346 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004347 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4348 rflow->last_qtail) <
4349 (int)(10 * flow_table->mask)))
4350 expire = false;
4351 }
4352 rcu_read_unlock();
4353 return expire;
4354}
4355EXPORT_SYMBOL(rps_may_expire_flow);
4356
4357#endif /* CONFIG_RFS_ACCEL */
4358
Tom Herbert0a9627f2010-03-16 08:03:29 +00004359/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004360static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004361{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004362 struct softnet_data *sd = data;
4363
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004364 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004365 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004366}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004367
Tom Herbertfec5e652010-04-16 16:01:27 -07004368#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004369
4370/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004371 * Check if this softnet_data structure is another cpu one
4372 * If yes, queue it to our IPI list and return 1
4373 * If no, return 0
4374 */
4375static int rps_ipi_queued(struct softnet_data *sd)
4376{
4377#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004378 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004379
4380 if (sd != mysd) {
4381 sd->rps_ipi_next = mysd->rps_ipi_list;
4382 mysd->rps_ipi_list = sd;
4383
4384 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4385 return 1;
4386 }
4387#endif /* CONFIG_RPS */
4388 return 0;
4389}
4390
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004391#ifdef CONFIG_NET_FLOW_LIMIT
4392int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4393#endif
4394
4395static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4396{
4397#ifdef CONFIG_NET_FLOW_LIMIT
4398 struct sd_flow_limit *fl;
4399 struct softnet_data *sd;
4400 unsigned int old_flow, new_flow;
4401
4402 if (qlen < (netdev_max_backlog >> 1))
4403 return false;
4404
Christoph Lameter903ceff2014-08-17 12:30:35 -05004405 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004406
4407 rcu_read_lock();
4408 fl = rcu_dereference(sd->flow_limit);
4409 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004410 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004411 old_flow = fl->history[fl->history_head];
4412 fl->history[fl->history_head] = new_flow;
4413
4414 fl->history_head++;
4415 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4416
4417 if (likely(fl->buckets[old_flow]))
4418 fl->buckets[old_flow]--;
4419
4420 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4421 fl->count++;
4422 rcu_read_unlock();
4423 return true;
4424 }
4425 }
4426 rcu_read_unlock();
4427#endif
4428 return false;
4429}
4430
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004431/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004432 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4433 * queue (may be a remote CPU queue).
4434 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004435static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4436 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004437{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004438 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004439 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004440 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004441
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004442 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004443
4444 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004445
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004446 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004447 if (!netif_running(skb->dev))
4448 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004449 qlen = skb_queue_len(&sd->input_pkt_queue);
4450 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004451 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004452enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004453 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004454 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004455 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004456 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004457 return NET_RX_SUCCESS;
4458 }
4459
Eric Dumazetebda37c22010-05-06 23:51:21 +00004460 /* Schedule NAPI for backlog device
4461 * We can use non atomic operation since we own the queue lock
4462 */
4463 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004464 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004465 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004466 }
4467 goto enqueue;
4468 }
4469
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004470drop:
Changli Gaodee42872010-05-02 05:42:16 +00004471 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004472 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004473
Tom Herbert0a9627f2010-03-16 08:03:29 +00004474 local_irq_restore(flags);
4475
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004476 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004477 kfree_skb(skb);
4478 return NET_RX_DROP;
4479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004481static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4482{
4483 struct net_device *dev = skb->dev;
4484 struct netdev_rx_queue *rxqueue;
4485
4486 rxqueue = dev->_rx;
4487
4488 if (skb_rx_queue_recorded(skb)) {
4489 u16 index = skb_get_rx_queue(skb);
4490
4491 if (unlikely(index >= dev->real_num_rx_queues)) {
4492 WARN_ONCE(dev->real_num_rx_queues > 1,
4493 "%s received packet on queue %u, but number "
4494 "of RX queues is %u\n",
4495 dev->name, index, dev->real_num_rx_queues);
4496
4497 return rxqueue; /* Return first rxqueue */
4498 }
4499 rxqueue += index;
4500 }
4501 return rxqueue;
4502}
4503
John Fastabendd4455162017-07-17 09:26:45 -07004504static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004505 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004506 struct bpf_prog *xdp_prog)
4507{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004508 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004509 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004510 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004511 __be16 orig_eth_type;
4512 struct ethhdr *eth;
4513 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004514 int hlen, off;
4515 u32 mac_len;
4516
4517 /* Reinjected packets coming from act_mirred or similar should
4518 * not get XDP generic processing.
4519 */
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01004520 if (skb_is_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004521 return XDP_PASS;
4522
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004523 /* XDP packets must be linear and must have sufficient headroom
4524 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4525 * native XDP provides, thus we need to do it here as well.
4526 */
Toke Høiland-Jørgensenad1e03b2020-02-10 17:10:46 +01004527 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004528 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4529 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4530 int troom = skb->tail + skb->data_len - skb->end;
4531
4532 /* In case we have to go down the path and also linearize,
4533 * then lets do the pskb_expand_head() work just once here.
4534 */
4535 if (pskb_expand_head(skb,
4536 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4537 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4538 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004539 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004540 goto do_drop;
4541 }
John Fastabendd4455162017-07-17 09:26:45 -07004542
4543 /* The XDP program wants to see the packet starting at the MAC
4544 * header.
4545 */
4546 mac_len = skb->data - skb_mac_header(skb);
4547 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004548 xdp->data = skb->data - mac_len;
4549 xdp->data_meta = xdp->data;
4550 xdp->data_end = xdp->data + hlen;
4551 xdp->data_hard_start = skb->data - skb_headroom(skb);
4552 orig_data_end = xdp->data_end;
4553 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004554 eth = (struct ethhdr *)xdp->data;
4555 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4556 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004557
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004558 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004559 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004560
Björn Töpel02671e22018-05-02 13:01:30 +02004561 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004562
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004563 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004564 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004565 if (off) {
4566 if (off > 0)
4567 __skb_pull(skb, off);
4568 else if (off < 0)
4569 __skb_push(skb, -off);
4570
4571 skb->mac_header += off;
4572 skb_reset_network_header(skb);
4573 }
John Fastabendd4455162017-07-17 09:26:45 -07004574
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004575 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4576 * pckt.
4577 */
Björn Töpel02671e22018-05-02 13:01:30 +02004578 off = orig_data_end - xdp->data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004579 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004580 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004581 skb->len -= off;
Björn Töpel02671e22018-05-02 13:01:30 +02004582
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004583 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004584
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004585 /* check if XDP changed eth hdr such SKB needs update */
4586 eth = (struct ethhdr *)xdp->data;
4587 if ((orig_eth_type != eth->h_proto) ||
4588 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4589 __skb_push(skb, ETH_HLEN);
4590 skb->protocol = eth_type_trans(skb, skb->dev);
4591 }
4592
John Fastabendd4455162017-07-17 09:26:45 -07004593 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004594 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004595 case XDP_TX:
4596 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004597 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004598 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004599 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004600 if (metalen)
4601 skb_metadata_set(skb, metalen);
4602 break;
John Fastabendd4455162017-07-17 09:26:45 -07004603 default:
4604 bpf_warn_invalid_xdp_action(act);
4605 /* fall through */
4606 case XDP_ABORTED:
4607 trace_xdp_exception(skb->dev, xdp_prog, act);
4608 /* fall through */
4609 case XDP_DROP:
4610 do_drop:
4611 kfree_skb(skb);
4612 break;
4613 }
4614
4615 return act;
4616}
4617
4618/* When doing generic XDP we have to bypass the qdisc layer and the
4619 * network taps in order to match in-driver-XDP behavior.
4620 */
Jason Wang7c497472017-08-11 19:41:17 +08004621void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004622{
4623 struct net_device *dev = skb->dev;
4624 struct netdev_queue *txq;
4625 bool free_skb = true;
4626 int cpu, rc;
4627
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004628 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004629 cpu = smp_processor_id();
4630 HARD_TX_LOCK(dev, txq, cpu);
4631 if (!netif_xmit_stopped(txq)) {
4632 rc = netdev_start_xmit(skb, dev, txq, 0);
4633 if (dev_xmit_complete(rc))
4634 free_skb = false;
4635 }
4636 HARD_TX_UNLOCK(dev, txq);
4637 if (free_skb) {
4638 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4639 kfree_skb(skb);
4640 }
4641}
4642
Davidlohr Bueso02786472018-05-08 09:07:02 -07004643static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004644
Jason Wang7c497472017-08-11 19:41:17 +08004645int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004646{
John Fastabendd4455162017-07-17 09:26:45 -07004647 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004648 struct xdp_buff xdp;
4649 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004650 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004651
Björn Töpel02671e22018-05-02 13:01:30 +02004652 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004653 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004654 switch (act) {
4655 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004656 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004657 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004658 if (err)
4659 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004660 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004661 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004662 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004663 break;
4664 }
John Fastabendd4455162017-07-17 09:26:45 -07004665 return XDP_DROP;
4666 }
4667 }
4668 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004669out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004670 kfree_skb(skb);
4671 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004672}
Jason Wang7c497472017-08-11 19:41:17 +08004673EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004674
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004675static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004677 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Eric Dumazet588f0332011-11-15 04:12:55 +00004679 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Koki Sanagicf66ba52010-08-23 18:45:02 +09004681 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004682
Eric Dumazetdf334542010-03-24 19:13:54 +00004683#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004684 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004685 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004686 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Changli Gaocece1942010-08-07 20:35:43 -07004688 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004689 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004690
4691 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004692 if (cpu < 0)
4693 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004694
4695 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4696
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004697 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004698 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004699 } else
4700#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004701 {
4702 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004703
Tom Herbertfec5e652010-04-16 16:01:27 -07004704 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4705 put_cpu();
4706 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004707 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004709
4710/**
4711 * netif_rx - post buffer to the network code
4712 * @skb: buffer to post
4713 *
4714 * This function receives a packet from a device driver and queues it for
4715 * the upper (protocol) levels to process. It always succeeds. The buffer
4716 * may be dropped during processing for congestion control or by the
4717 * protocol layers.
4718 *
4719 * return values:
4720 * NET_RX_SUCCESS (no congestion)
4721 * NET_RX_DROP (packet was dropped)
4722 *
4723 */
4724
4725int netif_rx(struct sk_buff *skb)
4726{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004727 int ret;
4728
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004729 trace_netif_rx_entry(skb);
4730
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004731 ret = netif_rx_internal(skb);
4732 trace_netif_rx_exit(ret);
4733
4734 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004735}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004736EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738int netif_rx_ni(struct sk_buff *skb)
4739{
4740 int err;
4741
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004742 trace_netif_rx_ni_entry(skb);
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004745 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (local_softirq_pending())
4747 do_softirq();
4748 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004749 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 return err;
4752}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753EXPORT_SYMBOL(netif_rx_ni);
4754
Emese Revfy0766f782016-06-20 20:42:34 +02004755static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004757 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
4759 if (sd->completion_queue) {
4760 struct sk_buff *clist;
4761
4762 local_irq_disable();
4763 clist = sd->completion_queue;
4764 sd->completion_queue = NULL;
4765 local_irq_enable();
4766
4767 while (clist) {
4768 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 clist = clist->next;
4771
Reshetova, Elena63354792017-06-30 13:07:58 +03004772 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004773 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4774 trace_consume_skb(skb);
4775 else
4776 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004777
4778 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4779 __kfree_skb(skb);
4780 else
4781 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004783
4784 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
4786
4787 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004788 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 local_irq_disable();
4791 head = sd->output_queue;
4792 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004793 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 local_irq_enable();
4795
4796 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004797 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004798 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 head = head->next_sched;
4801
John Fastabend6b3ba912017-12-07 09:54:25 -08004802 if (!(q->flags & TCQ_F_NOLOCK)) {
4803 root_lock = qdisc_lock(q);
4804 spin_lock(root_lock);
4805 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004806 /* We need to make sure head->next_sched is read
4807 * before clearing __QDISC_STATE_SCHED
4808 */
4809 smp_mb__before_atomic();
4810 clear_bit(__QDISC_STATE_SCHED, &q->state);
4811 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004812 if (root_lock)
4813 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
4815 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004816
4817 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004820#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004821/* This hook is defined here for ATM LANE */
4822int (*br_fdb_test_addr_hook)(struct net_device *dev,
4823 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004824EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004827static inline struct sk_buff *
4828sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4829 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004830{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004831#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004832 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004833 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004834
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004835 /* If there's at least one ingress present somewhere (so
4836 * we get here via enabled static key), remaining devices
4837 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004838 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004839 */
Jiri Pirko46209402017-11-03 11:46:25 +01004840 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004841 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004842
Herbert Xuf697c3e2007-10-14 00:38:47 -07004843 if (*pt_prev) {
4844 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4845 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004846 }
4847
Florian Westphal33654952015-05-14 00:36:28 +02004848 qdisc_skb_cb(skb)->pkt_len = skb->len;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004849 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004850 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004851
Paul Blakey7d17c542020-02-16 12:01:22 +02004852 switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list,
4853 &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004854 case TC_ACT_OK:
4855 case TC_ACT_RECLASSIFY:
4856 skb->tc_index = TC_H_MIN(cl_res.classid);
4857 break;
4858 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004859 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004860 kfree_skb(skb);
4861 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004862 case TC_ACT_STOLEN:
4863 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004864 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004865 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004866 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004867 case TC_ACT_REDIRECT:
4868 /* skb_mac_header check was done by cls/act_bpf, so
4869 * we can safely push the L2 header back before
4870 * redirecting to another netdev
4871 */
4872 __skb_push(skb, skb->mac_len);
4873 skb_do_redirect(skb);
4874 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01004875 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02004876 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004877 default:
4878 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004879 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004880#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004881 return skb;
4882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004884/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004885 * netdev_is_rx_handler_busy - check if receive handler is registered
4886 * @dev: device to check
4887 *
4888 * Check if a receive handler is already registered for a given device.
4889 * Return true if there one.
4890 *
4891 * The caller must hold the rtnl_mutex.
4892 */
4893bool netdev_is_rx_handler_busy(struct net_device *dev)
4894{
4895 ASSERT_RTNL();
4896 return dev && rtnl_dereference(dev->rx_handler);
4897}
4898EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4899
4900/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004901 * netdev_rx_handler_register - register receive handler
4902 * @dev: device to register a handler for
4903 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004904 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004905 *
Masanari Iidae2278672014-02-18 22:54:36 +09004906 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004907 * called from __netif_receive_skb. A negative errno code is returned
4908 * on a failure.
4909 *
4910 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004911 *
4912 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004913 */
4914int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004915 rx_handler_func_t *rx_handler,
4916 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004917{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004918 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004919 return -EBUSY;
4920
Paolo Abenif54262502018-03-09 10:39:24 +01004921 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4922 return -EINVAL;
4923
Eric Dumazet00cfec32013-03-29 03:01:22 +00004924 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004925 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004926 rcu_assign_pointer(dev->rx_handler, rx_handler);
4927
4928 return 0;
4929}
4930EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4931
4932/**
4933 * netdev_rx_handler_unregister - unregister receive handler
4934 * @dev: device to unregister a handler from
4935 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004936 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004937 *
4938 * The caller must hold the rtnl_mutex.
4939 */
4940void netdev_rx_handler_unregister(struct net_device *dev)
4941{
4942
4943 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004944 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004945 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4946 * section has a guarantee to see a non NULL rx_handler_data
4947 * as well.
4948 */
4949 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004950 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004951}
4952EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4953
Mel Gormanb4b9e352012-07-31 16:44:26 -07004954/*
4955 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4956 * the special handling of PFMEMALLOC skbs.
4957 */
4958static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4959{
4960 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004961 case htons(ETH_P_ARP):
4962 case htons(ETH_P_IP):
4963 case htons(ETH_P_IPV6):
4964 case htons(ETH_P_8021Q):
4965 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004966 return true;
4967 default:
4968 return false;
4969 }
4970}
4971
Pablo Neirae687ad62015-05-13 18:19:38 +02004972static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4973 int *ret, struct net_device *orig_dev)
4974{
4975 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004976 int ingress_retval;
4977
Pablo Neirae687ad62015-05-13 18:19:38 +02004978 if (*pt_prev) {
4979 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4980 *pt_prev = NULL;
4981 }
4982
Aaron Conole2c1e2702016-09-21 11:35:03 -04004983 rcu_read_lock();
4984 ingress_retval = nf_hook_ingress(skb);
4985 rcu_read_unlock();
4986 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004987 }
4988 return 0;
4989}
Pablo Neirae687ad62015-05-13 18:19:38 +02004990
Edward Cree88eb1942018-07-02 16:13:56 +01004991static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
4992 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
4994 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004995 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004996 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004997 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004999 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
Eric Dumazet588f0332011-11-15 04:12:55 +00005001 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005002
Koki Sanagicf66ba52010-08-23 18:45:02 +09005003 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005004
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005005 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005006
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005007 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005008 if (!skb_transport_header_was_set(skb))
5009 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005010 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
5012 pt_prev = NULL;
5013
David S. Miller63d8ea72011-02-28 10:48:59 -08005014another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005015 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005016
5017 __this_cpu_inc(softnet_data.processed);
5018
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005019 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5020 int ret2;
5021
5022 preempt_disable();
5023 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5024 preempt_enable();
5025
5026 if (ret2 != XDP_PASS)
5027 return NET_RX_DROP;
5028 skb_reset_mac_len(skb);
5029 }
5030
Patrick McHardy8ad227f2013-04-19 02:04:31 +00005031 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5032 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005033 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005034 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005035 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005036 }
5037
Willem de Bruijne7246e12017-01-07 17:06:35 -05005038 if (skb_skip_tc_classify(skb))
5039 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
David S. Miller9754e292013-02-14 15:57:38 -05005041 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005042 goto skip_taps;
5043
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005045 if (pt_prev)
5046 ret = deliver_skb(skb, pt_prev, orig_dev);
5047 pt_prev = ptype;
5048 }
5049
5050 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5051 if (pt_prev)
5052 ret = deliver_skb(skb, pt_prev, orig_dev);
5053 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 }
5055
Mel Gormanb4b9e352012-07-31 16:44:26 -07005056skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005057#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005058 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01005059 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02005060 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005061 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005062
5063 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005064 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005065 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005066#endif
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01005067 skb_reset_redirect(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005068skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005069 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005070 goto drop;
5071
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005072 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005073 if (pt_prev) {
5074 ret = deliver_skb(skb, pt_prev, orig_dev);
5075 pt_prev = NULL;
5076 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005077 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005078 goto another_round;
5079 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005080 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005081 }
5082
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005083 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005084 if (rx_handler) {
5085 if (pt_prev) {
5086 ret = deliver_skb(skb, pt_prev, orig_dev);
5087 pt_prev = NULL;
5088 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005089 switch (rx_handler(&skb)) {
5090 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005091 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005092 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005093 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005094 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005095 case RX_HANDLER_EXACT:
5096 deliver_exact = true;
5097 case RX_HANDLER_PASS:
5098 break;
5099 default:
5100 BUG();
5101 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005104 if (unlikely(skb_vlan_tag_present(skb))) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005105check_vlan_id:
5106 if (skb_vlan_tag_get_id(skb)) {
5107 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5108 * find vlan device.
5109 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005110 skb->pkt_type = PACKET_OTHERHOST;
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005111 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5112 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5113 /* Outer header is 802.1P with vlan 0, inner header is
5114 * 802.1Q or 802.1AD and vlan_do_receive() above could
5115 * not find vlan dev for vlan id 0.
5116 */
5117 __vlan_hwaccel_clear_tag(skb);
5118 skb = skb_vlan_untag(skb);
5119 if (unlikely(!skb))
5120 goto out;
5121 if (vlan_do_receive(&skb))
5122 /* After stripping off 802.1P header with vlan 0
5123 * vlan dev is found for inner header.
5124 */
5125 goto another_round;
5126 else if (unlikely(!skb))
5127 goto out;
5128 else
5129 /* We have stripped outer 802.1P vlan 0 header.
5130 * But could not find vlan dev.
5131 * check again for vlan id to set OTHERHOST.
5132 */
5133 goto check_vlan_id;
5134 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005135 /* Note: we might in the future use prio bits
5136 * and set skb->priority like in vlan_do_receive()
5137 * For the time being, just ignore Priority Code Point
5138 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005139 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005140 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005143
5144 /* deliver only exact match when indicated */
5145 if (likely(!deliver_exact)) {
5146 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5147 &ptype_base[ntohs(type) &
5148 PTYPE_HASH_MASK]);
5149 }
5150
5151 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5152 &orig_dev->ptype_specific);
5153
5154 if (unlikely(skb->dev != orig_dev)) {
5155 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5156 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 }
5158
5159 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005160 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005161 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005162 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005164drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005165 if (!deliver_exact)
5166 atomic_long_inc(&skb->dev->rx_dropped);
5167 else
5168 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 kfree_skb(skb);
5170 /* Jamal, now you will not able to escape explaining
5171 * me how you were going to use this. :-)
5172 */
5173 ret = NET_RX_DROP;
5174 }
5175
Julian Anastasov2c17d272015-07-09 09:59:10 +03005176out:
David S. Miller9754e292013-02-14 15:57:38 -05005177 return ret;
5178}
5179
Edward Cree88eb1942018-07-02 16:13:56 +01005180static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5181{
5182 struct net_device *orig_dev = skb->dev;
5183 struct packet_type *pt_prev = NULL;
5184 int ret;
5185
5186 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
5187 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005188 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5189 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005190 return ret;
5191}
5192
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005193/**
5194 * netif_receive_skb_core - special purpose version of netif_receive_skb
5195 * @skb: buffer to process
5196 *
5197 * More direct receive version of netif_receive_skb(). It should
5198 * only be used by callers that have a need to skip RPS and Generic XDP.
Mauro Carvalho Chehab2de97802020-03-17 15:54:20 +01005199 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005200 *
5201 * This function may only be called from softirq context and interrupts
5202 * should be enabled.
5203 *
5204 * Return values (usually ignored):
5205 * NET_RX_SUCCESS: no congestion
5206 * NET_RX_DROP: packet was dropped
5207 */
5208int netif_receive_skb_core(struct sk_buff *skb)
5209{
5210 int ret;
5211
5212 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005213 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005214 rcu_read_unlock();
5215
5216 return ret;
5217}
5218EXPORT_SYMBOL(netif_receive_skb_core);
5219
Edward Cree88eb1942018-07-02 16:13:56 +01005220static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5221 struct packet_type *pt_prev,
5222 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005223{
5224 struct sk_buff *skb, *next;
5225
Edward Cree88eb1942018-07-02 16:13:56 +01005226 if (!pt_prev)
5227 return;
5228 if (list_empty(head))
5229 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005230 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005231 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5232 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005233 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005234 list_for_each_entry_safe(skb, next, head, list) {
5235 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005236 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005237 }
Edward Cree88eb1942018-07-02 16:13:56 +01005238}
5239
5240static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5241{
5242 /* Fast-path assumptions:
5243 * - There is no RX handler.
5244 * - Only one packet_type matches.
5245 * If either of these fails, we will end up doing some per-packet
5246 * processing in-line, then handling the 'last ptype' for the whole
5247 * sublist. This can't cause out-of-order delivery to any single ptype,
5248 * because the 'last ptype' must be constant across the sublist, and all
5249 * other ptypes are handled per-packet.
5250 */
5251 /* Current (common) ptype of sublist */
5252 struct packet_type *pt_curr = NULL;
5253 /* Current (common) orig_dev of sublist */
5254 struct net_device *od_curr = NULL;
5255 struct list_head sublist;
5256 struct sk_buff *skb, *next;
5257
Edward Cree9af86f92018-07-09 18:10:19 +01005258 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005259 list_for_each_entry_safe(skb, next, head, list) {
5260 struct net_device *orig_dev = skb->dev;
5261 struct packet_type *pt_prev = NULL;
5262
Edward Cree22f6bbb2018-12-04 17:37:57 +00005263 skb_list_del_init(skb);
Edward Cree88eb1942018-07-02 16:13:56 +01005264 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005265 if (!pt_prev)
5266 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005267 if (pt_curr != pt_prev || od_curr != orig_dev) {
5268 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005269 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5270 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005271 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005272 pt_curr = pt_prev;
5273 od_curr = orig_dev;
5274 }
Edward Cree9af86f92018-07-09 18:10:19 +01005275 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005276 }
5277
5278 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005279 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005280}
5281
David S. Miller9754e292013-02-14 15:57:38 -05005282static int __netif_receive_skb(struct sk_buff *skb)
5283{
5284 int ret;
5285
5286 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005287 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005288
5289 /*
5290 * PFMEMALLOC skbs are special, they should
5291 * - be delivered to SOCK_MEMALLOC sockets only
5292 * - stay away from userspace
5293 * - have bounded memory usage
5294 *
5295 * Use PF_MEMALLOC as this saves us from propagating the allocation
5296 * context down to all allocation sites.
5297 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005298 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005299 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005300 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005301 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005302 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return ret;
5305}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005306
Edward Cree4ce00172018-07-02 16:13:40 +01005307static void __netif_receive_skb_list(struct list_head *head)
5308{
5309 unsigned long noreclaim_flag = 0;
5310 struct sk_buff *skb, *next;
5311 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5312
5313 list_for_each_entry_safe(skb, next, head, list) {
5314 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5315 struct list_head sublist;
5316
5317 /* Handle the previous sublist */
5318 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005319 if (!list_empty(&sublist))
5320 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005321 pfmemalloc = !pfmemalloc;
5322 /* See comments in __netif_receive_skb */
5323 if (pfmemalloc)
5324 noreclaim_flag = memalloc_noreclaim_save();
5325 else
5326 memalloc_noreclaim_restore(noreclaim_flag);
5327 }
5328 }
5329 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005330 if (!list_empty(head))
5331 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005332 /* Restore pflags */
5333 if (pfmemalloc)
5334 memalloc_noreclaim_restore(noreclaim_flag);
5335}
5336
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005337static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005338{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005339 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005340 struct bpf_prog *new = xdp->prog;
5341 int ret = 0;
5342
5343 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005344 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005345 rcu_assign_pointer(dev->xdp_prog, new);
5346 if (old)
5347 bpf_prog_put(old);
5348
5349 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005350 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005351 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005352 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005353 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005354 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005355 }
5356 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005357
5358 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07005359 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04005360 break;
5361
5362 default:
5363 ret = -EINVAL;
5364 break;
5365 }
5366
5367 return ret;
5368}
5369
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005370static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005371{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005372 int ret;
5373
Eric Dumazet588f0332011-11-15 04:12:55 +00005374 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005375
Richard Cochranc1f19b52010-07-17 08:49:36 +00005376 if (skb_defer_rx_timestamp(skb))
5377 return NET_RX_SUCCESS;
5378
John Fastabendbbbe2112017-09-08 14:00:30 -07005379 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005380#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005381 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005382 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005383 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005384
Eric Dumazet3b098e22010-05-15 23:57:10 -07005385 if (cpu >= 0) {
5386 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5387 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005388 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005389 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005390 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005391#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005392 ret = __netif_receive_skb(skb);
5393 rcu_read_unlock();
5394 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005395}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005396
Edward Cree7da517a2018-07-02 16:13:24 +01005397static void netif_receive_skb_list_internal(struct list_head *head)
5398{
Edward Cree7da517a2018-07-02 16:13:24 +01005399 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005400 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005401
Edward Cree8c057ef2018-07-09 18:09:54 +01005402 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005403 list_for_each_entry_safe(skb, next, head, list) {
5404 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005405 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005406 if (!skb_defer_rx_timestamp(skb))
5407 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005408 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005409 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005410
Edward Cree7da517a2018-07-02 16:13:24 +01005411 rcu_read_lock();
5412#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005413 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005414 list_for_each_entry_safe(skb, next, head, list) {
5415 struct rps_dev_flow voidflow, *rflow = &voidflow;
5416 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5417
5418 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005419 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005420 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005421 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005422 }
5423 }
5424 }
5425#endif
5426 __netif_receive_skb_list(head);
5427 rcu_read_unlock();
5428}
5429
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005430/**
5431 * netif_receive_skb - process receive buffer from network
5432 * @skb: buffer to process
5433 *
5434 * netif_receive_skb() is the main receive data processing function.
5435 * It always succeeds. The buffer may be dropped during processing
5436 * for congestion control or by the protocol layers.
5437 *
5438 * This function may only be called from softirq context and interrupts
5439 * should be enabled.
5440 *
5441 * Return values (usually ignored):
5442 * NET_RX_SUCCESS: no congestion
5443 * NET_RX_DROP: packet was dropped
5444 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005445int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005446{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005447 int ret;
5448
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005449 trace_netif_receive_skb_entry(skb);
5450
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005451 ret = netif_receive_skb_internal(skb);
5452 trace_netif_receive_skb_exit(ret);
5453
5454 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005455}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005456EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Edward Creef6ad8c12018-07-02 16:12:45 +01005458/**
5459 * netif_receive_skb_list - process many receive buffers from network
5460 * @head: list of skbs to process.
5461 *
Edward Cree7da517a2018-07-02 16:13:24 +01005462 * Since return value of netif_receive_skb() is normally ignored, and
5463 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005464 *
5465 * This function may only be called from softirq context and interrupts
5466 * should be enabled.
5467 */
5468void netif_receive_skb_list(struct list_head *head)
5469{
Edward Cree7da517a2018-07-02 16:13:24 +01005470 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005471
Edward Creeb9f463d2018-07-02 16:14:44 +01005472 if (list_empty(head))
5473 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005474 if (trace_netif_receive_skb_list_entry_enabled()) {
5475 list_for_each_entry(skb, head, list)
5476 trace_netif_receive_skb_list_entry(skb);
5477 }
Edward Cree7da517a2018-07-02 16:13:24 +01005478 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005479 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005480}
5481EXPORT_SYMBOL(netif_receive_skb_list);
5482
Eric Dumazet41852492016-08-26 12:50:39 -07005483DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005484
5485/* Network device is going away, flush any packets still pending */
5486static void flush_backlog(struct work_struct *work)
5487{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005488 struct sk_buff *skb, *tmp;
5489 struct softnet_data *sd;
5490
5491 local_bh_disable();
5492 sd = this_cpu_ptr(&softnet_data);
5493
5494 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005495 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005496 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005497 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005498 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005499 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005500 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005501 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005502 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005503 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005504 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005505
5506 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005507 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005508 __skb_unlink(skb, &sd->process_queue);
5509 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005510 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005511 }
5512 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005513 local_bh_enable();
5514}
5515
Eric Dumazet41852492016-08-26 12:50:39 -07005516static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005517{
5518 unsigned int cpu;
5519
5520 get_online_cpus();
5521
Eric Dumazet41852492016-08-26 12:50:39 -07005522 for_each_online_cpu(cpu)
5523 queue_work_on(cpu, system_highpri_wq,
5524 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005525
5526 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07005527 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005528
5529 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005530}
5531
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005532/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5533static void gro_normal_list(struct napi_struct *napi)
5534{
5535 if (!napi->rx_count)
5536 return;
5537 netif_receive_skb_list_internal(&napi->rx_list);
5538 INIT_LIST_HEAD(&napi->rx_list);
5539 napi->rx_count = 0;
5540}
5541
5542/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5543 * pass the whole batch up to the stack.
5544 */
5545static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5546{
5547 list_add_tail(&skb->list, &napi->rx_list);
5548 if (++napi->rx_count >= gro_normal_batch)
5549 gro_normal_list(napi);
5550}
5551
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005552INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5553INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005554static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005555{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005556 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005557 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005558 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005559 int err = -ENOENT;
5560
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005561 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5562
Herbert Xufc59f9a2009-04-14 15:11:06 -07005563 if (NAPI_GRO_CB(skb)->count == 1) {
5564 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005565 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005566 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005567
5568 rcu_read_lock();
5569 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005570 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005571 continue;
5572
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005573 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5574 ipv6_gro_complete, inet_gro_complete,
5575 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005576 break;
5577 }
5578 rcu_read_unlock();
5579
5580 if (err) {
5581 WARN_ON(&ptype->list == head);
5582 kfree_skb(skb);
5583 return NET_RX_SUCCESS;
5584 }
5585
5586out:
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005587 gro_normal_one(napi, skb);
5588 return NET_RX_SUCCESS;
Herbert Xud565b0a2008-12-15 23:38:52 -08005589}
5590
Li RongQing6312fe72018-07-05 14:34:32 +08005591static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005592 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005593{
Li RongQing6312fe72018-07-05 14:34:32 +08005594 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005595 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005596
David Miller07d78362018-06-24 14:14:02 +09005597 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005598 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5599 return;
David S. Miller992cba72018-07-31 15:27:56 -07005600 skb_list_del_init(skb);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005601 napi_gro_complete(napi, skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005602 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005603 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005604
5605 if (!napi->gro_hash[index].count)
5606 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005607}
David Miller07d78362018-06-24 14:14:02 +09005608
Li RongQing6312fe72018-07-05 14:34:32 +08005609/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005610 * youngest packets at the head of it.
5611 * Complete skbs in reverse order to reduce latencies.
5612 */
5613void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5614{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005615 unsigned long bitmask = napi->gro_bitmask;
5616 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005617
Eric Dumazet42519ed2018-11-21 11:39:28 -08005618 while ((i = ffs(bitmask)) != 0) {
5619 bitmask >>= i;
5620 base += i;
5621 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005622 }
David Miller07d78362018-06-24 14:14:02 +09005623}
Eric Dumazet86cac582010-08-31 18:25:32 +00005624EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005625
David Miller07d78362018-06-24 14:14:02 +09005626static struct list_head *gro_list_prepare(struct napi_struct *napi,
5627 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005628{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005629 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005630 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005631 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005632 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005633
Li RongQing6312fe72018-07-05 14:34:32 +08005634 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005635 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005636 unsigned long diffs;
5637
Tom Herbert0b4cec82014-01-15 08:58:06 -08005638 NAPI_GRO_CB(p)->flush = 0;
5639
5640 if (hash != skb_get_hash_raw(p)) {
5641 NAPI_GRO_CB(p)->same_flow = 0;
5642 continue;
5643 }
5644
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005645 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005646 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5647 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005648 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Jesse Grossce87fc62016-01-20 17:59:49 -08005649 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005650 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005651 if (maclen == ETH_HLEN)
5652 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005653 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005654 else if (!diffs)
5655 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005656 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005657 maclen);
5658 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005659 }
David Miller07d78362018-06-24 14:14:02 +09005660
5661 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005662}
5663
Jerry Chu299603e82013-12-11 20:53:45 -08005664static void skb_gro_reset_offset(struct sk_buff *skb)
5665{
5666 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5667 const skb_frag_t *frag0 = &pinfo->frags[0];
5668
5669 NAPI_GRO_CB(skb)->data_offset = 0;
5670 NAPI_GRO_CB(skb)->frag0 = NULL;
5671 NAPI_GRO_CB(skb)->frag0_len = 0;
5672
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005673 if (!skb_headlen(skb) && pinfo->nr_frags &&
Jerry Chu299603e82013-12-11 20:53:45 -08005674 !PageHighMem(skb_frag_page(frag0))) {
5675 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005676 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5677 skb_frag_size(frag0),
5678 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005679 }
5680}
5681
Eric Dumazeta50e2332014-03-29 21:28:21 -07005682static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5683{
5684 struct skb_shared_info *pinfo = skb_shinfo(skb);
5685
5686 BUG_ON(skb->end - skb->tail < grow);
5687
5688 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5689
5690 skb->data_len -= grow;
5691 skb->tail += grow;
5692
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07005693 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005694 skb_frag_size_sub(&pinfo->frags[0], grow);
5695
5696 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5697 skb_frag_unref(skb, 0);
5698 memmove(pinfo->frags, pinfo->frags + 1,
5699 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5700 }
5701}
5702
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005703static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005704{
Li RongQing6312fe72018-07-05 14:34:32 +08005705 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005706
Li RongQing6312fe72018-07-05 14:34:32 +08005707 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005708
Li RongQing6312fe72018-07-05 14:34:32 +08005709 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005710 * impossible.
5711 */
5712 if (WARN_ON_ONCE(!oldest))
5713 return;
5714
Li RongQingd9f37d02018-07-13 14:41:36 +08005715 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5716 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005717 */
David S. Millerece23712018-10-28 10:35:12 -07005718 skb_list_del_init(oldest);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005719 napi_gro_complete(napi, oldest);
David Miller07d78362018-06-24 14:14:02 +09005720}
5721
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005722INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5723 struct sk_buff *));
5724INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5725 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005726static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005727{
Li RongQing6312fe72018-07-05 14:34:32 +08005728 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005729 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005730 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005731 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005732 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005733 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005734 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005735 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005736 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005737
David S. Millerb5cdae32017-04-18 15:36:58 -04005738 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005739 goto normal;
5740
David Miller07d78362018-06-24 14:14:02 +09005741 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005742
Herbert Xud565b0a2008-12-15 23:38:52 -08005743 rcu_read_lock();
5744 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005745 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005746 continue;
5747
Herbert Xu86911732009-01-29 14:19:50 +00005748 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005749 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005750 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005751 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005752 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005753 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005754 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005755 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005756 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005757 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005758
Tom Herbert662880f2014-08-27 21:26:56 -07005759 /* Setup for GRO checksum validation */
5760 switch (skb->ip_summed) {
5761 case CHECKSUM_COMPLETE:
5762 NAPI_GRO_CB(skb)->csum = skb->csum;
5763 NAPI_GRO_CB(skb)->csum_valid = 1;
5764 NAPI_GRO_CB(skb)->csum_cnt = 0;
5765 break;
5766 case CHECKSUM_UNNECESSARY:
5767 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5768 NAPI_GRO_CB(skb)->csum_valid = 0;
5769 break;
5770 default:
5771 NAPI_GRO_CB(skb)->csum_cnt = 0;
5772 NAPI_GRO_CB(skb)->csum_valid = 0;
5773 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005774
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005775 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5776 ipv6_gro_receive, inet_gro_receive,
5777 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005778 break;
5779 }
5780 rcu_read_unlock();
5781
5782 if (&ptype->list == head)
5783 goto normal;
5784
Masahiro Yamada45586c72020-02-03 17:37:45 -08005785 if (PTR_ERR(pp) == -EINPROGRESS) {
Steffen Klassert25393d32017-02-15 09:39:44 +01005786 ret = GRO_CONSUMED;
5787 goto ok;
5788 }
5789
Herbert Xu0da2afd52008-12-26 14:57:42 -08005790 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005791 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005792
Herbert Xud565b0a2008-12-15 23:38:52 -08005793 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005794 skb_list_del_init(pp);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005795 napi_gro_complete(napi, pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005796 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005797 }
5798
Herbert Xu0da2afd52008-12-26 14:57:42 -08005799 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005800 goto ok;
5801
Eric Dumazet600adc12014-01-09 14:12:19 -08005802 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08005803 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08005804
Li RongQing6312fe72018-07-05 14:34:32 +08005805 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005806 gro_flush_oldest(napi, gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08005807 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08005808 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08005809 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005810 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005811 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07005812 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00005813 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09005814 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005815 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08005816
Herbert Xuad0f99042009-02-01 01:24:55 -08005817pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005818 grow = skb_gro_offset(skb) - skb_headlen(skb);
5819 if (grow > 0)
5820 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08005821ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08005822 if (napi->gro_hash[hash].count) {
5823 if (!test_bit(hash, &napi->gro_bitmask))
5824 __set_bit(hash, &napi->gro_bitmask);
5825 } else if (test_bit(hash, &napi->gro_bitmask)) {
5826 __clear_bit(hash, &napi->gro_bitmask);
5827 }
5828
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005829 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005830
5831normal:
Herbert Xuad0f99042009-02-01 01:24:55 -08005832 ret = GRO_NORMAL;
5833 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08005834}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005835
Jerry Chubf5a7552014-01-07 10:23:19 -08005836struct packet_offload *gro_find_receive_by_type(__be16 type)
5837{
5838 struct list_head *offload_head = &offload_base;
5839 struct packet_offload *ptype;
5840
5841 list_for_each_entry_rcu(ptype, offload_head, list) {
5842 if (ptype->type != type || !ptype->callbacks.gro_receive)
5843 continue;
5844 return ptype;
5845 }
5846 return NULL;
5847}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005848EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08005849
5850struct packet_offload *gro_find_complete_by_type(__be16 type)
5851{
5852 struct list_head *offload_head = &offload_base;
5853 struct packet_offload *ptype;
5854
5855 list_for_each_entry_rcu(ptype, offload_head, list) {
5856 if (ptype->type != type || !ptype->callbacks.gro_complete)
5857 continue;
5858 return ptype;
5859 }
5860 return NULL;
5861}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005862EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005863
Michal Kubečeke44699d2017-06-29 11:13:36 +02005864static void napi_skb_free_stolen_head(struct sk_buff *skb)
5865{
5866 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005867 skb_ext_put(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02005868 kmem_cache_free(skbuff_head_cache, skb);
5869}
5870
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005871static gro_result_t napi_skb_finish(struct napi_struct *napi,
5872 struct sk_buff *skb,
5873 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08005874{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005875 switch (ret) {
5876 case GRO_NORMAL:
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005877 gro_normal_one(napi, skb);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005878 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005879
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005880 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08005881 kfree_skb(skb);
5882 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005883
Eric Dumazetdaa86542012-04-19 07:07:40 +00005884 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02005885 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5886 napi_skb_free_stolen_head(skb);
5887 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00005888 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00005889 break;
5890
Ben Hutchings5b252f02009-10-29 07:17:09 +00005891 case GRO_HELD:
5892 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005893 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005894 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005895 }
5896
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005897 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005898}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005899
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005900gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005901{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005902 gro_result_t ret;
5903
Eric Dumazet93f93a42015-11-18 06:30:59 -08005904 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005905 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00005906
Eric Dumazeta50e2332014-03-29 21:28:21 -07005907 skb_gro_reset_offset(skb);
5908
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005909 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005910 trace_napi_gro_receive_exit(ret);
5911
5912 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005913}
5914EXPORT_SYMBOL(napi_gro_receive);
5915
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00005916static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005917{
Eric Dumazet93a35f52014-10-23 06:30:30 -07005918 if (unlikely(skb->pfmemalloc)) {
5919 consume_skb(skb);
5920 return;
5921 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005922 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00005923 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5924 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01005925 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08005926 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08005927 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08005928
5929 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5930 skb->pkt_type = PACKET_HOST;
5931
Jerry Chuc3caf112014-07-14 15:54:46 -07005932 skb->encapsulation = 0;
5933 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07005934 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Florian Westphal174e2382019-09-26 20:37:05 +02005935 skb_ext_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005936
5937 napi->skb = skb;
5938}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005939
Herbert Xu76620aa2009-04-16 02:02:07 -07005940struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005941{
Herbert Xu5d38a072009-01-04 16:13:40 -08005942 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005943
5944 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005945 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005946 if (skb) {
5947 napi->skb = skb;
5948 skb_mark_napi_id(skb, napi);
5949 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005950 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005951 return skb;
5952}
Herbert Xu76620aa2009-04-16 02:02:07 -07005953EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005954
Eric Dumazeta50e2332014-03-29 21:28:21 -07005955static gro_result_t napi_frags_finish(struct napi_struct *napi,
5956 struct sk_buff *skb,
5957 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005958{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005959 switch (ret) {
5960 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005961 case GRO_HELD:
5962 __skb_push(skb, ETH_HLEN);
5963 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01005964 if (ret == GRO_NORMAL)
5965 gro_normal_one(napi, skb);
Herbert Xu86911732009-01-29 14:19:50 +00005966 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005967
5968 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005969 napi_reuse_skb(napi, skb);
5970 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005971
Michal Kubečeke44699d2017-06-29 11:13:36 +02005972 case GRO_MERGED_FREE:
5973 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5974 napi_skb_free_stolen_head(skb);
5975 else
5976 napi_reuse_skb(napi, skb);
5977 break;
5978
Ben Hutchings5b252f02009-10-29 07:17:09 +00005979 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005980 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005981 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005982 }
5983
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005984 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005985}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005986
Eric Dumazeta50e2332014-03-29 21:28:21 -07005987/* Upper GRO stack assumes network header starts at gro_offset=0
5988 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5989 * We copy ethernet header into skb->data to have a common layout.
5990 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005991static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005992{
Herbert Xu76620aa2009-04-16 02:02:07 -07005993 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005994 const struct ethhdr *eth;
5995 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005996
5997 napi->skb = NULL;
5998
Eric Dumazeta50e2332014-03-29 21:28:21 -07005999 skb_reset_mac_header(skb);
6000 skb_gro_reset_offset(skb);
6001
Eric Dumazeta50e2332014-03-29 21:28:21 -07006002 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6003 eth = skb_gro_header_slow(skb, hlen, 0);
6004 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04006005 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6006 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006007 napi_reuse_skb(napi, skb);
6008 return NULL;
6009 }
6010 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07006011 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006012 gro_pull_from_frag0(skb, hlen);
6013 NAPI_GRO_CB(skb)->frag0 += hlen;
6014 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07006015 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07006016 __skb_pull(skb, hlen);
6017
6018 /*
6019 * This works because the only protocols we care about don't require
6020 * special handling.
6021 * We'll fix it up properly in napi_frags_finish()
6022 */
6023 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07006024
Herbert Xu76620aa2009-04-16 02:02:07 -07006025 return skb;
6026}
Herbert Xu76620aa2009-04-16 02:02:07 -07006027
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006028gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07006029{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006030 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07006031 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006032
6033 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006034 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08006035
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006036 trace_napi_gro_frags_entry(skb);
6037
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006038 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6039 trace_napi_gro_frags_exit(ret);
6040
6041 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006042}
6043EXPORT_SYMBOL(napi_gro_frags);
6044
Tom Herbert573e8fc2014-08-22 13:33:47 -07006045/* Compute the checksum from gro_offset and return the folded value
6046 * after adding in any pseudo checksum.
6047 */
6048__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6049{
6050 __wsum wsum;
6051 __sum16 sum;
6052
6053 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6054
6055 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6056 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006057 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006058 if (likely(!sum)) {
6059 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6060 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006061 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006062 }
6063
6064 NAPI_GRO_CB(skb)->csum = wsum;
6065 NAPI_GRO_CB(skb)->csum_valid = 1;
6066
6067 return sum;
6068}
6069EXPORT_SYMBOL(__skb_gro_checksum_complete);
6070
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306071static void net_rps_send_ipi(struct softnet_data *remsd)
6072{
6073#ifdef CONFIG_RPS
6074 while (remsd) {
6075 struct softnet_data *next = remsd->rps_ipi_next;
6076
6077 if (cpu_online(remsd->cpu))
6078 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6079 remsd = next;
6080 }
6081#endif
6082}
6083
Eric Dumazete326bed2010-04-22 00:22:45 -07006084/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006085 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006086 * Note: called with local irq disabled, but exits with local irq enabled.
6087 */
6088static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6089{
6090#ifdef CONFIG_RPS
6091 struct softnet_data *remsd = sd->rps_ipi_list;
6092
6093 if (remsd) {
6094 sd->rps_ipi_list = NULL;
6095
6096 local_irq_enable();
6097
6098 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306099 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006100 } else
6101#endif
6102 local_irq_enable();
6103}
6104
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006105static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6106{
6107#ifdef CONFIG_RPS
6108 return sd->rps_ipi_list != NULL;
6109#else
6110 return false;
6111#endif
6112}
6113
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006114static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006116 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006117 bool again = true;
6118 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Eric Dumazete326bed2010-04-22 00:22:45 -07006120 /* Check if we have pending ipi, its better to send them now,
6121 * not waiting net_rx_action() end.
6122 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006123 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006124 local_irq_disable();
6125 net_rps_action_and_irq_enable(sd);
6126 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006127
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006128 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006129 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Changli Gao6e7676c2010-04-27 15:07:33 -07006132 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006133 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006134 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006135 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006136 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006137 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006138 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006139
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006142 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006143 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006144 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006145 /*
6146 * Inline a custom version of __napi_complete().
6147 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006148 * and NAPI_STATE_SCHED is the only possible flag set
6149 * on backlog.
6150 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006151 * and we dont need an smp_mb() memory barrier.
6152 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006153 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006154 again = false;
6155 } else {
6156 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6157 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006158 }
6159 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006160 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006163 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164}
6165
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006166/**
6167 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006168 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006169 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006170 * The entry's receive function will be scheduled to run.
6171 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006172 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006173void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006174{
6175 unsigned long flags;
6176
6177 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006178 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006179 local_irq_restore(flags);
6180}
6181EXPORT_SYMBOL(__napi_schedule);
6182
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006183/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006184 * napi_schedule_prep - check if napi can be scheduled
6185 * @n: napi context
6186 *
6187 * Test if NAPI routine is already running, and if not mark
6188 * it as running. This is used as a condition variable
6189 * insure only one NAPI poll instance runs. We also make
6190 * sure there is no pending NAPI disable.
6191 */
6192bool napi_schedule_prep(struct napi_struct *n)
6193{
6194 unsigned long val, new;
6195
6196 do {
6197 val = READ_ONCE(n->state);
6198 if (unlikely(val & NAPIF_STATE_DISABLE))
6199 return false;
6200 new = val | NAPIF_STATE_SCHED;
6201
6202 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6203 * This was suggested by Alexander Duyck, as compiler
6204 * emits better code than :
6205 * if (val & NAPIF_STATE_SCHED)
6206 * new |= NAPIF_STATE_MISSED;
6207 */
6208 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6209 NAPIF_STATE_MISSED;
6210 } while (cmpxchg(&n->state, val, new) != val);
6211
6212 return !(val & NAPIF_STATE_SCHED);
6213}
6214EXPORT_SYMBOL(napi_schedule_prep);
6215
6216/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006217 * __napi_schedule_irqoff - schedule for receive
6218 * @n: entry to schedule
6219 *
6220 * Variant of __napi_schedule() assuming hard irqs are masked
6221 */
6222void __napi_schedule_irqoff(struct napi_struct *n)
6223{
6224 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6225}
6226EXPORT_SYMBOL(__napi_schedule_irqoff);
6227
Eric Dumazet364b6052016-11-15 10:15:13 -08006228bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006229{
Eric Dumazet39e6c822017-02-28 10:34:50 -08006230 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08006231
6232 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006233 * 1) Don't let napi dequeue from the cpu poll list
6234 * just in case its running on a different cpu.
6235 * 2) If we are busy polling, do nothing here, we have
6236 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006237 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006238 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6239 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006240 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006241
Li RongQingd9f37d02018-07-13 14:41:36 +08006242 if (n->gro_bitmask) {
Eric Dumazet3b47d302014-11-06 21:09:44 -08006243 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006244
Eric Dumazet3b47d302014-11-06 21:09:44 -08006245 if (work_done)
6246 timeout = n->dev->gro_flush_timeout;
6247
Paolo Abeni605108a2018-11-21 18:21:35 +01006248 /* When the NAPI instance uses a timeout and keeps postponing
6249 * it, we need to bound somehow the time packets are kept in
6250 * the GRO layer
6251 */
6252 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006253 if (timeout)
6254 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6255 HRTIMER_MODE_REL_PINNED);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006256 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006257
6258 gro_normal_list(n);
6259
Eric Dumazet02c16022017-02-04 15:25:02 -08006260 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006261 /* If n->poll_list is not empty, we need to mask irqs */
6262 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006263 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006264 local_irq_restore(flags);
6265 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006266
6267 do {
6268 val = READ_ONCE(n->state);
6269
6270 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6271
6272 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6273
6274 /* If STATE_MISSED was set, leave STATE_SCHED set,
6275 * because we will call napi->poll() one more time.
6276 * This C code was suggested by Alexander Duyck to help gcc.
6277 */
6278 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6279 NAPIF_STATE_SCHED;
6280 } while (cmpxchg(&n->state, val, new) != val);
6281
6282 if (unlikely(val & NAPIF_STATE_MISSED)) {
6283 __napi_schedule(n);
6284 return false;
6285 }
6286
Eric Dumazet364b6052016-11-15 10:15:13 -08006287 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006288}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006289EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006290
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006291/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006292static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006293{
6294 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6295 struct napi_struct *napi;
6296
6297 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6298 if (napi->napi_id == napi_id)
6299 return napi;
6300
6301 return NULL;
6302}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006303
6304#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006305
Eric Dumazetce6aea92015-11-18 06:30:54 -08006306#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006307
6308static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6309{
6310 int rc;
6311
Eric Dumazet39e6c822017-02-28 10:34:50 -08006312 /* Busy polling means there is a high chance device driver hard irq
6313 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6314 * set in napi_schedule_prep().
6315 * Since we are about to call napi->poll() once more, we can safely
6316 * clear NAPI_STATE_MISSED.
6317 *
6318 * Note: x86 could use a single "lock and ..." instruction
6319 * to perform these two clear_bit()
6320 */
6321 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006322 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6323
6324 local_bh_disable();
6325
6326 /* All we really want here is to re-enable device interrupts.
6327 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6328 */
6329 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006330 /* We can't gro_normal_list() here, because napi->poll() might have
6331 * rearmed the napi (napi_complete_done()) in which case it could
6332 * already be running on another CPU.
6333 */
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006334 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006335 netpoll_poll_unlock(have_poll_lock);
Edward Cree323ebb62019-08-06 14:53:55 +01006336 if (rc == BUSY_POLL_BUDGET) {
6337 /* As the whole budget was spent, we still own the napi so can
6338 * safely handle the rx_list.
6339 */
6340 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006341 __napi_schedule(napi);
Edward Cree323ebb62019-08-06 14:53:55 +01006342 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006343 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006344}
6345
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006346void napi_busy_loop(unsigned int napi_id,
6347 bool (*loop_end)(void *, unsigned long),
6348 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006349{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006350 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006351 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006352 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006353 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006354
6355restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006356 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006357
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006358 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006359
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006360 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006361 if (!napi)
6362 goto out;
6363
Eric Dumazet217f6972016-11-15 10:15:11 -08006364 preempt_disable();
6365 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006366 int work = 0;
6367
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006368 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006369 if (!napi_poll) {
6370 unsigned long val = READ_ONCE(napi->state);
6371
6372 /* If multiple threads are competing for this napi,
6373 * we avoid dirtying napi->state as much as we can.
6374 */
6375 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6376 NAPIF_STATE_IN_BUSY_POLL))
6377 goto count;
6378 if (cmpxchg(&napi->state, val,
6379 val | NAPIF_STATE_IN_BUSY_POLL |
6380 NAPIF_STATE_SCHED) != val)
6381 goto count;
6382 have_poll_lock = netpoll_poll_lock(napi);
6383 napi_poll = napi->poll;
6384 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006385 work = napi_poll(napi, BUSY_POLL_BUDGET);
6386 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006387 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006388count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006389 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006390 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006391 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006392 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006393
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006394 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006395 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006396
Eric Dumazet217f6972016-11-15 10:15:11 -08006397 if (unlikely(need_resched())) {
6398 if (napi_poll)
6399 busy_poll_stop(napi, have_poll_lock);
6400 preempt_enable();
6401 rcu_read_unlock();
6402 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006403 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006404 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006405 goto restart;
6406 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006407 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006408 }
6409 if (napi_poll)
6410 busy_poll_stop(napi, have_poll_lock);
6411 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006412out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006413 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006414}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006415EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006416
6417#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006418
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006419static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006420{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08006421 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6422 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006423 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006424
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006425 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006426
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006427 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006428 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006429 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6430 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006431 } while (napi_by_id(napi_gen_id));
6432 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006433
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006434 hlist_add_head_rcu(&napi->napi_hash_node,
6435 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006436
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006437 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006438}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006439
6440/* Warning : caller is responsible to make sure rcu grace period
6441 * is respected before freeing memory containing @napi
6442 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08006443bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006444{
Eric Dumazet34cbe272015-11-18 06:31:02 -08006445 bool rcu_sync_needed = false;
6446
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006447 spin_lock(&napi_hash_lock);
6448
Eric Dumazet34cbe272015-11-18 06:31:02 -08006449 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6450 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006451 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006452 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006453 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006454 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006455}
6456EXPORT_SYMBOL_GPL(napi_hash_del);
6457
Eric Dumazet3b47d302014-11-06 21:09:44 -08006458static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6459{
6460 struct napi_struct *napi;
6461
6462 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006463
6464 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6465 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6466 */
Li RongQingd9f37d02018-07-13 14:41:36 +08006467 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006468 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6469 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006470
6471 return HRTIMER_NORESTART;
6472}
6473
David S. Miller7c4ec742018-07-20 23:37:55 -07006474static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006475{
David Miller07d78362018-06-24 14:14:02 +09006476 int i;
6477
Li RongQing6312fe72018-07-05 14:34:32 +08006478 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6479 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6480 napi->gro_hash[i].count = 0;
6481 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006482 napi->gro_bitmask = 0;
6483}
6484
6485void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6486 int (*poll)(struct napi_struct *, int), int weight)
6487{
6488 INIT_LIST_HEAD(&napi->poll_list);
6489 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6490 napi->timer.function = napi_watchdog;
6491 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006492 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006493 INIT_LIST_HEAD(&napi->rx_list);
6494 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006495 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006496 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006497 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6498 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006499 napi->weight = weight;
6500 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08006501 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006502#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006503 napi->poll_owner = -1;
6504#endif
6505 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006506 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006507}
6508EXPORT_SYMBOL(netif_napi_add);
6509
Eric Dumazet3b47d302014-11-06 21:09:44 -08006510void napi_disable(struct napi_struct *n)
6511{
6512 might_sleep();
6513 set_bit(NAPI_STATE_DISABLE, &n->state);
6514
6515 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6516 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006517 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6518 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006519
6520 hrtimer_cancel(&n->timer);
6521
6522 clear_bit(NAPI_STATE_DISABLE, &n->state);
6523}
6524EXPORT_SYMBOL(napi_disable);
6525
David Miller07d78362018-06-24 14:14:02 +09006526static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006527{
David Miller07d78362018-06-24 14:14:02 +09006528 int i;
David Millerd4546c22018-06-24 14:13:49 +09006529
David Miller07d78362018-06-24 14:14:02 +09006530 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6531 struct sk_buff *skb, *n;
6532
Li RongQing6312fe72018-07-05 14:34:32 +08006533 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006534 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006535 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006536 }
David Millerd4546c22018-06-24 14:13:49 +09006537}
6538
Eric Dumazet93d05d42015-11-18 06:31:03 -08006539/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08006540void netif_napi_del(struct napi_struct *napi)
6541{
Eric Dumazet93d05d42015-11-18 06:31:03 -08006542 might_sleep();
6543 if (napi_hash_del(napi))
6544 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08006545 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006546 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006547
David Miller07d78362018-06-24 14:14:02 +09006548 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006549 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006550}
6551EXPORT_SYMBOL(netif_napi_del);
6552
Herbert Xu726ce702014-12-21 07:16:21 +11006553static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6554{
6555 void *have;
6556 int work, weight;
6557
6558 list_del_init(&n->poll_list);
6559
6560 have = netpoll_poll_lock(n);
6561
6562 weight = n->weight;
6563
6564 /* This NAPI_STATE_SCHED test is for avoiding a race
6565 * with netpoll's poll_napi(). Only the entity which
6566 * obtains the lock and sees NAPI_STATE_SCHED set will
6567 * actually make the ->poll() call. Therefore we avoid
6568 * accidentally calling ->poll() when NAPI is not scheduled.
6569 */
6570 work = 0;
6571 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6572 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006573 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006574 }
6575
6576 WARN_ON_ONCE(work > weight);
6577
6578 if (likely(work < weight))
6579 goto out_unlock;
6580
6581 /* Drivers must not modify the NAPI state if they
6582 * consume the entire weight. In such cases this code
6583 * still "owns" the NAPI instance and therefore can
6584 * move the instance around on the list at-will.
6585 */
6586 if (unlikely(napi_disable_pending(n))) {
6587 napi_complete(n);
6588 goto out_unlock;
6589 }
6590
Li RongQingd9f37d02018-07-13 14:41:36 +08006591 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006592 /* flush too old packets
6593 * If HZ < 1000, flush all packets.
6594 */
6595 napi_gro_flush(n, HZ >= 1000);
6596 }
6597
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006598 gro_normal_list(n);
6599
Herbert Xu001ce542014-12-21 07:16:22 +11006600 /* Some drivers may have called napi_schedule
6601 * prior to exhausting their budget.
6602 */
6603 if (unlikely(!list_empty(&n->poll_list))) {
6604 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6605 n->dev ? n->dev->name : "backlog");
6606 goto out_unlock;
6607 }
6608
Herbert Xu726ce702014-12-21 07:16:21 +11006609 list_add_tail(&n->poll_list, repoll);
6610
6611out_unlock:
6612 netpoll_poll_unlock(have);
6613
6614 return work;
6615}
6616
Emese Revfy0766f782016-06-20 20:42:34 +02006617static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006619 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006620 unsigned long time_limit = jiffies +
6621 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006622 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006623 LIST_HEAD(list);
6624 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006625
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006627 list_splice_init(&sd->poll_list, &list);
6628 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006630 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006631 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006633 if (list_empty(&list)) {
6634 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006635 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006636 break;
6637 }
6638
Herbert Xu6bd373e2014-12-21 07:16:24 +11006639 n = list_first_entry(&list, struct napi_struct, poll_list);
6640 budget -= napi_poll(n, &repoll);
6641
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006642 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006643 * Allow this to run for 2 jiffies since which will allow
6644 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006645 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006646 if (unlikely(budget <= 0 ||
6647 time_after_eq(jiffies, time_limit))) {
6648 sd->time_squeeze++;
6649 break;
6650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006652
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006653 local_irq_disable();
6654
6655 list_splice_tail_init(&sd->poll_list, &list);
6656 list_splice_tail(&repoll, &list);
6657 list_splice(&list, &sd->poll_list);
6658 if (!list_empty(&sd->poll_list))
6659 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6660
Eric Dumazete326bed2010-04-22 00:22:45 -07006661 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006662out:
6663 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664}
6665
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006666struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006667 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006668
6669 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006670 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006671
Taehee Yoo32b6d342019-10-21 18:47:56 +00006672 /* lookup ignore flag */
6673 bool ignore;
6674
Veaceslav Falico5d261912013-08-28 23:25:05 +02006675 /* counter for the number of times this device was added to us */
6676 u16 ref_nr;
6677
Veaceslav Falico402dae92013-09-25 09:20:09 +02006678 /* private field for the users */
6679 void *private;
6680
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006681 struct list_head list;
6682 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006683};
6684
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006685static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006686 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006687{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006688 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006689
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006690 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006691 if (adj->dev == adj_dev)
6692 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006693 }
6694 return NULL;
6695}
6696
Taehee Yoo32b6d342019-10-21 18:47:56 +00006697static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data)
David Ahernf1170fd2016-10-17 19:15:51 -07006698{
6699 struct net_device *dev = data;
6700
6701 return upper_dev == dev;
6702}
6703
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006704/**
6705 * netdev_has_upper_dev - Check if device is linked to an upper device
6706 * @dev: device
6707 * @upper_dev: upper device to check
6708 *
6709 * Find out if a device is linked to specified upper device and return true
6710 * in case it is. Note that this checks only immediate upper device,
6711 * not through a complete stack of devices. The caller must hold the RTNL lock.
6712 */
6713bool netdev_has_upper_dev(struct net_device *dev,
6714 struct net_device *upper_dev)
6715{
6716 ASSERT_RTNL();
6717
Taehee Yoo32b6d342019-10-21 18:47:56 +00006718 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
David Ahernf1170fd2016-10-17 19:15:51 -07006719 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006720}
6721EXPORT_SYMBOL(netdev_has_upper_dev);
6722
6723/**
David Ahern1a3f0602016-10-17 19:15:44 -07006724 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6725 * @dev: device
6726 * @upper_dev: upper device to check
6727 *
6728 * Find out if a device is linked to specified upper device and return true
6729 * in case it is. Note that this checks the entire upper device chain.
6730 * The caller must hold rcu lock.
6731 */
6732
David Ahern1a3f0602016-10-17 19:15:44 -07006733bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6734 struct net_device *upper_dev)
6735{
Taehee Yoo32b6d342019-10-21 18:47:56 +00006736 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
David Ahern1a3f0602016-10-17 19:15:44 -07006737 upper_dev);
6738}
6739EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6740
6741/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006742 * netdev_has_any_upper_dev - Check if device is linked to some device
6743 * @dev: device
6744 *
6745 * Find out if a device is linked to an upper device and return true in case
6746 * it is. The caller must hold the RTNL lock.
6747 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006748bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006749{
6750 ASSERT_RTNL();
6751
David Ahernf1170fd2016-10-17 19:15:51 -07006752 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006753}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006754EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006755
6756/**
6757 * netdev_master_upper_dev_get - Get master upper device
6758 * @dev: device
6759 *
6760 * Find a master upper device and return pointer to it or NULL in case
6761 * it's not there. The caller must hold the RTNL lock.
6762 */
6763struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6764{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006765 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006766
6767 ASSERT_RTNL();
6768
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006769 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006770 return NULL;
6771
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006772 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006773 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006774 if (likely(upper->master))
6775 return upper->dev;
6776 return NULL;
6777}
6778EXPORT_SYMBOL(netdev_master_upper_dev_get);
6779
Taehee Yoo32b6d342019-10-21 18:47:56 +00006780static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6781{
6782 struct netdev_adjacent *upper;
6783
6784 ASSERT_RTNL();
6785
6786 if (list_empty(&dev->adj_list.upper))
6787 return NULL;
6788
6789 upper = list_first_entry(&dev->adj_list.upper,
6790 struct netdev_adjacent, list);
6791 if (likely(upper->master) && !upper->ignore)
6792 return upper->dev;
6793 return NULL;
6794}
6795
David Ahern0f524a82016-10-17 19:15:52 -07006796/**
6797 * netdev_has_any_lower_dev - Check if device is linked to some device
6798 * @dev: device
6799 *
6800 * Find out if a device is linked to a lower device and return true in case
6801 * it is. The caller must hold the RTNL lock.
6802 */
6803static bool netdev_has_any_lower_dev(struct net_device *dev)
6804{
6805 ASSERT_RTNL();
6806
6807 return !list_empty(&dev->adj_list.lower);
6808}
6809
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02006810void *netdev_adjacent_get_private(struct list_head *adj_list)
6811{
6812 struct netdev_adjacent *adj;
6813
6814 adj = list_entry(adj_list, struct netdev_adjacent, list);
6815
6816 return adj->private;
6817}
6818EXPORT_SYMBOL(netdev_adjacent_get_private);
6819
Veaceslav Falico31088a12013-09-25 09:20:12 +02006820/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04006821 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6822 * @dev: device
6823 * @iter: list_head ** of the current position
6824 *
6825 * Gets the next device from the dev's upper list, starting from iter
6826 * position. The caller must hold RCU read lock.
6827 */
6828struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6829 struct list_head **iter)
6830{
6831 struct netdev_adjacent *upper;
6832
6833 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6834
6835 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6836
6837 if (&upper->list == &dev->adj_list.upper)
6838 return NULL;
6839
6840 *iter = &upper->list;
6841
6842 return upper->dev;
6843}
6844EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6845
Taehee Yoo32b6d342019-10-21 18:47:56 +00006846static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6847 struct list_head **iter,
6848 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00006849{
6850 struct netdev_adjacent *upper;
6851
6852 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6853
6854 if (&upper->list == &dev->adj_list.upper)
6855 return NULL;
6856
6857 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006858 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006859
6860 return upper->dev;
6861}
6862
David Ahern1a3f0602016-10-17 19:15:44 -07006863static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6864 struct list_head **iter)
6865{
6866 struct netdev_adjacent *upper;
6867
6868 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6869
6870 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6871
6872 if (&upper->list == &dev->adj_list.upper)
6873 return NULL;
6874
6875 *iter = &upper->list;
6876
6877 return upper->dev;
6878}
6879
Taehee Yoo32b6d342019-10-21 18:47:56 +00006880static int __netdev_walk_all_upper_dev(struct net_device *dev,
6881 int (*fn)(struct net_device *dev,
6882 void *data),
6883 void *data)
Taehee Yoo5343da42019-10-21 18:47:50 +00006884{
6885 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6886 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6887 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006888 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006889
6890 now = dev;
6891 iter = &dev->adj_list.upper;
6892
6893 while (1) {
6894 if (now != dev) {
6895 ret = fn(now, data);
6896 if (ret)
6897 return ret;
6898 }
6899
6900 next = NULL;
6901 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00006902 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00006903 if (!udev)
6904 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006905 if (ignore)
6906 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00006907
6908 next = udev;
6909 niter = &udev->adj_list.upper;
6910 dev_stack[cur] = now;
6911 iter_stack[cur++] = iter;
6912 break;
6913 }
6914
6915 if (!next) {
6916 if (!cur)
6917 return 0;
6918 next = dev_stack[--cur];
6919 niter = iter_stack[cur];
6920 }
6921
6922 now = next;
6923 iter = niter;
6924 }
6925
6926 return 0;
6927}
6928
David Ahern1a3f0602016-10-17 19:15:44 -07006929int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6930 int (*fn)(struct net_device *dev,
6931 void *data),
6932 void *data)
6933{
Taehee Yoo5343da42019-10-21 18:47:50 +00006934 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6935 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6936 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07006937
Taehee Yoo5343da42019-10-21 18:47:50 +00006938 now = dev;
6939 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07006940
Taehee Yoo5343da42019-10-21 18:47:50 +00006941 while (1) {
6942 if (now != dev) {
6943 ret = fn(now, data);
6944 if (ret)
6945 return ret;
6946 }
6947
6948 next = NULL;
6949 while (1) {
6950 udev = netdev_next_upper_dev_rcu(now, &iter);
6951 if (!udev)
6952 break;
6953
6954 next = udev;
6955 niter = &udev->adj_list.upper;
6956 dev_stack[cur] = now;
6957 iter_stack[cur++] = iter;
6958 break;
6959 }
6960
6961 if (!next) {
6962 if (!cur)
6963 return 0;
6964 next = dev_stack[--cur];
6965 niter = iter_stack[cur];
6966 }
6967
6968 now = next;
6969 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07006970 }
6971
6972 return 0;
6973}
6974EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6975
Taehee Yoo32b6d342019-10-21 18:47:56 +00006976static bool __netdev_has_upper_dev(struct net_device *dev,
6977 struct net_device *upper_dev)
6978{
6979 ASSERT_RTNL();
6980
6981 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
6982 upper_dev);
6983}
6984
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006985/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02006986 * netdev_lower_get_next_private - Get the next ->private from the
6987 * lower neighbour list
6988 * @dev: device
6989 * @iter: list_head ** of the current position
6990 *
6991 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6992 * list, starting from iter position. The caller must hold either hold the
6993 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006994 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02006995 */
6996void *netdev_lower_get_next_private(struct net_device *dev,
6997 struct list_head **iter)
6998{
6999 struct netdev_adjacent *lower;
7000
7001 lower = list_entry(*iter, struct netdev_adjacent, list);
7002
7003 if (&lower->list == &dev->adj_list.lower)
7004 return NULL;
7005
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007006 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007007
7008 return lower->private;
7009}
7010EXPORT_SYMBOL(netdev_lower_get_next_private);
7011
7012/**
7013 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7014 * lower neighbour list, RCU
7015 * variant
7016 * @dev: device
7017 * @iter: list_head ** of the current position
7018 *
7019 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7020 * list, starting from iter position. The caller must hold RCU read lock.
7021 */
7022void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7023 struct list_head **iter)
7024{
7025 struct netdev_adjacent *lower;
7026
7027 WARN_ON_ONCE(!rcu_read_lock_held());
7028
7029 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7030
7031 if (&lower->list == &dev->adj_list.lower)
7032 return NULL;
7033
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007034 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007035
7036 return lower->private;
7037}
7038EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7039
7040/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007041 * netdev_lower_get_next - Get the next device from the lower neighbour
7042 * list
7043 * @dev: device
7044 * @iter: list_head ** of the current position
7045 *
7046 * Gets the next netdev_adjacent from the dev's lower neighbour
7047 * list, starting from iter position. The caller must hold RTNL lock or
7048 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007049 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007050 */
7051void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7052{
7053 struct netdev_adjacent *lower;
7054
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007055 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007056
7057 if (&lower->list == &dev->adj_list.lower)
7058 return NULL;
7059
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007060 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007061
7062 return lower->dev;
7063}
7064EXPORT_SYMBOL(netdev_lower_get_next);
7065
David Ahern1a3f0602016-10-17 19:15:44 -07007066static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7067 struct list_head **iter)
7068{
7069 struct netdev_adjacent *lower;
7070
David Ahern46b5ab12016-10-26 13:21:33 -07007071 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007072
7073 if (&lower->list == &dev->adj_list.lower)
7074 return NULL;
7075
David Ahern46b5ab12016-10-26 13:21:33 -07007076 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007077
7078 return lower->dev;
7079}
7080
Taehee Yoo32b6d342019-10-21 18:47:56 +00007081static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7082 struct list_head **iter,
7083 bool *ignore)
7084{
7085 struct netdev_adjacent *lower;
7086
7087 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7088
7089 if (&lower->list == &dev->adj_list.lower)
7090 return NULL;
7091
7092 *iter = &lower->list;
7093 *ignore = lower->ignore;
7094
7095 return lower->dev;
7096}
7097
David Ahern1a3f0602016-10-17 19:15:44 -07007098int netdev_walk_all_lower_dev(struct net_device *dev,
7099 int (*fn)(struct net_device *dev,
7100 void *data),
7101 void *data)
7102{
Taehee Yoo5343da42019-10-21 18:47:50 +00007103 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7104 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7105 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007106
Taehee Yoo5343da42019-10-21 18:47:50 +00007107 now = dev;
7108 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007109
Taehee Yoo5343da42019-10-21 18:47:50 +00007110 while (1) {
7111 if (now != dev) {
7112 ret = fn(now, data);
7113 if (ret)
7114 return ret;
7115 }
7116
7117 next = NULL;
7118 while (1) {
7119 ldev = netdev_next_lower_dev(now, &iter);
7120 if (!ldev)
7121 break;
7122
7123 next = ldev;
7124 niter = &ldev->adj_list.lower;
7125 dev_stack[cur] = now;
7126 iter_stack[cur++] = iter;
7127 break;
7128 }
7129
7130 if (!next) {
7131 if (!cur)
7132 return 0;
7133 next = dev_stack[--cur];
7134 niter = iter_stack[cur];
7135 }
7136
7137 now = next;
7138 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007139 }
7140
7141 return 0;
7142}
7143EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7144
Taehee Yoo32b6d342019-10-21 18:47:56 +00007145static int __netdev_walk_all_lower_dev(struct net_device *dev,
7146 int (*fn)(struct net_device *dev,
7147 void *data),
7148 void *data)
7149{
7150 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7151 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7152 int ret, cur = 0;
7153 bool ignore;
7154
7155 now = dev;
7156 iter = &dev->adj_list.lower;
7157
7158 while (1) {
7159 if (now != dev) {
7160 ret = fn(now, data);
7161 if (ret)
7162 return ret;
7163 }
7164
7165 next = NULL;
7166 while (1) {
7167 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7168 if (!ldev)
7169 break;
7170 if (ignore)
7171 continue;
7172
7173 next = ldev;
7174 niter = &ldev->adj_list.lower;
7175 dev_stack[cur] = now;
7176 iter_stack[cur++] = iter;
7177 break;
7178 }
7179
7180 if (!next) {
7181 if (!cur)
7182 return 0;
7183 next = dev_stack[--cur];
7184 niter = iter_stack[cur];
7185 }
7186
7187 now = next;
7188 iter = niter;
7189 }
7190
7191 return 0;
7192}
7193
Taehee Yoo7151aff2020-02-15 10:50:21 +00007194struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7195 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007196{
7197 struct netdev_adjacent *lower;
7198
7199 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7200 if (&lower->list == &dev->adj_list.lower)
7201 return NULL;
7202
7203 *iter = &lower->list;
7204
7205 return lower->dev;
7206}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007207EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007208
Taehee Yoo5343da42019-10-21 18:47:50 +00007209static u8 __netdev_upper_depth(struct net_device *dev)
7210{
7211 struct net_device *udev;
7212 struct list_head *iter;
7213 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007214 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007215
7216 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007217 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007218 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007219 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7220 if (ignore)
7221 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007222 if (max_depth < udev->upper_level)
7223 max_depth = udev->upper_level;
7224 }
7225
7226 return max_depth;
7227}
7228
7229static u8 __netdev_lower_depth(struct net_device *dev)
7230{
7231 struct net_device *ldev;
7232 struct list_head *iter;
7233 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007234 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007235
7236 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007237 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007238 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007239 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7240 if (ignore)
7241 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007242 if (max_depth < ldev->lower_level)
7243 max_depth = ldev->lower_level;
7244 }
7245
7246 return max_depth;
7247}
7248
7249static int __netdev_update_upper_level(struct net_device *dev, void *data)
7250{
7251 dev->upper_level = __netdev_upper_depth(dev) + 1;
7252 return 0;
7253}
7254
7255static int __netdev_update_lower_level(struct net_device *dev, void *data)
7256{
7257 dev->lower_level = __netdev_lower_depth(dev) + 1;
7258 return 0;
7259}
7260
David Ahern1a3f0602016-10-17 19:15:44 -07007261int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7262 int (*fn)(struct net_device *dev,
7263 void *data),
7264 void *data)
7265{
Taehee Yoo5343da42019-10-21 18:47:50 +00007266 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7267 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7268 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007269
Taehee Yoo5343da42019-10-21 18:47:50 +00007270 now = dev;
7271 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007272
Taehee Yoo5343da42019-10-21 18:47:50 +00007273 while (1) {
7274 if (now != dev) {
7275 ret = fn(now, data);
7276 if (ret)
7277 return ret;
7278 }
7279
7280 next = NULL;
7281 while (1) {
7282 ldev = netdev_next_lower_dev_rcu(now, &iter);
7283 if (!ldev)
7284 break;
7285
7286 next = ldev;
7287 niter = &ldev->adj_list.lower;
7288 dev_stack[cur] = now;
7289 iter_stack[cur++] = iter;
7290 break;
7291 }
7292
7293 if (!next) {
7294 if (!cur)
7295 return 0;
7296 next = dev_stack[--cur];
7297 niter = iter_stack[cur];
7298 }
7299
7300 now = next;
7301 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007302 }
7303
7304 return 0;
7305}
7306EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7307
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007308/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007309 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7310 * lower neighbour list, RCU
7311 * variant
7312 * @dev: device
7313 *
7314 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7315 * list. The caller must hold RCU read lock.
7316 */
7317void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7318{
7319 struct netdev_adjacent *lower;
7320
7321 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7322 struct netdev_adjacent, list);
7323 if (lower)
7324 return lower->private;
7325 return NULL;
7326}
7327EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7328
7329/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007330 * netdev_master_upper_dev_get_rcu - Get master upper device
7331 * @dev: device
7332 *
7333 * Find a master upper device and return pointer to it or NULL in case
7334 * it's not there. The caller must hold the RCU read lock.
7335 */
7336struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7337{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007338 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007339
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007340 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007341 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007342 if (upper && likely(upper->master))
7343 return upper->dev;
7344 return NULL;
7345}
7346EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7347
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307348static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007349 struct net_device *adj_dev,
7350 struct list_head *dev_list)
7351{
7352 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007353
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007354 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7355 "upper_%s" : "lower_%s", adj_dev->name);
7356 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7357 linkname);
7358}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307359static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007360 char *name,
7361 struct list_head *dev_list)
7362{
7363 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007364
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007365 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7366 "upper_%s" : "lower_%s", name);
7367 sysfs_remove_link(&(dev->dev.kobj), linkname);
7368}
7369
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007370static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7371 struct net_device *adj_dev,
7372 struct list_head *dev_list)
7373{
7374 return (dev_list == &dev->adj_list.upper ||
7375 dev_list == &dev->adj_list.lower) &&
7376 net_eq(dev_net(dev), dev_net(adj_dev));
7377}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007378
Veaceslav Falico5d261912013-08-28 23:25:05 +02007379static int __netdev_adjacent_dev_insert(struct net_device *dev,
7380 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007381 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007382 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007383{
7384 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007385 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007386
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007387 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007388
7389 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007390 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007391 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7392 dev->name, adj_dev->name, adj->ref_nr);
7393
Veaceslav Falico5d261912013-08-28 23:25:05 +02007394 return 0;
7395 }
7396
7397 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7398 if (!adj)
7399 return -ENOMEM;
7400
7401 adj->dev = adj_dev;
7402 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007403 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007404 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007405 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007406 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007407
David Ahern67b62f92016-10-17 19:15:53 -07007408 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7409 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007410
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007411 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007412 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007413 if (ret)
7414 goto free_adj;
7415 }
7416
Veaceslav Falico7863c052013-09-25 09:20:06 +02007417 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007418 if (master) {
7419 ret = sysfs_create_link(&(dev->dev.kobj),
7420 &(adj_dev->dev.kobj), "master");
7421 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007422 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007423
Veaceslav Falico7863c052013-09-25 09:20:06 +02007424 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007425 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007426 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007427 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007428
7429 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007430
Veaceslav Falico5831d662013-09-25 09:20:32 +02007431remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007432 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007433 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007434free_adj:
7435 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007436 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007437
7438 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007439}
7440
stephen hemminger1d143d92013-12-29 14:01:29 -08007441static void __netdev_adjacent_dev_remove(struct net_device *dev,
7442 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007443 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007444 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007445{
7446 struct netdev_adjacent *adj;
7447
David Ahern67b62f92016-10-17 19:15:53 -07007448 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7449 dev->name, adj_dev->name, ref_nr);
7450
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007451 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007452
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007453 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007454 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007455 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007456 WARN_ON(1);
7457 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007458 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007459
Andrew Collins93409032016-10-03 13:43:02 -06007460 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007461 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7462 dev->name, adj_dev->name, ref_nr,
7463 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007464 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007465 return;
7466 }
7467
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007468 if (adj->master)
7469 sysfs_remove_link(&(dev->dev.kobj), "master");
7470
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007471 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007472 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007473
Veaceslav Falico5d261912013-08-28 23:25:05 +02007474 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007475 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007476 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007477 dev_put(adj_dev);
7478 kfree_rcu(adj, rcu);
7479}
7480
stephen hemminger1d143d92013-12-29 14:01:29 -08007481static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7482 struct net_device *upper_dev,
7483 struct list_head *up_list,
7484 struct list_head *down_list,
7485 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007486{
7487 int ret;
7488
David Ahern790510d2016-10-17 19:15:43 -07007489 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007490 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007491 if (ret)
7492 return ret;
7493
David Ahern790510d2016-10-17 19:15:43 -07007494 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007495 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007496 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007497 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007498 return ret;
7499 }
7500
7501 return 0;
7502}
7503
stephen hemminger1d143d92013-12-29 14:01:29 -08007504static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7505 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007506 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007507 struct list_head *up_list,
7508 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007509{
Andrew Collins93409032016-10-03 13:43:02 -06007510 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7511 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007512}
7513
stephen hemminger1d143d92013-12-29 14:01:29 -08007514static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7515 struct net_device *upper_dev,
7516 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007517{
David Ahernf1170fd2016-10-17 19:15:51 -07007518 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7519 &dev->adj_list.upper,
7520 &upper_dev->adj_list.lower,
7521 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007522}
7523
stephen hemminger1d143d92013-12-29 14:01:29 -08007524static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7525 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007526{
Andrew Collins93409032016-10-03 13:43:02 -06007527 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007528 &dev->adj_list.upper,
7529 &upper_dev->adj_list.lower);
7530}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007531
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007532static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007533 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007534 void *upper_priv, void *upper_info,
7535 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007536{
David Ahern51d0c0472017-10-04 17:48:45 -07007537 struct netdev_notifier_changeupper_info changeupper_info = {
7538 .info = {
7539 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007540 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007541 },
7542 .upper_dev = upper_dev,
7543 .master = master,
7544 .linking = true,
7545 .upper_info = upper_info,
7546 };
Mike Manning50d629e2018-02-26 23:49:30 +00007547 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007548 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007549
7550 ASSERT_RTNL();
7551
7552 if (dev == upper_dev)
7553 return -EBUSY;
7554
7555 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007556 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007557 return -EBUSY;
7558
Taehee Yoo5343da42019-10-21 18:47:50 +00007559 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7560 return -EMLINK;
7561
Mike Manning50d629e2018-02-26 23:49:30 +00007562 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007563 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007564 return -EEXIST;
7565 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007566 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007567 if (master_dev)
7568 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7569 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007570
David Ahern51d0c0472017-10-04 17:48:45 -07007571 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007572 &changeupper_info.info);
7573 ret = notifier_to_errno(ret);
7574 if (ret)
7575 return ret;
7576
Jiri Pirko6dffb042015-12-03 12:12:10 +01007577 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007578 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007579 if (ret)
7580 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007581
David Ahern51d0c0472017-10-04 17:48:45 -07007582 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007583 &changeupper_info.info);
7584 ret = notifier_to_errno(ret);
7585 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007586 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007587
Taehee Yoo5343da42019-10-21 18:47:50 +00007588 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007589 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007590
7591 __netdev_update_lower_level(upper_dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007592 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7593 NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007594
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007595 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007596
David Ahernf1170fd2016-10-17 19:15:51 -07007597rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007598 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007599
7600 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007601}
7602
7603/**
7604 * netdev_upper_dev_link - Add a link to the upper device
7605 * @dev: device
7606 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007607 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007608 *
7609 * Adds a link to device which is upper to this one. The caller must hold
7610 * the RTNL lock. On a failure a negative errno code is returned.
7611 * On success the reference counts are adjusted and the function
7612 * returns zero.
7613 */
7614int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007615 struct net_device *upper_dev,
7616 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007617{
David Ahern42ab19e2017-10-04 17:48:47 -07007618 return __netdev_upper_dev_link(dev, upper_dev, false,
7619 NULL, NULL, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007620}
7621EXPORT_SYMBOL(netdev_upper_dev_link);
7622
7623/**
7624 * netdev_master_upper_dev_link - Add a master link to the upper device
7625 * @dev: device
7626 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007627 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007628 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007629 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007630 *
7631 * Adds a link to device which is upper to this one. In this case, only
7632 * one master upper device can be linked, although other non-master devices
7633 * might be linked as well. The caller must hold the RTNL lock.
7634 * On a failure a negative errno code is returned. On success the reference
7635 * counts are adjusted and the function returns zero.
7636 */
7637int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007638 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007639 void *upper_priv, void *upper_info,
7640 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007641{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007642 return __netdev_upper_dev_link(dev, upper_dev, true,
David Ahern42ab19e2017-10-04 17:48:47 -07007643 upper_priv, upper_info, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007644}
7645EXPORT_SYMBOL(netdev_master_upper_dev_link);
7646
7647/**
7648 * netdev_upper_dev_unlink - Removes a link to upper device
7649 * @dev: device
7650 * @upper_dev: new upper device
7651 *
7652 * Removes a link to device which is upper to this one. The caller must hold
7653 * the RTNL lock.
7654 */
7655void netdev_upper_dev_unlink(struct net_device *dev,
7656 struct net_device *upper_dev)
7657{
David Ahern51d0c0472017-10-04 17:48:45 -07007658 struct netdev_notifier_changeupper_info changeupper_info = {
7659 .info = {
7660 .dev = dev,
7661 },
7662 .upper_dev = upper_dev,
7663 .linking = false,
7664 };
tchardingf4563a72017-02-09 17:56:07 +11007665
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007666 ASSERT_RTNL();
7667
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007668 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007669
David Ahern51d0c0472017-10-04 17:48:45 -07007670 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007671 &changeupper_info.info);
7672
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007673 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007674
David Ahern51d0c0472017-10-04 17:48:45 -07007675 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007676 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007677
7678 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007679 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007680
7681 __netdev_update_lower_level(upper_dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007682 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7683 NULL);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007684}
7685EXPORT_SYMBOL(netdev_upper_dev_unlink);
7686
Taehee Yoo32b6d342019-10-21 18:47:56 +00007687static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7688 struct net_device *lower_dev,
7689 bool val)
7690{
7691 struct netdev_adjacent *adj;
7692
7693 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7694 if (adj)
7695 adj->ignore = val;
7696
7697 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7698 if (adj)
7699 adj->ignore = val;
7700}
7701
7702static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7703 struct net_device *lower_dev)
7704{
7705 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7706}
7707
7708static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7709 struct net_device *lower_dev)
7710{
7711 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7712}
7713
7714int netdev_adjacent_change_prepare(struct net_device *old_dev,
7715 struct net_device *new_dev,
7716 struct net_device *dev,
7717 struct netlink_ext_ack *extack)
7718{
7719 int err;
7720
7721 if (!new_dev)
7722 return 0;
7723
7724 if (old_dev && new_dev != old_dev)
7725 netdev_adjacent_dev_disable(dev, old_dev);
7726
7727 err = netdev_upper_dev_link(new_dev, dev, extack);
7728 if (err) {
7729 if (old_dev && new_dev != old_dev)
7730 netdev_adjacent_dev_enable(dev, old_dev);
7731 return err;
7732 }
7733
7734 return 0;
7735}
7736EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7737
7738void netdev_adjacent_change_commit(struct net_device *old_dev,
7739 struct net_device *new_dev,
7740 struct net_device *dev)
7741{
7742 if (!new_dev || !old_dev)
7743 return;
7744
7745 if (new_dev == old_dev)
7746 return;
7747
7748 netdev_adjacent_dev_enable(dev, old_dev);
7749 netdev_upper_dev_unlink(old_dev, dev);
7750}
7751EXPORT_SYMBOL(netdev_adjacent_change_commit);
7752
7753void netdev_adjacent_change_abort(struct net_device *old_dev,
7754 struct net_device *new_dev,
7755 struct net_device *dev)
7756{
7757 if (!new_dev)
7758 return;
7759
7760 if (old_dev && new_dev != old_dev)
7761 netdev_adjacent_dev_enable(dev, old_dev);
7762
7763 netdev_upper_dev_unlink(new_dev, dev);
7764}
7765EXPORT_SYMBOL(netdev_adjacent_change_abort);
7766
Moni Shoua61bd3852015-02-03 16:48:29 +02007767/**
7768 * netdev_bonding_info_change - Dispatch event about slave change
7769 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09007770 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02007771 *
7772 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7773 * The caller must hold the RTNL lock.
7774 */
7775void netdev_bonding_info_change(struct net_device *dev,
7776 struct netdev_bonding_info *bonding_info)
7777{
David Ahern51d0c0472017-10-04 17:48:45 -07007778 struct netdev_notifier_bonding_info info = {
7779 .info.dev = dev,
7780 };
Moni Shoua61bd3852015-02-03 16:48:29 +02007781
7782 memcpy(&info.bonding_info, bonding_info,
7783 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07007784 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02007785 &info.info);
7786}
7787EXPORT_SYMBOL(netdev_bonding_info_change);
7788
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007789static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007790{
7791 struct netdev_adjacent *iter;
7792
7793 struct net *net = dev_net(dev);
7794
7795 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007796 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007797 continue;
7798 netdev_adjacent_sysfs_add(iter->dev, dev,
7799 &iter->dev->adj_list.lower);
7800 netdev_adjacent_sysfs_add(dev, iter->dev,
7801 &dev->adj_list.upper);
7802 }
7803
7804 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007805 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007806 continue;
7807 netdev_adjacent_sysfs_add(iter->dev, dev,
7808 &iter->dev->adj_list.upper);
7809 netdev_adjacent_sysfs_add(dev, iter->dev,
7810 &dev->adj_list.lower);
7811 }
7812}
7813
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007814static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007815{
7816 struct netdev_adjacent *iter;
7817
7818 struct net *net = dev_net(dev);
7819
7820 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007821 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007822 continue;
7823 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7824 &iter->dev->adj_list.lower);
7825 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7826 &dev->adj_list.upper);
7827 }
7828
7829 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007830 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007831 continue;
7832 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7833 &iter->dev->adj_list.upper);
7834 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7835 &dev->adj_list.lower);
7836 }
7837}
7838
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007839void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02007840{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007841 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007842
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007843 struct net *net = dev_net(dev);
7844
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007845 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007846 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007847 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007848 netdev_adjacent_sysfs_del(iter->dev, oldname,
7849 &iter->dev->adj_list.lower);
7850 netdev_adjacent_sysfs_add(iter->dev, dev,
7851 &iter->dev->adj_list.lower);
7852 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007853
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007854 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007855 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007856 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007857 netdev_adjacent_sysfs_del(iter->dev, oldname,
7858 &iter->dev->adj_list.upper);
7859 netdev_adjacent_sysfs_add(iter->dev, dev,
7860 &iter->dev->adj_list.upper);
7861 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007862}
Veaceslav Falico402dae92013-09-25 09:20:09 +02007863
7864void *netdev_lower_dev_get_private(struct net_device *dev,
7865 struct net_device *lower_dev)
7866{
7867 struct netdev_adjacent *lower;
7868
7869 if (!lower_dev)
7870 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007871 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02007872 if (!lower)
7873 return NULL;
7874
7875 return lower->private;
7876}
7877EXPORT_SYMBOL(netdev_lower_dev_get_private);
7878
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007879
Jiri Pirko04d48262015-12-03 12:12:15 +01007880/**
7881 * netdev_lower_change - Dispatch event about lower device state change
7882 * @lower_dev: device
7883 * @lower_state_info: state to dispatch
7884 *
7885 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7886 * The caller must hold the RTNL lock.
7887 */
7888void netdev_lower_state_changed(struct net_device *lower_dev,
7889 void *lower_state_info)
7890{
David Ahern51d0c0472017-10-04 17:48:45 -07007891 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7892 .info.dev = lower_dev,
7893 };
Jiri Pirko04d48262015-12-03 12:12:15 +01007894
7895 ASSERT_RTNL();
7896 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07007897 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01007898 &changelowerstate_info.info);
7899}
7900EXPORT_SYMBOL(netdev_lower_state_changed);
7901
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007902static void dev_change_rx_flags(struct net_device *dev, int flags)
7903{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007904 const struct net_device_ops *ops = dev->netdev_ops;
7905
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05007906 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007907 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007908}
7909
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007910static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07007911{
Eric Dumazetb536db92011-11-30 21:42:26 +00007912 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06007913 kuid_t uid;
7914 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07007915
Patrick McHardy24023452007-07-14 18:51:31 -07007916 ASSERT_RTNL();
7917
Wang Chendad9b332008-06-18 01:48:28 -07007918 dev->flags |= IFF_PROMISC;
7919 dev->promiscuity += inc;
7920 if (dev->promiscuity == 0) {
7921 /*
7922 * Avoid overflow.
7923 * If inc causes overflow, untouch promisc and return error.
7924 */
7925 if (inc < 0)
7926 dev->flags &= ~IFF_PROMISC;
7927 else {
7928 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007929 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7930 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007931 return -EOVERFLOW;
7932 }
7933 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007934 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007935 pr_info("device %s %s promiscuous mode\n",
7936 dev->name,
7937 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11007938 if (audit_enabled) {
7939 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04007940 audit_log(audit_context(), GFP_ATOMIC,
7941 AUDIT_ANOM_PROMISCUOUS,
7942 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7943 dev->name, (dev->flags & IFF_PROMISC),
7944 (old_flags & IFF_PROMISC),
7945 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7946 from_kuid(&init_user_ns, uid),
7947 from_kgid(&init_user_ns, gid),
7948 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11007949 }
Patrick McHardy24023452007-07-14 18:51:31 -07007950
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007951 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07007952 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007953 if (notify)
7954 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07007955 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007956}
7957
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958/**
7959 * dev_set_promiscuity - update promiscuity count on a device
7960 * @dev: device
7961 * @inc: modifier
7962 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07007963 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 * remains above zero the interface remains promiscuous. Once it hits zero
7965 * the device reverts back to normal filtering operation. A negative inc
7966 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07007967 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968 */
Wang Chendad9b332008-06-18 01:48:28 -07007969int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970{
Eric Dumazetb536db92011-11-30 21:42:26 +00007971 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07007972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007974 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07007975 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07007976 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07007977 if (dev->flags != old_flags)
7978 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07007979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007981EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007983static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007985 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986
Patrick McHardy24023452007-07-14 18:51:31 -07007987 ASSERT_RTNL();
7988
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07007990 dev->allmulti += inc;
7991 if (dev->allmulti == 0) {
7992 /*
7993 * Avoid overflow.
7994 * If inc causes overflow, untouch allmulti and return error.
7995 */
7996 if (inc < 0)
7997 dev->flags &= ~IFF_ALLMULTI;
7998 else {
7999 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008000 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8001 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008002 return -EOVERFLOW;
8003 }
8004 }
Patrick McHardy24023452007-07-14 18:51:31 -07008005 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008006 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008007 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008008 if (notify)
8009 __dev_notify_flags(dev, old_flags,
8010 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008011 }
Wang Chendad9b332008-06-18 01:48:28 -07008012 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008013}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008014
8015/**
8016 * dev_set_allmulti - update allmulti count on a device
8017 * @dev: device
8018 * @inc: modifier
8019 *
8020 * Add or remove reception of all multicast frames to a device. While the
8021 * count in the device remains above zero the interface remains listening
8022 * to all interfaces. Once it hits zero the device reverts back to normal
8023 * filtering operation. A negative @inc value is used to drop the counter
8024 * when releasing a resource needing all multicasts.
8025 * Return 0 if successful or a negative errno code on error.
8026 */
8027
8028int dev_set_allmulti(struct net_device *dev, int inc)
8029{
8030 return __dev_set_allmulti(dev, inc, true);
8031}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008032EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008033
8034/*
8035 * Upload unicast and multicast address lists to device and
8036 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008037 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008038 * are present.
8039 */
8040void __dev_set_rx_mode(struct net_device *dev)
8041{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008042 const struct net_device_ops *ops = dev->netdev_ops;
8043
Patrick McHardy4417da62007-06-27 01:28:10 -07008044 /* dev_open will call this function so the list will stay sane. */
8045 if (!(dev->flags&IFF_UP))
8046 return;
8047
8048 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008049 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008050
Jiri Pirko01789342011-08-16 06:29:00 +00008051 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008052 /* Unicast addresses changes may only happen under the rtnl,
8053 * therefore calling __dev_set_promiscuity here is safe.
8054 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008055 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008056 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008057 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008058 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008059 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008060 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008061 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008062 }
Jiri Pirko01789342011-08-16 06:29:00 +00008063
8064 if (ops->ndo_set_rx_mode)
8065 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008066}
8067
8068void dev_set_rx_mode(struct net_device *dev)
8069{
David S. Millerb9e40852008-07-15 00:15:08 -07008070 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008071 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008072 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073}
8074
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008075/**
8076 * dev_get_flags - get flags reported to userspace
8077 * @dev: device
8078 *
8079 * Get the combination of flag bits exported through APIs to userspace.
8080 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008081unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082{
Eric Dumazet95c96172012-04-15 05:58:06 +00008083 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084
8085 flags = (dev->flags & ~(IFF_PROMISC |
8086 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008087 IFF_RUNNING |
8088 IFF_LOWER_UP |
8089 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 (dev->gflags & (IFF_PROMISC |
8091 IFF_ALLMULTI));
8092
Stefan Rompfb00055a2006-03-20 17:09:11 -08008093 if (netif_running(dev)) {
8094 if (netif_oper_up(dev))
8095 flags |= IFF_RUNNING;
8096 if (netif_carrier_ok(dev))
8097 flags |= IFF_LOWER_UP;
8098 if (netif_dormant(dev))
8099 flags |= IFF_DORMANT;
8100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101
8102 return flags;
8103}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008104EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105
Petr Machata6d040322018-12-06 17:05:43 +00008106int __dev_change_flags(struct net_device *dev, unsigned int flags,
8107 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108{
Eric Dumazetb536db92011-11-30 21:42:26 +00008109 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008110 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111
Patrick McHardy24023452007-07-14 18:51:31 -07008112 ASSERT_RTNL();
8113
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 /*
8115 * Set the flags on our device.
8116 */
8117
8118 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8119 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8120 IFF_AUTOMEDIA)) |
8121 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8122 IFF_ALLMULTI));
8123
8124 /*
8125 * Load in the correct multicast list now the flags have changed.
8126 */
8127
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008128 if ((old_flags ^ flags) & IFF_MULTICAST)
8129 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008130
Patrick McHardy4417da62007-06-27 01:28:10 -07008131 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132
8133 /*
8134 * Have we downed the interface. We handle IFF_UP ourselves
8135 * according to user attempts to set it, rather than blindly
8136 * setting it.
8137 */
8138
8139 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008140 if ((old_flags ^ flags) & IFF_UP) {
8141 if (old_flags & IFF_UP)
8142 __dev_close(dev);
8143 else
Petr Machata40c900a2018-12-06 17:05:47 +00008144 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008148 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008149 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008150
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008152
8153 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8154 if (dev->flags != old_flags)
8155 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 }
8157
8158 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008159 * is important. Some (broken) drivers set IFF_PROMISC, when
8160 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 */
8162 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008163 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8164
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008166 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167 }
8168
Patrick McHardybd380812010-02-26 06:34:53 +00008169 return ret;
8170}
8171
Nicolas Dichtela528c212013-09-25 12:02:44 +02008172void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8173 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008174{
8175 unsigned int changes = dev->flags ^ old_flags;
8176
Nicolas Dichtela528c212013-09-25 12:02:44 +02008177 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008178 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008179
Patrick McHardybd380812010-02-26 06:34:53 +00008180 if (changes & IFF_UP) {
8181 if (dev->flags & IFF_UP)
8182 call_netdevice_notifiers(NETDEV_UP, dev);
8183 else
8184 call_netdevice_notifiers(NETDEV_DOWN, dev);
8185 }
8186
8187 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008188 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008189 struct netdev_notifier_change_info change_info = {
8190 .info = {
8191 .dev = dev,
8192 },
8193 .flags_changed = changes,
8194 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008195
David Ahern51d0c0472017-10-04 17:48:45 -07008196 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008197 }
Patrick McHardybd380812010-02-26 06:34:53 +00008198}
8199
8200/**
8201 * dev_change_flags - change device settings
8202 * @dev: device
8203 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008204 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008205 *
8206 * Change settings on device based state flags. The flags are
8207 * in the userspace exported format.
8208 */
Petr Machata567c5e12018-12-06 17:05:42 +00008209int dev_change_flags(struct net_device *dev, unsigned int flags,
8210 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008211{
Eric Dumazetb536db92011-11-30 21:42:26 +00008212 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008213 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008214
Petr Machata6d040322018-12-06 17:05:43 +00008215 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008216 if (ret < 0)
8217 return ret;
8218
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008219 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008220 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 return ret;
8222}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008223EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224
WANG Congf51048c2017-07-06 15:01:57 -07008225int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008226{
8227 const struct net_device_ops *ops = dev->netdev_ops;
8228
8229 if (ops->ndo_change_mtu)
8230 return ops->ndo_change_mtu(dev, new_mtu);
8231
Eric Dumazet501a90c2019-12-05 20:43:46 -08008232 /* Pairs with all the lockless reads of dev->mtu in the stack */
8233 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008234 return 0;
8235}
WANG Congf51048c2017-07-06 15:01:57 -07008236EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008237
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008238int dev_validate_mtu(struct net_device *dev, int new_mtu,
8239 struct netlink_ext_ack *extack)
8240{
8241 /* MTU must be positive, and in range */
8242 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8243 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8244 return -EINVAL;
8245 }
8246
8247 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8248 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8249 return -EINVAL;
8250 }
8251 return 0;
8252}
8253
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008254/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008255 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008256 * @dev: device
8257 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008258 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008259 *
8260 * Change the maximum transfer size of the network device.
8261 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008262int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8263 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008265 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266
8267 if (new_mtu == dev->mtu)
8268 return 0;
8269
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008270 err = dev_validate_mtu(dev, new_mtu, extack);
8271 if (err)
8272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273
8274 if (!netif_device_present(dev))
8275 return -ENODEV;
8276
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008277 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8278 err = notifier_to_errno(err);
8279 if (err)
8280 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008281
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008282 orig_mtu = dev->mtu;
8283 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008285 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008286 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8287 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008288 err = notifier_to_errno(err);
8289 if (err) {
8290 /* setting mtu back and notifying everyone again,
8291 * so that they have a chance to revert changes.
8292 */
8293 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008294 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8295 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008296 }
8297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 return err;
8299}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008300
8301int dev_set_mtu(struct net_device *dev, int new_mtu)
8302{
8303 struct netlink_ext_ack extack;
8304 int err;
8305
Li RongQinga6bcfc82018-08-03 15:45:21 +08008306 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008307 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008308 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008309 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8310 return err;
8311}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008312EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008314/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008315 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8316 * @dev: device
8317 * @new_len: new tx queue length
8318 */
8319int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8320{
8321 unsigned int orig_len = dev->tx_queue_len;
8322 int res;
8323
8324 if (new_len != (unsigned int)new_len)
8325 return -ERANGE;
8326
8327 if (new_len != orig_len) {
8328 dev->tx_queue_len = new_len;
8329 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8330 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008331 if (res)
8332 goto err_rollback;
8333 res = dev_qdisc_change_tx_queue_len(dev);
8334 if (res)
8335 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008336 }
8337
8338 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008339
8340err_rollback:
8341 netdev_err(dev, "refused to change device tx_queue_len\n");
8342 dev->tx_queue_len = orig_len;
8343 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008344}
8345
8346/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008347 * dev_set_group - Change group this device belongs to
8348 * @dev: device
8349 * @new_group: group this device should belong to
8350 */
8351void dev_set_group(struct net_device *dev, int new_group)
8352{
8353 dev->group = new_group;
8354}
8355EXPORT_SYMBOL(dev_set_group);
8356
8357/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008358 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8359 * @dev: device
8360 * @addr: new address
8361 * @extack: netlink extended ack
8362 */
8363int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8364 struct netlink_ext_ack *extack)
8365{
8366 struct netdev_notifier_pre_changeaddr_info info = {
8367 .info.dev = dev,
8368 .info.extack = extack,
8369 .dev_addr = addr,
8370 };
8371 int rc;
8372
8373 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8374 return notifier_to_errno(rc);
8375}
8376EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8377
8378/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008379 * dev_set_mac_address - Change Media Access Control Address
8380 * @dev: device
8381 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008382 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008383 *
8384 * Change the hardware (MAC) address of the device
8385 */
Petr Machata3a37a962018-12-13 11:54:30 +00008386int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8387 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008389 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 int err;
8391
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008392 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 return -EOPNOTSUPP;
8394 if (sa->sa_family != dev->type)
8395 return -EINVAL;
8396 if (!netif_device_present(dev))
8397 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008398 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8399 if (err)
8400 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008401 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008402 if (err)
8403 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008404 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008405 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008406 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008409EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008411/**
8412 * dev_change_carrier - Change device carrier
8413 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008414 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008415 *
8416 * Change device carrier
8417 */
8418int dev_change_carrier(struct net_device *dev, bool new_carrier)
8419{
8420 const struct net_device_ops *ops = dev->netdev_ops;
8421
8422 if (!ops->ndo_change_carrier)
8423 return -EOPNOTSUPP;
8424 if (!netif_device_present(dev))
8425 return -ENODEV;
8426 return ops->ndo_change_carrier(dev, new_carrier);
8427}
8428EXPORT_SYMBOL(dev_change_carrier);
8429
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008431 * dev_get_phys_port_id - Get device physical port ID
8432 * @dev: device
8433 * @ppid: port ID
8434 *
8435 * Get device physical port ID
8436 */
8437int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008438 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008439{
8440 const struct net_device_ops *ops = dev->netdev_ops;
8441
8442 if (!ops->ndo_get_phys_port_id)
8443 return -EOPNOTSUPP;
8444 return ops->ndo_get_phys_port_id(dev, ppid);
8445}
8446EXPORT_SYMBOL(dev_get_phys_port_id);
8447
8448/**
David Aherndb24a902015-03-17 20:23:15 -06008449 * dev_get_phys_port_name - Get device physical port name
8450 * @dev: device
8451 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008452 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008453 *
8454 * Get device physical port name
8455 */
8456int dev_get_phys_port_name(struct net_device *dev,
8457 char *name, size_t len)
8458{
8459 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008460 int err;
David Aherndb24a902015-03-17 20:23:15 -06008461
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008462 if (ops->ndo_get_phys_port_name) {
8463 err = ops->ndo_get_phys_port_name(dev, name, len);
8464 if (err != -EOPNOTSUPP)
8465 return err;
8466 }
8467 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008468}
8469EXPORT_SYMBOL(dev_get_phys_port_name);
8470
8471/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008472 * dev_get_port_parent_id - Get the device's port parent identifier
8473 * @dev: network device
8474 * @ppid: pointer to a storage for the port's parent identifier
8475 * @recurse: allow/disallow recursion to lower devices
8476 *
8477 * Get the devices's port parent identifier
8478 */
8479int dev_get_port_parent_id(struct net_device *dev,
8480 struct netdev_phys_item_id *ppid,
8481 bool recurse)
8482{
8483 const struct net_device_ops *ops = dev->netdev_ops;
8484 struct netdev_phys_item_id first = { };
8485 struct net_device *lower_dev;
8486 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008487 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008488
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008489 if (ops->ndo_get_port_parent_id) {
8490 err = ops->ndo_get_port_parent_id(dev, ppid);
8491 if (err != -EOPNOTSUPP)
8492 return err;
8493 }
8494
8495 err = devlink_compat_switch_id_get(dev, ppid);
8496 if (!err || err != -EOPNOTSUPP)
8497 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008498
8499 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008500 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008501
8502 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8503 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8504 if (err)
8505 break;
8506 if (!first.id_len)
8507 first = *ppid;
8508 else if (memcmp(&first, ppid, sizeof(*ppid)))
8509 return -ENODATA;
8510 }
8511
8512 return err;
8513}
8514EXPORT_SYMBOL(dev_get_port_parent_id);
8515
8516/**
8517 * netdev_port_same_parent_id - Indicate if two network devices have
8518 * the same port parent identifier
8519 * @a: first network device
8520 * @b: second network device
8521 */
8522bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8523{
8524 struct netdev_phys_item_id a_id = { };
8525 struct netdev_phys_item_id b_id = { };
8526
8527 if (dev_get_port_parent_id(a, &a_id, true) ||
8528 dev_get_port_parent_id(b, &b_id, true))
8529 return false;
8530
8531 return netdev_phys_item_id_same(&a_id, &b_id);
8532}
8533EXPORT_SYMBOL(netdev_port_same_parent_id);
8534
8535/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008536 * dev_change_proto_down - update protocol port state information
8537 * @dev: device
8538 * @proto_down: new value
8539 *
8540 * This info can be used by switch drivers to set the phys state of the
8541 * port.
8542 */
8543int dev_change_proto_down(struct net_device *dev, bool proto_down)
8544{
8545 const struct net_device_ops *ops = dev->netdev_ops;
8546
8547 if (!ops->ndo_change_proto_down)
8548 return -EOPNOTSUPP;
8549 if (!netif_device_present(dev))
8550 return -ENODEV;
8551 return ops->ndo_change_proto_down(dev, proto_down);
8552}
8553EXPORT_SYMBOL(dev_change_proto_down);
8554
Andy Roulinb5899672019-02-22 18:06:36 +00008555/**
8556 * dev_change_proto_down_generic - generic implementation for
8557 * ndo_change_proto_down that sets carrier according to
8558 * proto_down.
8559 *
8560 * @dev: device
8561 * @proto_down: new value
8562 */
8563int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8564{
8565 if (proto_down)
8566 netif_carrier_off(dev);
8567 else
8568 netif_carrier_on(dev);
8569 dev->proto_down = proto_down;
8570 return 0;
8571}
8572EXPORT_SYMBOL(dev_change_proto_down_generic);
8573
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008574u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
8575 enum bpf_netdev_command cmd)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008576{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008577 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008578
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008579 if (!bpf_op)
8580 return 0;
Martin KaFai Lau58038692017-06-15 17:29:09 -07008581
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008582 memset(&xdp, 0, sizeof(xdp));
8583 xdp.command = cmd;
8584
8585 /* Query must always succeed. */
8586 WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008587
Jakub Kicinski6b867582018-07-11 20:36:39 -07008588 return xdp.prog_id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008589}
8590
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008591static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07008592 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008593 struct bpf_prog *prog)
8594{
Björn Töpel7e6897f2019-12-13 18:51:09 +01008595 bool non_hw = !(flags & XDP_FLAGS_HW_MODE);
8596 struct bpf_prog *prev_prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008597 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01008598 int err;
8599
8600 if (non_hw) {
8601 prev_prog = bpf_prog_by_id(__dev_xdp_query(dev, bpf_op,
8602 XDP_QUERY_PROG));
8603 if (IS_ERR(prev_prog))
8604 prev_prog = NULL;
8605 }
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008606
8607 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07008608 if (flags & XDP_FLAGS_HW_MODE)
8609 xdp.command = XDP_SETUP_PROG_HW;
8610 else
8611 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008612 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07008613 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008614 xdp.prog = prog;
8615
Björn Töpel7e6897f2019-12-13 18:51:09 +01008616 err = bpf_op(dev, &xdp);
8617 if (!err && non_hw)
8618 bpf_prog_change_xdp(prev_prog, prog);
8619
8620 if (prev_prog)
8621 bpf_prog_put(prev_prog);
8622
8623 return err;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008624}
8625
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008626static void dev_xdp_uninstall(struct net_device *dev)
8627{
8628 struct netdev_bpf xdp;
8629 bpf_op_t ndo_bpf;
8630
8631 /* Remove generic XDP */
8632 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
8633
8634 /* Remove from the driver */
8635 ndo_bpf = dev->netdev_ops->ndo_bpf;
8636 if (!ndo_bpf)
8637 return;
8638
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008639 memset(&xdp, 0, sizeof(xdp));
8640 xdp.command = XDP_QUERY_PROG;
8641 WARN_ON(ndo_bpf(dev, &xdp));
8642 if (xdp.prog_id)
8643 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8644 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008645
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008646 /* Remove HW offload */
8647 memset(&xdp, 0, sizeof(xdp));
8648 xdp.command = XDP_QUERY_PROG_HW;
8649 if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
8650 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8651 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008652}
8653
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008654/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07008655 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
8656 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07008657 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07008658 * @fd: new program fd or negative value to clear
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01008659 * @expected_fd: old program fd that userspace expects to replace or clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01008660 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07008661 *
8662 * Set or clear a bpf program for a device
8663 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07008664int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01008665 int fd, int expected_fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07008666{
8667 const struct net_device_ops *ops = dev->netdev_ops;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008668 enum bpf_netdev_command query;
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01008669 u32 prog_id, expected_id = 0;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008670 bpf_op_t bpf_op, bpf_chk;
David Aherndfa74902020-04-12 07:32:04 -06008671 struct bpf_prog *prog;
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008672 bool offload;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008673 int err;
8674
Daniel Borkmann85de8572016-11-28 23:16:54 +01008675 ASSERT_RTNL();
8676
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008677 offload = flags & XDP_FLAGS_HW_MODE;
8678 query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008679
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008680 bpf_op = bpf_chk = ops->ndo_bpf;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008681 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) {
8682 NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode");
Daniel Borkmann0489df92017-05-12 01:04:45 +02008683 return -EOPNOTSUPP;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008684 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008685 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
8686 bpf_op = generic_xdp_install;
8687 if (bpf_op == bpf_chk)
8688 bpf_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04008689
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01008690 prog_id = __dev_xdp_query(dev, bpf_op, query);
8691 if (flags & XDP_FLAGS_REPLACE) {
8692 if (expected_fd >= 0) {
8693 prog = bpf_prog_get_type_dev(expected_fd,
8694 BPF_PROG_TYPE_XDP,
8695 bpf_op == ops->ndo_bpf);
8696 if (IS_ERR(prog))
8697 return PTR_ERR(prog);
8698 expected_id = prog->aux->id;
8699 bpf_prog_put(prog);
8700 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008701
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01008702 if (prog_id != expected_id) {
8703 NL_SET_ERR_MSG(extack, "Active program does not match expected");
8704 return -EEXIST;
8705 }
8706 }
8707 if (fd >= 0) {
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008708 if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) {
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008709 NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time");
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008710 return -EEXIST;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008711 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008712
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008713 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) {
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008714 NL_SET_ERR_MSG(extack, "XDP program already attached");
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008715 return -EBUSY;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008716 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01008717
Jakub Kicinski288b3de2017-11-20 15:21:54 -08008718 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
8719 bpf_op == ops->ndo_bpf);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008720 if (IS_ERR(prog))
8721 return PTR_ERR(prog);
Jakub Kicinski441a3302017-11-20 15:21:55 -08008722
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008723 if (!offload && bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinski441a3302017-11-20 15:21:55 -08008724 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
8725 bpf_prog_put(prog);
8726 return -EINVAL;
8727 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008728
Jakub Kicinskiaefc3e72019-10-31 20:07:00 -07008729 /* prog->aux->id may be 0 for orphaned device-bound progs */
8730 if (prog->aux->id && prog->aux->id == prog_id) {
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008731 bpf_prog_put(prog);
8732 return 0;
8733 }
8734 } else {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01008735 if (!prog_id)
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008736 return 0;
David Aherndfa74902020-04-12 07:32:04 -06008737 prog = NULL;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008738 }
8739
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008740 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008741 if (err < 0 && prog)
8742 bpf_prog_put(prog);
8743
8744 return err;
8745}
Brenden Blancoa7862b42016-07-19 12:16:48 -07008746
8747/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07008749 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 *
8751 * Returns a suitable unique value for a new device interface
8752 * number. The caller must hold the rtnl semaphore or the
8753 * dev_base_lock to be sure it remains unique.
8754 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07008755static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008756{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008757 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11008758
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759 for (;;) {
8760 if (++ifindex <= 0)
8761 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008762 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008763 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 }
8765}
8766
Linus Torvalds1da177e2005-04-16 15:20:36 -07008767/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08008768static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07008769DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008770
Stephen Hemminger6f05f622007-03-08 20:46:03 -08008771static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008774 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775}
8776
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008777static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008778{
Krishna Kumare93737b2009-12-08 22:26:02 +00008779 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008780 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008781
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008782 BUG_ON(dev_boot_phase);
8783 ASSERT_RTNL();
8784
Krishna Kumare93737b2009-12-08 22:26:02 +00008785 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008786 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00008787 * for initialization unwind. Remove those
8788 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008789 */
8790 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008791 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
8792 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008793
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008794 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00008795 list_del(&dev->unreg_list);
8796 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008797 }
Eric Dumazet449f4542011-05-19 12:24:16 +00008798 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008799 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00008800 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008801
Octavian Purdila44345722010-12-13 12:44:07 +00008802 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008803 list_for_each_entry(dev, head, unreg_list)
8804 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04008805 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008806
Octavian Purdila44345722010-12-13 12:44:07 +00008807 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008808 /* And unlink it from device chain. */
8809 unlist_netdevice(dev);
8810
8811 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008812 }
Eric Dumazet41852492016-08-26 12:50:39 -07008813 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008814
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008815 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008816
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008817 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008818 struct sk_buff *skb = NULL;
8819
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008820 /* Shutdown queueing discipline. */
8821 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008822
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008823 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008824
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008825 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008826 * this device. They should clean all the things.
8827 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008828 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
8829
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008830 if (!dev->rtnl_link_ops ||
8831 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04008832 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01008833 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008834
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008835 /*
8836 * Flush the unicast and multicast chains
8837 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008838 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008839 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008840
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02008841 netdev_name_node_alt_flush(dev);
Jiri Pirkoff927412019-09-30 11:48:15 +02008842 netdev_name_node_free(dev->name_node);
8843
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008844 if (dev->netdev_ops->ndo_uninit)
8845 dev->netdev_ops->ndo_uninit(dev);
8846
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008847 if (skb)
8848 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07008849
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008850 /* Notifier chain MUST detach us all upper devices. */
8851 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07008852 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008853
8854 /* Remove entries from kobject tree */
8855 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00008856#ifdef CONFIG_XPS
8857 /* Remove XPS queueing entries */
8858 netif_reset_xps_queues_gt(dev, 0);
8859#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008860 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008861
Eric W. Biederman850a5452011-10-13 22:25:23 +00008862 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008863
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00008864 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008865 dev_put(dev);
8866}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008867
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008868static void rollback_registered(struct net_device *dev)
8869{
8870 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008871
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008872 list_add(&dev->unreg_list, &single);
8873 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00008874 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008875}
8876
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008877static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
8878 struct net_device *upper, netdev_features_t features)
8879{
8880 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8881 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008882 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008883
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01008884 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008885 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008886 if (!(upper->wanted_features & feature)
8887 && (features & feature)) {
8888 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
8889 &feature, upper->name);
8890 features &= ~feature;
8891 }
8892 }
8893
8894 return features;
8895}
8896
8897static void netdev_sync_lower_features(struct net_device *upper,
8898 struct net_device *lower, netdev_features_t features)
8899{
8900 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8901 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008902 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008903
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01008904 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008905 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008906 if (!(features & feature) && (lower->features & feature)) {
8907 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
8908 &feature, lower->name);
8909 lower->wanted_features &= ~feature;
Cong Wangdd912302020-05-07 12:19:03 -07008910 __netdev_update_features(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008911
8912 if (unlikely(lower->features & feature))
8913 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
8914 &feature, lower->name);
Cong Wangdd912302020-05-07 12:19:03 -07008915 else
8916 netdev_features_change(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008917 }
8918 }
8919}
8920
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008921static netdev_features_t netdev_fix_features(struct net_device *dev,
8922 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07008923{
Michał Mirosław57422dc2011-01-22 12:14:12 +00008924 /* Fix illegal checksum combinations */
8925 if ((features & NETIF_F_HW_CSUM) &&
8926 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008927 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00008928 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
8929 }
8930
Herbert Xub63365a2008-10-23 01:11:29 -07008931 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008932 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008933 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008934 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07008935 }
8936
Pravin B Shelarec5f0612013-03-07 09:28:01 +00008937 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
8938 !(features & NETIF_F_IP_CSUM)) {
8939 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
8940 features &= ~NETIF_F_TSO;
8941 features &= ~NETIF_F_TSO_ECN;
8942 }
8943
8944 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
8945 !(features & NETIF_F_IPV6_CSUM)) {
8946 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
8947 features &= ~NETIF_F_TSO6;
8948 }
8949
Alexander Duyckb1dc4972016-05-02 09:38:24 -07008950 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8951 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
8952 features &= ~NETIF_F_TSO_MANGLEID;
8953
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00008954 /* TSO ECN requires that TSO is present as well. */
8955 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
8956 features &= ~NETIF_F_TSO_ECN;
8957
Michał Mirosław212b5732011-02-15 16:59:16 +00008958 /* Software GSO depends on SG. */
8959 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008960 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00008961 features &= ~NETIF_F_GSO;
8962 }
8963
Alexander Duyck802ab552016-04-10 21:45:03 -04008964 /* GSO partial features require GSO partial be set */
8965 if ((features & dev->gso_partial_features) &&
8966 !(features & NETIF_F_GSO_PARTIAL)) {
8967 netdev_dbg(dev,
8968 "Dropping partially supported GSO features since no GSO partial.\n");
8969 features &= ~dev->gso_partial_features;
8970 }
8971
Michael Chanfb1f5f72017-12-16 03:09:40 -05008972 if (!(features & NETIF_F_RXCSUM)) {
8973 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8974 * successfully merged by hardware must also have the
8975 * checksum verified by hardware. If the user does not
8976 * want to enable RXCSUM, logically, we should disable GRO_HW.
8977 */
8978 if (features & NETIF_F_GRO_HW) {
8979 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8980 features &= ~NETIF_F_GRO_HW;
8981 }
8982 }
8983
Gal Pressmande8d5ab2018-03-12 11:48:49 +02008984 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8985 if (features & NETIF_F_RXFCS) {
8986 if (features & NETIF_F_LRO) {
8987 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
8988 features &= ~NETIF_F_LRO;
8989 }
8990
8991 if (features & NETIF_F_GRO_HW) {
8992 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8993 features &= ~NETIF_F_GRO_HW;
8994 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02008995 }
8996
Herbert Xub63365a2008-10-23 01:11:29 -07008997 return features;
8998}
Herbert Xub63365a2008-10-23 01:11:29 -07008999
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009000int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00009001{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009002 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009003 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009004 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05009005 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00009006
Michał Mirosław87267482011-04-12 09:56:38 +00009007 ASSERT_RTNL();
9008
Michał Mirosław5455c692011-02-15 16:59:17 +00009009 features = netdev_get_wanted_features(dev);
9010
9011 if (dev->netdev_ops->ndo_fix_features)
9012 features = dev->netdev_ops->ndo_fix_features(dev, features);
9013
9014 /* driver might be less strict about feature dependencies */
9015 features = netdev_fix_features(dev, features);
9016
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009017 /* some features can't be enabled if they're off an an upper device */
9018 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9019 features = netdev_sync_upper_features(dev, upper, features);
9020
Michał Mirosław5455c692011-02-15 16:59:17 +00009021 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009022 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009023
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009024 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9025 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009026
9027 if (dev->netdev_ops->ndo_set_features)
9028 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009029 else
9030 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009031
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009032 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009033 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009034 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9035 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009036 /* return non-0 since some features might have changed and
9037 * it's better to fire a spurious notification than miss it
9038 */
9039 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009040 }
9041
Jarod Wilsone7868a82015-11-03 23:09:32 -05009042sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009043 /* some features must be disabled on lower devices when disabled
9044 * on an upper device (think: bonding master or bridge)
9045 */
9046 netdev_for_each_lower_dev(dev, lower, iter)
9047 netdev_sync_lower_features(dev, lower, features);
9048
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009049 if (!err) {
9050 netdev_features_t diff = features ^ dev->features;
9051
9052 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9053 /* udp_tunnel_{get,drop}_rx_info both need
9054 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9055 * device, or they won't do anything.
9056 * Thus we need to update dev->features
9057 * *before* calling udp_tunnel_get_rx_info,
9058 * but *after* calling udp_tunnel_drop_rx_info.
9059 */
9060 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9061 dev->features = features;
9062 udp_tunnel_get_rx_info(dev);
9063 } else {
9064 udp_tunnel_drop_rx_info(dev);
9065 }
9066 }
9067
Gal Pressman9daae9b2018-03-28 17:46:54 +03009068 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9069 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9070 dev->features = features;
9071 err |= vlan_get_rx_ctag_filter_info(dev);
9072 } else {
9073 vlan_drop_rx_ctag_filter_info(dev);
9074 }
9075 }
9076
9077 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9078 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9079 dev->features = features;
9080 err |= vlan_get_rx_stag_filter_info(dev);
9081 } else {
9082 vlan_drop_rx_stag_filter_info(dev);
9083 }
9084 }
9085
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009086 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009087 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009088
Jarod Wilsone7868a82015-11-03 23:09:32 -05009089 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009090}
9091
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009092/**
9093 * netdev_update_features - recalculate device features
9094 * @dev: the device to check
9095 *
9096 * Recalculate dev->features set and send notifications if it
9097 * has changed. Should be called after driver or hardware dependent
9098 * conditions might have changed that influence the features.
9099 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009100void netdev_update_features(struct net_device *dev)
9101{
9102 if (__netdev_update_features(dev))
9103 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009104}
9105EXPORT_SYMBOL(netdev_update_features);
9106
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009108 * netdev_change_features - recalculate device features
9109 * @dev: the device to check
9110 *
9111 * Recalculate dev->features set and send notifications even
9112 * if they have not changed. Should be called instead of
9113 * netdev_update_features() if also dev->vlan_features might
9114 * have changed to allow the changes to be propagated to stacked
9115 * VLAN devices.
9116 */
9117void netdev_change_features(struct net_device *dev)
9118{
9119 __netdev_update_features(dev);
9120 netdev_features_change(dev);
9121}
9122EXPORT_SYMBOL(netdev_change_features);
9123
9124/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009125 * netif_stacked_transfer_operstate - transfer operstate
9126 * @rootdev: the root or lower level device to transfer state from
9127 * @dev: the device to transfer operstate to
9128 *
9129 * Transfer operational state from root to device. This is normally
9130 * called when a stacking relationship exists between the root
9131 * device and the device(a leaf device).
9132 */
9133void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9134 struct net_device *dev)
9135{
9136 if (rootdev->operstate == IF_OPER_DORMANT)
9137 netif_dormant_on(dev);
9138 else
9139 netif_dormant_off(dev);
9140
Zhang Shengju0575c862017-04-26 17:49:38 +08009141 if (netif_carrier_ok(rootdev))
9142 netif_carrier_on(dev);
9143 else
9144 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009145}
9146EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9147
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009148static int netif_alloc_rx_queues(struct net_device *dev)
9149{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009150 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009151 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309152 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009153 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009154
Tom Herbertbd25fa72010-10-18 18:00:16 +00009155 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009156
Michal Hockodcda9b02017-07-12 14:36:45 -07009157 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009158 if (!rx)
9159 return -ENOMEM;
9160
Tom Herbertbd25fa72010-10-18 18:00:16 +00009161 dev->_rx = rx;
9162
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009163 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009164 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009165
9166 /* XDP RX-queue setup */
9167 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
9168 if (err < 0)
9169 goto err_rxq_info;
9170 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009171 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009172
9173err_rxq_info:
9174 /* Rollback successful reg's and free other resources */
9175 while (i--)
9176 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009177 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009178 dev->_rx = NULL;
9179 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009180}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009181
9182static void netif_free_rx_queues(struct net_device *dev)
9183{
9184 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009185
9186 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9187 if (!dev->_rx)
9188 return;
9189
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009190 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009191 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9192
9193 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009194}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009195
Changli Gaoaa942102010-12-04 02:31:41 +00009196static void netdev_init_one_queue(struct net_device *dev,
9197 struct netdev_queue *queue, void *_unused)
9198{
9199 /* Initialize queue lock */
9200 spin_lock_init(&queue->_xmit_lock);
Taehee Yooab92d682019-10-21 18:47:51 +00009201 lockdep_set_class(&queue->_xmit_lock, &dev->qdisc_xmit_lock_key);
Changli Gaoaa942102010-12-04 02:31:41 +00009202 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009203 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009204 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009205#ifdef CONFIG_BQL
9206 dql_init(&queue->dql, HZ);
9207#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009208}
9209
Eric Dumazet60877a32013-06-20 01:15:51 -07009210static void netif_free_tx_queues(struct net_device *dev)
9211{
WANG Cong4cb28972014-06-02 15:55:22 -07009212 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009213}
9214
Tom Herberte6484932010-10-18 18:04:39 +00009215static int netif_alloc_netdev_queues(struct net_device *dev)
9216{
9217 unsigned int count = dev->num_tx_queues;
9218 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009219 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009220
Eric Dumazetd3397272015-07-06 17:13:26 +02009221 if (count < 1 || count > 0xffff)
9222 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009223
Michal Hockodcda9b02017-07-12 14:36:45 -07009224 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009225 if (!tx)
9226 return -ENOMEM;
9227
Tom Herberte6484932010-10-18 18:04:39 +00009228 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009229
Tom Herberte6484932010-10-18 18:04:39 +00009230 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9231 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009232
9233 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009234}
9235
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009236void netif_tx_stop_all_queues(struct net_device *dev)
9237{
9238 unsigned int i;
9239
9240 for (i = 0; i < dev->num_tx_queues; i++) {
9241 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009242
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009243 netif_tx_stop_queue(txq);
9244 }
9245}
9246EXPORT_SYMBOL(netif_tx_stop_all_queues);
9247
Taehee Yooab92d682019-10-21 18:47:51 +00009248static void netdev_register_lockdep_key(struct net_device *dev)
9249{
9250 lockdep_register_key(&dev->qdisc_tx_busylock_key);
9251 lockdep_register_key(&dev->qdisc_running_key);
9252 lockdep_register_key(&dev->qdisc_xmit_lock_key);
9253 lockdep_register_key(&dev->addr_list_lock_key);
9254}
9255
9256static void netdev_unregister_lockdep_key(struct net_device *dev)
9257{
9258 lockdep_unregister_key(&dev->qdisc_tx_busylock_key);
9259 lockdep_unregister_key(&dev->qdisc_running_key);
9260 lockdep_unregister_key(&dev->qdisc_xmit_lock_key);
9261 lockdep_unregister_key(&dev->addr_list_lock_key);
9262}
9263
9264void netdev_update_lockdep_key(struct net_device *dev)
9265{
Taehee Yooab92d682019-10-21 18:47:51 +00009266 lockdep_unregister_key(&dev->addr_list_lock_key);
Taehee Yooab92d682019-10-21 18:47:51 +00009267 lockdep_register_key(&dev->addr_list_lock_key);
9268
9269 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
Taehee Yooab92d682019-10-21 18:47:51 +00009270}
9271EXPORT_SYMBOL(netdev_update_lockdep_key);
9272
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009273/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274 * register_netdevice - register a network device
9275 * @dev: device to register
9276 *
9277 * Take a completed network device structure and add it to the kernel
9278 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9279 * chain. 0 is returned on success. A negative errno code is returned
9280 * on a failure to set up the device, or if the name is a duplicate.
9281 *
9282 * Callers must hold the rtnl semaphore. You may want
9283 * register_netdev() instead of this.
9284 *
9285 * BUGS:
9286 * The locking appears insufficient to guarantee two parallel registers
9287 * will not get the same name.
9288 */
9289
9290int register_netdevice(struct net_device *dev)
9291{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009293 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294
Florian Fainellie283de32018-04-30 14:20:05 -07009295 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9296 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297 BUG_ON(dev_boot_phase);
9298 ASSERT_RTNL();
9299
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009300 might_sleep();
9301
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 /* When net_device's are persistent, this will be fatal. */
9303 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009304 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305
Jakub Kicinski9000edb2020-03-16 13:47:12 -07009306 ret = ethtool_check_ops(dev->ethtool_ops);
9307 if (ret)
9308 return ret;
9309
David S. Millerf1f28aa2008-07-15 00:08:33 -07009310 spin_lock_init(&dev->addr_list_lock);
Taehee Yooab92d682019-10-21 18:47:51 +00009311 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312
Gao feng828de4f2012-09-13 20:58:27 +00009313 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00009314 if (ret < 0)
9315 goto out;
9316
Eric Dumazet9077f052019-10-03 08:59:24 -07009317 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +02009318 dev->name_node = netdev_name_node_head_alloc(dev);
9319 if (!dev->name_node)
9320 goto out;
9321
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009323 if (dev->netdev_ops->ndo_init) {
9324 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 if (ret) {
9326 if (ret > 0)
9327 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009328 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 }
9330 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009331
Patrick McHardyf6469682013-04-19 02:04:27 +00009332 if (((dev->hw_features | dev->features) &
9333 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00009334 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9335 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9336 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9337 ret = -EINVAL;
9338 goto err_uninit;
9339 }
9340
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00009341 ret = -EBUSY;
9342 if (!dev->ifindex)
9343 dev->ifindex = dev_new_index(net);
9344 else if (__dev_get_by_index(net, dev->ifindex))
9345 goto err_uninit;
9346
Michał Mirosław5455c692011-02-15 16:59:17 +00009347 /* Transfer changeable features to wanted_features and enable
9348 * software offloads (GSO and GRO).
9349 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +01009350 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +00009351 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02009352
9353 if (dev->netdev_ops->ndo_udp_tunnel_add) {
9354 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9355 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9356 }
9357
Michał Mirosław14d12322011-02-22 16:52:28 +00009358 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009360 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00009361 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009362
Alexander Duyck7f348a62016-04-20 16:51:00 -04009363 /* If IPv4 TCP segmentation offload is supported we should also
9364 * allow the device to enable segmenting the frame with the option
9365 * of ignoring a static IP ID value. This doesn't enable the
9366 * feature itself but allows the user to enable it later.
9367 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009368 if (dev->hw_features & NETIF_F_TSO)
9369 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04009370 if (dev->vlan_features & NETIF_F_TSO)
9371 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9372 if (dev->mpls_features & NETIF_F_TSO)
9373 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9374 if (dev->hw_enc_features & NETIF_F_TSO)
9375 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07009376
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009377 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00009378 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009379 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00009380
Pravin B Shelaree579672013-03-07 09:28:08 +00009381 /* Make NETIF_F_SG inheritable to tunnel devices.
9382 */
Alexander Duyck802ab552016-04-10 21:45:03 -04009383 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00009384
Simon Horman0d89d202013-05-23 21:02:52 +00009385 /* Make NETIF_F_SG inheritable to MPLS.
9386 */
9387 dev->mpls_features |= NETIF_F_SG;
9388
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00009389 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9390 ret = notifier_to_errno(ret);
9391 if (ret)
9392 goto err_uninit;
9393
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009394 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +02009395 if (ret) {
9396 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009397 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +02009398 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009399 dev->reg_state = NETREG_REGISTERED;
9400
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009401 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00009402
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 /*
9404 * Default initial state at registry is that the
9405 * device is present.
9406 */
9407
9408 set_bit(__LINK_STATE_PRESENT, &dev->state);
9409
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01009410 linkwatch_init_dev(dev);
9411
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009414 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04009415 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416
Jiri Pirko948b3372013-01-08 01:38:25 +00009417 /* If the device has permanent device address, driver should
9418 * set dev_addr and also addr_assign_type should be set to
9419 * NET_ADDR_PERM (default value).
9420 */
9421 if (dev->addr_assign_type == NET_ADDR_PERM)
9422 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
9423
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009425 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07009426 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009427 if (ret) {
9428 rollback_registered(dev);
Subash Abhinov Kasiviswanathan10cc5142019-09-10 14:02:57 -06009429 rcu_barrier();
9430
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009431 dev->reg_state = NETREG_UNREGISTERED;
9432 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009433 /*
9434 * Prevent userspace races by waiting until the network
9435 * device is fully setup before sending notifications.
9436 */
Patrick McHardya2835762010-02-26 06:34:51 +00009437 if (!dev->rtnl_link_ops ||
9438 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07009439 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440
9441out:
9442 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009443
9444err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009445 if (dev->netdev_ops->ndo_uninit)
9446 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04009447 if (dev->priv_destructor)
9448 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009449err_free_name:
9450 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009453EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454
9455/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009456 * init_dummy_netdev - init a dummy network device for NAPI
9457 * @dev: device to init
9458 *
9459 * This takes a network device structure and initialize the minimum
9460 * amount of fields so it can be used to schedule NAPI polls without
9461 * registering a full blown interface. This is to be used by drivers
9462 * that need to tie several hardware interfaces to a single NAPI
9463 * poll scheduler due to HW limitations.
9464 */
9465int init_dummy_netdev(struct net_device *dev)
9466{
9467 /* Clear everything. Note we don't initialize spinlocks
9468 * are they aren't supposed to be taken by any of the
9469 * NAPI code and this dummy netdev is supposed to be
9470 * only ever used for NAPI polls
9471 */
9472 memset(dev, 0, sizeof(struct net_device));
9473
9474 /* make sure we BUG if trying to hit standard
9475 * register/unregister code path
9476 */
9477 dev->reg_state = NETREG_DUMMY;
9478
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009479 /* NAPI wants this */
9480 INIT_LIST_HEAD(&dev->napi_list);
9481
9482 /* a dummy interface is started by default */
9483 set_bit(__LINK_STATE_PRESENT, &dev->state);
9484 set_bit(__LINK_STATE_START, &dev->state);
9485
Josh Elsasser35edfdc2019-01-26 14:38:33 -08009486 /* napi_busy_loop stats accounting wants this */
9487 dev_net_set(dev, &init_net);
9488
Eric Dumazet29b44332010-10-11 10:22:12 +00009489 /* Note : We dont allocate pcpu_refcnt for dummy devices,
9490 * because users of this 'device' dont need to change
9491 * its refcount.
9492 */
9493
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009494 return 0;
9495}
9496EXPORT_SYMBOL_GPL(init_dummy_netdev);
9497
9498
9499/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500 * register_netdev - register a network device
9501 * @dev: device to register
9502 *
9503 * Take a completed network device structure and add it to the kernel
9504 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9505 * chain. 0 is returned on success. A negative errno code is returned
9506 * on a failure to set up the device, or if the name is a duplicate.
9507 *
Borislav Petkov38b4da382007-04-20 22:14:10 -07009508 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 * and expands the device name if you passed a format string to
9510 * alloc_netdev.
9511 */
9512int register_netdev(struct net_device *dev)
9513{
9514 int err;
9515
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +03009516 if (rtnl_lock_killable())
9517 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009519 rtnl_unlock();
9520 return err;
9521}
9522EXPORT_SYMBOL(register_netdev);
9523
Eric Dumazet29b44332010-10-11 10:22:12 +00009524int netdev_refcnt_read(const struct net_device *dev)
9525{
9526 int i, refcnt = 0;
9527
9528 for_each_possible_cpu(i)
9529 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
9530 return refcnt;
9531}
9532EXPORT_SYMBOL(netdev_refcnt_read);
9533
Ben Hutchings2c530402012-07-10 10:55:09 +00009534/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00009536 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 *
9538 * This is called when unregistering network devices.
9539 *
9540 * Any protocol or device that holds a reference should register
9541 * for netdevice notification, and cleanup and put back the
9542 * reference if they receive an UNREGISTER event.
9543 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009544 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 */
9546static void netdev_wait_allrefs(struct net_device *dev)
9547{
9548 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00009549 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550
Eric Dumazete014deb2009-11-17 05:59:21 +00009551 linkwatch_forget_dev(dev);
9552
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00009554 refcnt = netdev_refcnt_read(dev);
9555
9556 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009557 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009558 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559
9560 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009561 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562
Eric Dumazet748e2d92012-08-22 21:50:59 +00009563 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009564 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00009565 rtnl_lock();
9566
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
9568 &dev->state)) {
9569 /* We must not have linkwatch events
9570 * pending on unregister. If this
9571 * happens, we simply run the queue
9572 * unscheduled, resulting in a noop
9573 * for this device.
9574 */
9575 linkwatch_run_queue();
9576 }
9577
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009578 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579
9580 rebroadcast_time = jiffies;
9581 }
9582
9583 msleep(250);
9584
Eric Dumazet29b44332010-10-11 10:22:12 +00009585 refcnt = netdev_refcnt_read(dev);
9586
Eric Dumazetd7c04b02019-05-16 08:09:57 -07009587 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009588 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9589 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590 warning_time = jiffies;
9591 }
9592 }
9593}
9594
9595/* The sequence is:
9596 *
9597 * rtnl_lock();
9598 * ...
9599 * register_netdevice(x1);
9600 * register_netdevice(x2);
9601 * ...
9602 * unregister_netdevice(y1);
9603 * unregister_netdevice(y2);
9604 * ...
9605 * rtnl_unlock();
9606 * free_netdev(y1);
9607 * free_netdev(y2);
9608 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07009609 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07009610 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009611 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612 * without deadlocking with linkwatch via keventd.
9613 * 2) Since we run with the RTNL semaphore not held, we can sleep
9614 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07009615 *
9616 * We must not return until all unregister events added during
9617 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619void netdev_run_todo(void)
9620{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009621 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009624 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07009625
9626 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009627
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009628
9629 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00009630 if (!list_empty(&list))
9631 rcu_barrier();
9632
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633 while (!list_empty(&list)) {
9634 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00009635 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 list_del(&dev->todo_list);
9637
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009638 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009639 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009641 dump_stack();
9642 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009643 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009644
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009645 dev->reg_state = NETREG_UNREGISTERED;
9646
9647 netdev_wait_allrefs(dev);
9648
9649 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00009650 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08009651 BUG_ON(!list_empty(&dev->ptype_all));
9652 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00009653 WARN_ON(rcu_access_pointer(dev->ip_ptr));
9654 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -08009655#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -07009656 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -08009657#endif
David S. Millercf124db2017-05-08 12:52:56 -04009658 if (dev->priv_destructor)
9659 dev->priv_destructor(dev);
9660 if (dev->needs_free_netdev)
9661 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009662
Eric W. Biederman50624c92013-09-23 21:19:49 -07009663 /* Report a network device has been unregistered */
9664 rtnl_lock();
9665 dev_net(dev)->dev_unreg_count--;
9666 __rtnl_unlock();
9667 wake_up(&netdev_unregistering_wq);
9668
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009669 /* Free network device */
9670 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672}
9673
Jarod Wilson92566452016-02-01 18:51:04 -05009674/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
9675 * all the same fields in the same order as net_device_stats, with only
9676 * the type differing, but rtnl_link_stats64 may have additional fields
9677 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00009678 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009679void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
9680 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00009681{
9682#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05009683 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02009684 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05009685 /* zero out counters that only exist in rtnl_link_stats64 */
9686 memset((char *)stats64 + sizeof(*netdev_stats), 0,
9687 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009688#else
Jarod Wilson92566452016-02-01 18:51:04 -05009689 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009690 const unsigned long *src = (const unsigned long *)netdev_stats;
9691 u64 *dst = (u64 *)stats64;
9692
Jarod Wilson92566452016-02-01 18:51:04 -05009693 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009694 for (i = 0; i < n; i++)
9695 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05009696 /* zero out counters that only exist in rtnl_link_stats64 */
9697 memset((char *)stats64 + n * sizeof(u64), 0,
9698 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009699#endif
9700}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009701EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009702
Eric Dumazetd83345a2009-11-16 03:36:51 +00009703/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009704 * dev_get_stats - get network device statistics
9705 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07009706 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009707 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00009708 * Get network statistics from device. Return @storage.
9709 * The device driver may provide its own method by setting
9710 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
9711 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009712 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00009713struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
9714 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00009715{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009716 const struct net_device_ops *ops = dev->netdev_ops;
9717
Eric Dumazet28172732010-07-07 14:58:56 -07009718 if (ops->ndo_get_stats64) {
9719 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00009720 ops->ndo_get_stats64(dev, storage);
9721 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00009722 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00009723 } else {
9724 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07009725 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07009726 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
9727 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
9728 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07009729 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07009730}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009731EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07009732
Eric Dumazet24824a02010-10-02 06:11:55 +00009733struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07009734{
Eric Dumazet24824a02010-10-02 06:11:55 +00009735 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07009736
Eric Dumazet24824a02010-10-02 06:11:55 +00009737#ifdef CONFIG_NET_CLS_ACT
9738 if (queue)
9739 return queue;
9740 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
9741 if (!queue)
9742 return NULL;
9743 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08009744 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00009745 queue->qdisc_sleeping = &noop_qdisc;
9746 rcu_assign_pointer(dev->ingress_queue, queue);
9747#endif
9748 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07009749}
9750
Eric Dumazet2c60db02012-09-16 09:17:26 +00009751static const struct ethtool_ops default_ethtool_ops;
9752
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00009753void netdev_set_default_ethtool_ops(struct net_device *dev,
9754 const struct ethtool_ops *ops)
9755{
9756 if (dev->ethtool_ops == &default_ethtool_ops)
9757 dev->ethtool_ops = ops;
9758}
9759EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
9760
Eric Dumazet74d332c2013-10-30 13:10:44 -07009761void netdev_freemem(struct net_device *dev)
9762{
9763 char *addr = (char *)dev - dev->padded;
9764
WANG Cong4cb28972014-06-02 15:55:22 -07009765 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009766}
9767
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768/**
tcharding722c9a02017-02-09 17:56:04 +11009769 * alloc_netdev_mqs - allocate network device
9770 * @sizeof_priv: size of private data to allocate space for
9771 * @name: device name format string
9772 * @name_assign_type: origin of device name
9773 * @setup: callback to initialize device
9774 * @txqs: the number of TX subqueues to allocate
9775 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 *
tcharding722c9a02017-02-09 17:56:04 +11009777 * Allocates a struct net_device with private data area for driver use
9778 * and performs basic initialization. Also allocates subqueue structs
9779 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 */
Tom Herbert36909ea2011-01-09 19:36:31 +00009781struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02009782 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00009783 void (*setup)(struct net_device *),
9784 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03009787 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009788 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07009790 BUG_ON(strlen(name) >= sizeof(dev->name));
9791
Tom Herbert36909ea2011-01-09 19:36:31 +00009792 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009793 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00009794 return NULL;
9795 }
9796
Tom Herbert36909ea2011-01-09 19:36:31 +00009797 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009798 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00009799 return NULL;
9800 }
Tom Herbert36909ea2011-01-09 19:36:31 +00009801
David S. Millerfd2ea0a2008-07-17 01:56:23 -07009802 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009803 if (sizeof_priv) {
9804 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009805 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009806 alloc_size += sizeof_priv;
9807 }
9808 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009809 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810
Michal Hockodcda9b02017-07-12 14:36:45 -07009811 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00009812 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009815 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009817
Eric Dumazet29b44332010-10-11 10:22:12 +00009818 dev->pcpu_refcnt = alloc_percpu(int);
9819 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07009820 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009821
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00009823 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824
Jiri Pirko22bedad32010-04-01 21:22:57 +00009825 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009826 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00009827
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09009828 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829
Taehee Yooab92d682019-10-21 18:47:51 +00009830 netdev_register_lockdep_key(dev);
9831
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07009832 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00009833 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +00009834 dev->upper_level = 1;
9835 dev->lower_level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836
Herbert Xud565b0a2008-12-15 23:38:52 -08009837 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009838 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009839 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00009840 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02009841 INIT_LIST_HEAD(&dev->adj_list.upper);
9842 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08009843 INIT_LIST_HEAD(&dev->ptype_all);
9844 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +01009845 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02009846#ifdef CONFIG_NET_SCHED
9847 hash_init(dev->qdisc_hash);
9848#endif
Eric Dumazet02875872014-10-05 18:38:35 -07009849 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009851
Phil Suttera8131042016-02-17 15:37:43 +01009852 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02009853 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01009854 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01009855 }
Phil Sutter906470c2015-08-18 10:30:48 +02009856
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009857 dev->num_tx_queues = txqs;
9858 dev->real_num_tx_queues = txqs;
9859 if (netif_alloc_netdev_queues(dev))
9860 goto free_all;
9861
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009862 dev->num_rx_queues = rxqs;
9863 dev->real_num_rx_queues = rxqs;
9864 if (netif_alloc_rx_queues(dev))
9865 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009866
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02009868 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00009869 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00009870 if (!dev->ethtool_ops)
9871 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02009872
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01009873 nf_hook_ingress_init(dev);
Pablo Neirae687ad62015-05-13 18:19:38 +02009874
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009876
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009877free_all:
9878 free_netdev(dev);
9879 return NULL;
9880
Eric Dumazet29b44332010-10-11 10:22:12 +00009881free_pcpu:
9882 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009883free_dev:
9884 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009885 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886}
Tom Herbert36909ea2011-01-09 19:36:31 +00009887EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888
9889/**
tcharding722c9a02017-02-09 17:56:04 +11009890 * free_netdev - free network device
9891 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892 *
tcharding722c9a02017-02-09 17:56:04 +11009893 * This function does the last stage of destroying an allocated device
9894 * interface. The reference to the device object is released. If this
9895 * is the last reference then it will be freed.Must be called in process
9896 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 */
9898void free_netdev(struct net_device *dev)
9899{
Herbert Xud565b0a2008-12-15 23:38:52 -08009900 struct napi_struct *p, *n;
9901
Eric Dumazet93d05d42015-11-18 06:31:03 -08009902 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07009903 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009904 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -07009905
Eric Dumazet33d480c2011-08-11 19:30:52 +00009906 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00009907
Jiri Pirkof001fde2009-05-05 02:48:28 +00009908 /* Flush device addresses */
9909 dev_addr_flush(dev);
9910
Herbert Xud565b0a2008-12-15 23:38:52 -08009911 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
9912 netif_napi_del(p);
9913
Eric Dumazet29b44332010-10-11 10:22:12 +00009914 free_percpu(dev->pcpu_refcnt);
9915 dev->pcpu_refcnt = NULL;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +01009916 free_percpu(dev->xdp_bulkq);
9917 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +00009918
Taehee Yooab92d682019-10-21 18:47:51 +00009919 netdev_unregister_lockdep_key(dev);
9920
Stephen Hemminger3041a062006-05-26 13:25:24 -07009921 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009922 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07009923 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924 return;
9925 }
9926
9927 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
9928 dev->reg_state = NETREG_RELEASED;
9929
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07009930 /* will free via device release */
9931 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009933EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009934
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009935/**
9936 * synchronize_net - Synchronize with packet receive processing
9937 *
9938 * Wait for packets currently being received to be done.
9939 * Does not block later packets from starting.
9940 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009941void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009942{
9943 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00009944 if (rtnl_is_locked())
9945 synchronize_rcu_expedited();
9946 else
9947 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009949EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950
9951/**
Eric Dumazet44a08732009-10-27 07:03:04 +00009952 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00009954 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08009955 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009957 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00009958 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 *
9960 * Callers must hold the rtnl semaphore. You may want
9961 * unregister_netdev() instead of this.
9962 */
9963
Eric Dumazet44a08732009-10-27 07:03:04 +00009964void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965{
Herbert Xua6620712007-12-12 19:21:56 -08009966 ASSERT_RTNL();
9967
Eric Dumazet44a08732009-10-27 07:03:04 +00009968 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009969 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00009970 } else {
9971 rollback_registered(dev);
9972 /* Finish processing unregister after unlock */
9973 net_set_todo(dev);
9974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975}
Eric Dumazet44a08732009-10-27 07:03:04 +00009976EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977
9978/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009979 * unregister_netdevice_many - unregister many devices
9980 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07009981 *
9982 * Note: As most callers use a stack allocated list_head,
9983 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009984 */
9985void unregister_netdevice_many(struct list_head *head)
9986{
9987 struct net_device *dev;
9988
9989 if (!list_empty(head)) {
9990 rollback_registered_many(head);
9991 list_for_each_entry(dev, head, unreg_list)
9992 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07009993 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009994 }
9995}
Eric Dumazet63c80992009-10-27 07:06:49 +00009996EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009997
9998/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009999 * unregister_netdev - remove device from the kernel
10000 * @dev: device
10001 *
10002 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010003 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004 *
10005 * This is just a wrapper for unregister_netdevice that takes
10006 * the rtnl semaphore. In general you want to use this and not
10007 * unregister_netdevice.
10008 */
10009void unregister_netdev(struct net_device *dev)
10010{
10011 rtnl_lock();
10012 unregister_netdevice(dev);
10013 rtnl_unlock();
10014}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015EXPORT_SYMBOL(unregister_netdev);
10016
Eric W. Biedermance286d32007-09-12 13:53:49 +020010017/**
10018 * dev_change_net_namespace - move device to different nethost namespace
10019 * @dev: device
10020 * @net: network namespace
10021 * @pat: If not NULL name pattern to try if the current device name
10022 * is already taken in the destination network namespace.
10023 *
10024 * This function shuts down a device interface and moves it
10025 * to a new network namespace. On success 0 is returned, on
10026 * a failure a netagive errno code is returned.
10027 *
10028 * Callers must hold the rtnl semaphore.
10029 */
10030
10031int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10032{
Christian Brauneref6a4c82020-02-27 04:37:19 +010010033 struct net *net_old = dev_net(dev);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010034 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010035
10036 ASSERT_RTNL();
10037
10038 /* Don't allow namespace local devices to be moved. */
10039 err = -EINVAL;
10040 if (dev->features & NETIF_F_NETNS_LOCAL)
10041 goto out;
10042
10043 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010044 if (dev->reg_state != NETREG_REGISTERED)
10045 goto out;
10046
10047 /* Get out if there is nothing todo */
10048 err = 0;
Christian Brauneref6a4c82020-02-27 04:37:19 +010010049 if (net_eq(net_old, net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020010050 goto out;
10051
10052 /* Pick the destination device name, and ensure
10053 * we can use it in the destination network namespace.
10054 */
10055 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +000010056 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010057 /* We get here if we can't use the current device name */
10058 if (!pat)
10059 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080010060 err = dev_get_valid_name(net, dev, pat);
10061 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010062 goto out;
10063 }
10064
10065 /*
10066 * And now a mini version of register_netdevice unregister_netdevice.
10067 */
10068
10069 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070010070 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010071
10072 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010073 unlist_netdevice(dev);
10074
10075 synchronize_net();
10076
10077 /* Shutdown queueing discipline. */
10078 dev_shutdown(dev);
10079
10080 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110010081 * this device. They should clean all the things.
10082 *
10083 * Note that dev->reg_state stays at NETREG_REGISTERED.
10084 * This is wanted because this way 8021q and macvlan know
10085 * the device is just moving and can keep their slaves up.
10086 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010087 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010088 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010089
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010090 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010091 /* If there is an ifindex conflict assign a new one */
10092 if (__dev_get_by_index(net, dev->ifindex))
10093 new_ifindex = dev_new_index(net);
10094 else
10095 new_ifindex = dev->ifindex;
10096
10097 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10098 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010099
10100 /*
10101 * Flush the unicast and multicast chains
10102 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010103 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010104 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010105
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010106 /* Send a netdev-removed uevent to the old namespace */
10107 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010108 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010109
Jiri Pirko93642e12020-01-25 12:17:08 +010010110 /* Move per-net netdevice notifiers that are following the netdevice */
10111 move_netdevice_notifiers_dev_net(dev, net);
10112
Eric W. Biedermance286d32007-09-12 13:53:49 +020010113 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010114 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010115 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010116
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010117 /* Send a netdev-add uevent to the new namespace */
10118 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010119 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010120
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010121 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010122 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010123 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010124
Christian Brauneref6a4c82020-02-27 04:37:19 +010010125 /* Adapt owner in case owning user namespace of target network
10126 * namespace is different from the original one.
10127 */
10128 err = netdev_change_owner(dev, net_old, net);
10129 WARN_ON(err);
10130
Eric W. Biedermance286d32007-09-12 13:53:49 +020010131 /* Add the device back in the hashes */
10132 list_netdevice(dev);
10133
10134 /* Notify protocols, that a new device appeared. */
10135 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10136
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010137 /*
10138 * Prevent userspace races by waiting until the network
10139 * device is fully setup before sending notifications.
10140 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010141 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010142
Eric W. Biedermance286d32007-09-12 13:53:49 +020010143 synchronize_net();
10144 err = 0;
10145out:
10146 return err;
10147}
Johannes Berg463d0182009-07-14 00:33:35 +020010148EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010149
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010150static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151{
10152 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010154 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010155 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157 local_irq_disable();
10158 cpu = smp_processor_id();
10159 sd = &per_cpu(softnet_data, cpu);
10160 oldsd = &per_cpu(softnet_data, oldcpu);
10161
10162 /* Find end of our completion_queue. */
10163 list_skb = &sd->completion_queue;
10164 while (*list_skb)
10165 list_skb = &(*list_skb)->next;
10166 /* Append completion queue from offline CPU. */
10167 *list_skb = oldsd->completion_queue;
10168 oldsd->completion_queue = NULL;
10169
Linus Torvalds1da177e2005-04-16 15:20:36 -070010170 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010171 if (oldsd->output_queue) {
10172 *sd->output_queue_tailp = oldsd->output_queue;
10173 sd->output_queue_tailp = oldsd->output_queue_tailp;
10174 oldsd->output_queue = NULL;
10175 oldsd->output_queue_tailp = &oldsd->output_queue;
10176 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010177 /* Append NAPI poll list from offline CPU, with one exception :
10178 * process_backlog() must be called by cpu owning percpu backlog.
10179 * We properly handle process_queue & input_pkt_queue later.
10180 */
10181 while (!list_empty(&oldsd->poll_list)) {
10182 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10183 struct napi_struct,
10184 poll_list);
10185
10186 list_del_init(&napi->poll_list);
10187 if (napi->poll == process_backlog)
10188 napi->state = 0;
10189 else
10190 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192
10193 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10194 local_irq_enable();
10195
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010196#ifdef CONFIG_RPS
10197 remsd = oldsd->rps_ipi_list;
10198 oldsd->rps_ipi_list = NULL;
10199#endif
10200 /* send out pending IPI's on offline CPU */
10201 net_rps_send_ipi(remsd);
10202
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010204 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010205 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010206 input_queue_head_incr(oldsd);
10207 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010208 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010209 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010210 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010214}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215
Herbert Xu7f353bf2007-08-10 15:47:58 -070010216/**
Herbert Xub63365a2008-10-23 01:11:29 -070010217 * netdev_increment_features - increment feature set by one
10218 * @all: current feature set
10219 * @one: new feature set
10220 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010221 *
10222 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010223 * @one to the master device with current feature set @all. Will not
10224 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010225 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010226netdev_features_t netdev_increment_features(netdev_features_t all,
10227 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010228{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010229 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010230 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010231 mask |= NETIF_F_VLAN_CHALLENGED;
10232
Tom Herberta1882222015-12-14 11:19:43 -080010233 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010234 all &= one | ~NETIF_F_ALL_FOR_ALL;
10235
Michał Mirosław1742f182011-04-22 06:31:16 +000010236 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010237 if (all & NETIF_F_HW_CSUM)
10238 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010239
10240 return all;
10241}
Herbert Xub63365a2008-10-23 01:11:29 -070010242EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010243
Baruch Siach430f03c2013-06-02 20:43:55 +000010244static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010245{
10246 int i;
10247 struct hlist_head *hash;
10248
Kees Cook6da2ec52018-06-12 13:55:00 -070010249 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010250 if (hash != NULL)
10251 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10252 INIT_HLIST_HEAD(&hash[i]);
10253
10254 return hash;
10255}
10256
Eric W. Biederman881d9662007-09-17 11:56:21 -070010257/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070010258static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010259{
Li RongQingd9f37d02018-07-13 14:41:36 +080010260 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080010261 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080010262
Rustad, Mark D734b6542012-07-18 09:06:07 +000010263 if (net != &init_net)
10264 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070010265
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010266 net->dev_name_head = netdev_create_hash();
10267 if (net->dev_name_head == NULL)
10268 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010269
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010270 net->dev_index_head = netdev_create_hash();
10271 if (net->dev_index_head == NULL)
10272 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010273
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020010274 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10275
Eric W. Biederman881d9662007-09-17 11:56:21 -070010276 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010277
10278err_idx:
10279 kfree(net->dev_name_head);
10280err_name:
10281 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010282}
10283
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010284/**
10285 * netdev_drivername - network driver for the device
10286 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010287 *
10288 * Determine network driver for device.
10289 */
David S. Miller3019de12011-06-06 16:41:33 -070010290const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070010291{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070010292 const struct device_driver *driver;
10293 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070010294 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070010295
10296 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010297 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070010298 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010299
10300 driver = parent->driver;
10301 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070010302 return driver->name;
10303 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010304}
10305
Joe Perches6ea754e2014-09-22 11:10:50 -070010306static void __netdev_printk(const char *level, const struct net_device *dev,
10307 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000010308{
Joe Perchesb004ff42012-09-12 20:12:19 -070010309 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010310 dev_printk_emit(level[1] - '0',
10311 dev->dev.parent,
10312 "%s %s %s%s: %pV",
10313 dev_driver_string(dev->dev.parent),
10314 dev_name(dev->dev.parent),
10315 netdev_name(dev), netdev_reg_state(dev),
10316 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010317 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010318 printk("%s%s%s: %pV",
10319 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010320 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070010321 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010322 }
Joe Perches256df2f2010-06-27 01:02:35 +000010323}
10324
Joe Perches6ea754e2014-09-22 11:10:50 -070010325void netdev_printk(const char *level, const struct net_device *dev,
10326 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000010327{
10328 struct va_format vaf;
10329 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000010330
10331 va_start(args, format);
10332
10333 vaf.fmt = format;
10334 vaf.va = &args;
10335
Joe Perches6ea754e2014-09-22 11:10:50 -070010336 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010337
Joe Perches256df2f2010-06-27 01:02:35 +000010338 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000010339}
10340EXPORT_SYMBOL(netdev_printk);
10341
10342#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070010343void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000010344{ \
Joe Perches256df2f2010-06-27 01:02:35 +000010345 struct va_format vaf; \
10346 va_list args; \
10347 \
10348 va_start(args, fmt); \
10349 \
10350 vaf.fmt = fmt; \
10351 vaf.va = &args; \
10352 \
Joe Perches6ea754e2014-09-22 11:10:50 -070010353 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070010354 \
Joe Perches256df2f2010-06-27 01:02:35 +000010355 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000010356} \
10357EXPORT_SYMBOL(func);
10358
10359define_netdev_printk_level(netdev_emerg, KERN_EMERG);
10360define_netdev_printk_level(netdev_alert, KERN_ALERT);
10361define_netdev_printk_level(netdev_crit, KERN_CRIT);
10362define_netdev_printk_level(netdev_err, KERN_ERR);
10363define_netdev_printk_level(netdev_warn, KERN_WARNING);
10364define_netdev_printk_level(netdev_notice, KERN_NOTICE);
10365define_netdev_printk_level(netdev_info, KERN_INFO);
10366
Pavel Emelyanov46650792007-10-08 20:38:39 -070010367static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010368{
10369 kfree(net->dev_name_head);
10370 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030010371 if (net != &init_net)
10372 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070010373}
10374
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010375static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070010376 .init = netdev_init,
10377 .exit = netdev_exit,
10378};
10379
Pavel Emelyanov46650792007-10-08 20:38:39 -070010380static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010381{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010382 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010383 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010384 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020010385 * initial network namespace
10386 */
10387 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010388 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010389 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010390 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020010391
10392 /* Ignore unmoveable devices (i.e. loopback) */
10393 if (dev->features & NETIF_F_NETNS_LOCAL)
10394 continue;
10395
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010396 /* Leave virtual devices for the generic cleanup */
10397 if (dev->rtnl_link_ops)
10398 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080010399
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010400 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010401 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020010402 if (__dev_get_by_name(&init_net, fb_name))
10403 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010404 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010405 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010406 pr_emerg("%s: failed to move %s to init_net: %d\n",
10407 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010408 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020010409 }
10410 }
10411 rtnl_unlock();
10412}
10413
Eric W. Biederman50624c92013-09-23 21:19:49 -070010414static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
10415{
10416 /* Return with the rtnl_lock held when there are no network
10417 * devices unregistering in any network namespace in net_list.
10418 */
10419 struct net *net;
10420 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010010421 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010422
Peter Zijlstraff960a72014-10-29 17:04:56 +010010423 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010424 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070010425 unregistering = false;
10426 rtnl_lock();
10427 list_for_each_entry(net, net_list, exit_list) {
10428 if (net->dev_unreg_count > 0) {
10429 unregistering = true;
10430 break;
10431 }
10432 }
10433 if (!unregistering)
10434 break;
10435 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010010436
10437 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010438 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010010439 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010440}
10441
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010442static void __net_exit default_device_exit_batch(struct list_head *net_list)
10443{
10444 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040010445 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010446 * Do this across as many network namespaces as possible to
10447 * improve batching efficiency.
10448 */
10449 struct net_device *dev;
10450 struct net *net;
10451 LIST_HEAD(dev_kill_list);
10452
Eric W. Biederman50624c92013-09-23 21:19:49 -070010453 /* To prevent network device cleanup code from dereferencing
10454 * loopback devices or network devices that have been freed
10455 * wait here for all pending unregistrations to complete,
10456 * before unregistring the loopback device and allowing the
10457 * network namespace be freed.
10458 *
10459 * The netdev todo list containing all network devices
10460 * unregistrations that happen in default_device_exit_batch
10461 * will run in the rtnl_unlock() at the end of
10462 * default_device_exit_batch.
10463 */
10464 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010465 list_for_each_entry(net, net_list, exit_list) {
10466 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020010467 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010468 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
10469 else
10470 unregister_netdevice_queue(dev, &dev_kill_list);
10471 }
10472 }
10473 unregister_netdevice_many(&dev_kill_list);
10474 rtnl_unlock();
10475}
10476
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010477static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010478 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010479 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020010480};
10481
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482/*
10483 * Initialize the DEV module. At boot time this walks the device list and
10484 * unhooks any devices that fail to initialise (normally hardware not
10485 * present) and leaves us with a valid list of present and active devices.
10486 *
10487 */
10488
10489/*
10490 * This is called single threaded during boot, so no need
10491 * to take the rtnl semaphore.
10492 */
10493static int __init net_dev_init(void)
10494{
10495 int i, rc = -ENOMEM;
10496
10497 BUG_ON(!dev_boot_phase);
10498
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499 if (dev_proc_init())
10500 goto out;
10501
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010502 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070010503 goto out;
10504
10505 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080010506 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 INIT_LIST_HEAD(&ptype_base[i]);
10508
Vlad Yasevich62532da2012-11-15 08:49:10 +000010509 INIT_LIST_HEAD(&offload_base);
10510
Eric W. Biederman881d9662007-09-17 11:56:21 -070010511 if (register_pernet_subsys(&netdev_net_ops))
10512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513
10514 /*
10515 * Initialise the packet receive queues.
10516 */
10517
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070010518 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070010519 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010520 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010521
Eric Dumazet41852492016-08-26 12:50:39 -070010522 INIT_WORK(flush, flush_backlog);
10523
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010524 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070010525 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010010526#ifdef CONFIG_XFRM_OFFLOAD
10527 skb_queue_head_init(&sd->xfrm_backlog);
10528#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010529 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000010530 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000010531#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010532 sd->csd.func = rps_trigger_softirq;
10533 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010534 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070010535#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000010536
David S. Miller7c4ec742018-07-20 23:37:55 -070010537 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010538 sd->backlog.poll = process_backlog;
10539 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010540 }
10541
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542 dev_boot_phase = 0;
10543
Eric W. Biederman505d4f72008-11-07 22:54:20 -080010544 /* The loopback device is special if any other network devices
10545 * is present in a network namespace the loopback device must
10546 * be present. Since we now dynamically allocate and free the
10547 * loopback device ensure this invariant is maintained by
10548 * keeping the loopback device as the first device on the
10549 * list of network devices. Ensuring the loopback devices
10550 * is the first device that appears and the last network device
10551 * that disappears.
10552 */
10553 if (register_pernet_device(&loopback_net_ops))
10554 goto out;
10555
10556 if (register_pernet_device(&default_device_ops))
10557 goto out;
10558
Carlos R. Mafra962cf362008-05-15 11:15:37 -030010559 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
10560 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010562 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
10563 NULL, dev_cpu_dead);
10564 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565 rc = 0;
10566out:
10567 return rc;
10568}
10569
10570subsys_initcall(net_dev_init);