blob: c6c985fe7b1bcf784cedde2b2a86e26356471bee [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>
Pablo Neirae687ad62015-05-13 18:19:38 +0200138#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 *
3269 * Segmentation preserves SKB_SGO_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
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003298 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3299 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 }
3809
3810 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 Borkmann1f211a12016-01-07 22:29:47 +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 }
4006# endif
4007#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;
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04004143unsigned int __read_mostly netdev_budget_usecs = 2000;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004144int weight_p __read_mostly = 64; /* old backlog weight */
4145int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4146int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4147int dev_rx_weight __read_mostly = 64;
4148int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004149/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4150int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004152/* Called with irq disabled */
4153static inline void ____napi_schedule(struct softnet_data *sd,
4154 struct napi_struct *napi)
4155{
4156 list_add_tail(&napi->poll_list, &sd->poll_list);
4157 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4158}
4159
Eric Dumazetdf334542010-03-24 19:13:54 +00004160#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004161
4162/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004163struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004164EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004165u32 rps_cpu_mask __read_mostly;
4166EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004167
Eric Dumazetdc053602019-03-22 08:56:38 -07004168struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004169EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004170struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004171EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004172
Ben Hutchingsc4454772011-01-19 11:03:53 +00004173static struct rps_dev_flow *
4174set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4175 struct rps_dev_flow *rflow, u16 next_cpu)
4176{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004177 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004178#ifdef CONFIG_RFS_ACCEL
4179 struct netdev_rx_queue *rxqueue;
4180 struct rps_dev_flow_table *flow_table;
4181 struct rps_dev_flow *old_rflow;
4182 u32 flow_id;
4183 u16 rxq_index;
4184 int rc;
4185
4186 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004187 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4188 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004189 goto out;
4190 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4191 if (rxq_index == skb_get_rx_queue(skb))
4192 goto out;
4193
4194 rxqueue = dev->_rx + rxq_index;
4195 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4196 if (!flow_table)
4197 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004198 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004199 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4200 rxq_index, flow_id);
4201 if (rc < 0)
4202 goto out;
4203 old_rflow = rflow;
4204 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004205 rflow->filter = rc;
4206 if (old_rflow->filter == rflow->filter)
4207 old_rflow->filter = RPS_NO_FILTER;
4208 out:
4209#endif
4210 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004211 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004212 }
4213
Ben Hutchings09994d12011-10-03 04:42:46 +00004214 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004215 return rflow;
4216}
4217
Tom Herbert0a9627f2010-03-16 08:03:29 +00004218/*
4219 * get_rps_cpu is called from netif_receive_skb and returns the target
4220 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004221 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004222 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004223static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4224 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004225{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004226 const struct rps_sock_flow_table *sock_flow_table;
4227 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004228 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004229 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004230 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004231 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004232 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004233
Tom Herbert0a9627f2010-03-16 08:03:29 +00004234 if (skb_rx_queue_recorded(skb)) {
4235 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004236
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004237 if (unlikely(index >= dev->real_num_rx_queues)) {
4238 WARN_ONCE(dev->real_num_rx_queues > 1,
4239 "%s received packet on queue %u, but number "
4240 "of RX queues is %u\n",
4241 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004242 goto done;
4243 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004244 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004245 }
4246
Eric Dumazet567e4b72015-02-06 12:59:01 -08004247 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4248
4249 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4250 map = rcu_dereference(rxqueue->rps_map);
4251 if (!flow_table && !map)
4252 goto done;
4253
Changli Gao2d47b452010-08-17 19:00:56 +00004254 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004255 hash = skb_get_hash(skb);
4256 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004257 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004258
Tom Herbertfec5e652010-04-16 16:01:27 -07004259 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4260 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004261 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004262 u32 next_cpu;
4263 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004264
Eric Dumazet567e4b72015-02-06 12:59:01 -08004265 /* First check into global flow table if there is a match */
4266 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4267 if ((ident ^ hash) & ~rps_cpu_mask)
4268 goto try_rps;
4269
4270 next_cpu = ident & rps_cpu_mask;
4271
4272 /* OK, now we know there is a match,
4273 * we can look at the local (per receive queue) flow table
4274 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004275 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004276 tcpu = rflow->cpu;
4277
Tom Herbertfec5e652010-04-16 16:01:27 -07004278 /*
4279 * If the desired CPU (where last recvmsg was done) is
4280 * different from current CPU (one in the rx-queue flow
4281 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004282 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004283 * - Current CPU is offline.
4284 * - The current CPU's queue tail has advanced beyond the
4285 * last packet that was enqueued using this table entry.
4286 * This guarantees that all previous packets for the flow
4287 * have been dequeued, thus preserving in order delivery.
4288 */
4289 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004290 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004291 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004292 rflow->last_qtail)) >= 0)) {
4293 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004294 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004295 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004296
Eric Dumazeta31196b2015-04-25 09:35:24 -07004297 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004298 *rflowp = rflow;
4299 cpu = tcpu;
4300 goto done;
4301 }
4302 }
4303
Eric Dumazet567e4b72015-02-06 12:59:01 -08004304try_rps:
4305
Tom Herbert0a9627f2010-03-16 08:03:29 +00004306 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004307 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004308 if (cpu_online(tcpu)) {
4309 cpu = tcpu;
4310 goto done;
4311 }
4312 }
4313
4314done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004315 return cpu;
4316}
4317
Ben Hutchingsc4454772011-01-19 11:03:53 +00004318#ifdef CONFIG_RFS_ACCEL
4319
4320/**
4321 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4322 * @dev: Device on which the filter was set
4323 * @rxq_index: RX queue index
4324 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4325 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4326 *
4327 * Drivers that implement ndo_rx_flow_steer() should periodically call
4328 * this function for each installed filter and remove the filters for
4329 * which it returns %true.
4330 */
4331bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4332 u32 flow_id, u16 filter_id)
4333{
4334 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4335 struct rps_dev_flow_table *flow_table;
4336 struct rps_dev_flow *rflow;
4337 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004338 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004339
4340 rcu_read_lock();
4341 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4342 if (flow_table && flow_id <= flow_table->mask) {
4343 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004344 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004345 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004346 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4347 rflow->last_qtail) <
4348 (int)(10 * flow_table->mask)))
4349 expire = false;
4350 }
4351 rcu_read_unlock();
4352 return expire;
4353}
4354EXPORT_SYMBOL(rps_may_expire_flow);
4355
4356#endif /* CONFIG_RFS_ACCEL */
4357
Tom Herbert0a9627f2010-03-16 08:03:29 +00004358/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004359static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004360{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004361 struct softnet_data *sd = data;
4362
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004363 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004364 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004365}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004366
Tom Herbertfec5e652010-04-16 16:01:27 -07004367#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004368
4369/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004370 * Check if this softnet_data structure is another cpu one
4371 * If yes, queue it to our IPI list and return 1
4372 * If no, return 0
4373 */
4374static int rps_ipi_queued(struct softnet_data *sd)
4375{
4376#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004377 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004378
4379 if (sd != mysd) {
4380 sd->rps_ipi_next = mysd->rps_ipi_list;
4381 mysd->rps_ipi_list = sd;
4382
4383 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4384 return 1;
4385 }
4386#endif /* CONFIG_RPS */
4387 return 0;
4388}
4389
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004390#ifdef CONFIG_NET_FLOW_LIMIT
4391int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4392#endif
4393
4394static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4395{
4396#ifdef CONFIG_NET_FLOW_LIMIT
4397 struct sd_flow_limit *fl;
4398 struct softnet_data *sd;
4399 unsigned int old_flow, new_flow;
4400
4401 if (qlen < (netdev_max_backlog >> 1))
4402 return false;
4403
Christoph Lameter903ceff2014-08-17 12:30:35 -05004404 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004405
4406 rcu_read_lock();
4407 fl = rcu_dereference(sd->flow_limit);
4408 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004409 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004410 old_flow = fl->history[fl->history_head];
4411 fl->history[fl->history_head] = new_flow;
4412
4413 fl->history_head++;
4414 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4415
4416 if (likely(fl->buckets[old_flow]))
4417 fl->buckets[old_flow]--;
4418
4419 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4420 fl->count++;
4421 rcu_read_unlock();
4422 return true;
4423 }
4424 }
4425 rcu_read_unlock();
4426#endif
4427 return false;
4428}
4429
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004430/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004431 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4432 * queue (may be a remote CPU queue).
4433 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004434static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4435 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004436{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004437 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004438 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004439 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004440
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004441 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004442
4443 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004444
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004445 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004446 if (!netif_running(skb->dev))
4447 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004448 qlen = skb_queue_len(&sd->input_pkt_queue);
4449 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004450 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004451enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004452 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004453 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004454 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004455 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004456 return NET_RX_SUCCESS;
4457 }
4458
Eric Dumazetebda37c22010-05-06 23:51:21 +00004459 /* Schedule NAPI for backlog device
4460 * We can use non atomic operation since we own the queue lock
4461 */
4462 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004463 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004464 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004465 }
4466 goto enqueue;
4467 }
4468
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004469drop:
Changli Gaodee42872010-05-02 05:42:16 +00004470 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004471 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004472
Tom Herbert0a9627f2010-03-16 08:03:29 +00004473 local_irq_restore(flags);
4474
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004475 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004476 kfree_skb(skb);
4477 return NET_RX_DROP;
4478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004480static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4481{
4482 struct net_device *dev = skb->dev;
4483 struct netdev_rx_queue *rxqueue;
4484
4485 rxqueue = dev->_rx;
4486
4487 if (skb_rx_queue_recorded(skb)) {
4488 u16 index = skb_get_rx_queue(skb);
4489
4490 if (unlikely(index >= dev->real_num_rx_queues)) {
4491 WARN_ONCE(dev->real_num_rx_queues > 1,
4492 "%s received packet on queue %u, but number "
4493 "of RX queues is %u\n",
4494 dev->name, index, dev->real_num_rx_queues);
4495
4496 return rxqueue; /* Return first rxqueue */
4497 }
4498 rxqueue += index;
4499 }
4500 return rxqueue;
4501}
4502
John Fastabendd4455162017-07-17 09:26:45 -07004503static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004504 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004505 struct bpf_prog *xdp_prog)
4506{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004507 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004508 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004509 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004510 __be16 orig_eth_type;
4511 struct ethhdr *eth;
4512 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004513 int hlen, off;
4514 u32 mac_len;
4515
4516 /* Reinjected packets coming from act_mirred or similar should
4517 * not get XDP generic processing.
4518 */
Toke Høiland-Jørgensenad1e03b2020-02-10 17:10:46 +01004519 if (skb_is_tc_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004520 return XDP_PASS;
4521
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004522 /* XDP packets must be linear and must have sufficient headroom
4523 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4524 * native XDP provides, thus we need to do it here as well.
4525 */
Toke Høiland-Jørgensenad1e03b2020-02-10 17:10:46 +01004526 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004527 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4528 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4529 int troom = skb->tail + skb->data_len - skb->end;
4530
4531 /* In case we have to go down the path and also linearize,
4532 * then lets do the pskb_expand_head() work just once here.
4533 */
4534 if (pskb_expand_head(skb,
4535 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4536 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4537 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004538 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004539 goto do_drop;
4540 }
John Fastabendd4455162017-07-17 09:26:45 -07004541
4542 /* The XDP program wants to see the packet starting at the MAC
4543 * header.
4544 */
4545 mac_len = skb->data - skb_mac_header(skb);
4546 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004547 xdp->data = skb->data - mac_len;
4548 xdp->data_meta = xdp->data;
4549 xdp->data_end = xdp->data + hlen;
4550 xdp->data_hard_start = skb->data - skb_headroom(skb);
4551 orig_data_end = xdp->data_end;
4552 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004553 eth = (struct ethhdr *)xdp->data;
4554 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4555 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004556
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004557 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004558 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004559
Björn Töpel02671e22018-05-02 13:01:30 +02004560 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004561
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004562 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004563 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004564 if (off) {
4565 if (off > 0)
4566 __skb_pull(skb, off);
4567 else if (off < 0)
4568 __skb_push(skb, -off);
4569
4570 skb->mac_header += off;
4571 skb_reset_network_header(skb);
4572 }
John Fastabendd4455162017-07-17 09:26:45 -07004573
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004574 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4575 * pckt.
4576 */
Björn Töpel02671e22018-05-02 13:01:30 +02004577 off = orig_data_end - xdp->data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004578 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004579 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004580 skb->len -= off;
Björn Töpel02671e22018-05-02 13:01:30 +02004581
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004582 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004583
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004584 /* check if XDP changed eth hdr such SKB needs update */
4585 eth = (struct ethhdr *)xdp->data;
4586 if ((orig_eth_type != eth->h_proto) ||
4587 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4588 __skb_push(skb, ETH_HLEN);
4589 skb->protocol = eth_type_trans(skb, skb->dev);
4590 }
4591
John Fastabendd4455162017-07-17 09:26:45 -07004592 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004593 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004594 case XDP_TX:
4595 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004596 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004597 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004598 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004599 if (metalen)
4600 skb_metadata_set(skb, metalen);
4601 break;
John Fastabendd4455162017-07-17 09:26:45 -07004602 default:
4603 bpf_warn_invalid_xdp_action(act);
4604 /* fall through */
4605 case XDP_ABORTED:
4606 trace_xdp_exception(skb->dev, xdp_prog, act);
4607 /* fall through */
4608 case XDP_DROP:
4609 do_drop:
4610 kfree_skb(skb);
4611 break;
4612 }
4613
4614 return act;
4615}
4616
4617/* When doing generic XDP we have to bypass the qdisc layer and the
4618 * network taps in order to match in-driver-XDP behavior.
4619 */
Jason Wang7c497472017-08-11 19:41:17 +08004620void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004621{
4622 struct net_device *dev = skb->dev;
4623 struct netdev_queue *txq;
4624 bool free_skb = true;
4625 int cpu, rc;
4626
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004627 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004628 cpu = smp_processor_id();
4629 HARD_TX_LOCK(dev, txq, cpu);
4630 if (!netif_xmit_stopped(txq)) {
4631 rc = netdev_start_xmit(skb, dev, txq, 0);
4632 if (dev_xmit_complete(rc))
4633 free_skb = false;
4634 }
4635 HARD_TX_UNLOCK(dev, txq);
4636 if (free_skb) {
4637 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4638 kfree_skb(skb);
4639 }
4640}
Jason Wang7c497472017-08-11 19:41:17 +08004641EXPORT_SYMBOL_GPL(generic_xdp_tx);
John Fastabendd4455162017-07-17 09:26:45 -07004642
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
Jiri Pirko46209402017-11-03 11:46:25 +01004852 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004853 case TC_ACT_OK:
4854 case TC_ACT_RECLASSIFY:
4855 skb->tc_index = TC_H_MIN(cl_res.classid);
4856 break;
4857 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004858 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004859 kfree_skb(skb);
4860 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004861 case TC_ACT_STOLEN:
4862 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004863 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004864 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004865 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004866 case TC_ACT_REDIRECT:
4867 /* skb_mac_header check was done by cls/act_bpf, so
4868 * we can safely push the L2 header back before
4869 * redirecting to another netdev
4870 */
4871 __skb_push(skb, skb->mac_len);
4872 skb_do_redirect(skb);
4873 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01004874 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02004875 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004876 default:
4877 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004878 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004879#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004880 return skb;
4881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004883/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004884 * netdev_is_rx_handler_busy - check if receive handler is registered
4885 * @dev: device to check
4886 *
4887 * Check if a receive handler is already registered for a given device.
4888 * Return true if there one.
4889 *
4890 * The caller must hold the rtnl_mutex.
4891 */
4892bool netdev_is_rx_handler_busy(struct net_device *dev)
4893{
4894 ASSERT_RTNL();
4895 return dev && rtnl_dereference(dev->rx_handler);
4896}
4897EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4898
4899/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004900 * netdev_rx_handler_register - register receive handler
4901 * @dev: device to register a handler for
4902 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004903 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004904 *
Masanari Iidae2278672014-02-18 22:54:36 +09004905 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004906 * called from __netif_receive_skb. A negative errno code is returned
4907 * on a failure.
4908 *
4909 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004910 *
4911 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004912 */
4913int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004914 rx_handler_func_t *rx_handler,
4915 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004916{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004917 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004918 return -EBUSY;
4919
Paolo Abenif54262502018-03-09 10:39:24 +01004920 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4921 return -EINVAL;
4922
Eric Dumazet00cfec32013-03-29 03:01:22 +00004923 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004924 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004925 rcu_assign_pointer(dev->rx_handler, rx_handler);
4926
4927 return 0;
4928}
4929EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4930
4931/**
4932 * netdev_rx_handler_unregister - unregister receive handler
4933 * @dev: device to unregister a handler from
4934 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004935 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004936 *
4937 * The caller must hold the rtnl_mutex.
4938 */
4939void netdev_rx_handler_unregister(struct net_device *dev)
4940{
4941
4942 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004943 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004944 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4945 * section has a guarantee to see a non NULL rx_handler_data
4946 * as well.
4947 */
4948 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004949 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004950}
4951EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4952
Mel Gormanb4b9e352012-07-31 16:44:26 -07004953/*
4954 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4955 * the special handling of PFMEMALLOC skbs.
4956 */
4957static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4958{
4959 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004960 case htons(ETH_P_ARP):
4961 case htons(ETH_P_IP):
4962 case htons(ETH_P_IPV6):
4963 case htons(ETH_P_8021Q):
4964 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004965 return true;
4966 default:
4967 return false;
4968 }
4969}
4970
Pablo Neirae687ad62015-05-13 18:19:38 +02004971static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4972 int *ret, struct net_device *orig_dev)
4973{
4974 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004975 int ingress_retval;
4976
Pablo Neirae687ad62015-05-13 18:19:38 +02004977 if (*pt_prev) {
4978 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4979 *pt_prev = NULL;
4980 }
4981
Aaron Conole2c1e2702016-09-21 11:35:03 -04004982 rcu_read_lock();
4983 ingress_retval = nf_hook_ingress(skb);
4984 rcu_read_unlock();
4985 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004986 }
4987 return 0;
4988}
Pablo Neirae687ad62015-05-13 18:19:38 +02004989
Edward Cree88eb1942018-07-02 16:13:56 +01004990static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
4991 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
4993 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004994 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004995 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004996 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004998 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Eric Dumazet588f0332011-11-15 04:12:55 +00005000 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005001
Koki Sanagicf66ba52010-08-23 18:45:02 +09005002 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005003
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005004 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005005
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005006 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005007 if (!skb_transport_header_was_set(skb))
5008 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005009 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 pt_prev = NULL;
5012
David S. Miller63d8ea72011-02-28 10:48:59 -08005013another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005014 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005015
5016 __this_cpu_inc(softnet_data.processed);
5017
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005018 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5019 int ret2;
5020
5021 preempt_disable();
5022 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5023 preempt_enable();
5024
5025 if (ret2 != XDP_PASS)
5026 return NET_RX_DROP;
5027 skb_reset_mac_len(skb);
5028 }
5029
Patrick McHardy8ad227f2013-04-19 02:04:31 +00005030 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5031 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005032 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005033 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005034 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005035 }
5036
Willem de Bruijne7246e12017-01-07 17:06:35 -05005037 if (skb_skip_tc_classify(skb))
5038 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
David S. Miller9754e292013-02-14 15:57:38 -05005040 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005041 goto skip_taps;
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005044 if (pt_prev)
5045 ret = deliver_skb(skb, pt_prev, orig_dev);
5046 pt_prev = ptype;
5047 }
5048
5049 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5050 if (pt_prev)
5051 ret = deliver_skb(skb, pt_prev, orig_dev);
5052 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
5054
Mel Gormanb4b9e352012-07-31 16:44:26 -07005055skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005056#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005057 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01005058 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02005059 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005060 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005061
5062 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005063 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005064 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005065#endif
Willem de Bruijna5135bc2017-01-07 17:06:36 -05005066 skb_reset_tc(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005067skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005068 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005069 goto drop;
5070
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005071 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005072 if (pt_prev) {
5073 ret = deliver_skb(skb, pt_prev, orig_dev);
5074 pt_prev = NULL;
5075 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005076 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005077 goto another_round;
5078 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005079 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005080 }
5081
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005082 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005083 if (rx_handler) {
5084 if (pt_prev) {
5085 ret = deliver_skb(skb, pt_prev, orig_dev);
5086 pt_prev = NULL;
5087 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005088 switch (rx_handler(&skb)) {
5089 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005090 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005091 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005092 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005093 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005094 case RX_HANDLER_EXACT:
5095 deliver_exact = true;
5096 case RX_HANDLER_PASS:
5097 break;
5098 default:
5099 BUG();
5100 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005103 if (unlikely(skb_vlan_tag_present(skb))) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005104check_vlan_id:
5105 if (skb_vlan_tag_get_id(skb)) {
5106 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5107 * find vlan device.
5108 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005109 skb->pkt_type = PACKET_OTHERHOST;
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005110 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5111 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5112 /* Outer header is 802.1P with vlan 0, inner header is
5113 * 802.1Q or 802.1AD and vlan_do_receive() above could
5114 * not find vlan dev for vlan id 0.
5115 */
5116 __vlan_hwaccel_clear_tag(skb);
5117 skb = skb_vlan_untag(skb);
5118 if (unlikely(!skb))
5119 goto out;
5120 if (vlan_do_receive(&skb))
5121 /* After stripping off 802.1P header with vlan 0
5122 * vlan dev is found for inner header.
5123 */
5124 goto another_round;
5125 else if (unlikely(!skb))
5126 goto out;
5127 else
5128 /* We have stripped outer 802.1P vlan 0 header.
5129 * But could not find vlan dev.
5130 * check again for vlan id to set OTHERHOST.
5131 */
5132 goto check_vlan_id;
5133 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005134 /* Note: we might in the future use prio bits
5135 * and set skb->priority like in vlan_do_receive()
5136 * For the time being, just ignore Priority Code Point
5137 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005138 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005139 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005142
5143 /* deliver only exact match when indicated */
5144 if (likely(!deliver_exact)) {
5145 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5146 &ptype_base[ntohs(type) &
5147 PTYPE_HASH_MASK]);
5148 }
5149
5150 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5151 &orig_dev->ptype_specific);
5152
5153 if (unlikely(skb->dev != orig_dev)) {
5154 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5155 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 }
5157
5158 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005159 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005160 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005161 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005163drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005164 if (!deliver_exact)
5165 atomic_long_inc(&skb->dev->rx_dropped);
5166 else
5167 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 kfree_skb(skb);
5169 /* Jamal, now you will not able to escape explaining
5170 * me how you were going to use this. :-)
5171 */
5172 ret = NET_RX_DROP;
5173 }
5174
Julian Anastasov2c17d272015-07-09 09:59:10 +03005175out:
David S. Miller9754e292013-02-14 15:57:38 -05005176 return ret;
5177}
5178
Edward Cree88eb1942018-07-02 16:13:56 +01005179static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5180{
5181 struct net_device *orig_dev = skb->dev;
5182 struct packet_type *pt_prev = NULL;
5183 int ret;
5184
5185 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
5186 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005187 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5188 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005189 return ret;
5190}
5191
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005192/**
5193 * netif_receive_skb_core - special purpose version of netif_receive_skb
5194 * @skb: buffer to process
5195 *
5196 * More direct receive version of netif_receive_skb(). It should
5197 * only be used by callers that have a need to skip RPS and Generic XDP.
5198 * Caller must also take care of handling if (page_is_)pfmemalloc.
5199 *
5200 * This function may only be called from softirq context and interrupts
5201 * should be enabled.
5202 *
5203 * Return values (usually ignored):
5204 * NET_RX_SUCCESS: no congestion
5205 * NET_RX_DROP: packet was dropped
5206 */
5207int netif_receive_skb_core(struct sk_buff *skb)
5208{
5209 int ret;
5210
5211 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005212 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005213 rcu_read_unlock();
5214
5215 return ret;
5216}
5217EXPORT_SYMBOL(netif_receive_skb_core);
5218
Edward Cree88eb1942018-07-02 16:13:56 +01005219static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5220 struct packet_type *pt_prev,
5221 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005222{
5223 struct sk_buff *skb, *next;
5224
Edward Cree88eb1942018-07-02 16:13:56 +01005225 if (!pt_prev)
5226 return;
5227 if (list_empty(head))
5228 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005229 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005230 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5231 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005232 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005233 list_for_each_entry_safe(skb, next, head, list) {
5234 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005235 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005236 }
Edward Cree88eb1942018-07-02 16:13:56 +01005237}
5238
5239static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5240{
5241 /* Fast-path assumptions:
5242 * - There is no RX handler.
5243 * - Only one packet_type matches.
5244 * If either of these fails, we will end up doing some per-packet
5245 * processing in-line, then handling the 'last ptype' for the whole
5246 * sublist. This can't cause out-of-order delivery to any single ptype,
5247 * because the 'last ptype' must be constant across the sublist, and all
5248 * other ptypes are handled per-packet.
5249 */
5250 /* Current (common) ptype of sublist */
5251 struct packet_type *pt_curr = NULL;
5252 /* Current (common) orig_dev of sublist */
5253 struct net_device *od_curr = NULL;
5254 struct list_head sublist;
5255 struct sk_buff *skb, *next;
5256
Edward Cree9af86f92018-07-09 18:10:19 +01005257 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005258 list_for_each_entry_safe(skb, next, head, list) {
5259 struct net_device *orig_dev = skb->dev;
5260 struct packet_type *pt_prev = NULL;
5261
Edward Cree22f6bbb2018-12-04 17:37:57 +00005262 skb_list_del_init(skb);
Edward Cree88eb1942018-07-02 16:13:56 +01005263 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005264 if (!pt_prev)
5265 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005266 if (pt_curr != pt_prev || od_curr != orig_dev) {
5267 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005268 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5269 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005270 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005271 pt_curr = pt_prev;
5272 od_curr = orig_dev;
5273 }
Edward Cree9af86f92018-07-09 18:10:19 +01005274 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005275 }
5276
5277 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005278 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005279}
5280
David S. Miller9754e292013-02-14 15:57:38 -05005281static int __netif_receive_skb(struct sk_buff *skb)
5282{
5283 int ret;
5284
5285 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005286 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005287
5288 /*
5289 * PFMEMALLOC skbs are special, they should
5290 * - be delivered to SOCK_MEMALLOC sockets only
5291 * - stay away from userspace
5292 * - have bounded memory usage
5293 *
5294 * Use PF_MEMALLOC as this saves us from propagating the allocation
5295 * context down to all allocation sites.
5296 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005297 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005298 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005299 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005300 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005301 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 return ret;
5304}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005305
Edward Cree4ce00172018-07-02 16:13:40 +01005306static void __netif_receive_skb_list(struct list_head *head)
5307{
5308 unsigned long noreclaim_flag = 0;
5309 struct sk_buff *skb, *next;
5310 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5311
5312 list_for_each_entry_safe(skb, next, head, list) {
5313 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5314 struct list_head sublist;
5315
5316 /* Handle the previous sublist */
5317 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005318 if (!list_empty(&sublist))
5319 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005320 pfmemalloc = !pfmemalloc;
5321 /* See comments in __netif_receive_skb */
5322 if (pfmemalloc)
5323 noreclaim_flag = memalloc_noreclaim_save();
5324 else
5325 memalloc_noreclaim_restore(noreclaim_flag);
5326 }
5327 }
5328 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005329 if (!list_empty(head))
5330 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005331 /* Restore pflags */
5332 if (pfmemalloc)
5333 memalloc_noreclaim_restore(noreclaim_flag);
5334}
5335
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005336static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005337{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005338 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005339 struct bpf_prog *new = xdp->prog;
5340 int ret = 0;
5341
5342 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005343 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005344 rcu_assign_pointer(dev->xdp_prog, new);
5345 if (old)
5346 bpf_prog_put(old);
5347
5348 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005349 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005350 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005351 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005352 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005353 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005354 }
5355 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005356
5357 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07005358 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04005359 break;
5360
5361 default:
5362 ret = -EINVAL;
5363 break;
5364 }
5365
5366 return ret;
5367}
5368
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005369static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005370{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005371 int ret;
5372
Eric Dumazet588f0332011-11-15 04:12:55 +00005373 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005374
Richard Cochranc1f19b52010-07-17 08:49:36 +00005375 if (skb_defer_rx_timestamp(skb))
5376 return NET_RX_SUCCESS;
5377
John Fastabendbbbe2112017-09-08 14:00:30 -07005378 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005379#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005380 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005381 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005382 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005383
Eric Dumazet3b098e22010-05-15 23:57:10 -07005384 if (cpu >= 0) {
5385 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5386 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005387 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005388 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005389 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005390#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005391 ret = __netif_receive_skb(skb);
5392 rcu_read_unlock();
5393 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005394}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005395
Edward Cree7da517a2018-07-02 16:13:24 +01005396static void netif_receive_skb_list_internal(struct list_head *head)
5397{
Edward Cree7da517a2018-07-02 16:13:24 +01005398 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005399 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005400
Edward Cree8c057ef2018-07-09 18:09:54 +01005401 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005402 list_for_each_entry_safe(skb, next, head, list) {
5403 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005404 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005405 if (!skb_defer_rx_timestamp(skb))
5406 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005407 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005408 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005409
Edward Cree7da517a2018-07-02 16:13:24 +01005410 rcu_read_lock();
5411#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005412 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005413 list_for_each_entry_safe(skb, next, head, list) {
5414 struct rps_dev_flow voidflow, *rflow = &voidflow;
5415 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5416
5417 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005418 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005419 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005420 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005421 }
5422 }
5423 }
5424#endif
5425 __netif_receive_skb_list(head);
5426 rcu_read_unlock();
5427}
5428
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005429/**
5430 * netif_receive_skb - process receive buffer from network
5431 * @skb: buffer to process
5432 *
5433 * netif_receive_skb() is the main receive data processing function.
5434 * It always succeeds. The buffer may be dropped during processing
5435 * for congestion control or by the protocol layers.
5436 *
5437 * This function may only be called from softirq context and interrupts
5438 * should be enabled.
5439 *
5440 * Return values (usually ignored):
5441 * NET_RX_SUCCESS: no congestion
5442 * NET_RX_DROP: packet was dropped
5443 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005444int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005445{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005446 int ret;
5447
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005448 trace_netif_receive_skb_entry(skb);
5449
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005450 ret = netif_receive_skb_internal(skb);
5451 trace_netif_receive_skb_exit(ret);
5452
5453 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005454}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005455EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
Edward Creef6ad8c12018-07-02 16:12:45 +01005457/**
5458 * netif_receive_skb_list - process many receive buffers from network
5459 * @head: list of skbs to process.
5460 *
Edward Cree7da517a2018-07-02 16:13:24 +01005461 * Since return value of netif_receive_skb() is normally ignored, and
5462 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005463 *
5464 * This function may only be called from softirq context and interrupts
5465 * should be enabled.
5466 */
5467void netif_receive_skb_list(struct list_head *head)
5468{
Edward Cree7da517a2018-07-02 16:13:24 +01005469 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005470
Edward Creeb9f463d2018-07-02 16:14:44 +01005471 if (list_empty(head))
5472 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005473 if (trace_netif_receive_skb_list_entry_enabled()) {
5474 list_for_each_entry(skb, head, list)
5475 trace_netif_receive_skb_list_entry(skb);
5476 }
Edward Cree7da517a2018-07-02 16:13:24 +01005477 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005478 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005479}
5480EXPORT_SYMBOL(netif_receive_skb_list);
5481
Eric Dumazet41852492016-08-26 12:50:39 -07005482DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005483
5484/* Network device is going away, flush any packets still pending */
5485static void flush_backlog(struct work_struct *work)
5486{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005487 struct sk_buff *skb, *tmp;
5488 struct softnet_data *sd;
5489
5490 local_bh_disable();
5491 sd = this_cpu_ptr(&softnet_data);
5492
5493 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005494 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005495 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005496 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005497 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005498 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005499 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005500 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005501 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005502 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005503 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005504
5505 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005506 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005507 __skb_unlink(skb, &sd->process_queue);
5508 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005509 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005510 }
5511 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005512 local_bh_enable();
5513}
5514
Eric Dumazet41852492016-08-26 12:50:39 -07005515static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005516{
5517 unsigned int cpu;
5518
5519 get_online_cpus();
5520
Eric Dumazet41852492016-08-26 12:50:39 -07005521 for_each_online_cpu(cpu)
5522 queue_work_on(cpu, system_highpri_wq,
5523 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005524
5525 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07005526 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005527
5528 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005529}
5530
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005531/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5532static void gro_normal_list(struct napi_struct *napi)
5533{
5534 if (!napi->rx_count)
5535 return;
5536 netif_receive_skb_list_internal(&napi->rx_list);
5537 INIT_LIST_HEAD(&napi->rx_list);
5538 napi->rx_count = 0;
5539}
5540
5541/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5542 * pass the whole batch up to the stack.
5543 */
5544static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5545{
5546 list_add_tail(&skb->list, &napi->rx_list);
5547 if (++napi->rx_count >= gro_normal_batch)
5548 gro_normal_list(napi);
5549}
5550
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005551INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5552INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005553static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005554{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005555 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005556 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005557 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005558 int err = -ENOENT;
5559
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005560 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5561
Herbert Xufc59f9a2009-04-14 15:11:06 -07005562 if (NAPI_GRO_CB(skb)->count == 1) {
5563 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005564 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005565 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005566
5567 rcu_read_lock();
5568 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005569 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005570 continue;
5571
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005572 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5573 ipv6_gro_complete, inet_gro_complete,
5574 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005575 break;
5576 }
5577 rcu_read_unlock();
5578
5579 if (err) {
5580 WARN_ON(&ptype->list == head);
5581 kfree_skb(skb);
5582 return NET_RX_SUCCESS;
5583 }
5584
5585out:
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005586 gro_normal_one(napi, skb);
5587 return NET_RX_SUCCESS;
Herbert Xud565b0a2008-12-15 23:38:52 -08005588}
5589
Li RongQing6312fe72018-07-05 14:34:32 +08005590static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005591 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005592{
Li RongQing6312fe72018-07-05 14:34:32 +08005593 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005594 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005595
David Miller07d78362018-06-24 14:14:02 +09005596 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005597 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5598 return;
David S. Miller992cba72018-07-31 15:27:56 -07005599 skb_list_del_init(skb);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005600 napi_gro_complete(napi, skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005601 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005602 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005603
5604 if (!napi->gro_hash[index].count)
5605 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005606}
David Miller07d78362018-06-24 14:14:02 +09005607
Li RongQing6312fe72018-07-05 14:34:32 +08005608/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005609 * youngest packets at the head of it.
5610 * Complete skbs in reverse order to reduce latencies.
5611 */
5612void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5613{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005614 unsigned long bitmask = napi->gro_bitmask;
5615 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005616
Eric Dumazet42519ed2018-11-21 11:39:28 -08005617 while ((i = ffs(bitmask)) != 0) {
5618 bitmask >>= i;
5619 base += i;
5620 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005621 }
David Miller07d78362018-06-24 14:14:02 +09005622}
Eric Dumazet86cac582010-08-31 18:25:32 +00005623EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005624
David Miller07d78362018-06-24 14:14:02 +09005625static struct list_head *gro_list_prepare(struct napi_struct *napi,
5626 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005627{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005628 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005629 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005630 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005631 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005632
Li RongQing6312fe72018-07-05 14:34:32 +08005633 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005634 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005635 unsigned long diffs;
5636
Tom Herbert0b4cec82014-01-15 08:58:06 -08005637 NAPI_GRO_CB(p)->flush = 0;
5638
5639 if (hash != skb_get_hash_raw(p)) {
5640 NAPI_GRO_CB(p)->same_flow = 0;
5641 continue;
5642 }
5643
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005644 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005645 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5646 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005647 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Jesse Grossce87fc62016-01-20 17:59:49 -08005648 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005649 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005650 if (maclen == ETH_HLEN)
5651 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005652 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005653 else if (!diffs)
5654 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005655 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005656 maclen);
5657 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005658 }
David Miller07d78362018-06-24 14:14:02 +09005659
5660 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005661}
5662
Jerry Chu299603e82013-12-11 20:53:45 -08005663static void skb_gro_reset_offset(struct sk_buff *skb)
5664{
5665 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5666 const skb_frag_t *frag0 = &pinfo->frags[0];
5667
5668 NAPI_GRO_CB(skb)->data_offset = 0;
5669 NAPI_GRO_CB(skb)->frag0 = NULL;
5670 NAPI_GRO_CB(skb)->frag0_len = 0;
5671
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005672 if (!skb_headlen(skb) && pinfo->nr_frags &&
Jerry Chu299603e82013-12-11 20:53:45 -08005673 !PageHighMem(skb_frag_page(frag0))) {
5674 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005675 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5676 skb_frag_size(frag0),
5677 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005678 }
5679}
5680
Eric Dumazeta50e2332014-03-29 21:28:21 -07005681static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5682{
5683 struct skb_shared_info *pinfo = skb_shinfo(skb);
5684
5685 BUG_ON(skb->end - skb->tail < grow);
5686
5687 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5688
5689 skb->data_len -= grow;
5690 skb->tail += grow;
5691
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07005692 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005693 skb_frag_size_sub(&pinfo->frags[0], grow);
5694
5695 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5696 skb_frag_unref(skb, 0);
5697 memmove(pinfo->frags, pinfo->frags + 1,
5698 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5699 }
5700}
5701
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005702static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005703{
Li RongQing6312fe72018-07-05 14:34:32 +08005704 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005705
Li RongQing6312fe72018-07-05 14:34:32 +08005706 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005707
Li RongQing6312fe72018-07-05 14:34:32 +08005708 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005709 * impossible.
5710 */
5711 if (WARN_ON_ONCE(!oldest))
5712 return;
5713
Li RongQingd9f37d02018-07-13 14:41:36 +08005714 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5715 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005716 */
David S. Millerece23712018-10-28 10:35:12 -07005717 skb_list_del_init(oldest);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005718 napi_gro_complete(napi, oldest);
David Miller07d78362018-06-24 14:14:02 +09005719}
5720
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005721INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5722 struct sk_buff *));
5723INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5724 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005725static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005726{
Li RongQing6312fe72018-07-05 14:34:32 +08005727 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005728 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005729 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005730 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005731 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005732 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005733 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005734 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005735 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005736
David S. Millerb5cdae32017-04-18 15:36:58 -04005737 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005738 goto normal;
5739
David Miller07d78362018-06-24 14:14:02 +09005740 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005741
Herbert Xud565b0a2008-12-15 23:38:52 -08005742 rcu_read_lock();
5743 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005744 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005745 continue;
5746
Herbert Xu86911732009-01-29 14:19:50 +00005747 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005748 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005749 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005750 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005751 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005752 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005753 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005754 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005755 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005756 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005757
Tom Herbert662880f2014-08-27 21:26:56 -07005758 /* Setup for GRO checksum validation */
5759 switch (skb->ip_summed) {
5760 case CHECKSUM_COMPLETE:
5761 NAPI_GRO_CB(skb)->csum = skb->csum;
5762 NAPI_GRO_CB(skb)->csum_valid = 1;
5763 NAPI_GRO_CB(skb)->csum_cnt = 0;
5764 break;
5765 case CHECKSUM_UNNECESSARY:
5766 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5767 NAPI_GRO_CB(skb)->csum_valid = 0;
5768 break;
5769 default:
5770 NAPI_GRO_CB(skb)->csum_cnt = 0;
5771 NAPI_GRO_CB(skb)->csum_valid = 0;
5772 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005773
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005774 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5775 ipv6_gro_receive, inet_gro_receive,
5776 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005777 break;
5778 }
5779 rcu_read_unlock();
5780
5781 if (&ptype->list == head)
5782 goto normal;
5783
Masahiro Yamada45586c72020-02-03 17:37:45 -08005784 if (PTR_ERR(pp) == -EINPROGRESS) {
Steffen Klassert25393d32017-02-15 09:39:44 +01005785 ret = GRO_CONSUMED;
5786 goto ok;
5787 }
5788
Herbert Xu0da2afd52008-12-26 14:57:42 -08005789 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005790 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005791
Herbert Xud565b0a2008-12-15 23:38:52 -08005792 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005793 skb_list_del_init(pp);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005794 napi_gro_complete(napi, pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005795 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005796 }
5797
Herbert Xu0da2afd52008-12-26 14:57:42 -08005798 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005799 goto ok;
5800
Eric Dumazet600adc12014-01-09 14:12:19 -08005801 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08005802 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08005803
Li RongQing6312fe72018-07-05 14:34:32 +08005804 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005805 gro_flush_oldest(napi, gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08005806 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08005807 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08005808 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005809 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005810 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07005811 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00005812 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09005813 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005814 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08005815
Herbert Xuad0f99042009-02-01 01:24:55 -08005816pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005817 grow = skb_gro_offset(skb) - skb_headlen(skb);
5818 if (grow > 0)
5819 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08005820ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08005821 if (napi->gro_hash[hash].count) {
5822 if (!test_bit(hash, &napi->gro_bitmask))
5823 __set_bit(hash, &napi->gro_bitmask);
5824 } else if (test_bit(hash, &napi->gro_bitmask)) {
5825 __clear_bit(hash, &napi->gro_bitmask);
5826 }
5827
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005828 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005829
5830normal:
Herbert Xuad0f99042009-02-01 01:24:55 -08005831 ret = GRO_NORMAL;
5832 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08005833}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005834
Jerry Chubf5a7552014-01-07 10:23:19 -08005835struct packet_offload *gro_find_receive_by_type(__be16 type)
5836{
5837 struct list_head *offload_head = &offload_base;
5838 struct packet_offload *ptype;
5839
5840 list_for_each_entry_rcu(ptype, offload_head, list) {
5841 if (ptype->type != type || !ptype->callbacks.gro_receive)
5842 continue;
5843 return ptype;
5844 }
5845 return NULL;
5846}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005847EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08005848
5849struct packet_offload *gro_find_complete_by_type(__be16 type)
5850{
5851 struct list_head *offload_head = &offload_base;
5852 struct packet_offload *ptype;
5853
5854 list_for_each_entry_rcu(ptype, offload_head, list) {
5855 if (ptype->type != type || !ptype->callbacks.gro_complete)
5856 continue;
5857 return ptype;
5858 }
5859 return NULL;
5860}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005861EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005862
Michal Kubečeke44699d2017-06-29 11:13:36 +02005863static void napi_skb_free_stolen_head(struct sk_buff *skb)
5864{
5865 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005866 skb_ext_put(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02005867 kmem_cache_free(skbuff_head_cache, skb);
5868}
5869
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005870static gro_result_t napi_skb_finish(struct napi_struct *napi,
5871 struct sk_buff *skb,
5872 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08005873{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005874 switch (ret) {
5875 case GRO_NORMAL:
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005876 gro_normal_one(napi, skb);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005877 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005878
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005879 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08005880 kfree_skb(skb);
5881 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005882
Eric Dumazetdaa86542012-04-19 07:07:40 +00005883 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02005884 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5885 napi_skb_free_stolen_head(skb);
5886 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00005887 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00005888 break;
5889
Ben Hutchings5b252f02009-10-29 07:17:09 +00005890 case GRO_HELD:
5891 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005892 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005893 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005894 }
5895
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005896 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005897}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005898
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005899gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005900{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005901 gro_result_t ret;
5902
Eric Dumazet93f93a42015-11-18 06:30:59 -08005903 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005904 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00005905
Eric Dumazeta50e2332014-03-29 21:28:21 -07005906 skb_gro_reset_offset(skb);
5907
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005908 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005909 trace_napi_gro_receive_exit(ret);
5910
5911 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005912}
5913EXPORT_SYMBOL(napi_gro_receive);
5914
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00005915static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005916{
Eric Dumazet93a35f52014-10-23 06:30:30 -07005917 if (unlikely(skb->pfmemalloc)) {
5918 consume_skb(skb);
5919 return;
5920 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005921 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00005922 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5923 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01005924 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08005925 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08005926 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08005927
5928 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5929 skb->pkt_type = PACKET_HOST;
5930
Jerry Chuc3caf112014-07-14 15:54:46 -07005931 skb->encapsulation = 0;
5932 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07005933 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Florian Westphal174e2382019-09-26 20:37:05 +02005934 skb_ext_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005935
5936 napi->skb = skb;
5937}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005938
Herbert Xu76620aa2009-04-16 02:02:07 -07005939struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005940{
Herbert Xu5d38a072009-01-04 16:13:40 -08005941 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005942
5943 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005944 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005945 if (skb) {
5946 napi->skb = skb;
5947 skb_mark_napi_id(skb, napi);
5948 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005949 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005950 return skb;
5951}
Herbert Xu76620aa2009-04-16 02:02:07 -07005952EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005953
Eric Dumazeta50e2332014-03-29 21:28:21 -07005954static gro_result_t napi_frags_finish(struct napi_struct *napi,
5955 struct sk_buff *skb,
5956 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005957{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005958 switch (ret) {
5959 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005960 case GRO_HELD:
5961 __skb_push(skb, ETH_HLEN);
5962 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01005963 if (ret == GRO_NORMAL)
5964 gro_normal_one(napi, skb);
Herbert Xu86911732009-01-29 14:19:50 +00005965 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005966
5967 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005968 napi_reuse_skb(napi, skb);
5969 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005970
Michal Kubečeke44699d2017-06-29 11:13:36 +02005971 case GRO_MERGED_FREE:
5972 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5973 napi_skb_free_stolen_head(skb);
5974 else
5975 napi_reuse_skb(napi, skb);
5976 break;
5977
Ben Hutchings5b252f02009-10-29 07:17:09 +00005978 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005979 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005980 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005981 }
5982
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005983 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005984}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005985
Eric Dumazeta50e2332014-03-29 21:28:21 -07005986/* Upper GRO stack assumes network header starts at gro_offset=0
5987 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5988 * We copy ethernet header into skb->data to have a common layout.
5989 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005990static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005991{
Herbert Xu76620aa2009-04-16 02:02:07 -07005992 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005993 const struct ethhdr *eth;
5994 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005995
5996 napi->skb = NULL;
5997
Eric Dumazeta50e2332014-03-29 21:28:21 -07005998 skb_reset_mac_header(skb);
5999 skb_gro_reset_offset(skb);
6000
Eric Dumazeta50e2332014-03-29 21:28:21 -07006001 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6002 eth = skb_gro_header_slow(skb, hlen, 0);
6003 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04006004 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6005 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006006 napi_reuse_skb(napi, skb);
6007 return NULL;
6008 }
6009 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07006010 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006011 gro_pull_from_frag0(skb, hlen);
6012 NAPI_GRO_CB(skb)->frag0 += hlen;
6013 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07006014 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07006015 __skb_pull(skb, hlen);
6016
6017 /*
6018 * This works because the only protocols we care about don't require
6019 * special handling.
6020 * We'll fix it up properly in napi_frags_finish()
6021 */
6022 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07006023
Herbert Xu76620aa2009-04-16 02:02:07 -07006024 return skb;
6025}
Herbert Xu76620aa2009-04-16 02:02:07 -07006026
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006027gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07006028{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006029 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07006030 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006031
6032 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006033 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08006034
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006035 trace_napi_gro_frags_entry(skb);
6036
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006037 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6038 trace_napi_gro_frags_exit(ret);
6039
6040 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006041}
6042EXPORT_SYMBOL(napi_gro_frags);
6043
Tom Herbert573e8fc2014-08-22 13:33:47 -07006044/* Compute the checksum from gro_offset and return the folded value
6045 * after adding in any pseudo checksum.
6046 */
6047__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6048{
6049 __wsum wsum;
6050 __sum16 sum;
6051
6052 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6053
6054 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6055 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006056 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006057 if (likely(!sum)) {
6058 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6059 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006060 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006061 }
6062
6063 NAPI_GRO_CB(skb)->csum = wsum;
6064 NAPI_GRO_CB(skb)->csum_valid = 1;
6065
6066 return sum;
6067}
6068EXPORT_SYMBOL(__skb_gro_checksum_complete);
6069
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306070static void net_rps_send_ipi(struct softnet_data *remsd)
6071{
6072#ifdef CONFIG_RPS
6073 while (remsd) {
6074 struct softnet_data *next = remsd->rps_ipi_next;
6075
6076 if (cpu_online(remsd->cpu))
6077 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6078 remsd = next;
6079 }
6080#endif
6081}
6082
Eric Dumazete326bed2010-04-22 00:22:45 -07006083/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006084 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006085 * Note: called with local irq disabled, but exits with local irq enabled.
6086 */
6087static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6088{
6089#ifdef CONFIG_RPS
6090 struct softnet_data *remsd = sd->rps_ipi_list;
6091
6092 if (remsd) {
6093 sd->rps_ipi_list = NULL;
6094
6095 local_irq_enable();
6096
6097 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306098 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006099 } else
6100#endif
6101 local_irq_enable();
6102}
6103
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006104static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6105{
6106#ifdef CONFIG_RPS
6107 return sd->rps_ipi_list != NULL;
6108#else
6109 return false;
6110#endif
6111}
6112
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006113static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006115 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006116 bool again = true;
6117 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
Eric Dumazete326bed2010-04-22 00:22:45 -07006119 /* Check if we have pending ipi, its better to send them now,
6120 * not waiting net_rx_action() end.
6121 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006122 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006123 local_irq_disable();
6124 net_rps_action_and_irq_enable(sd);
6125 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006126
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006127 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006128 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Changli Gao6e7676c2010-04-27 15:07:33 -07006131 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006132 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006133 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006134 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006135 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006136 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006137 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006138
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006141 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006142 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006143 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006144 /*
6145 * Inline a custom version of __napi_complete().
6146 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006147 * and NAPI_STATE_SCHED is the only possible flag set
6148 * on backlog.
6149 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006150 * and we dont need an smp_mb() memory barrier.
6151 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006152 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006153 again = false;
6154 } else {
6155 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6156 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006157 }
6158 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006159 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006162 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163}
6164
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006165/**
6166 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006167 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006168 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006169 * The entry's receive function will be scheduled to run.
6170 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006171 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006172void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006173{
6174 unsigned long flags;
6175
6176 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006177 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006178 local_irq_restore(flags);
6179}
6180EXPORT_SYMBOL(__napi_schedule);
6181
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006182/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006183 * napi_schedule_prep - check if napi can be scheduled
6184 * @n: napi context
6185 *
6186 * Test if NAPI routine is already running, and if not mark
6187 * it as running. This is used as a condition variable
6188 * insure only one NAPI poll instance runs. We also make
6189 * sure there is no pending NAPI disable.
6190 */
6191bool napi_schedule_prep(struct napi_struct *n)
6192{
6193 unsigned long val, new;
6194
6195 do {
6196 val = READ_ONCE(n->state);
6197 if (unlikely(val & NAPIF_STATE_DISABLE))
6198 return false;
6199 new = val | NAPIF_STATE_SCHED;
6200
6201 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6202 * This was suggested by Alexander Duyck, as compiler
6203 * emits better code than :
6204 * if (val & NAPIF_STATE_SCHED)
6205 * new |= NAPIF_STATE_MISSED;
6206 */
6207 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6208 NAPIF_STATE_MISSED;
6209 } while (cmpxchg(&n->state, val, new) != val);
6210
6211 return !(val & NAPIF_STATE_SCHED);
6212}
6213EXPORT_SYMBOL(napi_schedule_prep);
6214
6215/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006216 * __napi_schedule_irqoff - schedule for receive
6217 * @n: entry to schedule
6218 *
6219 * Variant of __napi_schedule() assuming hard irqs are masked
6220 */
6221void __napi_schedule_irqoff(struct napi_struct *n)
6222{
6223 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6224}
6225EXPORT_SYMBOL(__napi_schedule_irqoff);
6226
Eric Dumazet364b6052016-11-15 10:15:13 -08006227bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006228{
Eric Dumazet39e6c822017-02-28 10:34:50 -08006229 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08006230
6231 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006232 * 1) Don't let napi dequeue from the cpu poll list
6233 * just in case its running on a different cpu.
6234 * 2) If we are busy polling, do nothing here, we have
6235 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006236 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006237 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6238 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006239 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006240
Li RongQingd9f37d02018-07-13 14:41:36 +08006241 if (n->gro_bitmask) {
Eric Dumazet3b47d302014-11-06 21:09:44 -08006242 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006243
Eric Dumazet3b47d302014-11-06 21:09:44 -08006244 if (work_done)
6245 timeout = n->dev->gro_flush_timeout;
6246
Paolo Abeni605108a2018-11-21 18:21:35 +01006247 /* When the NAPI instance uses a timeout and keeps postponing
6248 * it, we need to bound somehow the time packets are kept in
6249 * the GRO layer
6250 */
6251 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006252 if (timeout)
6253 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6254 HRTIMER_MODE_REL_PINNED);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006255 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006256
6257 gro_normal_list(n);
6258
Eric Dumazet02c16022017-02-04 15:25:02 -08006259 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006260 /* If n->poll_list is not empty, we need to mask irqs */
6261 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006262 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006263 local_irq_restore(flags);
6264 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006265
6266 do {
6267 val = READ_ONCE(n->state);
6268
6269 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6270
6271 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6272
6273 /* If STATE_MISSED was set, leave STATE_SCHED set,
6274 * because we will call napi->poll() one more time.
6275 * This C code was suggested by Alexander Duyck to help gcc.
6276 */
6277 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6278 NAPIF_STATE_SCHED;
6279 } while (cmpxchg(&n->state, val, new) != val);
6280
6281 if (unlikely(val & NAPIF_STATE_MISSED)) {
6282 __napi_schedule(n);
6283 return false;
6284 }
6285
Eric Dumazet364b6052016-11-15 10:15:13 -08006286 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006287}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006288EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006289
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006290/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006291static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006292{
6293 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6294 struct napi_struct *napi;
6295
6296 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6297 if (napi->napi_id == napi_id)
6298 return napi;
6299
6300 return NULL;
6301}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006302
6303#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006304
Eric Dumazetce6aea92015-11-18 06:30:54 -08006305#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006306
6307static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6308{
6309 int rc;
6310
Eric Dumazet39e6c822017-02-28 10:34:50 -08006311 /* Busy polling means there is a high chance device driver hard irq
6312 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6313 * set in napi_schedule_prep().
6314 * Since we are about to call napi->poll() once more, we can safely
6315 * clear NAPI_STATE_MISSED.
6316 *
6317 * Note: x86 could use a single "lock and ..." instruction
6318 * to perform these two clear_bit()
6319 */
6320 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006321 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6322
6323 local_bh_disable();
6324
6325 /* All we really want here is to re-enable device interrupts.
6326 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6327 */
6328 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006329 /* We can't gro_normal_list() here, because napi->poll() might have
6330 * rearmed the napi (napi_complete_done()) in which case it could
6331 * already be running on another CPU.
6332 */
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006333 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006334 netpoll_poll_unlock(have_poll_lock);
Edward Cree323ebb62019-08-06 14:53:55 +01006335 if (rc == BUSY_POLL_BUDGET) {
6336 /* As the whole budget was spent, we still own the napi so can
6337 * safely handle the rx_list.
6338 */
6339 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006340 __napi_schedule(napi);
Edward Cree323ebb62019-08-06 14:53:55 +01006341 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006342 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006343}
6344
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006345void napi_busy_loop(unsigned int napi_id,
6346 bool (*loop_end)(void *, unsigned long),
6347 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006348{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006349 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006350 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006351 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006352 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006353
6354restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006355 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006356
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006357 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006358
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006359 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006360 if (!napi)
6361 goto out;
6362
Eric Dumazet217f6972016-11-15 10:15:11 -08006363 preempt_disable();
6364 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006365 int work = 0;
6366
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006367 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006368 if (!napi_poll) {
6369 unsigned long val = READ_ONCE(napi->state);
6370
6371 /* If multiple threads are competing for this napi,
6372 * we avoid dirtying napi->state as much as we can.
6373 */
6374 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6375 NAPIF_STATE_IN_BUSY_POLL))
6376 goto count;
6377 if (cmpxchg(&napi->state, val,
6378 val | NAPIF_STATE_IN_BUSY_POLL |
6379 NAPIF_STATE_SCHED) != val)
6380 goto count;
6381 have_poll_lock = netpoll_poll_lock(napi);
6382 napi_poll = napi->poll;
6383 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006384 work = napi_poll(napi, BUSY_POLL_BUDGET);
6385 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006386 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006387count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006388 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006389 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006390 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006391 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006392
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006393 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006394 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006395
Eric Dumazet217f6972016-11-15 10:15:11 -08006396 if (unlikely(need_resched())) {
6397 if (napi_poll)
6398 busy_poll_stop(napi, have_poll_lock);
6399 preempt_enable();
6400 rcu_read_unlock();
6401 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006402 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006403 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006404 goto restart;
6405 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006406 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006407 }
6408 if (napi_poll)
6409 busy_poll_stop(napi, have_poll_lock);
6410 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006411out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006412 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006413}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006414EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006415
6416#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006417
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006418static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006419{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08006420 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6421 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006422 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006423
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006424 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006425
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006426 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006427 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006428 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6429 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006430 } while (napi_by_id(napi_gen_id));
6431 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006432
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006433 hlist_add_head_rcu(&napi->napi_hash_node,
6434 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006435
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006436 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006437}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006438
6439/* Warning : caller is responsible to make sure rcu grace period
6440 * is respected before freeing memory containing @napi
6441 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08006442bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006443{
Eric Dumazet34cbe272015-11-18 06:31:02 -08006444 bool rcu_sync_needed = false;
6445
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006446 spin_lock(&napi_hash_lock);
6447
Eric Dumazet34cbe272015-11-18 06:31:02 -08006448 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6449 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006450 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006451 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006452 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006453 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006454}
6455EXPORT_SYMBOL_GPL(napi_hash_del);
6456
Eric Dumazet3b47d302014-11-06 21:09:44 -08006457static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6458{
6459 struct napi_struct *napi;
6460
6461 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006462
6463 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6464 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6465 */
Li RongQingd9f37d02018-07-13 14:41:36 +08006466 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006467 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6468 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006469
6470 return HRTIMER_NORESTART;
6471}
6472
David S. Miller7c4ec742018-07-20 23:37:55 -07006473static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006474{
David Miller07d78362018-06-24 14:14:02 +09006475 int i;
6476
Li RongQing6312fe72018-07-05 14:34:32 +08006477 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6478 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6479 napi->gro_hash[i].count = 0;
6480 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006481 napi->gro_bitmask = 0;
6482}
6483
6484void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6485 int (*poll)(struct napi_struct *, int), int weight)
6486{
6487 INIT_LIST_HEAD(&napi->poll_list);
6488 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6489 napi->timer.function = napi_watchdog;
6490 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006491 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006492 INIT_LIST_HEAD(&napi->rx_list);
6493 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006494 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006495 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006496 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6497 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006498 napi->weight = weight;
6499 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08006500 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006501#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006502 napi->poll_owner = -1;
6503#endif
6504 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006505 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006506}
6507EXPORT_SYMBOL(netif_napi_add);
6508
Eric Dumazet3b47d302014-11-06 21:09:44 -08006509void napi_disable(struct napi_struct *n)
6510{
6511 might_sleep();
6512 set_bit(NAPI_STATE_DISABLE, &n->state);
6513
6514 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6515 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006516 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6517 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006518
6519 hrtimer_cancel(&n->timer);
6520
6521 clear_bit(NAPI_STATE_DISABLE, &n->state);
6522}
6523EXPORT_SYMBOL(napi_disable);
6524
David Miller07d78362018-06-24 14:14:02 +09006525static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006526{
David Miller07d78362018-06-24 14:14:02 +09006527 int i;
David Millerd4546c22018-06-24 14:13:49 +09006528
David Miller07d78362018-06-24 14:14:02 +09006529 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6530 struct sk_buff *skb, *n;
6531
Li RongQing6312fe72018-07-05 14:34:32 +08006532 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006533 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006534 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006535 }
David Millerd4546c22018-06-24 14:13:49 +09006536}
6537
Eric Dumazet93d05d42015-11-18 06:31:03 -08006538/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08006539void netif_napi_del(struct napi_struct *napi)
6540{
Eric Dumazet93d05d42015-11-18 06:31:03 -08006541 might_sleep();
6542 if (napi_hash_del(napi))
6543 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08006544 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006545 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006546
David Miller07d78362018-06-24 14:14:02 +09006547 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006548 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006549}
6550EXPORT_SYMBOL(netif_napi_del);
6551
Herbert Xu726ce702014-12-21 07:16:21 +11006552static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6553{
6554 void *have;
6555 int work, weight;
6556
6557 list_del_init(&n->poll_list);
6558
6559 have = netpoll_poll_lock(n);
6560
6561 weight = n->weight;
6562
6563 /* This NAPI_STATE_SCHED test is for avoiding a race
6564 * with netpoll's poll_napi(). Only the entity which
6565 * obtains the lock and sees NAPI_STATE_SCHED set will
6566 * actually make the ->poll() call. Therefore we avoid
6567 * accidentally calling ->poll() when NAPI is not scheduled.
6568 */
6569 work = 0;
6570 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6571 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006572 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006573 }
6574
6575 WARN_ON_ONCE(work > weight);
6576
6577 if (likely(work < weight))
6578 goto out_unlock;
6579
6580 /* Drivers must not modify the NAPI state if they
6581 * consume the entire weight. In such cases this code
6582 * still "owns" the NAPI instance and therefore can
6583 * move the instance around on the list at-will.
6584 */
6585 if (unlikely(napi_disable_pending(n))) {
6586 napi_complete(n);
6587 goto out_unlock;
6588 }
6589
Li RongQingd9f37d02018-07-13 14:41:36 +08006590 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006591 /* flush too old packets
6592 * If HZ < 1000, flush all packets.
6593 */
6594 napi_gro_flush(n, HZ >= 1000);
6595 }
6596
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006597 gro_normal_list(n);
6598
Herbert Xu001ce542014-12-21 07:16:22 +11006599 /* Some drivers may have called napi_schedule
6600 * prior to exhausting their budget.
6601 */
6602 if (unlikely(!list_empty(&n->poll_list))) {
6603 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6604 n->dev ? n->dev->name : "backlog");
6605 goto out_unlock;
6606 }
6607
Herbert Xu726ce702014-12-21 07:16:21 +11006608 list_add_tail(&n->poll_list, repoll);
6609
6610out_unlock:
6611 netpoll_poll_unlock(have);
6612
6613 return work;
6614}
6615
Emese Revfy0766f782016-06-20 20:42:34 +02006616static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006618 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006619 unsigned long time_limit = jiffies +
6620 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006621 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006622 LIST_HEAD(list);
6623 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006624
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006626 list_splice_init(&sd->poll_list, &list);
6627 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006629 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006630 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006632 if (list_empty(&list)) {
6633 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006634 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006635 break;
6636 }
6637
Herbert Xu6bd373e2014-12-21 07:16:24 +11006638 n = list_first_entry(&list, struct napi_struct, poll_list);
6639 budget -= napi_poll(n, &repoll);
6640
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006641 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006642 * Allow this to run for 2 jiffies since which will allow
6643 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006644 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006645 if (unlikely(budget <= 0 ||
6646 time_after_eq(jiffies, time_limit))) {
6647 sd->time_squeeze++;
6648 break;
6649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006651
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006652 local_irq_disable();
6653
6654 list_splice_tail_init(&sd->poll_list, &list);
6655 list_splice_tail(&repoll, &list);
6656 list_splice(&list, &sd->poll_list);
6657 if (!list_empty(&sd->poll_list))
6658 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6659
Eric Dumazete326bed2010-04-22 00:22:45 -07006660 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006661out:
6662 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663}
6664
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006665struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006666 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006667
6668 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006669 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006670
Taehee Yoo32b6d342019-10-21 18:47:56 +00006671 /* lookup ignore flag */
6672 bool ignore;
6673
Veaceslav Falico5d261912013-08-28 23:25:05 +02006674 /* counter for the number of times this device was added to us */
6675 u16 ref_nr;
6676
Veaceslav Falico402dae92013-09-25 09:20:09 +02006677 /* private field for the users */
6678 void *private;
6679
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006680 struct list_head list;
6681 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006682};
6683
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006684static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006685 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006686{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006687 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006688
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006689 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006690 if (adj->dev == adj_dev)
6691 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006692 }
6693 return NULL;
6694}
6695
Taehee Yoo32b6d342019-10-21 18:47:56 +00006696static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data)
David Ahernf1170fd2016-10-17 19:15:51 -07006697{
6698 struct net_device *dev = data;
6699
6700 return upper_dev == dev;
6701}
6702
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006703/**
6704 * netdev_has_upper_dev - Check if device is linked to an upper device
6705 * @dev: device
6706 * @upper_dev: upper device to check
6707 *
6708 * Find out if a device is linked to specified upper device and return true
6709 * in case it is. Note that this checks only immediate upper device,
6710 * not through a complete stack of devices. The caller must hold the RTNL lock.
6711 */
6712bool netdev_has_upper_dev(struct net_device *dev,
6713 struct net_device *upper_dev)
6714{
6715 ASSERT_RTNL();
6716
Taehee Yoo32b6d342019-10-21 18:47:56 +00006717 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
David Ahernf1170fd2016-10-17 19:15:51 -07006718 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006719}
6720EXPORT_SYMBOL(netdev_has_upper_dev);
6721
6722/**
David Ahern1a3f0602016-10-17 19:15:44 -07006723 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6724 * @dev: device
6725 * @upper_dev: upper device to check
6726 *
6727 * Find out if a device is linked to specified upper device and return true
6728 * in case it is. Note that this checks the entire upper device chain.
6729 * The caller must hold rcu lock.
6730 */
6731
David Ahern1a3f0602016-10-17 19:15:44 -07006732bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6733 struct net_device *upper_dev)
6734{
Taehee Yoo32b6d342019-10-21 18:47:56 +00006735 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
David Ahern1a3f0602016-10-17 19:15:44 -07006736 upper_dev);
6737}
6738EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6739
6740/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006741 * netdev_has_any_upper_dev - Check if device is linked to some device
6742 * @dev: device
6743 *
6744 * Find out if a device is linked to an upper device and return true in case
6745 * it is. The caller must hold the RTNL lock.
6746 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006747bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006748{
6749 ASSERT_RTNL();
6750
David Ahernf1170fd2016-10-17 19:15:51 -07006751 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006752}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006753EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006754
6755/**
6756 * netdev_master_upper_dev_get - Get master upper device
6757 * @dev: device
6758 *
6759 * Find a master upper device and return pointer to it or NULL in case
6760 * it's not there. The caller must hold the RTNL lock.
6761 */
6762struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6763{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006764 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006765
6766 ASSERT_RTNL();
6767
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006768 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006769 return NULL;
6770
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006771 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006772 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006773 if (likely(upper->master))
6774 return upper->dev;
6775 return NULL;
6776}
6777EXPORT_SYMBOL(netdev_master_upper_dev_get);
6778
Taehee Yoo32b6d342019-10-21 18:47:56 +00006779static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6780{
6781 struct netdev_adjacent *upper;
6782
6783 ASSERT_RTNL();
6784
6785 if (list_empty(&dev->adj_list.upper))
6786 return NULL;
6787
6788 upper = list_first_entry(&dev->adj_list.upper,
6789 struct netdev_adjacent, list);
6790 if (likely(upper->master) && !upper->ignore)
6791 return upper->dev;
6792 return NULL;
6793}
6794
David Ahern0f524a82016-10-17 19:15:52 -07006795/**
6796 * netdev_has_any_lower_dev - Check if device is linked to some device
6797 * @dev: device
6798 *
6799 * Find out if a device is linked to a lower device and return true in case
6800 * it is. The caller must hold the RTNL lock.
6801 */
6802static bool netdev_has_any_lower_dev(struct net_device *dev)
6803{
6804 ASSERT_RTNL();
6805
6806 return !list_empty(&dev->adj_list.lower);
6807}
6808
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02006809void *netdev_adjacent_get_private(struct list_head *adj_list)
6810{
6811 struct netdev_adjacent *adj;
6812
6813 adj = list_entry(adj_list, struct netdev_adjacent, list);
6814
6815 return adj->private;
6816}
6817EXPORT_SYMBOL(netdev_adjacent_get_private);
6818
Veaceslav Falico31088a12013-09-25 09:20:12 +02006819/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04006820 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6821 * @dev: device
6822 * @iter: list_head ** of the current position
6823 *
6824 * Gets the next device from the dev's upper list, starting from iter
6825 * position. The caller must hold RCU read lock.
6826 */
6827struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6828 struct list_head **iter)
6829{
6830 struct netdev_adjacent *upper;
6831
6832 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6833
6834 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6835
6836 if (&upper->list == &dev->adj_list.upper)
6837 return NULL;
6838
6839 *iter = &upper->list;
6840
6841 return upper->dev;
6842}
6843EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6844
Taehee Yoo32b6d342019-10-21 18:47:56 +00006845static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6846 struct list_head **iter,
6847 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00006848{
6849 struct netdev_adjacent *upper;
6850
6851 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6852
6853 if (&upper->list == &dev->adj_list.upper)
6854 return NULL;
6855
6856 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006857 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006858
6859 return upper->dev;
6860}
6861
David Ahern1a3f0602016-10-17 19:15:44 -07006862static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6863 struct list_head **iter)
6864{
6865 struct netdev_adjacent *upper;
6866
6867 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6868
6869 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6870
6871 if (&upper->list == &dev->adj_list.upper)
6872 return NULL;
6873
6874 *iter = &upper->list;
6875
6876 return upper->dev;
6877}
6878
Taehee Yoo32b6d342019-10-21 18:47:56 +00006879static int __netdev_walk_all_upper_dev(struct net_device *dev,
6880 int (*fn)(struct net_device *dev,
6881 void *data),
6882 void *data)
Taehee Yoo5343da42019-10-21 18:47:50 +00006883{
6884 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6885 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6886 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006887 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006888
6889 now = dev;
6890 iter = &dev->adj_list.upper;
6891
6892 while (1) {
6893 if (now != dev) {
6894 ret = fn(now, data);
6895 if (ret)
6896 return ret;
6897 }
6898
6899 next = NULL;
6900 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00006901 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00006902 if (!udev)
6903 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006904 if (ignore)
6905 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00006906
6907 next = udev;
6908 niter = &udev->adj_list.upper;
6909 dev_stack[cur] = now;
6910 iter_stack[cur++] = iter;
6911 break;
6912 }
6913
6914 if (!next) {
6915 if (!cur)
6916 return 0;
6917 next = dev_stack[--cur];
6918 niter = iter_stack[cur];
6919 }
6920
6921 now = next;
6922 iter = niter;
6923 }
6924
6925 return 0;
6926}
6927
David Ahern1a3f0602016-10-17 19:15:44 -07006928int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6929 int (*fn)(struct net_device *dev,
6930 void *data),
6931 void *data)
6932{
Taehee Yoo5343da42019-10-21 18:47:50 +00006933 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6934 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6935 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07006936
Taehee Yoo5343da42019-10-21 18:47:50 +00006937 now = dev;
6938 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07006939
Taehee Yoo5343da42019-10-21 18:47:50 +00006940 while (1) {
6941 if (now != dev) {
6942 ret = fn(now, data);
6943 if (ret)
6944 return ret;
6945 }
6946
6947 next = NULL;
6948 while (1) {
6949 udev = netdev_next_upper_dev_rcu(now, &iter);
6950 if (!udev)
6951 break;
6952
6953 next = udev;
6954 niter = &udev->adj_list.upper;
6955 dev_stack[cur] = now;
6956 iter_stack[cur++] = iter;
6957 break;
6958 }
6959
6960 if (!next) {
6961 if (!cur)
6962 return 0;
6963 next = dev_stack[--cur];
6964 niter = iter_stack[cur];
6965 }
6966
6967 now = next;
6968 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07006969 }
6970
6971 return 0;
6972}
6973EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6974
Taehee Yoo32b6d342019-10-21 18:47:56 +00006975static bool __netdev_has_upper_dev(struct net_device *dev,
6976 struct net_device *upper_dev)
6977{
6978 ASSERT_RTNL();
6979
6980 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
6981 upper_dev);
6982}
6983
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006984/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02006985 * netdev_lower_get_next_private - Get the next ->private from the
6986 * lower neighbour list
6987 * @dev: device
6988 * @iter: list_head ** of the current position
6989 *
6990 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6991 * list, starting from iter position. The caller must hold either hold the
6992 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006993 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02006994 */
6995void *netdev_lower_get_next_private(struct net_device *dev,
6996 struct list_head **iter)
6997{
6998 struct netdev_adjacent *lower;
6999
7000 lower = list_entry(*iter, struct netdev_adjacent, list);
7001
7002 if (&lower->list == &dev->adj_list.lower)
7003 return NULL;
7004
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007005 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007006
7007 return lower->private;
7008}
7009EXPORT_SYMBOL(netdev_lower_get_next_private);
7010
7011/**
7012 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7013 * lower neighbour list, RCU
7014 * variant
7015 * @dev: device
7016 * @iter: list_head ** of the current position
7017 *
7018 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7019 * list, starting from iter position. The caller must hold RCU read lock.
7020 */
7021void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7022 struct list_head **iter)
7023{
7024 struct netdev_adjacent *lower;
7025
7026 WARN_ON_ONCE(!rcu_read_lock_held());
7027
7028 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7029
7030 if (&lower->list == &dev->adj_list.lower)
7031 return NULL;
7032
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007033 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007034
7035 return lower->private;
7036}
7037EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7038
7039/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007040 * netdev_lower_get_next - Get the next device from the lower neighbour
7041 * list
7042 * @dev: device
7043 * @iter: list_head ** of the current position
7044 *
7045 * Gets the next netdev_adjacent from the dev's lower neighbour
7046 * list, starting from iter position. The caller must hold RTNL lock or
7047 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007048 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007049 */
7050void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7051{
7052 struct netdev_adjacent *lower;
7053
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007054 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007055
7056 if (&lower->list == &dev->adj_list.lower)
7057 return NULL;
7058
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007059 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007060
7061 return lower->dev;
7062}
7063EXPORT_SYMBOL(netdev_lower_get_next);
7064
David Ahern1a3f0602016-10-17 19:15:44 -07007065static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7066 struct list_head **iter)
7067{
7068 struct netdev_adjacent *lower;
7069
David Ahern46b5ab12016-10-26 13:21:33 -07007070 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007071
7072 if (&lower->list == &dev->adj_list.lower)
7073 return NULL;
7074
David Ahern46b5ab12016-10-26 13:21:33 -07007075 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007076
7077 return lower->dev;
7078}
7079
Taehee Yoo32b6d342019-10-21 18:47:56 +00007080static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7081 struct list_head **iter,
7082 bool *ignore)
7083{
7084 struct netdev_adjacent *lower;
7085
7086 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7087
7088 if (&lower->list == &dev->adj_list.lower)
7089 return NULL;
7090
7091 *iter = &lower->list;
7092 *ignore = lower->ignore;
7093
7094 return lower->dev;
7095}
7096
David Ahern1a3f0602016-10-17 19:15:44 -07007097int netdev_walk_all_lower_dev(struct net_device *dev,
7098 int (*fn)(struct net_device *dev,
7099 void *data),
7100 void *data)
7101{
Taehee Yoo5343da42019-10-21 18:47:50 +00007102 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7103 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7104 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007105
Taehee Yoo5343da42019-10-21 18:47:50 +00007106 now = dev;
7107 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007108
Taehee Yoo5343da42019-10-21 18:47:50 +00007109 while (1) {
7110 if (now != dev) {
7111 ret = fn(now, data);
7112 if (ret)
7113 return ret;
7114 }
7115
7116 next = NULL;
7117 while (1) {
7118 ldev = netdev_next_lower_dev(now, &iter);
7119 if (!ldev)
7120 break;
7121
7122 next = ldev;
7123 niter = &ldev->adj_list.lower;
7124 dev_stack[cur] = now;
7125 iter_stack[cur++] = iter;
7126 break;
7127 }
7128
7129 if (!next) {
7130 if (!cur)
7131 return 0;
7132 next = dev_stack[--cur];
7133 niter = iter_stack[cur];
7134 }
7135
7136 now = next;
7137 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007138 }
7139
7140 return 0;
7141}
7142EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7143
Taehee Yoo32b6d342019-10-21 18:47:56 +00007144static int __netdev_walk_all_lower_dev(struct net_device *dev,
7145 int (*fn)(struct net_device *dev,
7146 void *data),
7147 void *data)
7148{
7149 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7150 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7151 int ret, cur = 0;
7152 bool ignore;
7153
7154 now = dev;
7155 iter = &dev->adj_list.lower;
7156
7157 while (1) {
7158 if (now != dev) {
7159 ret = fn(now, data);
7160 if (ret)
7161 return ret;
7162 }
7163
7164 next = NULL;
7165 while (1) {
7166 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7167 if (!ldev)
7168 break;
7169 if (ignore)
7170 continue;
7171
7172 next = ldev;
7173 niter = &ldev->adj_list.lower;
7174 dev_stack[cur] = now;
7175 iter_stack[cur++] = iter;
7176 break;
7177 }
7178
7179 if (!next) {
7180 if (!cur)
7181 return 0;
7182 next = dev_stack[--cur];
7183 niter = iter_stack[cur];
7184 }
7185
7186 now = next;
7187 iter = niter;
7188 }
7189
7190 return 0;
7191}
7192
Taehee Yoo7151aff2020-02-15 10:50:21 +00007193struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7194 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007195{
7196 struct netdev_adjacent *lower;
7197
7198 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7199 if (&lower->list == &dev->adj_list.lower)
7200 return NULL;
7201
7202 *iter = &lower->list;
7203
7204 return lower->dev;
7205}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007206EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007207
Taehee Yoo5343da42019-10-21 18:47:50 +00007208static u8 __netdev_upper_depth(struct net_device *dev)
7209{
7210 struct net_device *udev;
7211 struct list_head *iter;
7212 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007213 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007214
7215 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007216 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007217 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007218 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7219 if (ignore)
7220 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007221 if (max_depth < udev->upper_level)
7222 max_depth = udev->upper_level;
7223 }
7224
7225 return max_depth;
7226}
7227
7228static u8 __netdev_lower_depth(struct net_device *dev)
7229{
7230 struct net_device *ldev;
7231 struct list_head *iter;
7232 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007233 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007234
7235 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007236 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007237 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007238 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7239 if (ignore)
7240 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007241 if (max_depth < ldev->lower_level)
7242 max_depth = ldev->lower_level;
7243 }
7244
7245 return max_depth;
7246}
7247
7248static int __netdev_update_upper_level(struct net_device *dev, void *data)
7249{
7250 dev->upper_level = __netdev_upper_depth(dev) + 1;
7251 return 0;
7252}
7253
7254static int __netdev_update_lower_level(struct net_device *dev, void *data)
7255{
7256 dev->lower_level = __netdev_lower_depth(dev) + 1;
7257 return 0;
7258}
7259
David Ahern1a3f0602016-10-17 19:15:44 -07007260int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7261 int (*fn)(struct net_device *dev,
7262 void *data),
7263 void *data)
7264{
Taehee Yoo5343da42019-10-21 18:47:50 +00007265 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7266 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7267 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007268
Taehee Yoo5343da42019-10-21 18:47:50 +00007269 now = dev;
7270 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007271
Taehee Yoo5343da42019-10-21 18:47:50 +00007272 while (1) {
7273 if (now != dev) {
7274 ret = fn(now, data);
7275 if (ret)
7276 return ret;
7277 }
7278
7279 next = NULL;
7280 while (1) {
7281 ldev = netdev_next_lower_dev_rcu(now, &iter);
7282 if (!ldev)
7283 break;
7284
7285 next = ldev;
7286 niter = &ldev->adj_list.lower;
7287 dev_stack[cur] = now;
7288 iter_stack[cur++] = iter;
7289 break;
7290 }
7291
7292 if (!next) {
7293 if (!cur)
7294 return 0;
7295 next = dev_stack[--cur];
7296 niter = iter_stack[cur];
7297 }
7298
7299 now = next;
7300 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007301 }
7302
7303 return 0;
7304}
7305EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7306
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007307/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007308 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7309 * lower neighbour list, RCU
7310 * variant
7311 * @dev: device
7312 *
7313 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7314 * list. The caller must hold RCU read lock.
7315 */
7316void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7317{
7318 struct netdev_adjacent *lower;
7319
7320 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7321 struct netdev_adjacent, list);
7322 if (lower)
7323 return lower->private;
7324 return NULL;
7325}
7326EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7327
7328/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007329 * netdev_master_upper_dev_get_rcu - Get master upper device
7330 * @dev: device
7331 *
7332 * Find a master upper device and return pointer to it or NULL in case
7333 * it's not there. The caller must hold the RCU read lock.
7334 */
7335struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7336{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007337 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007338
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007339 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007340 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007341 if (upper && likely(upper->master))
7342 return upper->dev;
7343 return NULL;
7344}
7345EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7346
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307347static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007348 struct net_device *adj_dev,
7349 struct list_head *dev_list)
7350{
7351 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007352
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007353 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7354 "upper_%s" : "lower_%s", adj_dev->name);
7355 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7356 linkname);
7357}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307358static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007359 char *name,
7360 struct list_head *dev_list)
7361{
7362 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007363
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007364 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7365 "upper_%s" : "lower_%s", name);
7366 sysfs_remove_link(&(dev->dev.kobj), linkname);
7367}
7368
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007369static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7370 struct net_device *adj_dev,
7371 struct list_head *dev_list)
7372{
7373 return (dev_list == &dev->adj_list.upper ||
7374 dev_list == &dev->adj_list.lower) &&
7375 net_eq(dev_net(dev), dev_net(adj_dev));
7376}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007377
Veaceslav Falico5d261912013-08-28 23:25:05 +02007378static int __netdev_adjacent_dev_insert(struct net_device *dev,
7379 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007380 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007381 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007382{
7383 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007384 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007385
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007386 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007387
7388 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007389 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007390 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7391 dev->name, adj_dev->name, adj->ref_nr);
7392
Veaceslav Falico5d261912013-08-28 23:25:05 +02007393 return 0;
7394 }
7395
7396 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7397 if (!adj)
7398 return -ENOMEM;
7399
7400 adj->dev = adj_dev;
7401 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007402 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007403 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007404 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007405 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007406
David Ahern67b62f92016-10-17 19:15:53 -07007407 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7408 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007409
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007410 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007411 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007412 if (ret)
7413 goto free_adj;
7414 }
7415
Veaceslav Falico7863c052013-09-25 09:20:06 +02007416 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007417 if (master) {
7418 ret = sysfs_create_link(&(dev->dev.kobj),
7419 &(adj_dev->dev.kobj), "master");
7420 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007421 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007422
Veaceslav Falico7863c052013-09-25 09:20:06 +02007423 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007424 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007425 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007426 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007427
7428 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007429
Veaceslav Falico5831d662013-09-25 09:20:32 +02007430remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007431 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007432 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007433free_adj:
7434 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007435 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007436
7437 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007438}
7439
stephen hemminger1d143d92013-12-29 14:01:29 -08007440static void __netdev_adjacent_dev_remove(struct net_device *dev,
7441 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007442 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007443 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007444{
7445 struct netdev_adjacent *adj;
7446
David Ahern67b62f92016-10-17 19:15:53 -07007447 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7448 dev->name, adj_dev->name, ref_nr);
7449
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007450 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007451
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007452 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007453 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007454 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007455 WARN_ON(1);
7456 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007457 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007458
Andrew Collins93409032016-10-03 13:43:02 -06007459 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007460 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7461 dev->name, adj_dev->name, ref_nr,
7462 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007463 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007464 return;
7465 }
7466
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007467 if (adj->master)
7468 sysfs_remove_link(&(dev->dev.kobj), "master");
7469
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007470 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007471 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007472
Veaceslav Falico5d261912013-08-28 23:25:05 +02007473 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007474 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007475 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007476 dev_put(adj_dev);
7477 kfree_rcu(adj, rcu);
7478}
7479
stephen hemminger1d143d92013-12-29 14:01:29 -08007480static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7481 struct net_device *upper_dev,
7482 struct list_head *up_list,
7483 struct list_head *down_list,
7484 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007485{
7486 int ret;
7487
David Ahern790510d2016-10-17 19:15:43 -07007488 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007489 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007490 if (ret)
7491 return ret;
7492
David Ahern790510d2016-10-17 19:15:43 -07007493 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007494 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007495 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007496 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007497 return ret;
7498 }
7499
7500 return 0;
7501}
7502
stephen hemminger1d143d92013-12-29 14:01:29 -08007503static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7504 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007505 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007506 struct list_head *up_list,
7507 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007508{
Andrew Collins93409032016-10-03 13:43:02 -06007509 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7510 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007511}
7512
stephen hemminger1d143d92013-12-29 14:01:29 -08007513static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7514 struct net_device *upper_dev,
7515 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007516{
David Ahernf1170fd2016-10-17 19:15:51 -07007517 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7518 &dev->adj_list.upper,
7519 &upper_dev->adj_list.lower,
7520 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007521}
7522
stephen hemminger1d143d92013-12-29 14:01:29 -08007523static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7524 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007525{
Andrew Collins93409032016-10-03 13:43:02 -06007526 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007527 &dev->adj_list.upper,
7528 &upper_dev->adj_list.lower);
7529}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007530
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007531static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007532 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007533 void *upper_priv, void *upper_info,
7534 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007535{
David Ahern51d0c0472017-10-04 17:48:45 -07007536 struct netdev_notifier_changeupper_info changeupper_info = {
7537 .info = {
7538 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007539 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007540 },
7541 .upper_dev = upper_dev,
7542 .master = master,
7543 .linking = true,
7544 .upper_info = upper_info,
7545 };
Mike Manning50d629e2018-02-26 23:49:30 +00007546 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007547 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007548
7549 ASSERT_RTNL();
7550
7551 if (dev == upper_dev)
7552 return -EBUSY;
7553
7554 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007555 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007556 return -EBUSY;
7557
Taehee Yoo5343da42019-10-21 18:47:50 +00007558 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7559 return -EMLINK;
7560
Mike Manning50d629e2018-02-26 23:49:30 +00007561 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007562 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007563 return -EEXIST;
7564 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007565 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007566 if (master_dev)
7567 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7568 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007569
David Ahern51d0c0472017-10-04 17:48:45 -07007570 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007571 &changeupper_info.info);
7572 ret = notifier_to_errno(ret);
7573 if (ret)
7574 return ret;
7575
Jiri Pirko6dffb042015-12-03 12:12:10 +01007576 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007577 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007578 if (ret)
7579 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007580
David Ahern51d0c0472017-10-04 17:48:45 -07007581 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007582 &changeupper_info.info);
7583 ret = notifier_to_errno(ret);
7584 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007585 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007586
Taehee Yoo5343da42019-10-21 18:47:50 +00007587 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007588 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007589
7590 __netdev_update_lower_level(upper_dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007591 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7592 NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007593
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007594 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007595
David Ahernf1170fd2016-10-17 19:15:51 -07007596rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007597 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007598
7599 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007600}
7601
7602/**
7603 * netdev_upper_dev_link - Add a link to the upper device
7604 * @dev: device
7605 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007606 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007607 *
7608 * Adds a link to device which is upper to this one. The caller must hold
7609 * the RTNL lock. On a failure a negative errno code is returned.
7610 * On success the reference counts are adjusted and the function
7611 * returns zero.
7612 */
7613int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007614 struct net_device *upper_dev,
7615 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007616{
David Ahern42ab19e2017-10-04 17:48:47 -07007617 return __netdev_upper_dev_link(dev, upper_dev, false,
7618 NULL, NULL, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007619}
7620EXPORT_SYMBOL(netdev_upper_dev_link);
7621
7622/**
7623 * netdev_master_upper_dev_link - Add a master link to the upper device
7624 * @dev: device
7625 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007626 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007627 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007628 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007629 *
7630 * Adds a link to device which is upper to this one. In this case, only
7631 * one master upper device can be linked, although other non-master devices
7632 * might be linked as well. The caller must hold the RTNL lock.
7633 * On a failure a negative errno code is returned. On success the reference
7634 * counts are adjusted and the function returns zero.
7635 */
7636int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007637 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007638 void *upper_priv, void *upper_info,
7639 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007640{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007641 return __netdev_upper_dev_link(dev, upper_dev, true,
David Ahern42ab19e2017-10-04 17:48:47 -07007642 upper_priv, upper_info, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007643}
7644EXPORT_SYMBOL(netdev_master_upper_dev_link);
7645
7646/**
7647 * netdev_upper_dev_unlink - Removes a link to upper device
7648 * @dev: device
7649 * @upper_dev: new upper device
7650 *
7651 * Removes a link to device which is upper to this one. The caller must hold
7652 * the RTNL lock.
7653 */
7654void netdev_upper_dev_unlink(struct net_device *dev,
7655 struct net_device *upper_dev)
7656{
David Ahern51d0c0472017-10-04 17:48:45 -07007657 struct netdev_notifier_changeupper_info changeupper_info = {
7658 .info = {
7659 .dev = dev,
7660 },
7661 .upper_dev = upper_dev,
7662 .linking = false,
7663 };
tchardingf4563a72017-02-09 17:56:07 +11007664
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007665 ASSERT_RTNL();
7666
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007667 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007668
David Ahern51d0c0472017-10-04 17:48:45 -07007669 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007670 &changeupper_info.info);
7671
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007672 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007673
David Ahern51d0c0472017-10-04 17:48:45 -07007674 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007675 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007676
7677 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007678 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007679
7680 __netdev_update_lower_level(upper_dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007681 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7682 NULL);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007683}
7684EXPORT_SYMBOL(netdev_upper_dev_unlink);
7685
Taehee Yoo32b6d342019-10-21 18:47:56 +00007686static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7687 struct net_device *lower_dev,
7688 bool val)
7689{
7690 struct netdev_adjacent *adj;
7691
7692 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7693 if (adj)
7694 adj->ignore = val;
7695
7696 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7697 if (adj)
7698 adj->ignore = val;
7699}
7700
7701static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7702 struct net_device *lower_dev)
7703{
7704 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7705}
7706
7707static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7708 struct net_device *lower_dev)
7709{
7710 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7711}
7712
7713int netdev_adjacent_change_prepare(struct net_device *old_dev,
7714 struct net_device *new_dev,
7715 struct net_device *dev,
7716 struct netlink_ext_ack *extack)
7717{
7718 int err;
7719
7720 if (!new_dev)
7721 return 0;
7722
7723 if (old_dev && new_dev != old_dev)
7724 netdev_adjacent_dev_disable(dev, old_dev);
7725
7726 err = netdev_upper_dev_link(new_dev, dev, extack);
7727 if (err) {
7728 if (old_dev && new_dev != old_dev)
7729 netdev_adjacent_dev_enable(dev, old_dev);
7730 return err;
7731 }
7732
7733 return 0;
7734}
7735EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7736
7737void netdev_adjacent_change_commit(struct net_device *old_dev,
7738 struct net_device *new_dev,
7739 struct net_device *dev)
7740{
7741 if (!new_dev || !old_dev)
7742 return;
7743
7744 if (new_dev == old_dev)
7745 return;
7746
7747 netdev_adjacent_dev_enable(dev, old_dev);
7748 netdev_upper_dev_unlink(old_dev, dev);
7749}
7750EXPORT_SYMBOL(netdev_adjacent_change_commit);
7751
7752void netdev_adjacent_change_abort(struct net_device *old_dev,
7753 struct net_device *new_dev,
7754 struct net_device *dev)
7755{
7756 if (!new_dev)
7757 return;
7758
7759 if (old_dev && new_dev != old_dev)
7760 netdev_adjacent_dev_enable(dev, old_dev);
7761
7762 netdev_upper_dev_unlink(new_dev, dev);
7763}
7764EXPORT_SYMBOL(netdev_adjacent_change_abort);
7765
Moni Shoua61bd3852015-02-03 16:48:29 +02007766/**
7767 * netdev_bonding_info_change - Dispatch event about slave change
7768 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09007769 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02007770 *
7771 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7772 * The caller must hold the RTNL lock.
7773 */
7774void netdev_bonding_info_change(struct net_device *dev,
7775 struct netdev_bonding_info *bonding_info)
7776{
David Ahern51d0c0472017-10-04 17:48:45 -07007777 struct netdev_notifier_bonding_info info = {
7778 .info.dev = dev,
7779 };
Moni Shoua61bd3852015-02-03 16:48:29 +02007780
7781 memcpy(&info.bonding_info, bonding_info,
7782 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07007783 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02007784 &info.info);
7785}
7786EXPORT_SYMBOL(netdev_bonding_info_change);
7787
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007788static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007789{
7790 struct netdev_adjacent *iter;
7791
7792 struct net *net = dev_net(dev);
7793
7794 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007795 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007796 continue;
7797 netdev_adjacent_sysfs_add(iter->dev, dev,
7798 &iter->dev->adj_list.lower);
7799 netdev_adjacent_sysfs_add(dev, iter->dev,
7800 &dev->adj_list.upper);
7801 }
7802
7803 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007804 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007805 continue;
7806 netdev_adjacent_sysfs_add(iter->dev, dev,
7807 &iter->dev->adj_list.upper);
7808 netdev_adjacent_sysfs_add(dev, iter->dev,
7809 &dev->adj_list.lower);
7810 }
7811}
7812
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007813static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007814{
7815 struct netdev_adjacent *iter;
7816
7817 struct net *net = dev_net(dev);
7818
7819 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007820 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007821 continue;
7822 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7823 &iter->dev->adj_list.lower);
7824 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7825 &dev->adj_list.upper);
7826 }
7827
7828 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007829 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007830 continue;
7831 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7832 &iter->dev->adj_list.upper);
7833 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7834 &dev->adj_list.lower);
7835 }
7836}
7837
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007838void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02007839{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007840 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007841
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007842 struct net *net = dev_net(dev);
7843
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007844 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007845 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007846 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007847 netdev_adjacent_sysfs_del(iter->dev, oldname,
7848 &iter->dev->adj_list.lower);
7849 netdev_adjacent_sysfs_add(iter->dev, dev,
7850 &iter->dev->adj_list.lower);
7851 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007852
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007853 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007854 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007855 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007856 netdev_adjacent_sysfs_del(iter->dev, oldname,
7857 &iter->dev->adj_list.upper);
7858 netdev_adjacent_sysfs_add(iter->dev, dev,
7859 &iter->dev->adj_list.upper);
7860 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007861}
Veaceslav Falico402dae92013-09-25 09:20:09 +02007862
7863void *netdev_lower_dev_get_private(struct net_device *dev,
7864 struct net_device *lower_dev)
7865{
7866 struct netdev_adjacent *lower;
7867
7868 if (!lower_dev)
7869 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007870 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02007871 if (!lower)
7872 return NULL;
7873
7874 return lower->private;
7875}
7876EXPORT_SYMBOL(netdev_lower_dev_get_private);
7877
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007878
Jiri Pirko04d48262015-12-03 12:12:15 +01007879/**
7880 * netdev_lower_change - Dispatch event about lower device state change
7881 * @lower_dev: device
7882 * @lower_state_info: state to dispatch
7883 *
7884 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7885 * The caller must hold the RTNL lock.
7886 */
7887void netdev_lower_state_changed(struct net_device *lower_dev,
7888 void *lower_state_info)
7889{
David Ahern51d0c0472017-10-04 17:48:45 -07007890 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7891 .info.dev = lower_dev,
7892 };
Jiri Pirko04d48262015-12-03 12:12:15 +01007893
7894 ASSERT_RTNL();
7895 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07007896 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01007897 &changelowerstate_info.info);
7898}
7899EXPORT_SYMBOL(netdev_lower_state_changed);
7900
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007901static void dev_change_rx_flags(struct net_device *dev, int flags)
7902{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007903 const struct net_device_ops *ops = dev->netdev_ops;
7904
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05007905 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007906 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007907}
7908
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007909static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07007910{
Eric Dumazetb536db92011-11-30 21:42:26 +00007911 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06007912 kuid_t uid;
7913 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07007914
Patrick McHardy24023452007-07-14 18:51:31 -07007915 ASSERT_RTNL();
7916
Wang Chendad9b332008-06-18 01:48:28 -07007917 dev->flags |= IFF_PROMISC;
7918 dev->promiscuity += inc;
7919 if (dev->promiscuity == 0) {
7920 /*
7921 * Avoid overflow.
7922 * If inc causes overflow, untouch promisc and return error.
7923 */
7924 if (inc < 0)
7925 dev->flags &= ~IFF_PROMISC;
7926 else {
7927 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007928 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7929 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007930 return -EOVERFLOW;
7931 }
7932 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007933 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007934 pr_info("device %s %s promiscuous mode\n",
7935 dev->name,
7936 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11007937 if (audit_enabled) {
7938 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04007939 audit_log(audit_context(), GFP_ATOMIC,
7940 AUDIT_ANOM_PROMISCUOUS,
7941 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7942 dev->name, (dev->flags & IFF_PROMISC),
7943 (old_flags & IFF_PROMISC),
7944 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7945 from_kuid(&init_user_ns, uid),
7946 from_kgid(&init_user_ns, gid),
7947 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11007948 }
Patrick McHardy24023452007-07-14 18:51:31 -07007949
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007950 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07007951 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007952 if (notify)
7953 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07007954 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007955}
7956
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957/**
7958 * dev_set_promiscuity - update promiscuity count on a device
7959 * @dev: device
7960 * @inc: modifier
7961 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07007962 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 * remains above zero the interface remains promiscuous. Once it hits zero
7964 * the device reverts back to normal filtering operation. A negative inc
7965 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07007966 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967 */
Wang Chendad9b332008-06-18 01:48:28 -07007968int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969{
Eric Dumazetb536db92011-11-30 21:42:26 +00007970 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07007971 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007973 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07007974 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07007975 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07007976 if (dev->flags != old_flags)
7977 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07007978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007980EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007982static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007984 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985
Patrick McHardy24023452007-07-14 18:51:31 -07007986 ASSERT_RTNL();
7987
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07007989 dev->allmulti += inc;
7990 if (dev->allmulti == 0) {
7991 /*
7992 * Avoid overflow.
7993 * If inc causes overflow, untouch allmulti and return error.
7994 */
7995 if (inc < 0)
7996 dev->flags &= ~IFF_ALLMULTI;
7997 else {
7998 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007999 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8000 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008001 return -EOVERFLOW;
8002 }
8003 }
Patrick McHardy24023452007-07-14 18:51:31 -07008004 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008005 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008006 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008007 if (notify)
8008 __dev_notify_flags(dev, old_flags,
8009 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008010 }
Wang Chendad9b332008-06-18 01:48:28 -07008011 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008012}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008013
8014/**
8015 * dev_set_allmulti - update allmulti count on a device
8016 * @dev: device
8017 * @inc: modifier
8018 *
8019 * Add or remove reception of all multicast frames to a device. While the
8020 * count in the device remains above zero the interface remains listening
8021 * to all interfaces. Once it hits zero the device reverts back to normal
8022 * filtering operation. A negative @inc value is used to drop the counter
8023 * when releasing a resource needing all multicasts.
8024 * Return 0 if successful or a negative errno code on error.
8025 */
8026
8027int dev_set_allmulti(struct net_device *dev, int inc)
8028{
8029 return __dev_set_allmulti(dev, inc, true);
8030}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008031EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008032
8033/*
8034 * Upload unicast and multicast address lists to device and
8035 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008036 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008037 * are present.
8038 */
8039void __dev_set_rx_mode(struct net_device *dev)
8040{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008041 const struct net_device_ops *ops = dev->netdev_ops;
8042
Patrick McHardy4417da62007-06-27 01:28:10 -07008043 /* dev_open will call this function so the list will stay sane. */
8044 if (!(dev->flags&IFF_UP))
8045 return;
8046
8047 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008048 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008049
Jiri Pirko01789342011-08-16 06:29:00 +00008050 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008051 /* Unicast addresses changes may only happen under the rtnl,
8052 * therefore calling __dev_set_promiscuity here is safe.
8053 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008054 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008055 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008056 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008057 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008058 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008059 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008060 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008061 }
Jiri Pirko01789342011-08-16 06:29:00 +00008062
8063 if (ops->ndo_set_rx_mode)
8064 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008065}
8066
8067void dev_set_rx_mode(struct net_device *dev)
8068{
David S. Millerb9e40852008-07-15 00:15:08 -07008069 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008070 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008071 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072}
8073
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008074/**
8075 * dev_get_flags - get flags reported to userspace
8076 * @dev: device
8077 *
8078 * Get the combination of flag bits exported through APIs to userspace.
8079 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008080unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081{
Eric Dumazet95c96172012-04-15 05:58:06 +00008082 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
8084 flags = (dev->flags & ~(IFF_PROMISC |
8085 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008086 IFF_RUNNING |
8087 IFF_LOWER_UP |
8088 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 (dev->gflags & (IFF_PROMISC |
8090 IFF_ALLMULTI));
8091
Stefan Rompfb00055a2006-03-20 17:09:11 -08008092 if (netif_running(dev)) {
8093 if (netif_oper_up(dev))
8094 flags |= IFF_RUNNING;
8095 if (netif_carrier_ok(dev))
8096 flags |= IFF_LOWER_UP;
8097 if (netif_dormant(dev))
8098 flags |= IFF_DORMANT;
8099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100
8101 return flags;
8102}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008103EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008104
Petr Machata6d040322018-12-06 17:05:43 +00008105int __dev_change_flags(struct net_device *dev, unsigned int flags,
8106 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107{
Eric Dumazetb536db92011-11-30 21:42:26 +00008108 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008109 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110
Patrick McHardy24023452007-07-14 18:51:31 -07008111 ASSERT_RTNL();
8112
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113 /*
8114 * Set the flags on our device.
8115 */
8116
8117 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8118 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8119 IFF_AUTOMEDIA)) |
8120 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8121 IFF_ALLMULTI));
8122
8123 /*
8124 * Load in the correct multicast list now the flags have changed.
8125 */
8126
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008127 if ((old_flags ^ flags) & IFF_MULTICAST)
8128 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008129
Patrick McHardy4417da62007-06-27 01:28:10 -07008130 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131
8132 /*
8133 * Have we downed the interface. We handle IFF_UP ourselves
8134 * according to user attempts to set it, rather than blindly
8135 * setting it.
8136 */
8137
8138 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008139 if ((old_flags ^ flags) & IFF_UP) {
8140 if (old_flags & IFF_UP)
8141 __dev_close(dev);
8142 else
Petr Machata40c900a2018-12-06 17:05:47 +00008143 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008147 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008148 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008149
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008151
8152 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8153 if (dev->flags != old_flags)
8154 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 }
8156
8157 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008158 * is important. Some (broken) drivers set IFF_PROMISC, when
8159 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 */
8161 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008162 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8163
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008165 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 }
8167
Patrick McHardybd380812010-02-26 06:34:53 +00008168 return ret;
8169}
8170
Nicolas Dichtela528c212013-09-25 12:02:44 +02008171void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8172 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008173{
8174 unsigned int changes = dev->flags ^ old_flags;
8175
Nicolas Dichtela528c212013-09-25 12:02:44 +02008176 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008177 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008178
Patrick McHardybd380812010-02-26 06:34:53 +00008179 if (changes & IFF_UP) {
8180 if (dev->flags & IFF_UP)
8181 call_netdevice_notifiers(NETDEV_UP, dev);
8182 else
8183 call_netdevice_notifiers(NETDEV_DOWN, dev);
8184 }
8185
8186 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008187 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008188 struct netdev_notifier_change_info change_info = {
8189 .info = {
8190 .dev = dev,
8191 },
8192 .flags_changed = changes,
8193 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008194
David Ahern51d0c0472017-10-04 17:48:45 -07008195 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008196 }
Patrick McHardybd380812010-02-26 06:34:53 +00008197}
8198
8199/**
8200 * dev_change_flags - change device settings
8201 * @dev: device
8202 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008203 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008204 *
8205 * Change settings on device based state flags. The flags are
8206 * in the userspace exported format.
8207 */
Petr Machata567c5e12018-12-06 17:05:42 +00008208int dev_change_flags(struct net_device *dev, unsigned int flags,
8209 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008210{
Eric Dumazetb536db92011-11-30 21:42:26 +00008211 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008212 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008213
Petr Machata6d040322018-12-06 17:05:43 +00008214 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008215 if (ret < 0)
8216 return ret;
8217
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008218 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008219 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220 return ret;
8221}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008222EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
WANG Congf51048c2017-07-06 15:01:57 -07008224int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008225{
8226 const struct net_device_ops *ops = dev->netdev_ops;
8227
8228 if (ops->ndo_change_mtu)
8229 return ops->ndo_change_mtu(dev, new_mtu);
8230
Eric Dumazet501a90c2019-12-05 20:43:46 -08008231 /* Pairs with all the lockless reads of dev->mtu in the stack */
8232 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008233 return 0;
8234}
WANG Congf51048c2017-07-06 15:01:57 -07008235EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008236
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008237int dev_validate_mtu(struct net_device *dev, int new_mtu,
8238 struct netlink_ext_ack *extack)
8239{
8240 /* MTU must be positive, and in range */
8241 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8242 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8243 return -EINVAL;
8244 }
8245
8246 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8247 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8248 return -EINVAL;
8249 }
8250 return 0;
8251}
8252
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008253/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008254 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008255 * @dev: device
8256 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008257 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008258 *
8259 * Change the maximum transfer size of the network device.
8260 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008261int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8262 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008264 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265
8266 if (new_mtu == dev->mtu)
8267 return 0;
8268
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008269 err = dev_validate_mtu(dev, new_mtu, extack);
8270 if (err)
8271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272
8273 if (!netif_device_present(dev))
8274 return -ENODEV;
8275
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008276 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8277 err = notifier_to_errno(err);
8278 if (err)
8279 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008280
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008281 orig_mtu = dev->mtu;
8282 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008284 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008285 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8286 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008287 err = notifier_to_errno(err);
8288 if (err) {
8289 /* setting mtu back and notifying everyone again,
8290 * so that they have a chance to revert changes.
8291 */
8292 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008293 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8294 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008295 }
8296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 return err;
8298}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008299
8300int dev_set_mtu(struct net_device *dev, int new_mtu)
8301{
8302 struct netlink_ext_ack extack;
8303 int err;
8304
Li RongQinga6bcfc82018-08-03 15:45:21 +08008305 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008306 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008307 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008308 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8309 return err;
8310}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008311EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008313/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008314 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8315 * @dev: device
8316 * @new_len: new tx queue length
8317 */
8318int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8319{
8320 unsigned int orig_len = dev->tx_queue_len;
8321 int res;
8322
8323 if (new_len != (unsigned int)new_len)
8324 return -ERANGE;
8325
8326 if (new_len != orig_len) {
8327 dev->tx_queue_len = new_len;
8328 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8329 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008330 if (res)
8331 goto err_rollback;
8332 res = dev_qdisc_change_tx_queue_len(dev);
8333 if (res)
8334 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008335 }
8336
8337 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008338
8339err_rollback:
8340 netdev_err(dev, "refused to change device tx_queue_len\n");
8341 dev->tx_queue_len = orig_len;
8342 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008343}
8344
8345/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008346 * dev_set_group - Change group this device belongs to
8347 * @dev: device
8348 * @new_group: group this device should belong to
8349 */
8350void dev_set_group(struct net_device *dev, int new_group)
8351{
8352 dev->group = new_group;
8353}
8354EXPORT_SYMBOL(dev_set_group);
8355
8356/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008357 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8358 * @dev: device
8359 * @addr: new address
8360 * @extack: netlink extended ack
8361 */
8362int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8363 struct netlink_ext_ack *extack)
8364{
8365 struct netdev_notifier_pre_changeaddr_info info = {
8366 .info.dev = dev,
8367 .info.extack = extack,
8368 .dev_addr = addr,
8369 };
8370 int rc;
8371
8372 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8373 return notifier_to_errno(rc);
8374}
8375EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8376
8377/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008378 * dev_set_mac_address - Change Media Access Control Address
8379 * @dev: device
8380 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008381 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008382 *
8383 * Change the hardware (MAC) address of the device
8384 */
Petr Machata3a37a962018-12-13 11:54:30 +00008385int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8386 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008388 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389 int err;
8390
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008391 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 return -EOPNOTSUPP;
8393 if (sa->sa_family != dev->type)
8394 return -EINVAL;
8395 if (!netif_device_present(dev))
8396 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008397 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8398 if (err)
8399 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008400 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008401 if (err)
8402 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008403 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008404 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008405 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008408EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008410/**
8411 * dev_change_carrier - Change device carrier
8412 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008413 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008414 *
8415 * Change device carrier
8416 */
8417int dev_change_carrier(struct net_device *dev, bool new_carrier)
8418{
8419 const struct net_device_ops *ops = dev->netdev_ops;
8420
8421 if (!ops->ndo_change_carrier)
8422 return -EOPNOTSUPP;
8423 if (!netif_device_present(dev))
8424 return -ENODEV;
8425 return ops->ndo_change_carrier(dev, new_carrier);
8426}
8427EXPORT_SYMBOL(dev_change_carrier);
8428
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008430 * dev_get_phys_port_id - Get device physical port ID
8431 * @dev: device
8432 * @ppid: port ID
8433 *
8434 * Get device physical port ID
8435 */
8436int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008437 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008438{
8439 const struct net_device_ops *ops = dev->netdev_ops;
8440
8441 if (!ops->ndo_get_phys_port_id)
8442 return -EOPNOTSUPP;
8443 return ops->ndo_get_phys_port_id(dev, ppid);
8444}
8445EXPORT_SYMBOL(dev_get_phys_port_id);
8446
8447/**
David Aherndb24a902015-03-17 20:23:15 -06008448 * dev_get_phys_port_name - Get device physical port name
8449 * @dev: device
8450 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008451 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008452 *
8453 * Get device physical port name
8454 */
8455int dev_get_phys_port_name(struct net_device *dev,
8456 char *name, size_t len)
8457{
8458 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008459 int err;
David Aherndb24a902015-03-17 20:23:15 -06008460
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008461 if (ops->ndo_get_phys_port_name) {
8462 err = ops->ndo_get_phys_port_name(dev, name, len);
8463 if (err != -EOPNOTSUPP)
8464 return err;
8465 }
8466 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008467}
8468EXPORT_SYMBOL(dev_get_phys_port_name);
8469
8470/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008471 * dev_get_port_parent_id - Get the device's port parent identifier
8472 * @dev: network device
8473 * @ppid: pointer to a storage for the port's parent identifier
8474 * @recurse: allow/disallow recursion to lower devices
8475 *
8476 * Get the devices's port parent identifier
8477 */
8478int dev_get_port_parent_id(struct net_device *dev,
8479 struct netdev_phys_item_id *ppid,
8480 bool recurse)
8481{
8482 const struct net_device_ops *ops = dev->netdev_ops;
8483 struct netdev_phys_item_id first = { };
8484 struct net_device *lower_dev;
8485 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008486 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008487
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008488 if (ops->ndo_get_port_parent_id) {
8489 err = ops->ndo_get_port_parent_id(dev, ppid);
8490 if (err != -EOPNOTSUPP)
8491 return err;
8492 }
8493
8494 err = devlink_compat_switch_id_get(dev, ppid);
8495 if (!err || err != -EOPNOTSUPP)
8496 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008497
8498 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008499 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008500
8501 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8502 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8503 if (err)
8504 break;
8505 if (!first.id_len)
8506 first = *ppid;
8507 else if (memcmp(&first, ppid, sizeof(*ppid)))
8508 return -ENODATA;
8509 }
8510
8511 return err;
8512}
8513EXPORT_SYMBOL(dev_get_port_parent_id);
8514
8515/**
8516 * netdev_port_same_parent_id - Indicate if two network devices have
8517 * the same port parent identifier
8518 * @a: first network device
8519 * @b: second network device
8520 */
8521bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8522{
8523 struct netdev_phys_item_id a_id = { };
8524 struct netdev_phys_item_id b_id = { };
8525
8526 if (dev_get_port_parent_id(a, &a_id, true) ||
8527 dev_get_port_parent_id(b, &b_id, true))
8528 return false;
8529
8530 return netdev_phys_item_id_same(&a_id, &b_id);
8531}
8532EXPORT_SYMBOL(netdev_port_same_parent_id);
8533
8534/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008535 * dev_change_proto_down - update protocol port state information
8536 * @dev: device
8537 * @proto_down: new value
8538 *
8539 * This info can be used by switch drivers to set the phys state of the
8540 * port.
8541 */
8542int dev_change_proto_down(struct net_device *dev, bool proto_down)
8543{
8544 const struct net_device_ops *ops = dev->netdev_ops;
8545
8546 if (!ops->ndo_change_proto_down)
8547 return -EOPNOTSUPP;
8548 if (!netif_device_present(dev))
8549 return -ENODEV;
8550 return ops->ndo_change_proto_down(dev, proto_down);
8551}
8552EXPORT_SYMBOL(dev_change_proto_down);
8553
Andy Roulinb5899672019-02-22 18:06:36 +00008554/**
8555 * dev_change_proto_down_generic - generic implementation for
8556 * ndo_change_proto_down that sets carrier according to
8557 * proto_down.
8558 *
8559 * @dev: device
8560 * @proto_down: new value
8561 */
8562int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8563{
8564 if (proto_down)
8565 netif_carrier_off(dev);
8566 else
8567 netif_carrier_on(dev);
8568 dev->proto_down = proto_down;
8569 return 0;
8570}
8571EXPORT_SYMBOL(dev_change_proto_down_generic);
8572
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008573u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
8574 enum bpf_netdev_command cmd)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008575{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008576 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008577
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008578 if (!bpf_op)
8579 return 0;
Martin KaFai Lau58038692017-06-15 17:29:09 -07008580
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008581 memset(&xdp, 0, sizeof(xdp));
8582 xdp.command = cmd;
8583
8584 /* Query must always succeed. */
8585 WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008586
Jakub Kicinski6b867582018-07-11 20:36:39 -07008587 return xdp.prog_id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008588}
8589
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008590static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07008591 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008592 struct bpf_prog *prog)
8593{
Björn Töpel7e6897f2019-12-13 18:51:09 +01008594 bool non_hw = !(flags & XDP_FLAGS_HW_MODE);
8595 struct bpf_prog *prev_prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008596 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01008597 int err;
8598
8599 if (non_hw) {
8600 prev_prog = bpf_prog_by_id(__dev_xdp_query(dev, bpf_op,
8601 XDP_QUERY_PROG));
8602 if (IS_ERR(prev_prog))
8603 prev_prog = NULL;
8604 }
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008605
8606 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07008607 if (flags & XDP_FLAGS_HW_MODE)
8608 xdp.command = XDP_SETUP_PROG_HW;
8609 else
8610 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008611 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07008612 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008613 xdp.prog = prog;
8614
Björn Töpel7e6897f2019-12-13 18:51:09 +01008615 err = bpf_op(dev, &xdp);
8616 if (!err && non_hw)
8617 bpf_prog_change_xdp(prev_prog, prog);
8618
8619 if (prev_prog)
8620 bpf_prog_put(prev_prog);
8621
8622 return err;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008623}
8624
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008625static void dev_xdp_uninstall(struct net_device *dev)
8626{
8627 struct netdev_bpf xdp;
8628 bpf_op_t ndo_bpf;
8629
8630 /* Remove generic XDP */
8631 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
8632
8633 /* Remove from the driver */
8634 ndo_bpf = dev->netdev_ops->ndo_bpf;
8635 if (!ndo_bpf)
8636 return;
8637
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008638 memset(&xdp, 0, sizeof(xdp));
8639 xdp.command = XDP_QUERY_PROG;
8640 WARN_ON(ndo_bpf(dev, &xdp));
8641 if (xdp.prog_id)
8642 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8643 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008644
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008645 /* Remove HW offload */
8646 memset(&xdp, 0, sizeof(xdp));
8647 xdp.command = XDP_QUERY_PROG_HW;
8648 if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
8649 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8650 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008651}
8652
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008653/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07008654 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
8655 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07008656 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07008657 * @fd: new program fd or negative value to clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01008658 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07008659 *
8660 * Set or clear a bpf program for a device
8661 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07008662int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
8663 int fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07008664{
8665 const struct net_device_ops *ops = dev->netdev_ops;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008666 enum bpf_netdev_command query;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008667 struct bpf_prog *prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008668 bpf_op_t bpf_op, bpf_chk;
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008669 bool offload;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008670 int err;
8671
Daniel Borkmann85de8572016-11-28 23:16:54 +01008672 ASSERT_RTNL();
8673
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008674 offload = flags & XDP_FLAGS_HW_MODE;
8675 query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008676
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008677 bpf_op = bpf_chk = ops->ndo_bpf;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008678 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) {
8679 NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode");
Daniel Borkmann0489df92017-05-12 01:04:45 +02008680 return -EOPNOTSUPP;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008681 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008682 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
8683 bpf_op = generic_xdp_install;
8684 if (bpf_op == bpf_chk)
8685 bpf_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04008686
Brenden Blancoa7862b42016-07-19 12:16:48 -07008687 if (fd >= 0) {
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008688 u32 prog_id;
8689
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008690 if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) {
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008691 NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time");
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008692 return -EEXIST;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008693 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008694
8695 prog_id = __dev_xdp_query(dev, bpf_op, query);
8696 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) {
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008697 NL_SET_ERR_MSG(extack, "XDP program already attached");
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008698 return -EBUSY;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008699 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01008700
Jakub Kicinski288b3de2017-11-20 15:21:54 -08008701 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
8702 bpf_op == ops->ndo_bpf);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008703 if (IS_ERR(prog))
8704 return PTR_ERR(prog);
Jakub Kicinski441a3302017-11-20 15:21:55 -08008705
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008706 if (!offload && bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinski441a3302017-11-20 15:21:55 -08008707 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
8708 bpf_prog_put(prog);
8709 return -EINVAL;
8710 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008711
Jakub Kicinskiaefc3e72019-10-31 20:07:00 -07008712 /* prog->aux->id may be 0 for orphaned device-bound progs */
8713 if (prog->aux->id && prog->aux->id == prog_id) {
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008714 bpf_prog_put(prog);
8715 return 0;
8716 }
8717 } else {
8718 if (!__dev_xdp_query(dev, bpf_op, query))
8719 return 0;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008720 }
8721
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008722 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008723 if (err < 0 && prog)
8724 bpf_prog_put(prog);
8725
8726 return err;
8727}
Brenden Blancoa7862b42016-07-19 12:16:48 -07008728
8729/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07008731 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 *
8733 * Returns a suitable unique value for a new device interface
8734 * number. The caller must hold the rtnl semaphore or the
8735 * dev_base_lock to be sure it remains unique.
8736 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07008737static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008739 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11008740
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 for (;;) {
8742 if (++ifindex <= 0)
8743 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008744 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008745 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 }
8747}
8748
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08008750static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07008751DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752
Stephen Hemminger6f05f622007-03-08 20:46:03 -08008753static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008756 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008757}
8758
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008759static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008760{
Krishna Kumare93737b2009-12-08 22:26:02 +00008761 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008762 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008763
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008764 BUG_ON(dev_boot_phase);
8765 ASSERT_RTNL();
8766
Krishna Kumare93737b2009-12-08 22:26:02 +00008767 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008768 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00008769 * for initialization unwind. Remove those
8770 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008771 */
8772 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008773 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
8774 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008775
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008776 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00008777 list_del(&dev->unreg_list);
8778 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008779 }
Eric Dumazet449f4542011-05-19 12:24:16 +00008780 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008781 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00008782 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008783
Octavian Purdila44345722010-12-13 12:44:07 +00008784 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008785 list_for_each_entry(dev, head, unreg_list)
8786 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04008787 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008788
Octavian Purdila44345722010-12-13 12:44:07 +00008789 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008790 /* And unlink it from device chain. */
8791 unlist_netdevice(dev);
8792
8793 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008794 }
Eric Dumazet41852492016-08-26 12:50:39 -07008795 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008796
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008797 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008798
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008799 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008800 struct sk_buff *skb = NULL;
8801
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008802 /* Shutdown queueing discipline. */
8803 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008804
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008805 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008806
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008807 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008808 * this device. They should clean all the things.
8809 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008810 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
8811
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008812 if (!dev->rtnl_link_ops ||
8813 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04008814 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01008815 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008816
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008817 /*
8818 * Flush the unicast and multicast chains
8819 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008820 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008821 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008822
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02008823 netdev_name_node_alt_flush(dev);
Jiri Pirkoff927412019-09-30 11:48:15 +02008824 netdev_name_node_free(dev->name_node);
8825
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008826 if (dev->netdev_ops->ndo_uninit)
8827 dev->netdev_ops->ndo_uninit(dev);
8828
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008829 if (skb)
8830 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07008831
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008832 /* Notifier chain MUST detach us all upper devices. */
8833 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07008834 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008835
8836 /* Remove entries from kobject tree */
8837 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00008838#ifdef CONFIG_XPS
8839 /* Remove XPS queueing entries */
8840 netif_reset_xps_queues_gt(dev, 0);
8841#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008842 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008843
Eric W. Biederman850a5452011-10-13 22:25:23 +00008844 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008845
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00008846 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008847 dev_put(dev);
8848}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008849
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008850static void rollback_registered(struct net_device *dev)
8851{
8852 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008853
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008854 list_add(&dev->unreg_list, &single);
8855 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00008856 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008857}
8858
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008859static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
8860 struct net_device *upper, netdev_features_t features)
8861{
8862 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8863 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008864 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008865
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01008866 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008867 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008868 if (!(upper->wanted_features & feature)
8869 && (features & feature)) {
8870 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
8871 &feature, upper->name);
8872 features &= ~feature;
8873 }
8874 }
8875
8876 return features;
8877}
8878
8879static void netdev_sync_lower_features(struct net_device *upper,
8880 struct net_device *lower, netdev_features_t features)
8881{
8882 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8883 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008884 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008885
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01008886 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008887 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008888 if (!(features & feature) && (lower->features & feature)) {
8889 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
8890 &feature, lower->name);
8891 lower->wanted_features &= ~feature;
8892 netdev_update_features(lower);
8893
8894 if (unlikely(lower->features & feature))
8895 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
8896 &feature, lower->name);
8897 }
8898 }
8899}
8900
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008901static netdev_features_t netdev_fix_features(struct net_device *dev,
8902 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07008903{
Michał Mirosław57422dc2011-01-22 12:14:12 +00008904 /* Fix illegal checksum combinations */
8905 if ((features & NETIF_F_HW_CSUM) &&
8906 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008907 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00008908 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
8909 }
8910
Herbert Xub63365a2008-10-23 01:11:29 -07008911 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008912 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008913 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008914 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07008915 }
8916
Pravin B Shelarec5f0612013-03-07 09:28:01 +00008917 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
8918 !(features & NETIF_F_IP_CSUM)) {
8919 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
8920 features &= ~NETIF_F_TSO;
8921 features &= ~NETIF_F_TSO_ECN;
8922 }
8923
8924 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
8925 !(features & NETIF_F_IPV6_CSUM)) {
8926 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
8927 features &= ~NETIF_F_TSO6;
8928 }
8929
Alexander Duyckb1dc4972016-05-02 09:38:24 -07008930 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8931 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
8932 features &= ~NETIF_F_TSO_MANGLEID;
8933
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00008934 /* TSO ECN requires that TSO is present as well. */
8935 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
8936 features &= ~NETIF_F_TSO_ECN;
8937
Michał Mirosław212b5732011-02-15 16:59:16 +00008938 /* Software GSO depends on SG. */
8939 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008940 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00008941 features &= ~NETIF_F_GSO;
8942 }
8943
Alexander Duyck802ab552016-04-10 21:45:03 -04008944 /* GSO partial features require GSO partial be set */
8945 if ((features & dev->gso_partial_features) &&
8946 !(features & NETIF_F_GSO_PARTIAL)) {
8947 netdev_dbg(dev,
8948 "Dropping partially supported GSO features since no GSO partial.\n");
8949 features &= ~dev->gso_partial_features;
8950 }
8951
Michael Chanfb1f5f72017-12-16 03:09:40 -05008952 if (!(features & NETIF_F_RXCSUM)) {
8953 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8954 * successfully merged by hardware must also have the
8955 * checksum verified by hardware. If the user does not
8956 * want to enable RXCSUM, logically, we should disable GRO_HW.
8957 */
8958 if (features & NETIF_F_GRO_HW) {
8959 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8960 features &= ~NETIF_F_GRO_HW;
8961 }
8962 }
8963
Gal Pressmande8d5ab2018-03-12 11:48:49 +02008964 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8965 if (features & NETIF_F_RXFCS) {
8966 if (features & NETIF_F_LRO) {
8967 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
8968 features &= ~NETIF_F_LRO;
8969 }
8970
8971 if (features & NETIF_F_GRO_HW) {
8972 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8973 features &= ~NETIF_F_GRO_HW;
8974 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02008975 }
8976
Herbert Xub63365a2008-10-23 01:11:29 -07008977 return features;
8978}
Herbert Xub63365a2008-10-23 01:11:29 -07008979
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008980int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00008981{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008982 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008983 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008984 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05008985 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00008986
Michał Mirosław87267482011-04-12 09:56:38 +00008987 ASSERT_RTNL();
8988
Michał Mirosław5455c692011-02-15 16:59:17 +00008989 features = netdev_get_wanted_features(dev);
8990
8991 if (dev->netdev_ops->ndo_fix_features)
8992 features = dev->netdev_ops->ndo_fix_features(dev, features);
8993
8994 /* driver might be less strict about feature dependencies */
8995 features = netdev_fix_features(dev, features);
8996
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008997 /* some features can't be enabled if they're off an an upper device */
8998 netdev_for_each_upper_dev_rcu(dev, upper, iter)
8999 features = netdev_sync_upper_features(dev, upper, features);
9000
Michał Mirosław5455c692011-02-15 16:59:17 +00009001 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009002 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009003
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009004 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9005 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009006
9007 if (dev->netdev_ops->ndo_set_features)
9008 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009009 else
9010 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009011
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009012 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009013 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009014 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9015 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009016 /* return non-0 since some features might have changed and
9017 * it's better to fire a spurious notification than miss it
9018 */
9019 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009020 }
9021
Jarod Wilsone7868a82015-11-03 23:09:32 -05009022sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009023 /* some features must be disabled on lower devices when disabled
9024 * on an upper device (think: bonding master or bridge)
9025 */
9026 netdev_for_each_lower_dev(dev, lower, iter)
9027 netdev_sync_lower_features(dev, lower, features);
9028
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009029 if (!err) {
9030 netdev_features_t diff = features ^ dev->features;
9031
9032 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9033 /* udp_tunnel_{get,drop}_rx_info both need
9034 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9035 * device, or they won't do anything.
9036 * Thus we need to update dev->features
9037 * *before* calling udp_tunnel_get_rx_info,
9038 * but *after* calling udp_tunnel_drop_rx_info.
9039 */
9040 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9041 dev->features = features;
9042 udp_tunnel_get_rx_info(dev);
9043 } else {
9044 udp_tunnel_drop_rx_info(dev);
9045 }
9046 }
9047
Gal Pressman9daae9b2018-03-28 17:46:54 +03009048 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9049 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9050 dev->features = features;
9051 err |= vlan_get_rx_ctag_filter_info(dev);
9052 } else {
9053 vlan_drop_rx_ctag_filter_info(dev);
9054 }
9055 }
9056
9057 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9058 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9059 dev->features = features;
9060 err |= vlan_get_rx_stag_filter_info(dev);
9061 } else {
9062 vlan_drop_rx_stag_filter_info(dev);
9063 }
9064 }
9065
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009066 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009067 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009068
Jarod Wilsone7868a82015-11-03 23:09:32 -05009069 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009070}
9071
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009072/**
9073 * netdev_update_features - recalculate device features
9074 * @dev: the device to check
9075 *
9076 * Recalculate dev->features set and send notifications if it
9077 * has changed. Should be called after driver or hardware dependent
9078 * conditions might have changed that influence the features.
9079 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009080void netdev_update_features(struct net_device *dev)
9081{
9082 if (__netdev_update_features(dev))
9083 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009084}
9085EXPORT_SYMBOL(netdev_update_features);
9086
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009088 * netdev_change_features - recalculate device features
9089 * @dev: the device to check
9090 *
9091 * Recalculate dev->features set and send notifications even
9092 * if they have not changed. Should be called instead of
9093 * netdev_update_features() if also dev->vlan_features might
9094 * have changed to allow the changes to be propagated to stacked
9095 * VLAN devices.
9096 */
9097void netdev_change_features(struct net_device *dev)
9098{
9099 __netdev_update_features(dev);
9100 netdev_features_change(dev);
9101}
9102EXPORT_SYMBOL(netdev_change_features);
9103
9104/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009105 * netif_stacked_transfer_operstate - transfer operstate
9106 * @rootdev: the root or lower level device to transfer state from
9107 * @dev: the device to transfer operstate to
9108 *
9109 * Transfer operational state from root to device. This is normally
9110 * called when a stacking relationship exists between the root
9111 * device and the device(a leaf device).
9112 */
9113void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9114 struct net_device *dev)
9115{
9116 if (rootdev->operstate == IF_OPER_DORMANT)
9117 netif_dormant_on(dev);
9118 else
9119 netif_dormant_off(dev);
9120
Zhang Shengju0575c862017-04-26 17:49:38 +08009121 if (netif_carrier_ok(rootdev))
9122 netif_carrier_on(dev);
9123 else
9124 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009125}
9126EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9127
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009128static int netif_alloc_rx_queues(struct net_device *dev)
9129{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009130 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009131 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309132 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009133 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009134
Tom Herbertbd25fa72010-10-18 18:00:16 +00009135 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009136
Michal Hockodcda9b02017-07-12 14:36:45 -07009137 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009138 if (!rx)
9139 return -ENOMEM;
9140
Tom Herbertbd25fa72010-10-18 18:00:16 +00009141 dev->_rx = rx;
9142
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009143 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009144 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009145
9146 /* XDP RX-queue setup */
9147 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
9148 if (err < 0)
9149 goto err_rxq_info;
9150 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009151 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009152
9153err_rxq_info:
9154 /* Rollback successful reg's and free other resources */
9155 while (i--)
9156 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009157 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009158 dev->_rx = NULL;
9159 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009160}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009161
9162static void netif_free_rx_queues(struct net_device *dev)
9163{
9164 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009165
9166 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9167 if (!dev->_rx)
9168 return;
9169
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009170 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009171 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9172
9173 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009174}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009175
Changli Gaoaa942102010-12-04 02:31:41 +00009176static void netdev_init_one_queue(struct net_device *dev,
9177 struct netdev_queue *queue, void *_unused)
9178{
9179 /* Initialize queue lock */
9180 spin_lock_init(&queue->_xmit_lock);
Taehee Yooab92d682019-10-21 18:47:51 +00009181 lockdep_set_class(&queue->_xmit_lock, &dev->qdisc_xmit_lock_key);
Changli Gaoaa942102010-12-04 02:31:41 +00009182 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009183 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009184 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009185#ifdef CONFIG_BQL
9186 dql_init(&queue->dql, HZ);
9187#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009188}
9189
Eric Dumazet60877a32013-06-20 01:15:51 -07009190static void netif_free_tx_queues(struct net_device *dev)
9191{
WANG Cong4cb28972014-06-02 15:55:22 -07009192 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009193}
9194
Tom Herberte6484932010-10-18 18:04:39 +00009195static int netif_alloc_netdev_queues(struct net_device *dev)
9196{
9197 unsigned int count = dev->num_tx_queues;
9198 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009199 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009200
Eric Dumazetd3397272015-07-06 17:13:26 +02009201 if (count < 1 || count > 0xffff)
9202 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009203
Michal Hockodcda9b02017-07-12 14:36:45 -07009204 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009205 if (!tx)
9206 return -ENOMEM;
9207
Tom Herberte6484932010-10-18 18:04:39 +00009208 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009209
Tom Herberte6484932010-10-18 18:04:39 +00009210 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9211 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009212
9213 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009214}
9215
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009216void netif_tx_stop_all_queues(struct net_device *dev)
9217{
9218 unsigned int i;
9219
9220 for (i = 0; i < dev->num_tx_queues; i++) {
9221 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009222
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009223 netif_tx_stop_queue(txq);
9224 }
9225}
9226EXPORT_SYMBOL(netif_tx_stop_all_queues);
9227
Taehee Yooab92d682019-10-21 18:47:51 +00009228static void netdev_register_lockdep_key(struct net_device *dev)
9229{
9230 lockdep_register_key(&dev->qdisc_tx_busylock_key);
9231 lockdep_register_key(&dev->qdisc_running_key);
9232 lockdep_register_key(&dev->qdisc_xmit_lock_key);
9233 lockdep_register_key(&dev->addr_list_lock_key);
9234}
9235
9236static void netdev_unregister_lockdep_key(struct net_device *dev)
9237{
9238 lockdep_unregister_key(&dev->qdisc_tx_busylock_key);
9239 lockdep_unregister_key(&dev->qdisc_running_key);
9240 lockdep_unregister_key(&dev->qdisc_xmit_lock_key);
9241 lockdep_unregister_key(&dev->addr_list_lock_key);
9242}
9243
9244void netdev_update_lockdep_key(struct net_device *dev)
9245{
Taehee Yooab92d682019-10-21 18:47:51 +00009246 lockdep_unregister_key(&dev->addr_list_lock_key);
Taehee Yooab92d682019-10-21 18:47:51 +00009247 lockdep_register_key(&dev->addr_list_lock_key);
9248
9249 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
Taehee Yooab92d682019-10-21 18:47:51 +00009250}
9251EXPORT_SYMBOL(netdev_update_lockdep_key);
9252
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009253/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 * register_netdevice - register a network device
9255 * @dev: device to register
9256 *
9257 * Take a completed network device structure and add it to the kernel
9258 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9259 * chain. 0 is returned on success. A negative errno code is returned
9260 * on a failure to set up the device, or if the name is a duplicate.
9261 *
9262 * Callers must hold the rtnl semaphore. You may want
9263 * register_netdev() instead of this.
9264 *
9265 * BUGS:
9266 * The locking appears insufficient to guarantee two parallel registers
9267 * will not get the same name.
9268 */
9269
9270int register_netdevice(struct net_device *dev)
9271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009273 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274
Florian Fainellie283de32018-04-30 14:20:05 -07009275 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9276 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 BUG_ON(dev_boot_phase);
9278 ASSERT_RTNL();
9279
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009280 might_sleep();
9281
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282 /* When net_device's are persistent, this will be fatal. */
9283 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009284 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285
David S. Millerf1f28aa2008-07-15 00:08:33 -07009286 spin_lock_init(&dev->addr_list_lock);
Taehee Yooab92d682019-10-21 18:47:51 +00009287 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288
Gao feng828de4f2012-09-13 20:58:27 +00009289 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00009290 if (ret < 0)
9291 goto out;
9292
Eric Dumazet9077f052019-10-03 08:59:24 -07009293 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +02009294 dev->name_node = netdev_name_node_head_alloc(dev);
9295 if (!dev->name_node)
9296 goto out;
9297
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009299 if (dev->netdev_ops->ndo_init) {
9300 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301 if (ret) {
9302 if (ret > 0)
9303 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009304 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 }
9306 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009307
Patrick McHardyf6469682013-04-19 02:04:27 +00009308 if (((dev->hw_features | dev->features) &
9309 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00009310 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9311 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9312 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9313 ret = -EINVAL;
9314 goto err_uninit;
9315 }
9316
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00009317 ret = -EBUSY;
9318 if (!dev->ifindex)
9319 dev->ifindex = dev_new_index(net);
9320 else if (__dev_get_by_index(net, dev->ifindex))
9321 goto err_uninit;
9322
Michał Mirosław5455c692011-02-15 16:59:17 +00009323 /* Transfer changeable features to wanted_features and enable
9324 * software offloads (GSO and GRO).
9325 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +01009326 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +00009327 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02009328
9329 if (dev->netdev_ops->ndo_udp_tunnel_add) {
9330 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9331 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9332 }
9333
Michał Mirosław14d12322011-02-22 16:52:28 +00009334 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009336 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00009337 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009338
Alexander Duyck7f348a62016-04-20 16:51:00 -04009339 /* If IPv4 TCP segmentation offload is supported we should also
9340 * allow the device to enable segmenting the frame with the option
9341 * of ignoring a static IP ID value. This doesn't enable the
9342 * feature itself but allows the user to enable it later.
9343 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009344 if (dev->hw_features & NETIF_F_TSO)
9345 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04009346 if (dev->vlan_features & NETIF_F_TSO)
9347 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9348 if (dev->mpls_features & NETIF_F_TSO)
9349 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9350 if (dev->hw_enc_features & NETIF_F_TSO)
9351 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07009352
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009353 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00009354 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009355 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00009356
Pravin B Shelaree579672013-03-07 09:28:08 +00009357 /* Make NETIF_F_SG inheritable to tunnel devices.
9358 */
Alexander Duyck802ab552016-04-10 21:45:03 -04009359 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00009360
Simon Horman0d89d202013-05-23 21:02:52 +00009361 /* Make NETIF_F_SG inheritable to MPLS.
9362 */
9363 dev->mpls_features |= NETIF_F_SG;
9364
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00009365 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9366 ret = notifier_to_errno(ret);
9367 if (ret)
9368 goto err_uninit;
9369
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009370 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +02009371 if (ret) {
9372 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009373 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +02009374 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009375 dev->reg_state = NETREG_REGISTERED;
9376
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009377 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00009378
Linus Torvalds1da177e2005-04-16 15:20:36 -07009379 /*
9380 * Default initial state at registry is that the
9381 * device is present.
9382 */
9383
9384 set_bit(__LINK_STATE_PRESENT, &dev->state);
9385
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01009386 linkwatch_init_dev(dev);
9387
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009390 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04009391 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392
Jiri Pirko948b3372013-01-08 01:38:25 +00009393 /* If the device has permanent device address, driver should
9394 * set dev_addr and also addr_assign_type should be set to
9395 * NET_ADDR_PERM (default value).
9396 */
9397 if (dev->addr_assign_type == NET_ADDR_PERM)
9398 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
9399
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009401 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07009402 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009403 if (ret) {
9404 rollback_registered(dev);
Subash Abhinov Kasiviswanathan10cc5142019-09-10 14:02:57 -06009405 rcu_barrier();
9406
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009407 dev->reg_state = NETREG_UNREGISTERED;
9408 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009409 /*
9410 * Prevent userspace races by waiting until the network
9411 * device is fully setup before sending notifications.
9412 */
Patrick McHardya2835762010-02-26 06:34:51 +00009413 if (!dev->rtnl_link_ops ||
9414 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07009415 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416
9417out:
9418 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009419
9420err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009421 if (dev->netdev_ops->ndo_uninit)
9422 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04009423 if (dev->priv_destructor)
9424 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009425err_free_name:
9426 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009427 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009429EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430
9431/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009432 * init_dummy_netdev - init a dummy network device for NAPI
9433 * @dev: device to init
9434 *
9435 * This takes a network device structure and initialize the minimum
9436 * amount of fields so it can be used to schedule NAPI polls without
9437 * registering a full blown interface. This is to be used by drivers
9438 * that need to tie several hardware interfaces to a single NAPI
9439 * poll scheduler due to HW limitations.
9440 */
9441int init_dummy_netdev(struct net_device *dev)
9442{
9443 /* Clear everything. Note we don't initialize spinlocks
9444 * are they aren't supposed to be taken by any of the
9445 * NAPI code and this dummy netdev is supposed to be
9446 * only ever used for NAPI polls
9447 */
9448 memset(dev, 0, sizeof(struct net_device));
9449
9450 /* make sure we BUG if trying to hit standard
9451 * register/unregister code path
9452 */
9453 dev->reg_state = NETREG_DUMMY;
9454
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009455 /* NAPI wants this */
9456 INIT_LIST_HEAD(&dev->napi_list);
9457
9458 /* a dummy interface is started by default */
9459 set_bit(__LINK_STATE_PRESENT, &dev->state);
9460 set_bit(__LINK_STATE_START, &dev->state);
9461
Josh Elsasser35edfdc2019-01-26 14:38:33 -08009462 /* napi_busy_loop stats accounting wants this */
9463 dev_net_set(dev, &init_net);
9464
Eric Dumazet29b44332010-10-11 10:22:12 +00009465 /* Note : We dont allocate pcpu_refcnt for dummy devices,
9466 * because users of this 'device' dont need to change
9467 * its refcount.
9468 */
9469
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009470 return 0;
9471}
9472EXPORT_SYMBOL_GPL(init_dummy_netdev);
9473
9474
9475/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 * register_netdev - register a network device
9477 * @dev: device to register
9478 *
9479 * Take a completed network device structure and add it to the kernel
9480 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9481 * chain. 0 is returned on success. A negative errno code is returned
9482 * on a failure to set up the device, or if the name is a duplicate.
9483 *
Borislav Petkov38b4da382007-04-20 22:14:10 -07009484 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485 * and expands the device name if you passed a format string to
9486 * alloc_netdev.
9487 */
9488int register_netdev(struct net_device *dev)
9489{
9490 int err;
9491
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +03009492 if (rtnl_lock_killable())
9493 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495 rtnl_unlock();
9496 return err;
9497}
9498EXPORT_SYMBOL(register_netdev);
9499
Eric Dumazet29b44332010-10-11 10:22:12 +00009500int netdev_refcnt_read(const struct net_device *dev)
9501{
9502 int i, refcnt = 0;
9503
9504 for_each_possible_cpu(i)
9505 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
9506 return refcnt;
9507}
9508EXPORT_SYMBOL(netdev_refcnt_read);
9509
Ben Hutchings2c530402012-07-10 10:55:09 +00009510/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009511 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00009512 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009513 *
9514 * This is called when unregistering network devices.
9515 *
9516 * Any protocol or device that holds a reference should register
9517 * for netdevice notification, and cleanup and put back the
9518 * reference if they receive an UNREGISTER event.
9519 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009520 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 */
9522static void netdev_wait_allrefs(struct net_device *dev)
9523{
9524 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00009525 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526
Eric Dumazete014deb2009-11-17 05:59:21 +00009527 linkwatch_forget_dev(dev);
9528
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00009530 refcnt = netdev_refcnt_read(dev);
9531
9532 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009534 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535
9536 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009537 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538
Eric Dumazet748e2d92012-08-22 21:50:59 +00009539 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009540 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00009541 rtnl_lock();
9542
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
9544 &dev->state)) {
9545 /* We must not have linkwatch events
9546 * pending on unregister. If this
9547 * happens, we simply run the queue
9548 * unscheduled, resulting in a noop
9549 * for this device.
9550 */
9551 linkwatch_run_queue();
9552 }
9553
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009554 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555
9556 rebroadcast_time = jiffies;
9557 }
9558
9559 msleep(250);
9560
Eric Dumazet29b44332010-10-11 10:22:12 +00009561 refcnt = netdev_refcnt_read(dev);
9562
Eric Dumazetd7c04b02019-05-16 08:09:57 -07009563 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009564 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9565 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566 warning_time = jiffies;
9567 }
9568 }
9569}
9570
9571/* The sequence is:
9572 *
9573 * rtnl_lock();
9574 * ...
9575 * register_netdevice(x1);
9576 * register_netdevice(x2);
9577 * ...
9578 * unregister_netdevice(y1);
9579 * unregister_netdevice(y2);
9580 * ...
9581 * rtnl_unlock();
9582 * free_netdev(y1);
9583 * free_netdev(y2);
9584 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07009585 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009587 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588 * without deadlocking with linkwatch via keventd.
9589 * 2) Since we run with the RTNL semaphore not held, we can sleep
9590 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07009591 *
9592 * We must not return until all unregister events added during
9593 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595void netdev_run_todo(void)
9596{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009597 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009598
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009600 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07009601
9602 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009603
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009604
9605 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00009606 if (!list_empty(&list))
9607 rcu_barrier();
9608
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609 while (!list_empty(&list)) {
9610 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00009611 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612 list_del(&dev->todo_list);
9613
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009614 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009615 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009617 dump_stack();
9618 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009620
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009621 dev->reg_state = NETREG_UNREGISTERED;
9622
9623 netdev_wait_allrefs(dev);
9624
9625 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00009626 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08009627 BUG_ON(!list_empty(&dev->ptype_all));
9628 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00009629 WARN_ON(rcu_access_pointer(dev->ip_ptr));
9630 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -08009631#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -07009632 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -08009633#endif
David S. Millercf124db2017-05-08 12:52:56 -04009634 if (dev->priv_destructor)
9635 dev->priv_destructor(dev);
9636 if (dev->needs_free_netdev)
9637 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009638
Eric W. Biederman50624c92013-09-23 21:19:49 -07009639 /* Report a network device has been unregistered */
9640 rtnl_lock();
9641 dev_net(dev)->dev_unreg_count--;
9642 __rtnl_unlock();
9643 wake_up(&netdev_unregistering_wq);
9644
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009645 /* Free network device */
9646 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648}
9649
Jarod Wilson92566452016-02-01 18:51:04 -05009650/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
9651 * all the same fields in the same order as net_device_stats, with only
9652 * the type differing, but rtnl_link_stats64 may have additional fields
9653 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00009654 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009655void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
9656 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00009657{
9658#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05009659 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02009660 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05009661 /* zero out counters that only exist in rtnl_link_stats64 */
9662 memset((char *)stats64 + sizeof(*netdev_stats), 0,
9663 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009664#else
Jarod Wilson92566452016-02-01 18:51:04 -05009665 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009666 const unsigned long *src = (const unsigned long *)netdev_stats;
9667 u64 *dst = (u64 *)stats64;
9668
Jarod Wilson92566452016-02-01 18:51:04 -05009669 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009670 for (i = 0; i < n; i++)
9671 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05009672 /* zero out counters that only exist in rtnl_link_stats64 */
9673 memset((char *)stats64 + n * sizeof(u64), 0,
9674 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009675#endif
9676}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009677EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009678
Eric Dumazetd83345a2009-11-16 03:36:51 +00009679/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009680 * dev_get_stats - get network device statistics
9681 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07009682 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009683 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00009684 * Get network statistics from device. Return @storage.
9685 * The device driver may provide its own method by setting
9686 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
9687 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009688 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00009689struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
9690 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00009691{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009692 const struct net_device_ops *ops = dev->netdev_ops;
9693
Eric Dumazet28172732010-07-07 14:58:56 -07009694 if (ops->ndo_get_stats64) {
9695 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00009696 ops->ndo_get_stats64(dev, storage);
9697 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00009698 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00009699 } else {
9700 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07009701 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07009702 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
9703 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
9704 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07009705 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07009706}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009707EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07009708
Eric Dumazet24824a02010-10-02 06:11:55 +00009709struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07009710{
Eric Dumazet24824a02010-10-02 06:11:55 +00009711 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07009712
Eric Dumazet24824a02010-10-02 06:11:55 +00009713#ifdef CONFIG_NET_CLS_ACT
9714 if (queue)
9715 return queue;
9716 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
9717 if (!queue)
9718 return NULL;
9719 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08009720 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00009721 queue->qdisc_sleeping = &noop_qdisc;
9722 rcu_assign_pointer(dev->ingress_queue, queue);
9723#endif
9724 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07009725}
9726
Eric Dumazet2c60db02012-09-16 09:17:26 +00009727static const struct ethtool_ops default_ethtool_ops;
9728
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00009729void netdev_set_default_ethtool_ops(struct net_device *dev,
9730 const struct ethtool_ops *ops)
9731{
9732 if (dev->ethtool_ops == &default_ethtool_ops)
9733 dev->ethtool_ops = ops;
9734}
9735EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
9736
Eric Dumazet74d332c2013-10-30 13:10:44 -07009737void netdev_freemem(struct net_device *dev)
9738{
9739 char *addr = (char *)dev - dev->padded;
9740
WANG Cong4cb28972014-06-02 15:55:22 -07009741 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009742}
9743
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744/**
tcharding722c9a02017-02-09 17:56:04 +11009745 * alloc_netdev_mqs - allocate network device
9746 * @sizeof_priv: size of private data to allocate space for
9747 * @name: device name format string
9748 * @name_assign_type: origin of device name
9749 * @setup: callback to initialize device
9750 * @txqs: the number of TX subqueues to allocate
9751 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 *
tcharding722c9a02017-02-09 17:56:04 +11009753 * Allocates a struct net_device with private data area for driver use
9754 * and performs basic initialization. Also allocates subqueue structs
9755 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 */
Tom Herbert36909ea2011-01-09 19:36:31 +00009757struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02009758 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00009759 void (*setup)(struct net_device *),
9760 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03009763 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009764 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07009766 BUG_ON(strlen(name) >= sizeof(dev->name));
9767
Tom Herbert36909ea2011-01-09 19:36:31 +00009768 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009769 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00009770 return NULL;
9771 }
9772
Tom Herbert36909ea2011-01-09 19:36:31 +00009773 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009774 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00009775 return NULL;
9776 }
Tom Herbert36909ea2011-01-09 19:36:31 +00009777
David S. Millerfd2ea0a2008-07-17 01:56:23 -07009778 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009779 if (sizeof_priv) {
9780 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009781 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009782 alloc_size += sizeof_priv;
9783 }
9784 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009785 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786
Michal Hockodcda9b02017-07-12 14:36:45 -07009787 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00009788 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009791 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009793
Eric Dumazet29b44332010-10-11 10:22:12 +00009794 dev->pcpu_refcnt = alloc_percpu(int);
9795 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07009796 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009797
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00009799 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800
Jiri Pirko22bedad32010-04-01 21:22:57 +00009801 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009802 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00009803
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09009804 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
Taehee Yooab92d682019-10-21 18:47:51 +00009806 netdev_register_lockdep_key(dev);
9807
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07009808 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00009809 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +00009810 dev->upper_level = 1;
9811 dev->lower_level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812
Herbert Xud565b0a2008-12-15 23:38:52 -08009813 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009814 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009815 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00009816 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02009817 INIT_LIST_HEAD(&dev->adj_list.upper);
9818 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08009819 INIT_LIST_HEAD(&dev->ptype_all);
9820 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +01009821 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02009822#ifdef CONFIG_NET_SCHED
9823 hash_init(dev->qdisc_hash);
9824#endif
Eric Dumazet02875872014-10-05 18:38:35 -07009825 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009827
Phil Suttera8131042016-02-17 15:37:43 +01009828 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02009829 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01009830 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01009831 }
Phil Sutter906470c2015-08-18 10:30:48 +02009832
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009833 dev->num_tx_queues = txqs;
9834 dev->real_num_tx_queues = txqs;
9835 if (netif_alloc_netdev_queues(dev))
9836 goto free_all;
9837
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009838 dev->num_rx_queues = rxqs;
9839 dev->real_num_rx_queues = rxqs;
9840 if (netif_alloc_rx_queues(dev))
9841 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009842
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02009844 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00009845 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00009846 if (!dev->ethtool_ops)
9847 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02009848
9849 nf_hook_ingress_init(dev);
9850
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009852
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009853free_all:
9854 free_netdev(dev);
9855 return NULL;
9856
Eric Dumazet29b44332010-10-11 10:22:12 +00009857free_pcpu:
9858 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009859free_dev:
9860 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009861 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862}
Tom Herbert36909ea2011-01-09 19:36:31 +00009863EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864
9865/**
tcharding722c9a02017-02-09 17:56:04 +11009866 * free_netdev - free network device
9867 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868 *
tcharding722c9a02017-02-09 17:56:04 +11009869 * This function does the last stage of destroying an allocated device
9870 * interface. The reference to the device object is released. If this
9871 * is the last reference then it will be freed.Must be called in process
9872 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873 */
9874void free_netdev(struct net_device *dev)
9875{
Herbert Xud565b0a2008-12-15 23:38:52 -08009876 struct napi_struct *p, *n;
9877
Eric Dumazet93d05d42015-11-18 06:31:03 -08009878 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07009879 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009880 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -07009881
Eric Dumazet33d480c2011-08-11 19:30:52 +00009882 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00009883
Jiri Pirkof001fde2009-05-05 02:48:28 +00009884 /* Flush device addresses */
9885 dev_addr_flush(dev);
9886
Herbert Xud565b0a2008-12-15 23:38:52 -08009887 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
9888 netif_napi_del(p);
9889
Eric Dumazet29b44332010-10-11 10:22:12 +00009890 free_percpu(dev->pcpu_refcnt);
9891 dev->pcpu_refcnt = NULL;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +01009892 free_percpu(dev->xdp_bulkq);
9893 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +00009894
Taehee Yooab92d682019-10-21 18:47:51 +00009895 netdev_unregister_lockdep_key(dev);
9896
Stephen Hemminger3041a062006-05-26 13:25:24 -07009897 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009898 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07009899 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 return;
9901 }
9902
9903 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
9904 dev->reg_state = NETREG_RELEASED;
9905
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07009906 /* will free via device release */
9907 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009909EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009910
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009911/**
9912 * synchronize_net - Synchronize with packet receive processing
9913 *
9914 * Wait for packets currently being received to be done.
9915 * Does not block later packets from starting.
9916 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009917void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009918{
9919 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00009920 if (rtnl_is_locked())
9921 synchronize_rcu_expedited();
9922 else
9923 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009925EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926
9927/**
Eric Dumazet44a08732009-10-27 07:03:04 +00009928 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00009930 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08009931 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009933 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00009934 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009935 *
9936 * Callers must hold the rtnl semaphore. You may want
9937 * unregister_netdev() instead of this.
9938 */
9939
Eric Dumazet44a08732009-10-27 07:03:04 +00009940void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941{
Herbert Xua6620712007-12-12 19:21:56 -08009942 ASSERT_RTNL();
9943
Eric Dumazet44a08732009-10-27 07:03:04 +00009944 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009945 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00009946 } else {
9947 rollback_registered(dev);
9948 /* Finish processing unregister after unlock */
9949 net_set_todo(dev);
9950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951}
Eric Dumazet44a08732009-10-27 07:03:04 +00009952EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953
9954/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009955 * unregister_netdevice_many - unregister many devices
9956 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07009957 *
9958 * Note: As most callers use a stack allocated list_head,
9959 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009960 */
9961void unregister_netdevice_many(struct list_head *head)
9962{
9963 struct net_device *dev;
9964
9965 if (!list_empty(head)) {
9966 rollback_registered_many(head);
9967 list_for_each_entry(dev, head, unreg_list)
9968 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07009969 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009970 }
9971}
Eric Dumazet63c80992009-10-27 07:06:49 +00009972EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009973
9974/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975 * unregister_netdev - remove device from the kernel
9976 * @dev: device
9977 *
9978 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009979 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 *
9981 * This is just a wrapper for unregister_netdevice that takes
9982 * the rtnl semaphore. In general you want to use this and not
9983 * unregister_netdevice.
9984 */
9985void unregister_netdev(struct net_device *dev)
9986{
9987 rtnl_lock();
9988 unregister_netdevice(dev);
9989 rtnl_unlock();
9990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991EXPORT_SYMBOL(unregister_netdev);
9992
Eric W. Biedermance286d32007-09-12 13:53:49 +02009993/**
9994 * dev_change_net_namespace - move device to different nethost namespace
9995 * @dev: device
9996 * @net: network namespace
9997 * @pat: If not NULL name pattern to try if the current device name
9998 * is already taken in the destination network namespace.
9999 *
10000 * This function shuts down a device interface and moves it
10001 * to a new network namespace. On success 0 is returned, on
10002 * a failure a netagive errno code is returned.
10003 *
10004 * Callers must hold the rtnl semaphore.
10005 */
10006
10007int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10008{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010009 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010010
10011 ASSERT_RTNL();
10012
10013 /* Don't allow namespace local devices to be moved. */
10014 err = -EINVAL;
10015 if (dev->features & NETIF_F_NETNS_LOCAL)
10016 goto out;
10017
10018 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010019 if (dev->reg_state != NETREG_REGISTERED)
10020 goto out;
10021
10022 /* Get out if there is nothing todo */
10023 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +090010024 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020010025 goto out;
10026
10027 /* Pick the destination device name, and ensure
10028 * we can use it in the destination network namespace.
10029 */
10030 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +000010031 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010032 /* We get here if we can't use the current device name */
10033 if (!pat)
10034 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080010035 err = dev_get_valid_name(net, dev, pat);
10036 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010037 goto out;
10038 }
10039
10040 /*
10041 * And now a mini version of register_netdevice unregister_netdevice.
10042 */
10043
10044 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070010045 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010046
10047 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010048 unlist_netdevice(dev);
10049
10050 synchronize_net();
10051
10052 /* Shutdown queueing discipline. */
10053 dev_shutdown(dev);
10054
10055 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110010056 * this device. They should clean all the things.
10057 *
10058 * Note that dev->reg_state stays at NETREG_REGISTERED.
10059 * This is wanted because this way 8021q and macvlan know
10060 * the device is just moving and can keep their slaves up.
10061 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010062 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010063 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010064
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010065 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010066 /* If there is an ifindex conflict assign a new one */
10067 if (__dev_get_by_index(net, dev->ifindex))
10068 new_ifindex = dev_new_index(net);
10069 else
10070 new_ifindex = dev->ifindex;
10071
10072 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10073 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010074
10075 /*
10076 * Flush the unicast and multicast chains
10077 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010078 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010079 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010080
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010081 /* Send a netdev-removed uevent to the old namespace */
10082 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010083 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010084
Jiri Pirko93642e12020-01-25 12:17:08 +010010085 /* Move per-net netdevice notifiers that are following the netdevice */
10086 move_netdevice_notifiers_dev_net(dev, net);
10087
Eric W. Biedermance286d32007-09-12 13:53:49 +020010088 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010089 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010090 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010091
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010092 /* Send a netdev-add uevent to the new namespace */
10093 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010094 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010095
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010096 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010097 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010098 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010099
10100 /* Add the device back in the hashes */
10101 list_netdevice(dev);
10102
10103 /* Notify protocols, that a new device appeared. */
10104 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10105
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010106 /*
10107 * Prevent userspace races by waiting until the network
10108 * device is fully setup before sending notifications.
10109 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010110 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010111
Eric W. Biedermance286d32007-09-12 13:53:49 +020010112 synchronize_net();
10113 err = 0;
10114out:
10115 return err;
10116}
Johannes Berg463d0182009-07-14 00:33:35 +020010117EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010118
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010119static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120{
10121 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010123 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010124 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126 local_irq_disable();
10127 cpu = smp_processor_id();
10128 sd = &per_cpu(softnet_data, cpu);
10129 oldsd = &per_cpu(softnet_data, oldcpu);
10130
10131 /* Find end of our completion_queue. */
10132 list_skb = &sd->completion_queue;
10133 while (*list_skb)
10134 list_skb = &(*list_skb)->next;
10135 /* Append completion queue from offline CPU. */
10136 *list_skb = oldsd->completion_queue;
10137 oldsd->completion_queue = NULL;
10138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010140 if (oldsd->output_queue) {
10141 *sd->output_queue_tailp = oldsd->output_queue;
10142 sd->output_queue_tailp = oldsd->output_queue_tailp;
10143 oldsd->output_queue = NULL;
10144 oldsd->output_queue_tailp = &oldsd->output_queue;
10145 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010146 /* Append NAPI poll list from offline CPU, with one exception :
10147 * process_backlog() must be called by cpu owning percpu backlog.
10148 * We properly handle process_queue & input_pkt_queue later.
10149 */
10150 while (!list_empty(&oldsd->poll_list)) {
10151 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10152 struct napi_struct,
10153 poll_list);
10154
10155 list_del_init(&napi->poll_list);
10156 if (napi->poll == process_backlog)
10157 napi->state = 0;
10158 else
10159 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161
10162 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10163 local_irq_enable();
10164
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010165#ifdef CONFIG_RPS
10166 remsd = oldsd->rps_ipi_list;
10167 oldsd->rps_ipi_list = NULL;
10168#endif
10169 /* send out pending IPI's on offline CPU */
10170 net_rps_send_ipi(remsd);
10171
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010173 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010174 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010175 input_queue_head_incr(oldsd);
10176 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010177 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010178 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010179 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010181
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184
Herbert Xu7f353bf2007-08-10 15:47:58 -070010185/**
Herbert Xub63365a2008-10-23 01:11:29 -070010186 * netdev_increment_features - increment feature set by one
10187 * @all: current feature set
10188 * @one: new feature set
10189 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010190 *
10191 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010192 * @one to the master device with current feature set @all. Will not
10193 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010194 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010195netdev_features_t netdev_increment_features(netdev_features_t all,
10196 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010197{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010198 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010199 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010200 mask |= NETIF_F_VLAN_CHALLENGED;
10201
Tom Herberta1882222015-12-14 11:19:43 -080010202 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010203 all &= one | ~NETIF_F_ALL_FOR_ALL;
10204
Michał Mirosław1742f182011-04-22 06:31:16 +000010205 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010206 if (all & NETIF_F_HW_CSUM)
10207 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010208
10209 return all;
10210}
Herbert Xub63365a2008-10-23 01:11:29 -070010211EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010212
Baruch Siach430f03c2013-06-02 20:43:55 +000010213static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010214{
10215 int i;
10216 struct hlist_head *hash;
10217
Kees Cook6da2ec52018-06-12 13:55:00 -070010218 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010219 if (hash != NULL)
10220 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10221 INIT_HLIST_HEAD(&hash[i]);
10222
10223 return hash;
10224}
10225
Eric W. Biederman881d9662007-09-17 11:56:21 -070010226/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070010227static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010228{
Li RongQingd9f37d02018-07-13 14:41:36 +080010229 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080010230 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080010231
Rustad, Mark D734b6542012-07-18 09:06:07 +000010232 if (net != &init_net)
10233 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070010234
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010235 net->dev_name_head = netdev_create_hash();
10236 if (net->dev_name_head == NULL)
10237 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010238
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010239 net->dev_index_head = netdev_create_hash();
10240 if (net->dev_index_head == NULL)
10241 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010242
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020010243 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10244
Eric W. Biederman881d9662007-09-17 11:56:21 -070010245 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010246
10247err_idx:
10248 kfree(net->dev_name_head);
10249err_name:
10250 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010251}
10252
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010253/**
10254 * netdev_drivername - network driver for the device
10255 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010256 *
10257 * Determine network driver for device.
10258 */
David S. Miller3019de12011-06-06 16:41:33 -070010259const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070010260{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070010261 const struct device_driver *driver;
10262 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070010263 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070010264
10265 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010266 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070010267 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010268
10269 driver = parent->driver;
10270 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070010271 return driver->name;
10272 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010273}
10274
Joe Perches6ea754e2014-09-22 11:10:50 -070010275static void __netdev_printk(const char *level, const struct net_device *dev,
10276 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000010277{
Joe Perchesb004ff42012-09-12 20:12:19 -070010278 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010279 dev_printk_emit(level[1] - '0',
10280 dev->dev.parent,
10281 "%s %s %s%s: %pV",
10282 dev_driver_string(dev->dev.parent),
10283 dev_name(dev->dev.parent),
10284 netdev_name(dev), netdev_reg_state(dev),
10285 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010286 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010287 printk("%s%s%s: %pV",
10288 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010289 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070010290 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010291 }
Joe Perches256df2f2010-06-27 01:02:35 +000010292}
10293
Joe Perches6ea754e2014-09-22 11:10:50 -070010294void netdev_printk(const char *level, const struct net_device *dev,
10295 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000010296{
10297 struct va_format vaf;
10298 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000010299
10300 va_start(args, format);
10301
10302 vaf.fmt = format;
10303 vaf.va = &args;
10304
Joe Perches6ea754e2014-09-22 11:10:50 -070010305 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010306
Joe Perches256df2f2010-06-27 01:02:35 +000010307 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000010308}
10309EXPORT_SYMBOL(netdev_printk);
10310
10311#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070010312void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000010313{ \
Joe Perches256df2f2010-06-27 01:02:35 +000010314 struct va_format vaf; \
10315 va_list args; \
10316 \
10317 va_start(args, fmt); \
10318 \
10319 vaf.fmt = fmt; \
10320 vaf.va = &args; \
10321 \
Joe Perches6ea754e2014-09-22 11:10:50 -070010322 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070010323 \
Joe Perches256df2f2010-06-27 01:02:35 +000010324 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000010325} \
10326EXPORT_SYMBOL(func);
10327
10328define_netdev_printk_level(netdev_emerg, KERN_EMERG);
10329define_netdev_printk_level(netdev_alert, KERN_ALERT);
10330define_netdev_printk_level(netdev_crit, KERN_CRIT);
10331define_netdev_printk_level(netdev_err, KERN_ERR);
10332define_netdev_printk_level(netdev_warn, KERN_WARNING);
10333define_netdev_printk_level(netdev_notice, KERN_NOTICE);
10334define_netdev_printk_level(netdev_info, KERN_INFO);
10335
Pavel Emelyanov46650792007-10-08 20:38:39 -070010336static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010337{
10338 kfree(net->dev_name_head);
10339 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030010340 if (net != &init_net)
10341 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070010342}
10343
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010344static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070010345 .init = netdev_init,
10346 .exit = netdev_exit,
10347};
10348
Pavel Emelyanov46650792007-10-08 20:38:39 -070010349static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010350{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010351 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010352 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010353 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020010354 * initial network namespace
10355 */
10356 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010357 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010358 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010359 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020010360
10361 /* Ignore unmoveable devices (i.e. loopback) */
10362 if (dev->features & NETIF_F_NETNS_LOCAL)
10363 continue;
10364
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010365 /* Leave virtual devices for the generic cleanup */
10366 if (dev->rtnl_link_ops)
10367 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080010368
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010369 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010370 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020010371 if (__dev_get_by_name(&init_net, fb_name))
10372 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010373 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010374 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010375 pr_emerg("%s: failed to move %s to init_net: %d\n",
10376 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010377 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020010378 }
10379 }
10380 rtnl_unlock();
10381}
10382
Eric W. Biederman50624c92013-09-23 21:19:49 -070010383static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
10384{
10385 /* Return with the rtnl_lock held when there are no network
10386 * devices unregistering in any network namespace in net_list.
10387 */
10388 struct net *net;
10389 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010010390 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010391
Peter Zijlstraff960a72014-10-29 17:04:56 +010010392 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010393 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070010394 unregistering = false;
10395 rtnl_lock();
10396 list_for_each_entry(net, net_list, exit_list) {
10397 if (net->dev_unreg_count > 0) {
10398 unregistering = true;
10399 break;
10400 }
10401 }
10402 if (!unregistering)
10403 break;
10404 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010010405
10406 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010407 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010010408 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010409}
10410
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010411static void __net_exit default_device_exit_batch(struct list_head *net_list)
10412{
10413 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040010414 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010415 * Do this across as many network namespaces as possible to
10416 * improve batching efficiency.
10417 */
10418 struct net_device *dev;
10419 struct net *net;
10420 LIST_HEAD(dev_kill_list);
10421
Eric W. Biederman50624c92013-09-23 21:19:49 -070010422 /* To prevent network device cleanup code from dereferencing
10423 * loopback devices or network devices that have been freed
10424 * wait here for all pending unregistrations to complete,
10425 * before unregistring the loopback device and allowing the
10426 * network namespace be freed.
10427 *
10428 * The netdev todo list containing all network devices
10429 * unregistrations that happen in default_device_exit_batch
10430 * will run in the rtnl_unlock() at the end of
10431 * default_device_exit_batch.
10432 */
10433 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010434 list_for_each_entry(net, net_list, exit_list) {
10435 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020010436 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010437 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
10438 else
10439 unregister_netdevice_queue(dev, &dev_kill_list);
10440 }
10441 }
10442 unregister_netdevice_many(&dev_kill_list);
10443 rtnl_unlock();
10444}
10445
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010446static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010447 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010448 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020010449};
10450
Linus Torvalds1da177e2005-04-16 15:20:36 -070010451/*
10452 * Initialize the DEV module. At boot time this walks the device list and
10453 * unhooks any devices that fail to initialise (normally hardware not
10454 * present) and leaves us with a valid list of present and active devices.
10455 *
10456 */
10457
10458/*
10459 * This is called single threaded during boot, so no need
10460 * to take the rtnl semaphore.
10461 */
10462static int __init net_dev_init(void)
10463{
10464 int i, rc = -ENOMEM;
10465
10466 BUG_ON(!dev_boot_phase);
10467
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468 if (dev_proc_init())
10469 goto out;
10470
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010471 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070010472 goto out;
10473
10474 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080010475 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 INIT_LIST_HEAD(&ptype_base[i]);
10477
Vlad Yasevich62532da2012-11-15 08:49:10 +000010478 INIT_LIST_HEAD(&offload_base);
10479
Eric W. Biederman881d9662007-09-17 11:56:21 -070010480 if (register_pernet_subsys(&netdev_net_ops))
10481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482
10483 /*
10484 * Initialise the packet receive queues.
10485 */
10486
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070010487 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070010488 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010489 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010490
Eric Dumazet41852492016-08-26 12:50:39 -070010491 INIT_WORK(flush, flush_backlog);
10492
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010493 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070010494 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010010495#ifdef CONFIG_XFRM_OFFLOAD
10496 skb_queue_head_init(&sd->xfrm_backlog);
10497#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010498 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000010499 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000010500#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010501 sd->csd.func = rps_trigger_softirq;
10502 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010503 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070010504#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000010505
David S. Miller7c4ec742018-07-20 23:37:55 -070010506 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010507 sd->backlog.poll = process_backlog;
10508 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 }
10510
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511 dev_boot_phase = 0;
10512
Eric W. Biederman505d4f72008-11-07 22:54:20 -080010513 /* The loopback device is special if any other network devices
10514 * is present in a network namespace the loopback device must
10515 * be present. Since we now dynamically allocate and free the
10516 * loopback device ensure this invariant is maintained by
10517 * keeping the loopback device as the first device on the
10518 * list of network devices. Ensuring the loopback devices
10519 * is the first device that appears and the last network device
10520 * that disappears.
10521 */
10522 if (register_pernet_device(&loopback_net_ops))
10523 goto out;
10524
10525 if (register_pernet_device(&default_device_ops))
10526 goto out;
10527
Carlos R. Mafra962cf362008-05-15 11:15:37 -030010528 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
10529 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010531 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
10532 NULL, dev_cpu_dead);
10533 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534 rc = 0;
10535out:
10536 return rc;
10537}
10538
10539subsys_initcall(net_dev_init);