blob: 2f17a4ac82f0e35fa4240069197c317340c336f9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
tcharding722c9a02017-02-09 17:56:04 +11003 * NET3 Protocol independent device support routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Derived from the non IP parts of dev.c 1.0.19
tcharding722c9a02017-02-09 17:56:04 +11006 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
9 *
10 * Additional Authors:
11 * Florian la Roche <rzsfl@rz.uni-sb.de>
12 * Alan Cox <gw4pts@gw4pts.ampr.org>
13 * David Hinds <dahinds@users.sourceforge.net>
14 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
15 * Adam Sulmicki <adam@cfar.umd.edu>
16 * Pekka Riikonen <priikone@poesidon.pspt.fi>
17 *
18 * Changes:
19 * D.J. Barrow : Fixed bug where dev->refcnt gets set
tcharding722c9a02017-02-09 17:56:04 +110020 * to 2 if register_netdev gets called
21 * before net_dev_init & also removed a
22 * few lines of code in the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Alan Cox : device private ioctl copies fields back.
24 * Alan Cox : Transmit queue code does relevant
25 * stunts to keep the queue safe.
26 * Alan Cox : Fixed double lock.
27 * Alan Cox : Fixed promisc NULL pointer trap
28 * ???????? : Support the full private ioctl range
29 * Alan Cox : Moved ioctl permission check into
30 * drivers
31 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
32 * Alan Cox : 100 backlog just doesn't cut it when
33 * you start doing multicast video 8)
34 * Alan Cox : Rewrote net_bh and list manager.
tcharding722c9a02017-02-09 17:56:04 +110035 * Alan Cox : Fix ETH_P_ALL echoback lengths.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * Alan Cox : Took out transmit every packet pass
37 * Saved a few bytes in the ioctl handler
38 * Alan Cox : Network driver sets packet type before
39 * calling netif_rx. Saves a function
40 * call a packet.
41 * Alan Cox : Hashed net_bh()
42 * Richard Kooijman: Timestamp fixes.
43 * Alan Cox : Wrong field in SIOCGIFDSTADDR
44 * Alan Cox : Device lock protection.
tcharding722c9a02017-02-09 17:56:04 +110045 * Alan Cox : Fixed nasty side effect of device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * changes.
47 * Rudi Cilibrasi : Pass the right thing to
48 * set_mac_address()
49 * Dave Miller : 32bit quantity for the device lock to
50 * make it work out on a Sparc.
51 * Bjorn Ekwall : Added KERNELD hack.
52 * Alan Cox : Cleaned up the backlog initialise.
53 * Craig Metz : SIOCGIFCONF fix if space for under
54 * 1 device.
55 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
56 * is no device open function.
57 * Andi Kleen : Fix error reporting for SIOCGIFCONF
58 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
59 * Cyrus Durgin : Cleaned for KMOD
60 * Adam Sulmicki : Bug Fix : Network Device Unload
61 * A network device unload needs to purge
62 * the backlog queue.
63 * Paul Rusty Russell : SIOCSIFNAME
64 * Pekka Riikonen : Netdev boot-time settings code
65 * Andrew Morton : Make unregister_netdevice wait
tcharding722c9a02017-02-09 17:56:04 +110066 * indefinitely on dev->refcnt
67 * J Hadi Salim : - Backlog queue sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * - netif_rx() feedback
69 */
70
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080073#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/cpu.h>
75#include <linux/types.h>
76#include <linux/kernel.h>
stephen hemminger08e98972009-11-10 07:20:34 +000077#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090078#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -070080#include <linux/sched/mm.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080081#include <linux/mutex.h>
Ahmed S. Darwish11d60112020-06-03 16:49:44 +020082#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/string.h>
84#include <linux/mm.h>
85#include <linux/socket.h>
86#include <linux/sockios.h>
87#include <linux/errno.h>
88#include <linux/interrupt.h>
89#include <linux/if_ether.h>
90#include <linux/netdevice.h>
91#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070092#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/skbuff.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070094#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040095#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020096#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -080098#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/stat.h>
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +0300101#include <net/dsa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700103#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200105#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000107#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/highmem.h>
109#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <linux/netpoll.h>
112#include <linux/rcupdate.h>
113#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500116#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700117#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700118#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700119#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700120#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700121#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700122#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700123#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700124#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700125#include <linux/ipv6.h>
126#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700127#include <linux/jhash.h>
128#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700129#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900130#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900131#include <trace/events/skb.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700132#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000133#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100134#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300135#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700136#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100137#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400138#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800139#include <linux/hrtimer.h>
Daniel Borkmann357b6cc2020-03-18 10:33:22 +0100140#include <linux/netfilter_ingress.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530141#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200142#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200143#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200144#include <linux/net_namespace.h>
Paolo Abeniaaa5d902018-12-14 11:51:58 +0100145#include <linux/indirect_call_wrapper.h>
Jiri Pirkoaf3836d2019-03-28 13:56:37 +0100146#include <net/devlink.h>
Heiner Kallweitbd869242020-06-20 22:35:42 +0200147#include <linux/pm_runtime.h>
Willy Tarreau37447412020-08-10 10:27:42 +0200148#include <linux/prandom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700150#include "net-sysfs.h"
151
Herbert Xud565b0a2008-12-15 23:38:52 -0800152#define MAX_GRO_SKBS 8
153
Herbert Xu5d38a072009-01-04 16:13:40 -0800154/* This should be increased if a protocol with a bigger head is added. */
155#define GRO_MAX_HEAD (MAX_HEADER + 128)
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000158static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000159struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
160struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000161static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000163static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700164static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700165 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000166static int call_netdevice_notifiers_extack(unsigned long val,
167 struct net_device *dev,
168 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200169static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700172 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * semaphore.
174 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800175 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700178 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * actual updates. This allows pure readers to access the list even
180 * while a writer is preparing to update it.
181 *
182 * To put it another way, dev_base_lock is held for writing only to
183 * protect against pure readers; the rtnl semaphore provides the
184 * protection against other writers.
185 *
186 * See, for example usages, register_netdevice() and
187 * unregister_netdevice(), which must be called with the rtnl
188 * semaphore held.
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191EXPORT_SYMBOL(dev_base_lock);
192
Florian Westphal6c557002017-10-02 23:50:05 +0200193static DEFINE_MUTEX(ifalias_mutex);
194
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300195/* protects napi_hash addition/deletion and napi_gen_id */
196static DEFINE_SPINLOCK(napi_hash_lock);
197
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800198static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800199static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300200
Ahmed S. Darwish11d60112020-06-03 16:49:44 +0200201static DECLARE_RWSEM(devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +0000202
Thomas Graf4e985ad2011-06-21 03:11:20 +0000203static inline void dev_base_seq_inc(struct net *net)
204{
tcharding643aa9c2017-02-09 17:56:05 +1100205 while (++net->dev_base_seq == 0)
206 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000207}
208
Eric W. Biederman881d9662007-09-17 11:56:21 -0700209static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700211 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000212
stephen hemminger08e98972009-11-10 07:20:34 +0000213 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Eric W. Biederman881d9662007-09-17 11:56:21 -0700216static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700218 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000221static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000222{
223#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000224 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000225#endif
226}
227
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000228static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000229{
230#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000231 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000232#endif
233}
234
Jiri Pirkoff927412019-09-30 11:48:15 +0200235static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
236 const char *name)
237{
238 struct netdev_name_node *name_node;
239
240 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
241 if (!name_node)
242 return NULL;
243 INIT_HLIST_NODE(&name_node->hlist);
244 name_node->dev = dev;
245 name_node->name = name;
246 return name_node;
247}
248
249static struct netdev_name_node *
250netdev_name_node_head_alloc(struct net_device *dev)
251{
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200252 struct netdev_name_node *name_node;
253
254 name_node = netdev_name_node_alloc(dev, dev->name);
255 if (!name_node)
256 return NULL;
257 INIT_LIST_HEAD(&name_node->list);
258 return name_node;
Jiri Pirkoff927412019-09-30 11:48:15 +0200259}
260
261static void netdev_name_node_free(struct netdev_name_node *name_node)
262{
263 kfree(name_node);
264}
265
266static void netdev_name_node_add(struct net *net,
267 struct netdev_name_node *name_node)
268{
269 hlist_add_head_rcu(&name_node->hlist,
270 dev_name_hash(net, name_node->name));
271}
272
273static void netdev_name_node_del(struct netdev_name_node *name_node)
274{
275 hlist_del_rcu(&name_node->hlist);
276}
277
278static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
279 const char *name)
280{
281 struct hlist_head *head = dev_name_hash(net, name);
282 struct netdev_name_node *name_node;
283
284 hlist_for_each_entry(name_node, head, hlist)
285 if (!strcmp(name_node->name, name))
286 return name_node;
287 return NULL;
288}
289
290static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
291 const char *name)
292{
293 struct hlist_head *head = dev_name_hash(net, name);
294 struct netdev_name_node *name_node;
295
296 hlist_for_each_entry_rcu(name_node, head, hlist)
297 if (!strcmp(name_node->name, name))
298 return name_node;
299 return NULL;
300}
301
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200302int netdev_name_node_alt_create(struct net_device *dev, const char *name)
303{
304 struct netdev_name_node *name_node;
305 struct net *net = dev_net(dev);
306
307 name_node = netdev_name_node_lookup(net, name);
308 if (name_node)
309 return -EEXIST;
310 name_node = netdev_name_node_alloc(dev, name);
311 if (!name_node)
312 return -ENOMEM;
313 netdev_name_node_add(net, name_node);
314 /* The node that holds dev->name acts as a head of per-device list. */
315 list_add_tail(&name_node->list, &dev->name_node->list);
316
317 return 0;
318}
319EXPORT_SYMBOL(netdev_name_node_alt_create);
320
321static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
322{
323 list_del(&name_node->list);
324 netdev_name_node_del(name_node);
325 kfree(name_node->name);
326 netdev_name_node_free(name_node);
327}
328
329int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
330{
331 struct netdev_name_node *name_node;
332 struct net *net = dev_net(dev);
333
334 name_node = netdev_name_node_lookup(net, name);
335 if (!name_node)
336 return -ENOENT;
Eric Dumazete08ad802020-02-14 07:53:53 -0800337 /* lookup might have found our primary name or a name belonging
338 * to another device.
339 */
340 if (name_node == dev->name_node || name_node->dev != dev)
341 return -EINVAL;
342
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200343 __netdev_name_node_alt_destroy(name_node);
344
345 return 0;
346}
347EXPORT_SYMBOL(netdev_name_node_alt_destroy);
348
349static void netdev_name_node_alt_flush(struct net_device *dev)
350{
351 struct netdev_name_node *name_node, *tmp;
352
353 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
354 __netdev_name_node_alt_destroy(name_node);
355}
356
Eric W. Biedermance286d32007-09-12 13:53:49 +0200357/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000358static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200359{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900360 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200361
362 ASSERT_RTNL();
363
364 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800365 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Jiri Pirkoff927412019-09-30 11:48:15 +0200366 netdev_name_node_add(net, dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000367 hlist_add_head_rcu(&dev->index_hlist,
368 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200369 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000370
371 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200372}
373
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000374/* Device list removal
375 * caller must respect a RCU grace period before freeing/reusing dev
376 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200377static void unlist_netdevice(struct net_device *dev)
378{
379 ASSERT_RTNL();
380
381 /* Unlink dev from the device chain */
382 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800383 list_del_rcu(&dev->dev_list);
Jiri Pirkoff927412019-09-30 11:48:15 +0200384 netdev_name_node_del(dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000385 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200386 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000387
388 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * Our notifier list
393 */
394
Alan Sternf07d5b92006-05-09 15:23:03 -0700395static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/*
398 * Device drivers call our routines to queue packets here. We empty the
399 * queue in the local softnet handler.
400 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700401
Eric Dumazet9958da02010-04-17 04:17:02 +0000402DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700403EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Cong Wang1a33e102020-05-02 22:22:19 -0700405#ifdef CONFIG_LOCKDEP
406/*
407 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
408 * according to dev->type
409 */
410static const unsigned short netdev_lock_type[] = {
411 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
412 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
413 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
414 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
415 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
416 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
417 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
418 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
419 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
420 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
421 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
422 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
423 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
424 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
425 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
426
427static const char *const netdev_lock_name[] = {
428 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
429 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
430 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
431 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
432 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
433 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
434 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
435 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
436 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
437 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
438 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
439 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
440 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
441 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
442 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
443
444static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
Cong Wang845e0eb2020-06-08 14:53:01 -0700445static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Cong Wang1a33e102020-05-02 22:22:19 -0700446
447static inline unsigned short netdev_lock_pos(unsigned short dev_type)
448{
449 int i;
450
451 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
452 if (netdev_lock_type[i] == dev_type)
453 return i;
454 /* the last key is used by default */
455 return ARRAY_SIZE(netdev_lock_type) - 1;
456}
457
458static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
459 unsigned short dev_type)
460{
461 int i;
462
463 i = netdev_lock_pos(dev_type);
464 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
465 netdev_lock_name[i]);
466}
Cong Wang845e0eb2020-06-08 14:53:01 -0700467
468static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
469{
470 int i;
471
472 i = netdev_lock_pos(dev->type);
473 lockdep_set_class_and_name(&dev->addr_list_lock,
474 &netdev_addr_lock_key[i],
475 netdev_lock_name[i]);
476}
Cong Wang1a33e102020-05-02 22:22:19 -0700477#else
478static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
479 unsigned short dev_type)
480{
481}
Cong Wang845e0eb2020-06-08 14:53:01 -0700482
483static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
484{
485}
Cong Wang1a33e102020-05-02 22:22:19 -0700486#endif
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100489 *
490 * Protocol management and registration routines
491 *
492 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * Add a protocol ID to the list. Now that the input handler is
497 * smarter we can dispense with all the messy stuff that used to be
498 * here.
499 *
500 * BEWARE!!! Protocol handlers, mangling input packets,
501 * MUST BE last in hash buckets and checking protocol handlers
502 * MUST start from promiscuous ptype_all chain in net_bh.
503 * It is true now, do not change it.
504 * Explanation follows: if protocol handler, mangling packet, will
505 * be the first on list, it is not able to sense, that packet
506 * is cloned and should be copied-on-write, so that it will
507 * change it and subsequent readers will get broken packet.
508 * --ANK (980803)
509 */
510
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000511static inline struct list_head *ptype_head(const struct packet_type *pt)
512{
513 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800514 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000515 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800516 return pt->dev ? &pt->dev->ptype_specific :
517 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
521 * dev_add_pack - add packet handler
522 * @pt: packet type declaration
523 *
524 * Add a protocol handler to the networking stack. The passed &packet_type
525 * is linked into kernel lists and may not be freed until it has been
526 * removed from the kernel lists.
527 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900528 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * guarantee all CPU's that are in middle of receiving packets
530 * will see the new packet type (until the next received packet).
531 */
532
533void dev_add_pack(struct packet_type *pt)
534{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000535 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000537 spin_lock(&ptype_lock);
538 list_add_rcu(&pt->list, head);
539 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700541EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
544 * __dev_remove_pack - remove packet handler
545 * @pt: packet type declaration
546 *
547 * Remove a protocol handler that was previously added to the kernel
548 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
549 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900550 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
552 * The packet type might still be in use by receivers
553 * and must not be freed until after all the CPU's have gone
554 * through a quiescent state.
555 */
556void __dev_remove_pack(struct packet_type *pt)
557{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000558 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct packet_type *pt1;
560
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000561 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 list_for_each_entry(pt1, head, list) {
564 if (pt == pt1) {
565 list_del_rcu(&pt->list);
566 goto out;
567 }
568 }
569
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000570 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000572 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700574EXPORT_SYMBOL(__dev_remove_pack);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/**
577 * dev_remove_pack - remove packet handler
578 * @pt: packet type declaration
579 *
580 * Remove a protocol handler that was previously added to the kernel
581 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
582 * from the kernel lists and can be freed or reused once this function
583 * returns.
584 *
585 * This call sleeps to guarantee that no CPU is looking at the packet
586 * type after return.
587 */
588void dev_remove_pack(struct packet_type *pt)
589{
590 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 synchronize_net();
593}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700594EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Vlad Yasevich62532da2012-11-15 08:49:10 +0000596
597/**
598 * dev_add_offload - register offload handlers
599 * @po: protocol offload declaration
600 *
601 * Add protocol offload handlers to the networking stack. The passed
602 * &proto_offload is linked into kernel lists and may not be freed until
603 * it has been removed from the kernel lists.
604 *
605 * This call does not sleep therefore it can not
606 * guarantee all CPU's that are in middle of receiving packets
607 * will see the new offload handlers (until the next received packet).
608 */
609void dev_add_offload(struct packet_offload *po)
610{
David S. Millerbdef7de2015-06-01 14:56:09 -0700611 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000612
613 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700614 list_for_each_entry(elem, &offload_base, list) {
615 if (po->priority < elem->priority)
616 break;
617 }
618 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000619 spin_unlock(&offload_lock);
620}
621EXPORT_SYMBOL(dev_add_offload);
622
623/**
624 * __dev_remove_offload - remove offload handler
625 * @po: packet offload declaration
626 *
627 * Remove a protocol offload handler that was previously added to the
628 * kernel offload handlers by dev_add_offload(). The passed &offload_type
629 * is removed from the kernel lists and can be freed or reused once this
630 * function returns.
631 *
632 * The packet type might still be in use by receivers
633 * and must not be freed until after all the CPU's have gone
634 * through a quiescent state.
635 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800636static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000637{
638 struct list_head *head = &offload_base;
639 struct packet_offload *po1;
640
Eric Dumazetc53aa502012-11-16 08:08:23 +0000641 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000642
643 list_for_each_entry(po1, head, list) {
644 if (po == po1) {
645 list_del_rcu(&po->list);
646 goto out;
647 }
648 }
649
650 pr_warn("dev_remove_offload: %p not found\n", po);
651out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000652 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000653}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000654
655/**
656 * dev_remove_offload - remove packet offload handler
657 * @po: packet offload declaration
658 *
659 * Remove a packet offload handler that was previously added to the kernel
660 * offload handlers by dev_add_offload(). The passed &offload_type is
661 * removed from the kernel lists and can be freed or reused once this
662 * function returns.
663 *
664 * This call sleeps to guarantee that no CPU is looking at the packet
665 * type after return.
666 */
667void dev_remove_offload(struct packet_offload *po)
668{
669 __dev_remove_offload(po);
670
671 synchronize_net();
672}
673EXPORT_SYMBOL(dev_remove_offload);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100676 *
677 * Device Boot-time Settings Routines
678 *
679 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/* Boot time configuration table */
682static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
683
684/**
685 * netdev_boot_setup_add - add new setup entry
686 * @name: name of the device
687 * @map: configured settings for the device
688 *
689 * Adds new setup entry to the dev_boot_setup list. The function
690 * returns 0 on error and 1 on success. This is a generic routine to
691 * all netdevices.
692 */
693static int netdev_boot_setup_add(char *name, struct ifmap *map)
694{
695 struct netdev_boot_setup *s;
696 int i;
697
698 s = dev_boot_setup;
699 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
700 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
701 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700702 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 memcpy(&s[i].map, map, sizeof(s[i].map));
704 break;
705 }
706 }
707
708 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
709}
710
711/**
tcharding722c9a02017-02-09 17:56:04 +1100712 * netdev_boot_setup_check - check boot time settings
713 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *
tcharding722c9a02017-02-09 17:56:04 +1100715 * Check boot time settings for the device.
716 * The found settings are set for the device to be used
717 * later in the device probing.
718 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720int netdev_boot_setup_check(struct net_device *dev)
721{
722 struct netdev_boot_setup *s = dev_boot_setup;
723 int i;
724
725 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
726 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700727 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100728 dev->irq = s[i].map.irq;
729 dev->base_addr = s[i].map.base_addr;
730 dev->mem_start = s[i].map.mem_start;
731 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 1;
733 }
734 }
735 return 0;
736}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700737EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739
740/**
tcharding722c9a02017-02-09 17:56:04 +1100741 * netdev_boot_base - get address from boot time settings
742 * @prefix: prefix for network device
743 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *
tcharding722c9a02017-02-09 17:56:04 +1100745 * Check boot time settings for the base address of device.
746 * The found settings are set for the device to be used
747 * later in the device probing.
748 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
750unsigned long netdev_boot_base(const char *prefix, int unit)
751{
752 const struct netdev_boot_setup *s = dev_boot_setup;
753 char name[IFNAMSIZ];
754 int i;
755
756 sprintf(name, "%s%d", prefix, unit);
757
758 /*
759 * If device already registered then return base of 1
760 * to indicate not to probe for this interface
761 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700762 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 1;
764
765 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
766 if (!strcmp(name, s[i].name))
767 return s[i].map.base_addr;
768 return 0;
769}
770
771/*
772 * Saves at boot time configured settings for any netdevice.
773 */
774int __init netdev_boot_setup(char *str)
775{
776 int ints[5];
777 struct ifmap map;
778
779 str = get_options(str, ARRAY_SIZE(ints), ints);
780 if (!str || !*str)
781 return 0;
782
783 /* Save settings */
784 memset(&map, 0, sizeof(map));
785 if (ints[0] > 0)
786 map.irq = ints[1];
787 if (ints[0] > 1)
788 map.base_addr = ints[2];
789 if (ints[0] > 2)
790 map.mem_start = ints[3];
791 if (ints[0] > 3)
792 map.mem_end = ints[4];
793
794 /* Add new entry to the list */
795 return netdev_boot_setup_add(str, &map);
796}
797
798__setup("netdev=", netdev_boot_setup);
799
800/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100801 *
802 * Device Interface Subroutines
803 *
804 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200807 * dev_get_iflink - get 'iflink' value of a interface
808 * @dev: targeted interface
809 *
810 * Indicates the ifindex the interface is linked to.
811 * Physical interfaces have the same 'ifindex' and 'iflink' values.
812 */
813
814int dev_get_iflink(const struct net_device *dev)
815{
816 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
817 return dev->netdev_ops->ndo_get_iflink(dev);
818
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200819 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200820}
821EXPORT_SYMBOL(dev_get_iflink);
822
823/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700824 * dev_fill_metadata_dst - Retrieve tunnel egress information.
825 * @dev: targeted interface
826 * @skb: The packet.
827 *
828 * For better visibility of tunnel traffic OVS needs to retrieve
829 * egress tunnel information for a packet. Following API allows
830 * user to get this info.
831 */
832int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
833{
834 struct ip_tunnel_info *info;
835
836 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
837 return -EINVAL;
838
839 info = skb_tunnel_info_unclone(skb);
840 if (!info)
841 return -ENOMEM;
842 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
843 return -EINVAL;
844
845 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
846}
847EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
848
849/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700851 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * @name: name to find
853 *
854 * Find an interface by name. Must be called under RTNL semaphore
855 * or @dev_base_lock. If the name is found a pointer to the device
856 * is returned. If the name is not found then %NULL is returned. The
857 * reference counters are not incremented so the caller must be
858 * careful with locks.
859 */
860
Eric W. Biederman881d9662007-09-17 11:56:21 -0700861struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Jiri Pirkoff927412019-09-30 11:48:15 +0200863 struct netdev_name_node *node_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jiri Pirkoff927412019-09-30 11:48:15 +0200865 node_name = netdev_name_node_lookup(net, name);
866 return node_name ? node_name->dev : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700868EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870/**
tcharding722c9a02017-02-09 17:56:04 +1100871 * dev_get_by_name_rcu - find a device by its name
872 * @net: the applicable net namespace
873 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000874 *
tcharding722c9a02017-02-09 17:56:04 +1100875 * Find an interface by name.
876 * If the name is found a pointer to the device is returned.
877 * If the name is not found then %NULL is returned.
878 * The reference counters are not incremented so the caller must be
879 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000880 */
881
882struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
883{
Jiri Pirkoff927412019-09-30 11:48:15 +0200884 struct netdev_name_node *node_name;
Eric Dumazet72c95282009-10-30 07:11:27 +0000885
Jiri Pirkoff927412019-09-30 11:48:15 +0200886 node_name = netdev_name_node_lookup_rcu(net, name);
887 return node_name ? node_name->dev : NULL;
Eric Dumazet72c95282009-10-30 07:11:27 +0000888}
889EXPORT_SYMBOL(dev_get_by_name_rcu);
890
891/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700893 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * @name: name to find
895 *
896 * Find an interface by name. This can be called from any
897 * context and does its own locking. The returned handle has
898 * the usage count incremented and the caller must use dev_put() to
899 * release it when it is no longer needed. %NULL is returned if no
900 * matching device is found.
901 */
902
Eric W. Biederman881d9662007-09-17 11:56:21 -0700903struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct net_device *dev;
906
Eric Dumazet72c95282009-10-30 07:11:27 +0000907 rcu_read_lock();
908 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (dev)
910 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000911 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return dev;
913}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700914EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916/**
917 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700918 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * @ifindex: index of device
920 *
921 * Search for an interface by index. Returns %NULL if the device
922 * is not found or a pointer to the device. The device has not
923 * had its reference counter increased so the caller must be careful
924 * about locking. The caller must hold either the RTNL semaphore
925 * or @dev_base_lock.
926 */
927
Eric W. Biederman881d9662007-09-17 11:56:21 -0700928struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700930 struct net_device *dev;
931 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Sasha Levinb67bfe02013-02-27 17:06:00 -0800933 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (dev->ifindex == ifindex)
935 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return NULL;
938}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700939EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000941/**
942 * dev_get_by_index_rcu - find a device by its ifindex
943 * @net: the applicable net namespace
944 * @ifindex: index of device
945 *
946 * Search for an interface by index. Returns %NULL if the device
947 * is not found or a pointer to the device. The device has not
948 * had its reference counter increased so the caller must be careful
949 * about locking. The caller must hold RCU lock.
950 */
951
952struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
953{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000954 struct net_device *dev;
955 struct hlist_head *head = dev_index_hash(net, ifindex);
956
Sasha Levinb67bfe02013-02-27 17:06:00 -0800957 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000958 if (dev->ifindex == ifindex)
959 return dev;
960
961 return NULL;
962}
963EXPORT_SYMBOL(dev_get_by_index_rcu);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966/**
967 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700968 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * @ifindex: index of device
970 *
971 * Search for an interface by index. Returns NULL if the device
972 * is not found or a pointer to the device. The device returned has
973 * had a reference added and the pointer is safe until the user calls
974 * dev_put to indicate they have finished with it.
975 */
976
Eric W. Biederman881d9662007-09-17 11:56:21 -0700977struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct net_device *dev;
980
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000981 rcu_read_lock();
982 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (dev)
984 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000985 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return dev;
987}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700988EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200991 * dev_get_by_napi_id - find a device by napi_id
992 * @napi_id: ID of the NAPI struct
993 *
994 * Search for an interface by NAPI ID. Returns %NULL if the device
995 * is not found or a pointer to the device. The device has not had
996 * its reference counter increased so the caller must be careful
997 * about locking. The caller must hold RCU lock.
998 */
999
1000struct net_device *dev_get_by_napi_id(unsigned int napi_id)
1001{
1002 struct napi_struct *napi;
1003
1004 WARN_ON_ONCE(!rcu_read_lock_held());
1005
1006 if (napi_id < MIN_NAPI_ID)
1007 return NULL;
1008
1009 napi = napi_by_id(napi_id);
1010
1011 return napi ? napi->dev : NULL;
1012}
1013EXPORT_SYMBOL(dev_get_by_napi_id);
1014
1015/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001016 * netdev_get_name - get a netdevice name, knowing its ifindex.
1017 * @net: network namespace
1018 * @name: a pointer to the buffer where the name will be stored.
1019 * @ifindex: the ifindex of the interface to get the name from.
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001020 */
1021int netdev_get_name(struct net *net, char *name, int ifindex)
1022{
1023 struct net_device *dev;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001024 int ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001025
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001026 down_read(&devnet_rename_sem);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001027 rcu_read_lock();
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001028
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001029 dev = dev_get_by_index_rcu(net, ifindex);
1030 if (!dev) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001031 ret = -ENODEV;
1032 goto out;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001033 }
1034
1035 strcpy(name, dev->name);
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001036
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001037 ret = 0;
1038out:
1039 rcu_read_unlock();
1040 up_read(&devnet_rename_sem);
1041 return ret;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +02001042}
1043
1044/**
Eric Dumazet941666c2010-12-05 01:23:53 +00001045 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001046 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * @type: media type of device
1048 * @ha: hardware address
1049 *
1050 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -08001051 * is not found or a pointer to the device.
1052 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +00001053 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * and the caller must therefore be careful about locking
1055 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
1057
Eric Dumazet941666c2010-12-05 01:23:53 +00001058struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
1059 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct net_device *dev;
1062
Eric Dumazet941666c2010-12-05 01:23:53 +00001063 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (dev->type == type &&
1065 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -07001066 return dev;
1067
1068 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
Eric Dumazet941666c2010-12-05 01:23:53 +00001070EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -03001071
Eric W. Biederman881d9662007-09-17 11:56:21 -07001072struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001073{
1074 struct net_device *dev;
1075
1076 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -07001077 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001078 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001079 return dev;
1080
1081 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001082}
Patrick McHardy4e9cac22007-05-03 03:28:13 -07001083EXPORT_SYMBOL(__dev_getfirstbyhwtype);
1084
Eric W. Biederman881d9662007-09-17 11:56:21 -07001085struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001087 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001089 rcu_read_lock();
1090 for_each_netdev_rcu(net, dev)
1091 if (dev->type == type) {
1092 dev_hold(dev);
1093 ret = dev;
1094 break;
1095 }
1096 rcu_read_unlock();
1097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099EXPORT_SYMBOL(dev_getfirstbyhwtype);
1100
1101/**
WANG Cong6c555492014-09-11 15:35:09 -07001102 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001103 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * @if_flags: IFF_* values
1105 * @mask: bitmask of bits in if_flags to check
1106 *
1107 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +00001108 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -07001109 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
1111
WANG Cong6c555492014-09-11 15:35:09 -07001112struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1113 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001115 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
WANG Cong6c555492014-09-11 15:35:09 -07001117 ASSERT_RTNL();
1118
Pavel Emelianov7562f872007-05-03 15:13:45 -07001119 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001120 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001122 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124 }
1125 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
WANG Cong6c555492014-09-11 15:35:09 -07001128EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130/**
1131 * dev_valid_name - check if name is okay for network device
1132 * @name: name string
1133 *
1134 * Network device names need to be valid file names to
Randy Dunlap4250b752020-09-17 21:35:15 -07001135 * allow sysfs to work. We also disallow any kind of
David S. Millerc7fa9d12006-08-15 16:34:13 -07001136 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
David S. Miller95f050b2012-03-06 16:12:15 -05001138bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001140 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001141 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001142 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001143 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001144 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001145 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001146
1147 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001148 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001149 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001150 name++;
1151 }
David S. Miller95f050b2012-03-06 16:12:15 -05001152 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001154EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001157 * __dev_alloc_name - allocate a name for a device
1158 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001160 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
1162 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001163 * id. It scans list of devices to build up a free map, then chooses
1164 * the first empty slot. The caller must hold the dev_base or rtnl lock
1165 * while allocating the name and adding the device in order to avoid
1166 * duplicates.
1167 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1168 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001171static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 const char *p;
1175 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001176 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 struct net_device *d;
1178
Rasmus Villemoes93809102017-11-13 00:15:08 +01001179 if (!dev_valid_name(name))
1180 return -EINVAL;
1181
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001182 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (p) {
1184 /*
1185 * Verify the string as this thing may have come from
1186 * the user. There must be either one "%d" and no other "%"
1187 * characters.
1188 */
1189 if (p[1] != 'd' || strchr(p + 2, '%'))
1190 return -EINVAL;
1191
1192 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001193 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!inuse)
1195 return -ENOMEM;
1196
Eric W. Biederman881d9662007-09-17 11:56:21 -07001197 for_each_netdev(net, d) {
Jiri Bohac943e1582021-03-18 04:42:53 +01001198 struct netdev_name_node *name_node;
1199 list_for_each_entry(name_node, &d->name_node->list, list) {
1200 if (!sscanf(name_node->name, name, &i))
1201 continue;
1202 if (i < 0 || i >= max_netdevices)
1203 continue;
1204
1205 /* avoid cases where sscanf is not exact inverse of printf */
1206 snprintf(buf, IFNAMSIZ, name, i);
1207 if (!strncmp(buf, name_node->name, IFNAMSIZ))
1208 set_bit(i, inuse);
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!sscanf(d->name, name, &i))
1211 continue;
1212 if (i < 0 || i >= max_netdevices)
1213 continue;
1214
1215 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001216 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!strncmp(buf, d->name, IFNAMSIZ))
1218 set_bit(i, inuse);
1219 }
1220
1221 i = find_first_zero_bit(inuse, max_netdevices);
1222 free_page((unsigned long) inuse);
1223 }
1224
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001225 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001226 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /* It is possible to run out of possible slots
1230 * when the name is long and there isn't enough space left
1231 * for the digits, or if all bits are used.
1232 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001233 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001236static int dev_alloc_name_ns(struct net *net,
1237 struct net_device *dev,
1238 const char *name)
1239{
1240 char buf[IFNAMSIZ];
1241 int ret;
1242
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001243 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001244 ret = __dev_alloc_name(net, name, buf);
1245 if (ret >= 0)
1246 strlcpy(dev->name, buf, IFNAMSIZ);
1247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001250/**
1251 * dev_alloc_name - allocate a name for a device
1252 * @dev: device
1253 * @name: name format string
1254 *
1255 * Passed a format string - eg "lt%d" it will try and find a suitable
1256 * id. It scans list of devices to build up a free map, then chooses
1257 * the first empty slot. The caller must hold the dev_base or rtnl lock
1258 * while allocating the name and adding the device in order to avoid
1259 * duplicates.
1260 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1261 * Returns the number of the unit assigned or a negative errno code.
1262 */
1263
1264int dev_alloc_name(struct net_device *dev, const char *name)
1265{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001266 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001267}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001268EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001269
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001270static int dev_get_valid_name(struct net *net, struct net_device *dev,
1271 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001272{
David S. Miller55a5ec92018-01-02 11:45:07 -05001273 BUG_ON(!net);
1274
1275 if (!dev_valid_name(name))
1276 return -EINVAL;
1277
1278 if (strchr(name, '%'))
1279 return dev_alloc_name_ns(net, dev, name);
1280 else if (__dev_get_by_name(net, name))
1281 return -EEXIST;
1282 else if (dev->name != name)
1283 strlcpy(dev->name, name, IFNAMSIZ);
1284
1285 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/**
1289 * dev_change_name - change name of a device
1290 * @dev: device
1291 * @newname: name (or format string) must be at least IFNAMSIZ
1292 *
1293 * Change name of a device, can pass format strings "eth%d".
1294 * for wildcarding.
1295 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001296int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Tom Gundersen238fa362014-07-14 16:37:23 +02001298 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001299 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001301 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001302 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001305 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001307 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001308
1309 /* Some auto-enslaved devices e.g. failover slaves are
1310 * special, as userspace might rename the device after
1311 * the interface had been brought up and running since
1312 * the point kernel initiated auto-enslavement. Allow
1313 * live name change even when these slave devices are
1314 * up and running.
1315 *
1316 * Typically, users of these auto-enslaving devices
1317 * don't actually care about slave name change, as
1318 * they are supposed to operate on master interface
1319 * directly.
1320 */
1321 if (dev->flags & IFF_UP &&
1322 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -EBUSY;
1324
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001325 down_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001326
1327 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001328 up_write(&devnet_rename_sem);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001329 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001330 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001331
Herbert Xufcc5a032007-07-30 17:03:38 -07001332 memcpy(oldname, dev->name, IFNAMSIZ);
1333
Gao feng828de4f2012-09-13 20:58:27 +00001334 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001335 if (err < 0) {
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001336 up_write(&devnet_rename_sem);
Octavian Purdilad9031022009-11-18 02:36:59 +00001337 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001340 if (oldname[0] && !strchr(oldname, '%'))
1341 netdev_info(dev, "renamed from %s\n", oldname);
1342
Tom Gundersen238fa362014-07-14 16:37:23 +02001343 old_assign_type = dev->name_assign_type;
1344 dev->name_assign_type = NET_NAME_RENAMED;
1345
Herbert Xufcc5a032007-07-30 17:03:38 -07001346rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001347 ret = device_rename(&dev->dev, dev->name);
1348 if (ret) {
1349 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001350 dev->name_assign_type = old_assign_type;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001351 up_write(&devnet_rename_sem);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001352 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001353 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001354
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001355 up_write(&devnet_rename_sem);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001356
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001357 netdev_adjacent_rename_links(dev, oldname);
1358
Herbert Xu7f988ea2007-07-30 16:35:46 -07001359 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001360 netdev_name_node_del(dev->name_node);
Eric Dumazet72c95282009-10-30 07:11:27 +00001361 write_unlock_bh(&dev_base_lock);
1362
1363 synchronize_rcu();
1364
1365 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001366 netdev_name_node_add(net, dev->name_node);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001367 write_unlock_bh(&dev_base_lock);
1368
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001369 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001370 ret = notifier_to_errno(ret);
1371
1372 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001373 /* err >= 0 after dev_alloc_name() or stores the first errno */
1374 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001375 err = ret;
Ahmed S. Darwish11d60112020-06-03 16:49:44 +02001376 down_write(&devnet_rename_sem);
Herbert Xufcc5a032007-07-30 17:03:38 -07001377 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001378 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001379 dev->name_assign_type = old_assign_type;
1380 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001381 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001382 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001383 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001384 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001385 }
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 return err;
1389}
1390
1391/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001392 * dev_set_alias - change ifalias of a device
1393 * @dev: device
1394 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001395 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001396 *
1397 * Set ifalias for a device,
1398 */
1399int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1400{
Florian Westphal6c557002017-10-02 23:50:05 +02001401 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001402
1403 if (len >= IFALIASZ)
1404 return -EINVAL;
1405
Florian Westphal6c557002017-10-02 23:50:05 +02001406 if (len) {
1407 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1408 if (!new_alias)
1409 return -ENOMEM;
1410
1411 memcpy(new_alias->ifalias, alias, len);
1412 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001413 }
1414
Florian Westphal6c557002017-10-02 23:50:05 +02001415 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001416 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1417 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001418 mutex_unlock(&ifalias_mutex);
1419
1420 if (new_alias)
1421 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001422
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001423 return len;
1424}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001425EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001426
Florian Westphal6c557002017-10-02 23:50:05 +02001427/**
1428 * dev_get_alias - get ifalias of a device
1429 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001430 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001431 * @len: size of buffer
1432 *
1433 * get ifalias for a device. Caller must make sure dev cannot go
1434 * away, e.g. rcu read lock or own a reference count to device.
1435 */
1436int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1437{
1438 const struct dev_ifalias *alias;
1439 int ret = 0;
1440
1441 rcu_read_lock();
1442 alias = rcu_dereference(dev->ifalias);
1443 if (alias)
1444 ret = snprintf(name, len, "%s", alias->ifalias);
1445 rcu_read_unlock();
1446
1447 return ret;
1448}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001449
1450/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001451 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001452 * @dev: device to cause notification
1453 *
1454 * Called to indicate a device has changed features.
1455 */
1456void netdev_features_change(struct net_device *dev)
1457{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001458 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001459}
1460EXPORT_SYMBOL(netdev_features_change);
1461
1462/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * netdev_state_change - device changes state
1464 * @dev: device to cause notification
1465 *
1466 * Called to indicate a device has changed state. This function calls
1467 * the notifier chains for netdev_chain and sends a NEWLINK message
1468 * to the routing socket.
1469 */
1470void netdev_state_change(struct net_device *dev)
1471{
1472 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001473 struct netdev_notifier_change_info change_info = {
1474 .info.dev = dev,
1475 };
Loic Prylli54951192014-07-01 21:39:43 -07001476
David Ahern51d0c0472017-10-04 17:48:45 -07001477 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001478 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001479 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001482EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Amerigo Wangee89bab2012-08-09 22:14:56 +00001484/**
tcharding722c9a02017-02-09 17:56:04 +11001485 * netdev_notify_peers - notify network peers about existence of @dev
1486 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001487 *
1488 * Generate traffic such that interested network peers are aware of
1489 * @dev, such as by generating a gratuitous ARP. This may be used when
1490 * a device wants to inform the rest of the network about some sort of
1491 * reconfiguration such as a failover event or virtual machine
1492 * migration.
1493 */
1494void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001495{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001496 rtnl_lock();
1497 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001498 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001499 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001500}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001501EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001502
Petr Machata40c900a2018-12-06 17:05:47 +00001503static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001505 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001508 ASSERT_RTNL();
1509
Heiner Kallweitbd869242020-06-20 22:35:42 +02001510 if (!netif_device_present(dev)) {
1511 /* may be detached because parent is runtime-suspended */
1512 if (dev->dev.parent)
1513 pm_runtime_resume(dev->dev.parent);
1514 if (!netif_device_present(dev))
1515 return -ENODEV;
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Neil Hormanca99ca12013-02-05 08:05:43 +00001518 /* Block netpoll from trying to do any rx path servicing.
1519 * If we don't do this there is a chance ndo_poll_controller
1520 * or ndo_poll may be running while we open the device
1521 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001522 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001523
Petr Machata40c900a2018-12-06 17:05:47 +00001524 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001525 ret = notifier_to_errno(ret);
1526 if (ret)
1527 return ret;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001530
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001531 if (ops->ndo_validate_addr)
1532 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001533
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001534 if (!ret && ops->ndo_open)
1535 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Eric W. Biederman66b55522014-03-27 15:39:03 -07001537 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001538
Jeff Garzikbada3392007-10-23 20:19:37 -07001539 if (ret)
1540 clear_bit(__LINK_STATE_START, &dev->state);
1541 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001543 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001545 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return ret;
1549}
Patrick McHardybd380812010-02-26 06:34:53 +00001550
1551/**
1552 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001553 * @dev: device to open
1554 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001555 *
1556 * Takes a device from down to up state. The device's private open
1557 * function is invoked and then the multicast lists are loaded. Finally
1558 * the device is moved into the up state and a %NETDEV_UP message is
1559 * sent to the netdev notifier chain.
1560 *
1561 * Calling this function on an active interface is a nop. On a failure
1562 * a negative errno code is returned.
1563 */
Petr Machata00f54e62018-12-06 17:05:36 +00001564int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001565{
1566 int ret;
1567
Patrick McHardybd380812010-02-26 06:34:53 +00001568 if (dev->flags & IFF_UP)
1569 return 0;
1570
Petr Machata40c900a2018-12-06 17:05:47 +00001571 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001572 if (ret < 0)
1573 return ret;
1574
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001575 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001576 call_netdevice_notifiers(NETDEV_UP, dev);
1577
1578 return ret;
1579}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001580EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
stephen hemminger7051b882017-07-18 15:59:27 -07001582static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Octavian Purdila44345722010-12-13 12:44:07 +00001584 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001585
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001586 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001587 might_sleep();
1588
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001589 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001590 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001591 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001592
Octavian Purdila44345722010-12-13 12:44:07 +00001593 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Octavian Purdila44345722010-12-13 12:44:07 +00001595 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Octavian Purdila44345722010-12-13 12:44:07 +00001597 /* Synchronize to scheduled poll. We cannot touch poll list, it
1598 * can be even on different cpu. So just clear netif_running().
1599 *
1600 * dev->stop() will invoke napi_disable() on all of it's
1601 * napi_struct instances on this device.
1602 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001603 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Octavian Purdila44345722010-12-13 12:44:07 +00001606 dev_deactivate_many(head);
1607
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001608 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001609 const struct net_device_ops *ops = dev->netdev_ops;
1610
1611 /*
1612 * Call the device specific close. This cannot fail.
1613 * Only if device is UP
1614 *
1615 * We allow it to be called even after a DETACH hot-plug
1616 * event.
1617 */
1618 if (ops->ndo_stop)
1619 ops->ndo_stop(dev);
1620
Octavian Purdila44345722010-12-13 12:44:07 +00001621 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001622 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001623 }
Octavian Purdila44345722010-12-13 12:44:07 +00001624}
1625
stephen hemminger7051b882017-07-18 15:59:27 -07001626static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001627{
1628 LIST_HEAD(single);
1629
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001630 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001631 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001632 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001633}
1634
stephen hemminger7051b882017-07-18 15:59:27 -07001635void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001636{
1637 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001638
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001639 /* Remove the devices that don't need to be closed */
1640 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001641 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001642 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001643
1644 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001645
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001646 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001647 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001648 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001649 if (unlink)
1650 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
David S. Miller99c4a262015-03-18 22:52:33 -04001653EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001654
1655/**
1656 * dev_close - shutdown an interface.
1657 * @dev: device to shutdown
1658 *
1659 * This function moves an active device into down state. A
1660 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1661 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1662 * chain.
1663 */
stephen hemminger7051b882017-07-18 15:59:27 -07001664void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001665{
Eric Dumazete14a5992011-05-10 12:26:06 -07001666 if (dev->flags & IFF_UP) {
1667 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001668
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001669 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001670 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001671 list_del(&single);
1672 }
Patrick McHardybd380812010-02-26 06:34:53 +00001673}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001674EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001677/**
1678 * dev_disable_lro - disable Large Receive Offload on a device
1679 * @dev: device
1680 *
1681 * Disable Large Receive Offload (LRO) on a net device. Must be
1682 * called under RTNL. This is needed if received packets may be
1683 * forwarded to another interface.
1684 */
1685void dev_disable_lro(struct net_device *dev)
1686{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001687 struct net_device *lower_dev;
1688 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001689
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001690 dev->wanted_features &= ~NETIF_F_LRO;
1691 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001692
Michał Mirosław22d59692011-04-21 12:42:15 +00001693 if (unlikely(dev->features & NETIF_F_LRO))
1694 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001695
1696 netdev_for_each_lower_dev(dev, lower_dev, iter)
1697 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001698}
1699EXPORT_SYMBOL(dev_disable_lro);
1700
Michael Chan56f5aa72017-12-16 03:09:41 -05001701/**
1702 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1703 * @dev: device
1704 *
1705 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1706 * called under RTNL. This is needed if Generic XDP is installed on
1707 * the device.
1708 */
1709static void dev_disable_gro_hw(struct net_device *dev)
1710{
1711 dev->wanted_features &= ~NETIF_F_GRO_HW;
1712 netdev_update_features(dev);
1713
1714 if (unlikely(dev->features & NETIF_F_GRO_HW))
1715 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1716}
1717
Kirill Tkhaiede27622018-03-23 19:47:19 +03001718const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1719{
1720#define N(val) \
1721 case NETDEV_##val: \
1722 return "NETDEV_" __stringify(val);
1723 switch (cmd) {
1724 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1725 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1726 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1727 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1728 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1729 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1730 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001731 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1732 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001733 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001734 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001735#undef N
1736 return "UNKNOWN_NETDEV_EVENT";
1737}
1738EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1739
Jiri Pirko351638e2013-05-28 01:30:21 +00001740static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1741 struct net_device *dev)
1742{
David Ahern51d0c0472017-10-04 17:48:45 -07001743 struct netdev_notifier_info info = {
1744 .dev = dev,
1745 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001746
Jiri Pirko351638e2013-05-28 01:30:21 +00001747 return nb->notifier_call(nb, val, &info);
1748}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001749
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001750static int call_netdevice_register_notifiers(struct notifier_block *nb,
1751 struct net_device *dev)
1752{
1753 int err;
1754
1755 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1756 err = notifier_to_errno(err);
1757 if (err)
1758 return err;
1759
1760 if (!(dev->flags & IFF_UP))
1761 return 0;
1762
1763 call_netdevice_notifier(nb, NETDEV_UP, dev);
1764 return 0;
1765}
1766
1767static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1768 struct net_device *dev)
1769{
1770 if (dev->flags & IFF_UP) {
1771 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1772 dev);
1773 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1774 }
1775 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1776}
1777
1778static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1779 struct net *net)
1780{
1781 struct net_device *dev;
1782 int err;
1783
1784 for_each_netdev(net, dev) {
1785 err = call_netdevice_register_notifiers(nb, dev);
1786 if (err)
1787 goto rollback;
1788 }
1789 return 0;
1790
1791rollback:
1792 for_each_netdev_continue_reverse(net, dev)
1793 call_netdevice_unregister_notifiers(nb, dev);
1794 return err;
1795}
1796
1797static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1798 struct net *net)
1799{
1800 struct net_device *dev;
1801
1802 for_each_netdev(net, dev)
1803 call_netdevice_unregister_notifiers(nb, dev);
1804}
1805
Eric W. Biederman881d9662007-09-17 11:56:21 -07001806static int dev_boot_phase = 1;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/**
tcharding722c9a02017-02-09 17:56:04 +11001809 * register_netdevice_notifier - register a network notifier block
1810 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 *
tcharding722c9a02017-02-09 17:56:04 +11001812 * Register a notifier to be called when network device events occur.
1813 * The notifier passed is linked into the kernel structures and must
1814 * not be reused until it has been unregistered. A negative errno code
1815 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
tcharding722c9a02017-02-09 17:56:04 +11001817 * When registered all registration and up events are replayed
1818 * to the new notifier to allow device to have a race free
1819 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
1821
1822int register_netdevice_notifier(struct notifier_block *nb)
1823{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001824 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 int err;
1826
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001827 /* Close race with setup_net() and cleanup_net() */
1828 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001830 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001831 if (err)
1832 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001833 if (dev_boot_phase)
1834 goto unlock;
1835 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001836 err = call_netdevice_register_net_notifiers(nb, net);
1837 if (err)
1838 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001840
1841unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001843 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001845
1846rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001847 for_each_net_continue_reverse(net)
1848 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001849
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001850 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001851 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001853EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855/**
tcharding722c9a02017-02-09 17:56:04 +11001856 * unregister_netdevice_notifier - unregister a network notifier block
1857 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *
tcharding722c9a02017-02-09 17:56:04 +11001859 * Unregister a notifier previously registered by
1860 * register_netdevice_notifier(). The notifier is unlinked into the
1861 * kernel structures and may then be reused. A negative errno code
1862 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001863 *
tcharding722c9a02017-02-09 17:56:04 +11001864 * After unregistering unregister and down device events are synthesized
1865 * for all devices on the device list to the removed notifier to remove
1866 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868
1869int unregister_netdevice_notifier(struct notifier_block *nb)
1870{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001871 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001872 int err;
1873
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001874 /* Close race with setup_net() and cleanup_net() */
1875 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001876 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001877 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001878 if (err)
1879 goto unlock;
1880
Jiri Pirko48b3a132020-01-25 12:17:06 +01001881 for_each_net(net)
1882 call_netdevice_unregister_net_notifiers(nb, net);
1883
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001884unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001885 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001886 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001887 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001889EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Jiri Pirko1f637702020-01-25 12:17:07 +01001891static int __register_netdevice_notifier_net(struct net *net,
1892 struct notifier_block *nb,
1893 bool ignore_call_fail)
1894{
1895 int err;
1896
1897 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1898 if (err)
1899 return err;
1900 if (dev_boot_phase)
1901 return 0;
1902
1903 err = call_netdevice_register_net_notifiers(nb, net);
1904 if (err && !ignore_call_fail)
1905 goto chain_unregister;
1906
1907 return 0;
1908
1909chain_unregister:
1910 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1911 return err;
1912}
1913
1914static int __unregister_netdevice_notifier_net(struct net *net,
1915 struct notifier_block *nb)
1916{
1917 int err;
1918
1919 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1920 if (err)
1921 return err;
1922
1923 call_netdevice_unregister_net_notifiers(nb, net);
1924 return 0;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001928 * register_netdevice_notifier_net - register a per-netns network notifier block
1929 * @net: network namespace
1930 * @nb: notifier
1931 *
1932 * Register a notifier to be called when network device events occur.
1933 * The notifier passed is linked into the kernel structures and must
1934 * not be reused until it has been unregistered. A negative errno code
1935 * is returned on a failure.
1936 *
1937 * When registered all registration and up events are replayed
1938 * to the new notifier to allow device to have a race free
1939 * view of the network device list.
1940 */
1941
1942int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1943{
1944 int err;
1945
1946 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001947 err = __register_netdevice_notifier_net(net, nb, false);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001948 rtnl_unlock();
1949 return err;
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001950}
1951EXPORT_SYMBOL(register_netdevice_notifier_net);
1952
1953/**
1954 * unregister_netdevice_notifier_net - unregister a per-netns
1955 * network notifier block
1956 * @net: network namespace
1957 * @nb: notifier
1958 *
1959 * Unregister a notifier previously registered by
1960 * register_netdevice_notifier(). The notifier is unlinked into the
1961 * kernel structures and may then be reused. A negative errno code
1962 * is returned on a failure.
1963 *
1964 * After unregistering unregister and down device events are synthesized
1965 * for all devices on the device list to the removed notifier to remove
1966 * the need for special case cleanup code.
1967 */
1968
1969int unregister_netdevice_notifier_net(struct net *net,
1970 struct notifier_block *nb)
1971{
1972 int err;
1973
1974 rtnl_lock();
Jiri Pirko1f637702020-01-25 12:17:07 +01001975 err = __unregister_netdevice_notifier_net(net, nb);
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001976 rtnl_unlock();
1977 return err;
1978}
1979EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1980
Jiri Pirko93642e12020-01-25 12:17:08 +01001981int register_netdevice_notifier_dev_net(struct net_device *dev,
1982 struct notifier_block *nb,
1983 struct netdev_net_notifier *nn)
1984{
1985 int err;
1986
1987 rtnl_lock();
1988 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1989 if (!err) {
1990 nn->nb = nb;
1991 list_add(&nn->list, &dev->net_notifier_list);
1992 }
1993 rtnl_unlock();
1994 return err;
1995}
1996EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1997
1998int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1999 struct notifier_block *nb,
2000 struct netdev_net_notifier *nn)
2001{
2002 int err;
2003
2004 rtnl_lock();
2005 list_del(&nn->list);
2006 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
2007 rtnl_unlock();
2008 return err;
2009}
2010EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
2011
2012static void move_netdevice_notifiers_dev_net(struct net_device *dev,
2013 struct net *net)
2014{
2015 struct netdev_net_notifier *nn;
2016
2017 list_for_each_entry(nn, &dev->net_notifier_list, list) {
2018 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
2019 __register_netdevice_notifier_net(net, nn->nb, true);
2020 }
2021}
2022
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002023/**
Jiri Pirko351638e2013-05-28 01:30:21 +00002024 * call_netdevice_notifiers_info - call all network notifier blocks
2025 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00002026 * @info: notifier information data
2027 *
2028 * Call all network notifier blocks. Parameters and return value
2029 * are as for raw_notifier_call_chain().
2030 */
2031
stephen hemminger1d143d92013-12-29 14:01:29 -08002032static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08002033 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00002034{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002035 struct net *net = dev_net(info->dev);
2036 int ret;
2037
Jiri Pirko351638e2013-05-28 01:30:21 +00002038 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02002039
2040 /* Run per-netns notifier block chain first, then run the global one.
2041 * Hopefully, one day, the global one is going to be removed after
2042 * all notifier block registrators get converted to be per-netns.
2043 */
2044 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
2045 if (ret & NOTIFY_STOP_MASK)
2046 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00002047 return raw_notifier_call_chain(&netdev_chain, val, info);
2048}
Jiri Pirko351638e2013-05-28 01:30:21 +00002049
Petr Machata26372602018-12-06 17:05:45 +00002050static int call_netdevice_notifiers_extack(unsigned long val,
2051 struct net_device *dev,
2052 struct netlink_ext_ack *extack)
2053{
2054 struct netdev_notifier_info info = {
2055 .dev = dev,
2056 .extack = extack,
2057 };
2058
2059 return call_netdevice_notifiers_info(val, &info);
2060}
2061
Jiri Pirko351638e2013-05-28 01:30:21 +00002062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * call_netdevice_notifiers - call all network notifier blocks
2064 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07002065 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 *
2067 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07002068 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 */
2070
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07002071int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Petr Machata26372602018-12-06 17:05:45 +00002073 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
stephen hemmingeredf947f2011-03-24 13:24:01 +00002075EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02002077/**
2078 * call_netdevice_notifiers_mtu - call all network notifier blocks
2079 * @val: value passed unmodified to notifier function
2080 * @dev: net_device pointer passed unmodified to notifier function
2081 * @arg: additional u32 argument passed to the notifier function
2082 *
2083 * Call all network notifier blocks. Parameters and return value
2084 * are as for raw_notifier_call_chain().
2085 */
2086static int call_netdevice_notifiers_mtu(unsigned long val,
2087 struct net_device *dev, u32 arg)
2088{
2089 struct netdev_notifier_info_ext info = {
2090 .info.dev = dev,
2091 .ext.mtu = arg,
2092 };
2093
2094 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
2095
2096 return call_netdevice_notifiers_info(val, &info.info);
2097}
2098
Pablo Neira1cf519002015-05-13 18:19:37 +02002099#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002100static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002101
2102void net_inc_ingress_queue(void)
2103{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002104 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002105}
2106EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2107
2108void net_dec_ingress_queue(void)
2109{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002110 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02002111}
2112EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2113#endif
2114
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002115#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002116static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002117
2118void net_inc_egress_queue(void)
2119{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002120 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002121}
2122EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2123
2124void net_dec_egress_queue(void)
2125{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07002126 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002127}
2128EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2129#endif
2130
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002131static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002132#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00002133static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08002134static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002135static void netstamp_clear(struct work_struct *work)
2136{
2137 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08002138 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002139
Eric Dumazet13baa002017-03-01 14:28:39 -08002140 wanted = atomic_add_return(deferred, &netstamp_wanted);
2141 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002142 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002143 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002144 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002145}
2146static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00002147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149void net_enable_timestamp(void)
2150{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002151#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002152 int wanted;
2153
2154 while (1) {
2155 wanted = atomic_read(&netstamp_wanted);
2156 if (wanted <= 0)
2157 break;
2158 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2159 return;
2160 }
2161 atomic_inc(&netstamp_needed_deferred);
2162 schedule_work(&netstamp_work);
2163#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002164 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002167EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169void net_disable_timestamp(void)
2170{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002171#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002172 int wanted;
2173
2174 while (1) {
2175 wanted = atomic_read(&netstamp_wanted);
2176 if (wanted <= 1)
2177 break;
2178 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2179 return;
2180 }
2181 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002182 schedule_work(&netstamp_work);
2183#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002184 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002187EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Eric Dumazet3b098e22010-05-15 23:57:10 -07002189static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002191 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002192 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002193 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002196#define net_timestamp_check(COND, SKB) \
2197 if (static_branch_unlikely(&netstamp_needed_key)) { \
2198 if ((COND) && !(SKB)->tstamp) \
2199 __net_timestamp(SKB); \
2200 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002201
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002202bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002203{
2204 unsigned int len;
2205
2206 if (!(dev->flags & IFF_UP))
2207 return false;
2208
2209 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2210 if (skb->len <= len)
2211 return true;
2212
2213 /* if TSO is enabled, we don't care about the length as the packet
2214 * could be forwarded without being segmented before
2215 */
2216 if (skb_is_gso(skb))
2217 return true;
2218
2219 return false;
2220}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002221EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002222
Herbert Xua0265d22014-04-17 13:45:03 +08002223int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2224{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002225 int ret = ____dev_forward_skb(dev, skb);
2226
2227 if (likely(!ret)) {
2228 skb->protocol = eth_type_trans(skb, dev);
2229 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002230 }
2231
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002232 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002233}
2234EXPORT_SYMBOL_GPL(__dev_forward_skb);
2235
Arnd Bergmann44540962009-11-26 06:07:08 +00002236/**
2237 * dev_forward_skb - loopback an skb to another netif
2238 *
2239 * @dev: destination network device
2240 * @skb: buffer to forward
2241 *
2242 * return values:
2243 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002244 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002245 *
2246 * dev_forward_skb can be used for injecting an skb from the
2247 * start_xmit function of one device into the receive queue
2248 * of another device.
2249 *
2250 * The receiving device may be in another namespace, so
2251 * we have to clear all information in the skb that could
2252 * impact namespace isolation.
2253 */
2254int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2255{
Herbert Xua0265d22014-04-17 13:45:03 +08002256 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002257}
2258EXPORT_SYMBOL_GPL(dev_forward_skb);
2259
Changli Gao71d9dec2010-12-15 19:57:25 +00002260static inline int deliver_skb(struct sk_buff *skb,
2261 struct packet_type *pt_prev,
2262 struct net_device *orig_dev)
2263{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002264 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002265 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002266 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002267 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2268}
2269
Salam Noureddine7866a622015-01-27 11:35:48 -08002270static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2271 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002272 struct net_device *orig_dev,
2273 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002274 struct list_head *ptype_list)
2275{
2276 struct packet_type *ptype, *pt_prev = *pt;
2277
2278 list_for_each_entry_rcu(ptype, ptype_list, list) {
2279 if (ptype->type != type)
2280 continue;
2281 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002282 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002283 pt_prev = ptype;
2284 }
2285 *pt = pt_prev;
2286}
2287
Eric Leblondc0de08d2012-08-16 22:02:58 +00002288static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2289{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002290 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002291 return false;
2292
2293 if (ptype->id_match)
2294 return ptype->id_match(ptype, skb->sk);
2295 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2296 return true;
2297
2298 return false;
2299}
2300
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002301/**
2302 * dev_nit_active - return true if any network interface taps are in use
2303 *
2304 * @dev: network device to check for the presence of taps
2305 */
2306bool dev_nit_active(struct net_device *dev)
2307{
2308 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2309}
2310EXPORT_SYMBOL_GPL(dev_nit_active);
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
2313 * Support routine. Sends outgoing frames to any network
2314 * taps currently in use.
2315 */
2316
David Ahern74b20582016-05-10 11:19:50 -07002317void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002320 struct sk_buff *skb2 = NULL;
2321 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002322 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002325again:
2326 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002327 if (ptype->ignore_outgoing)
2328 continue;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* Never send packets back to the socket
2331 * they originated from - MvS (miquels@drinkel.ow.org)
2332 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002333 if (skb_loop_sk(ptype, skb))
2334 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002335
Salam Noureddine7866a622015-01-27 11:35:48 -08002336 if (pt_prev) {
2337 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002338 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002339 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002341
2342 /* need to clone skb, done only once */
2343 skb2 = skb_clone(skb, GFP_ATOMIC);
2344 if (!skb2)
2345 goto out_unlock;
2346
2347 net_timestamp_set(skb2);
2348
2349 /* skb->nh should be correctly
2350 * set by sender, so that the second statement is
2351 * just protection against buggy protocols.
2352 */
2353 skb_reset_mac_header(skb2);
2354
2355 if (skb_network_header(skb2) < skb2->data ||
2356 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2357 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2358 ntohs(skb2->protocol),
2359 dev->name);
2360 skb_reset_network_header(skb2);
2361 }
2362
2363 skb2->transport_header = skb2->network_header;
2364 skb2->pkt_type = PACKET_OUTGOING;
2365 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002367
2368 if (ptype_list == &ptype_all) {
2369 ptype_list = &dev->ptype_all;
2370 goto again;
2371 }
2372out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002373 if (pt_prev) {
2374 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2375 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2376 else
2377 kfree_skb(skb2);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 rcu_read_unlock();
2380}
David Ahern74b20582016-05-10 11:19:50 -07002381EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Ben Hutchings2c530402012-07-10 10:55:09 +00002383/**
2384 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002385 * @dev: Network device
2386 * @txq: number of queues available
2387 *
2388 * If real_num_tx_queues is changed the tc mappings may no longer be
2389 * valid. To resolve this verify the tc mapping remains valid and if
2390 * not NULL the mapping. With no priorities mapping to this
2391 * offset/count pair it will no longer be used. In the worst case TC0
2392 * is invalid nothing can be done so disable priority mappings. If is
2393 * expected that drivers will fix this mapping if they can before
2394 * calling netif_set_real_num_tx_queues.
2395 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002396static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002397{
2398 int i;
2399 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2400
2401 /* If TC0 is invalidated disable TC mapping */
2402 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002403 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002404 dev->num_tc = 0;
2405 return;
2406 }
2407
2408 /* Invalidated prio to tc mappings set to TC0 */
2409 for (i = 1; i < TC_BITMASK + 1; i++) {
2410 int q = netdev_get_prio_tc_map(dev, i);
2411
2412 tc = &dev->tc_to_txq[q];
2413 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002414 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2415 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002416 netdev_set_prio_tc_map(dev, i, 0);
2417 }
2418 }
2419}
2420
Alexander Duyck8d059b02016-10-28 11:43:49 -04002421int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2422{
2423 if (dev->num_tc) {
2424 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2425 int i;
2426
Alexander Duyckffcfe252018-07-09 12:19:38 -04002427 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002428 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2429 if ((txq - tc->offset) < tc->count)
2430 return i;
2431 }
2432
Alexander Duyckffcfe252018-07-09 12:19:38 -04002433 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002434 return -1;
2435 }
2436
2437 return 0;
2438}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002439EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002440
Alexander Duyck537c00d2013-01-10 08:57:02 +00002441#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002442struct static_key xps_needed __read_mostly;
2443EXPORT_SYMBOL(xps_needed);
2444struct static_key xps_rxqs_needed __read_mostly;
2445EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002446static DEFINE_MUTEX(xps_map_mutex);
2447#define xmap_dereference(P) \
2448 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2449
Alexander Duyck6234f872016-10-28 11:46:49 -04002450static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2451 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002452{
2453 struct xps_map *map = NULL;
2454 int pos;
2455
2456 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002457 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002458 if (!map)
2459 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002460
Alexander Duyck6234f872016-10-28 11:46:49 -04002461 for (pos = map->len; pos--;) {
2462 if (map->queues[pos] != index)
2463 continue;
2464
2465 if (map->len > 1) {
2466 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002467 break;
2468 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002469
Amritha Nambiar80d19662018-06-29 21:26:41 -07002470 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002471 kfree_rcu(map, rcu);
2472 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002473 }
2474
Alexander Duyck6234f872016-10-28 11:46:49 -04002475 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002476}
2477
Alexander Duyck6234f872016-10-28 11:46:49 -04002478static bool remove_xps_queue_cpu(struct net_device *dev,
2479 struct xps_dev_maps *dev_maps,
2480 int cpu, u16 offset, u16 count)
2481{
Alexander Duyck184c4492016-10-28 11:50:13 -04002482 int num_tc = dev->num_tc ? : 1;
2483 bool active = false;
2484 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002485
Alexander Duyck184c4492016-10-28 11:50:13 -04002486 for (tci = cpu * num_tc; num_tc--; tci++) {
2487 int i, j;
2488
2489 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002490 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002491 break;
2492 }
2493
2494 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002495 }
2496
Alexander Duyck184c4492016-10-28 11:50:13 -04002497 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002498}
2499
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002500static void reset_xps_maps(struct net_device *dev,
2501 struct xps_dev_maps *dev_maps,
2502 bool is_rxqs_map)
2503{
2504 if (is_rxqs_map) {
2505 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2506 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2507 } else {
2508 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2509 }
2510 static_key_slow_dec_cpuslocked(&xps_needed);
2511 kfree_rcu(dev_maps, rcu);
2512}
2513
Amritha Nambiar80d19662018-06-29 21:26:41 -07002514static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2515 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2516 u16 offset, u16 count, bool is_rxqs_map)
2517{
2518 bool active = false;
2519 int i, j;
2520
2521 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2522 j < nr_ids;)
2523 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2524 count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002525 if (!active)
2526 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002527
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002528 if (!is_rxqs_map) {
2529 for (i = offset + (count - 1); count--; i--) {
2530 netdev_queue_numa_node_write(
2531 netdev_get_tx_queue(dev, i),
2532 NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002533 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002534 }
2535}
2536
Alexander Duyck6234f872016-10-28 11:46:49 -04002537static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2538 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002539{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002540 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002541 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002542 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002543
Amritha Nambiar04157462018-06-29 21:26:46 -07002544 if (!static_key_false(&xps_needed))
2545 return;
2546
Andrei Vagin4d99f662018-08-08 20:07:35 -07002547 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002548 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002549
Amritha Nambiar04157462018-06-29 21:26:46 -07002550 if (static_key_false(&xps_rxqs_needed)) {
2551 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2552 if (dev_maps) {
2553 nr_ids = dev->num_rx_queues;
2554 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2555 offset, count, true);
2556 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002557 }
2558
2559 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002560 if (!dev_maps)
2561 goto out_no_maps;
2562
Amritha Nambiar80d19662018-06-29 21:26:41 -07002563 if (num_possible_cpus() > 1)
2564 possible_mask = cpumask_bits(cpu_possible_mask);
2565 nr_ids = nr_cpu_ids;
2566 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2567 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002568
Alexander Duyck537c00d2013-01-10 08:57:02 +00002569out_no_maps:
2570 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002571 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002572}
2573
Alexander Duyck6234f872016-10-28 11:46:49 -04002574static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2575{
2576 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2577}
2578
Amritha Nambiar80d19662018-06-29 21:26:41 -07002579static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2580 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002581{
2582 struct xps_map *new_map;
2583 int alloc_len = XPS_MIN_MAP_ALLOC;
2584 int i, pos;
2585
2586 for (pos = 0; map && pos < map->len; pos++) {
2587 if (map->queues[pos] != index)
2588 continue;
2589 return map;
2590 }
2591
Amritha Nambiar80d19662018-06-29 21:26:41 -07002592 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002593 if (map) {
2594 if (pos < map->alloc_len)
2595 return map;
2596
2597 alloc_len = map->alloc_len * 2;
2598 }
2599
Amritha Nambiar80d19662018-06-29 21:26:41 -07002600 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2601 * map
2602 */
2603 if (is_rxqs_map)
2604 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2605 else
2606 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2607 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002608 if (!new_map)
2609 return NULL;
2610
2611 for (i = 0; i < pos; i++)
2612 new_map->queues[i] = map->queues[i];
2613 new_map->alloc_len = alloc_len;
2614 new_map->len = pos;
2615
2616 return new_map;
2617}
2618
Andrei Vagin4d99f662018-08-08 20:07:35 -07002619/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002620int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2621 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002622{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002623 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002624 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002625 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002626 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002627 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002628 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002629 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002630
Alexander Duyck184c4492016-10-28 11:50:13 -04002631 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002632 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002633 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002634 if (num_tc < 0)
2635 return -EINVAL;
2636
2637 /* If queue belongs to subordinate dev use its map */
2638 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2639
Alexander Duyck184c4492016-10-28 11:50:13 -04002640 tc = netdev_txq_to_tc(dev, index);
2641 if (tc < 0)
2642 return -EINVAL;
2643 }
2644
Amritha Nambiar80d19662018-06-29 21:26:41 -07002645 mutex_lock(&xps_map_mutex);
2646 if (is_rxqs_map) {
2647 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2648 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2649 nr_ids = dev->num_rx_queues;
2650 } else {
2651 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2652 if (num_possible_cpus() > 1) {
2653 online_mask = cpumask_bits(cpu_online_mask);
2654 possible_mask = cpumask_bits(cpu_possible_mask);
2655 }
2656 dev_maps = xmap_dereference(dev->xps_cpus_map);
2657 nr_ids = nr_cpu_ids;
2658 }
2659
Alexander Duyck184c4492016-10-28 11:50:13 -04002660 if (maps_sz < L1_CACHE_BYTES)
2661 maps_sz = L1_CACHE_BYTES;
2662
Alexander Duyck01c5f862013-01-10 08:57:35 +00002663 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002664 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2665 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002666 if (!new_dev_maps)
2667 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002668 if (!new_dev_maps) {
2669 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002670 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002671 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002672
Amritha Nambiar80d19662018-06-29 21:26:41 -07002673 tci = j * num_tc + tc;
2674 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002675 NULL;
2676
Amritha Nambiar80d19662018-06-29 21:26:41 -07002677 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002678 if (!map)
2679 goto error;
2680
Amritha Nambiar80d19662018-06-29 21:26:41 -07002681 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002682 }
2683
2684 if (!new_dev_maps)
2685 goto out_no_new_maps;
2686
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002687 if (!dev_maps) {
2688 /* Increment static keys at most once per type */
2689 static_key_slow_inc_cpuslocked(&xps_needed);
2690 if (is_rxqs_map)
2691 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2692 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002693
Amritha Nambiar80d19662018-06-29 21:26:41 -07002694 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2695 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002696 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002697 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002698 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002699 map = xmap_dereference(dev_maps->attr_map[tci]);
2700 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002701 }
2702
2703 /* We need to explicitly update tci as prevous loop
2704 * could break out early if dev_maps is NULL.
2705 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002706 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002707
Amritha Nambiar80d19662018-06-29 21:26:41 -07002708 if (netif_attr_test_mask(j, mask, nr_ids) &&
2709 netif_attr_test_online(j, online_mask, nr_ids)) {
2710 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002711 int pos = 0;
2712
Amritha Nambiar80d19662018-06-29 21:26:41 -07002713 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002714 while ((pos < map->len) && (map->queues[pos] != index))
2715 pos++;
2716
2717 if (pos == map->len)
2718 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002719#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002720 if (!is_rxqs_map) {
2721 if (numa_node_id == -2)
2722 numa_node_id = cpu_to_node(j);
2723 else if (numa_node_id != cpu_to_node(j))
2724 numa_node_id = -1;
2725 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002726#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002727 } else if (dev_maps) {
2728 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002729 map = xmap_dereference(dev_maps->attr_map[tci]);
2730 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002731 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002732
Alexander Duyck184c4492016-10-28 11:50:13 -04002733 /* copy maps belonging to foreign traffic classes */
2734 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2735 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002736 map = xmap_dereference(dev_maps->attr_map[tci]);
2737 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002738 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002739 }
2740
Amritha Nambiar80d19662018-06-29 21:26:41 -07002741 if (is_rxqs_map)
2742 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2743 else
2744 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002745
Alexander Duyck537c00d2013-01-10 08:57:02 +00002746 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002747 if (!dev_maps)
2748 goto out_no_old_maps;
2749
Amritha Nambiar80d19662018-06-29 21:26:41 -07002750 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2751 j < nr_ids;) {
2752 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2753 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2754 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002755 if (map && map != new_map)
2756 kfree_rcu(map, rcu);
2757 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002758 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002759
Alexander Duyck184c4492016-10-28 11:50:13 -04002760 kfree_rcu(dev_maps, rcu);
2761
2762out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002763 dev_maps = new_dev_maps;
2764 active = true;
2765
2766out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002767 if (!is_rxqs_map) {
2768 /* update Tx queue numa node */
2769 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2770 (numa_node_id >= 0) ?
2771 numa_node_id : NUMA_NO_NODE);
2772 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002773
Alexander Duyck01c5f862013-01-10 08:57:35 +00002774 if (!dev_maps)
2775 goto out_no_maps;
2776
Amritha Nambiar80d19662018-06-29 21:26:41 -07002777 /* removes tx-queue from unused CPUs/rx-queues */
2778 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2779 j < nr_ids;) {
2780 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002781 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002782 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2783 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002784 active |= remove_xps_queue(dev_maps, tci, index);
2785 for (i = num_tc - tc, tci++; --i; tci++)
2786 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002787 }
2788
2789 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002790 if (!active)
2791 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002792
2793out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002794 mutex_unlock(&xps_map_mutex);
2795
2796 return 0;
2797error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002798 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002799 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2800 j < nr_ids;) {
2801 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2802 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002803 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002804 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002805 NULL;
2806 if (new_map && new_map != map)
2807 kfree(new_map);
2808 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002809 }
2810
Alexander Duyck537c00d2013-01-10 08:57:02 +00002811 mutex_unlock(&xps_map_mutex);
2812
Alexander Duyck537c00d2013-01-10 08:57:02 +00002813 kfree(new_dev_maps);
2814 return -ENOMEM;
2815}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002816EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002817
2818int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2819 u16 index)
2820{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002821 int ret;
2822
2823 cpus_read_lock();
2824 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2825 cpus_read_unlock();
2826
2827 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002828}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002829EXPORT_SYMBOL(netif_set_xps_queue);
2830
2831#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002832static void netdev_unbind_all_sb_channels(struct net_device *dev)
2833{
2834 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2835
2836 /* Unbind any subordinate channels */
2837 while (txq-- != &dev->_tx[0]) {
2838 if (txq->sb_dev)
2839 netdev_unbind_sb_channel(dev, txq->sb_dev);
2840 }
2841}
2842
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002843void netdev_reset_tc(struct net_device *dev)
2844{
Alexander Duyck6234f872016-10-28 11:46:49 -04002845#ifdef CONFIG_XPS
2846 netif_reset_xps_queues_gt(dev, 0);
2847#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002848 netdev_unbind_all_sb_channels(dev);
2849
2850 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002851 dev->num_tc = 0;
2852 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2853 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2854}
2855EXPORT_SYMBOL(netdev_reset_tc);
2856
2857int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2858{
2859 if (tc >= dev->num_tc)
2860 return -EINVAL;
2861
Alexander Duyck6234f872016-10-28 11:46:49 -04002862#ifdef CONFIG_XPS
2863 netif_reset_xps_queues(dev, offset, count);
2864#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002865 dev->tc_to_txq[tc].count = count;
2866 dev->tc_to_txq[tc].offset = offset;
2867 return 0;
2868}
2869EXPORT_SYMBOL(netdev_set_tc_queue);
2870
2871int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2872{
2873 if (num_tc > TC_MAX_QUEUE)
2874 return -EINVAL;
2875
Alexander Duyck6234f872016-10-28 11:46:49 -04002876#ifdef CONFIG_XPS
2877 netif_reset_xps_queues_gt(dev, 0);
2878#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002879 netdev_unbind_all_sb_channels(dev);
2880
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002881 dev->num_tc = num_tc;
2882 return 0;
2883}
2884EXPORT_SYMBOL(netdev_set_num_tc);
2885
Alexander Duyckffcfe252018-07-09 12:19:38 -04002886void netdev_unbind_sb_channel(struct net_device *dev,
2887 struct net_device *sb_dev)
2888{
2889 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2890
2891#ifdef CONFIG_XPS
2892 netif_reset_xps_queues_gt(sb_dev, 0);
2893#endif
2894 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2895 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2896
2897 while (txq-- != &dev->_tx[0]) {
2898 if (txq->sb_dev == sb_dev)
2899 txq->sb_dev = NULL;
2900 }
2901}
2902EXPORT_SYMBOL(netdev_unbind_sb_channel);
2903
2904int netdev_bind_sb_channel_queue(struct net_device *dev,
2905 struct net_device *sb_dev,
2906 u8 tc, u16 count, u16 offset)
2907{
2908 /* Make certain the sb_dev and dev are already configured */
2909 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2910 return -EINVAL;
2911
2912 /* We cannot hand out queues we don't have */
2913 if ((offset + count) > dev->real_num_tx_queues)
2914 return -EINVAL;
2915
2916 /* Record the mapping */
2917 sb_dev->tc_to_txq[tc].count = count;
2918 sb_dev->tc_to_txq[tc].offset = offset;
2919
2920 /* Provide a way for Tx queue to find the tc_to_txq map or
2921 * XPS map for itself.
2922 */
2923 while (count--)
2924 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2925
2926 return 0;
2927}
2928EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2929
2930int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2931{
2932 /* Do not use a multiqueue device to represent a subordinate channel */
2933 if (netif_is_multiqueue(dev))
2934 return -ENODEV;
2935
2936 /* We allow channels 1 - 32767 to be used for subordinate channels.
2937 * Channel 0 is meant to be "native" mode and used only to represent
2938 * the main root device. We allow writing 0 to reset the device back
2939 * to normal mode after being used as a subordinate channel.
2940 */
2941 if (channel > S16_MAX)
2942 return -EINVAL;
2943
2944 dev->num_tc = -channel;
2945
2946 return 0;
2947}
2948EXPORT_SYMBOL(netdev_set_sb_channel);
2949
John Fastabendf0796d52010-07-01 13:21:57 +00002950/*
2951 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002952 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002953 */
Tom Herberte6484932010-10-18 18:04:39 +00002954int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002955{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002956 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002957 int rc;
2958
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002959 disabling = txq < dev->real_num_tx_queues;
2960
Tom Herberte6484932010-10-18 18:04:39 +00002961 if (txq < 1 || txq > dev->num_tx_queues)
2962 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002963
Ben Hutchings5c565802011-02-15 19:39:21 +00002964 if (dev->reg_state == NETREG_REGISTERED ||
2965 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002966 ASSERT_RTNL();
2967
Tom Herbert1d24eb42010-11-21 13:17:27 +00002968 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2969 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002970 if (rc)
2971 return rc;
2972
John Fastabend4f57c082011-01-17 08:06:04 +00002973 if (dev->num_tc)
2974 netif_setup_tc(dev, txq);
2975
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002976 dev->real_num_tx_queues = txq;
2977
2978 if (disabling) {
2979 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002980 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002981#ifdef CONFIG_XPS
2982 netif_reset_xps_queues_gt(dev, txq);
2983#endif
2984 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002985 } else {
2986 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002987 }
Tom Herberte6484932010-10-18 18:04:39 +00002988
Tom Herberte6484932010-10-18 18:04:39 +00002989 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002990}
2991EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002992
Michael Daltona953be52014-01-16 22:23:28 -08002993#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002994/**
2995 * netif_set_real_num_rx_queues - set actual number of RX queues used
2996 * @dev: Network device
2997 * @rxq: Actual number of RX queues
2998 *
2999 * This must be called either with the rtnl_lock held or before
3000 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07003001 * negative error code. If called before registration, it always
3002 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003003 */
3004int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
3005{
3006 int rc;
3007
Tom Herbertbd25fa72010-10-18 18:00:16 +00003008 if (rxq < 1 || rxq > dev->num_rx_queues)
3009 return -EINVAL;
3010
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003011 if (dev->reg_state == NETREG_REGISTERED) {
3012 ASSERT_RTNL();
3013
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003014 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
3015 rxq);
3016 if (rc)
3017 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003018 }
3019
3020 dev->real_num_rx_queues = rxq;
3021 return 0;
3022}
3023EXPORT_SYMBOL(netif_set_real_num_rx_queues);
3024#endif
3025
Ben Hutchings2c530402012-07-10 10:55:09 +00003026/**
3027 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00003028 *
3029 * This routine should set an upper limit on the number of RSS queues
3030 * used by default by multiqueue devices.
3031 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00003032int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00003033{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05303034 return is_kdump_kernel() ?
3035 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00003036}
3037EXPORT_SYMBOL(netif_get_num_default_rss_queues);
3038
Eric Dumazet3bcb8462016-06-04 20:02:28 -07003039static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003040{
3041 struct softnet_data *sd;
3042 unsigned long flags;
3043
3044 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05003045 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00003046 q->next_sched = NULL;
3047 *sd->output_queue_tailp = q;
3048 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003049 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3050 local_irq_restore(flags);
3051}
3052
David S. Miller37437bb2008-07-16 02:15:04 -07003053void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08003054{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003055 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
3056 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08003057}
3058EXPORT_SYMBOL(__netif_schedule);
3059
Eric Dumazete6247022013-12-05 04:45:08 -08003060struct dev_kfree_skb_cb {
3061 enum skb_free_reason reason;
3062};
3063
3064static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08003065{
Eric Dumazete6247022013-12-05 04:45:08 -08003066 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08003067}
Denis Vlasenko56079432006-03-29 15:57:29 -08003068
John Fastabend46e5da40a2014-09-12 20:04:52 -07003069void netif_schedule_queue(struct netdev_queue *txq)
3070{
3071 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03003072 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07003073 struct Qdisc *q = rcu_dereference(txq->qdisc);
3074
3075 __netif_schedule(q);
3076 }
3077 rcu_read_unlock();
3078}
3079EXPORT_SYMBOL(netif_schedule_queue);
3080
John Fastabend46e5da40a2014-09-12 20:04:52 -07003081void netif_tx_wake_queue(struct netdev_queue *dev_queue)
3082{
3083 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3084 struct Qdisc *q;
3085
3086 rcu_read_lock();
3087 q = rcu_dereference(dev_queue->qdisc);
3088 __netif_schedule(q);
3089 rcu_read_unlock();
3090 }
3091}
3092EXPORT_SYMBOL(netif_tx_wake_queue);
3093
Eric Dumazete6247022013-12-05 04:45:08 -08003094void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
3095{
3096 unsigned long flags;
3097
Myungho Jung98998862017-04-25 11:58:15 -07003098 if (unlikely(!skb))
3099 return;
3100
Reshetova, Elena63354792017-06-30 13:07:58 +03003101 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08003102 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03003103 refcount_set(&skb->users, 0);
3104 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08003105 return;
3106 }
3107 get_kfree_skb_cb(skb)->reason = reason;
3108 local_irq_save(flags);
3109 skb->next = __this_cpu_read(softnet_data.completion_queue);
3110 __this_cpu_write(softnet_data.completion_queue, skb);
3111 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3112 local_irq_restore(flags);
3113}
3114EXPORT_SYMBOL(__dev_kfree_skb_irq);
3115
3116void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08003117{
3118 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08003119 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08003120 else
3121 dev_kfree_skb(skb);
3122}
Eric Dumazete6247022013-12-05 04:45:08 -08003123EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08003124
3125
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003126/**
3127 * netif_device_detach - mark device as removed
3128 * @dev: network device
3129 *
3130 * Mark device as removed from system and therefore no longer available.
3131 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003132void netif_device_detach(struct net_device *dev)
3133{
3134 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3135 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003136 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003137 }
3138}
3139EXPORT_SYMBOL(netif_device_detach);
3140
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003141/**
3142 * netif_device_attach - mark device as attached
3143 * @dev: network device
3144 *
3145 * Mark device as attached from system and restart if needed.
3146 */
Denis Vlasenko56079432006-03-29 15:57:29 -08003147void netif_device_attach(struct net_device *dev)
3148{
3149 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3150 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00003151 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003152 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08003153 }
3154}
3155EXPORT_SYMBOL(netif_device_attach);
3156
Jiri Pirko5605c762015-05-12 14:56:12 +02003157/*
3158 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3159 * to be used as a distribution range.
3160 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003161static u16 skb_tx_hash(const struct net_device *dev,
3162 const struct net_device *sb_dev,
3163 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003164{
3165 u32 hash;
3166 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003167 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003168
Alexander Duyckeadec8772018-07-09 12:19:48 -04003169 if (dev->num_tc) {
3170 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3171
3172 qoffset = sb_dev->tc_to_txq[tc].offset;
3173 qcount = sb_dev->tc_to_txq[tc].count;
3174 }
3175
Jiri Pirko5605c762015-05-12 14:56:12 +02003176 if (skb_rx_queue_recorded(skb)) {
3177 hash = skb_get_rx_queue(skb);
Amritha Nambiar6e11d152020-02-24 10:56:00 -08003178 if (hash >= qoffset)
3179 hash -= qoffset;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003180 while (unlikely(hash >= qcount))
3181 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003182 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003183 }
3184
3185 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3186}
Jiri Pirko5605c762015-05-12 14:56:12 +02003187
Ben Hutchings36c92472012-01-17 07:57:56 +00003188static void skb_warn_bad_offload(const struct sk_buff *skb)
3189{
Wei Tang84d15ae2016-06-16 21:17:49 +08003190 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003191 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003192 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003193
Ben Greearc846ad92013-04-19 10:45:52 +00003194 if (!net_ratelimit())
3195 return;
3196
Bjørn Mork88ad4172015-11-16 19:16:40 +01003197 if (dev) {
3198 if (dev->dev.parent)
3199 name = dev_driver_string(dev->dev.parent);
3200 else
3201 name = netdev_name(dev);
3202 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003203 skb_dump(KERN_WARNING, skb, false);
3204 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003205 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003206 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003207}
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209/*
3210 * Invalidate hardware checksum when packet is to be mangled, and
3211 * complete checksum manually on outgoing path.
3212 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003213int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Al Virod3bc23e2006-11-14 21:24:49 -08003215 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003216 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Patrick McHardy84fa7932006-08-29 16:44:56 -07003218 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003219 goto out_set_summed;
3220
3221 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003222 skb_warn_bad_offload(skb);
3223 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225
Eric Dumazetcef401d2013-01-25 20:34:37 +00003226 /* Before computing a checksum, we should make sure no frag could
3227 * be modified by an external entity : checksum could be wrong.
3228 */
3229 if (skb_has_shared_frag(skb)) {
3230 ret = __skb_linearize(skb);
3231 if (ret)
3232 goto out;
3233 }
3234
Michał Mirosław55508d62010-12-14 15:24:08 +00003235 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003236 BUG_ON(offset >= skb_headlen(skb));
3237 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3238
3239 offset += skb->csum_offset;
3240 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3241
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003242 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3243 if (ret)
3244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003246 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003247out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003249out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return ret;
3251}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003252EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Davide Carattib72b5bf2017-05-18 15:44:38 +02003254int skb_crc32c_csum_help(struct sk_buff *skb)
3255{
3256 __le32 crc32c_csum;
3257 int ret = 0, offset, start;
3258
3259 if (skb->ip_summed != CHECKSUM_PARTIAL)
3260 goto out;
3261
3262 if (unlikely(skb_is_gso(skb)))
3263 goto out;
3264
3265 /* Before computing a checksum, we should make sure no frag could
3266 * be modified by an external entity : checksum could be wrong.
3267 */
3268 if (unlikely(skb_has_shared_frag(skb))) {
3269 ret = __skb_linearize(skb);
3270 if (ret)
3271 goto out;
3272 }
3273 start = skb_checksum_start_offset(skb);
3274 offset = start + offsetof(struct sctphdr, checksum);
3275 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3276 ret = -EINVAL;
3277 goto out;
3278 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003279
3280 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3281 if (ret)
3282 goto out;
3283
Davide Carattib72b5bf2017-05-18 15:44:38 +02003284 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3285 skb->len - start, ~(__u32)0,
3286 crc32c_csum_stub));
3287 *(__le32 *)(skb->data + offset) = crc32c_csum;
3288 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003289 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003290out:
3291 return ret;
3292}
3293
Vlad Yasevich53d64712014-03-27 17:26:18 -04003294__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003295{
3296 __be16 type = skb->protocol;
3297
Pravin B Shelar19acc322013-05-07 20:41:07 +00003298 /* Tunnel gso handlers can set protocol to ethernet. */
3299 if (type == htons(ETH_P_TEB)) {
3300 struct ethhdr *eth;
3301
3302 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3303 return 0;
3304
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003305 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003306 type = eth->h_proto;
3307 }
3308
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003309 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003310}
3311
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003312/**
3313 * skb_mac_gso_segment - mac layer segmentation handler.
3314 * @skb: buffer to segment
3315 * @features: features for the output path (see dev->features)
3316 */
3317struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3318 netdev_features_t features)
3319{
3320 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3321 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003322 int vlan_depth = skb->mac_len;
3323 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003324
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003325 if (unlikely(!type))
3326 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003327
Vlad Yasevich53d64712014-03-27 17:26:18 -04003328 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003329
3330 rcu_read_lock();
3331 list_for_each_entry_rcu(ptype, &offload_base, list) {
3332 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003333 segs = ptype->callbacks.gso_segment(skb, features);
3334 break;
3335 }
3336 }
3337 rcu_read_unlock();
3338
3339 __skb_push(skb, skb->data - skb_mac_header(skb));
3340
3341 return segs;
3342}
3343EXPORT_SYMBOL(skb_mac_gso_segment);
3344
3345
Cong Wang12b00042013-02-05 16:36:38 +00003346/* openvswitch calls this on rx path, so we need a different check.
3347 */
3348static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3349{
3350 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003351 return skb->ip_summed != CHECKSUM_PARTIAL &&
3352 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003353
3354 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003355}
3356
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003357/**
Cong Wang12b00042013-02-05 16:36:38 +00003358 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003359 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003360 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003361 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003362 *
3363 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003364 *
3365 * It may return NULL if the skb requires no segmentation. This is
3366 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003367 *
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003368 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003369 */
Cong Wang12b00042013-02-05 16:36:38 +00003370struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3371 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003372{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003373 struct sk_buff *segs;
3374
Cong Wang12b00042013-02-05 16:36:38 +00003375 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003376 int err;
3377
Eric Dumazetb2504a52017-01-31 10:20:32 -08003378 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003379 err = skb_cow_head(skb, 0);
3380 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003381 return ERR_PTR(err);
3382 }
3383
Alexander Duyck802ab552016-04-10 21:45:03 -04003384 /* Only report GSO partial support if it will enable us to
3385 * support segmentation on this frame without needing additional
3386 * work.
3387 */
3388 if (features & NETIF_F_GSO_PARTIAL) {
3389 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3390 struct net_device *dev = skb->dev;
3391
3392 partial_features |= dev->features & dev->gso_partial_features;
3393 if (!skb_gso_ok(skb, features | partial_features))
3394 features &= ~NETIF_F_GSO_PARTIAL;
3395 }
3396
Cambda Zhua08e7fd2020-03-26 15:33:14 +08003397 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003398 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3399
Pravin B Shelar68c33162013-02-14 14:02:41 +00003400 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003401 SKB_GSO_CB(skb)->encap_level = 0;
3402
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003403 skb_reset_mac_header(skb);
3404 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003405
Eric Dumazetb2504a52017-01-31 10:20:32 -08003406 segs = skb_mac_gso_segment(skb, features);
3407
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003408 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003409 skb_warn_bad_offload(skb);
3410
3411 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003412}
Cong Wang12b00042013-02-05 16:36:38 +00003413EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003414
Herbert Xufb286bb2005-11-10 13:01:24 -08003415/* Take action when hardware reception checksum errors are detected. */
3416#ifdef CONFIG_BUG
Cong Wang7fe50ac2018-11-12 14:47:18 -08003417void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003418{
3419 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003420 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Willem de Bruijn64131392019-07-07 05:51:55 -04003421 skb_dump(KERN_ERR, skb, true);
Herbert Xufb286bb2005-11-10 13:01:24 -08003422 dump_stack();
3423 }
3424}
3425EXPORT_SYMBOL(netdev_rx_csum_fault);
3426#endif
3427
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003428/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003429static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003431#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 int i;
tchardingf4563a72017-02-09 17:56:07 +11003433
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003434 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003435 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3436 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003437
Ian Campbellea2ab692011-08-22 23:44:58 +00003438 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003439 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003440 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003441 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return 0;
3444}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Simon Horman3b392dd2014-06-04 08:53:17 +09003446/* If MPLS offload request, verify we are testing hardware MPLS features
3447 * instead of standard features for the netdev.
3448 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003449#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003450static netdev_features_t net_mpls_features(struct sk_buff *skb,
3451 netdev_features_t features,
3452 __be16 type)
3453{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003454 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003455 features &= skb->dev->mpls_features;
3456
3457 return features;
3458}
3459#else
3460static netdev_features_t net_mpls_features(struct sk_buff *skb,
3461 netdev_features_t features,
3462 __be16 type)
3463{
3464 return features;
3465}
3466#endif
3467
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003468static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003469 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003470{
Simon Horman3b392dd2014-06-04 08:53:17 +09003471 __be16 type;
3472
Miaohe Lin9fc95f52020-07-30 19:02:36 +08003473 type = skb_network_protocol(skb, NULL);
Simon Horman3b392dd2014-06-04 08:53:17 +09003474 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003475
Ed Cashinc0d680e2012-09-19 15:49:00 +00003476 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003477 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003478 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003479 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003480 if (illegal_highdma(skb->dev, skb))
3481 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003482
3483 return features;
3484}
3485
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003486netdev_features_t passthru_features_check(struct sk_buff *skb,
3487 struct net_device *dev,
3488 netdev_features_t features)
3489{
3490 return features;
3491}
3492EXPORT_SYMBOL(passthru_features_check);
3493
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003494static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003495 struct net_device *dev,
3496 netdev_features_t features)
3497{
3498 return vlan_features_check(skb, features);
3499}
3500
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003501static netdev_features_t gso_features_check(const struct sk_buff *skb,
3502 struct net_device *dev,
3503 netdev_features_t features)
3504{
3505 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3506
3507 if (gso_segs > dev->gso_max_segs)
3508 return features & ~NETIF_F_GSO_MASK;
3509
Alexander Duyck802ab552016-04-10 21:45:03 -04003510 /* Support for GSO partial features requires software
3511 * intervention before we can actually process the packets
3512 * so we need to strip support for any partial features now
3513 * and we can pull them back in after we have partially
3514 * segmented the frame.
3515 */
3516 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3517 features &= ~dev->gso_partial_features;
3518
3519 /* Make sure to clear the IPv4 ID mangling feature if the
3520 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003521 */
3522 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3523 struct iphdr *iph = skb->encapsulation ?
3524 inner_ip_hdr(skb) : ip_hdr(skb);
3525
3526 if (!(iph->frag_off & htons(IP_DF)))
3527 features &= ~NETIF_F_TSO_MANGLEID;
3528 }
3529
3530 return features;
3531}
3532
Florian Westphalc1e756b2014-05-05 15:00:44 +02003533netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003534{
Jesse Gross5f352272014-12-23 22:37:26 -08003535 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003536 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003537
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003538 if (skb_is_gso(skb))
3539 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003540
Jesse Gross5f352272014-12-23 22:37:26 -08003541 /* If encapsulation offload request, verify we are testing
3542 * hardware encapsulation features instead of standard
3543 * features for the netdev
3544 */
3545 if (skb->encapsulation)
3546 features &= dev->hw_enc_features;
3547
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003548 if (skb_vlan_tagged(skb))
3549 features = netdev_intersect_features(features,
3550 dev->vlan_features |
3551 NETIF_F_HW_VLAN_CTAG_TX |
3552 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003553
Jesse Gross5f352272014-12-23 22:37:26 -08003554 if (dev->netdev_ops->ndo_features_check)
3555 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3556 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003557 else
3558 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003559
Florian Westphalc1e756b2014-05-05 15:00:44 +02003560 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003561}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003562EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003563
David S. Miller2ea25512014-08-29 21:10:01 -07003564static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003565 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003566{
David S. Miller2ea25512014-08-29 21:10:01 -07003567 unsigned int len;
3568 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003569
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003570 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003571 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003572
David S. Miller2ea25512014-08-29 21:10:01 -07003573 len = skb->len;
Willy Tarreau37447412020-08-10 10:27:42 +02003574 PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies);
David S. Miller2ea25512014-08-29 21:10:01 -07003575 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003576 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003577 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003578
Patrick McHardy572a9d72009-11-10 06:14:14 +00003579 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003580}
David S. Miller2ea25512014-08-29 21:10:01 -07003581
David S. Miller8dcda222014-09-01 15:06:40 -07003582struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3583 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003584{
3585 struct sk_buff *skb = first;
3586 int rc = NETDEV_TX_OK;
3587
3588 while (skb) {
3589 struct sk_buff *next = skb->next;
3590
David S. Millera8305bf2018-07-29 20:42:53 -07003591 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003592 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003593 if (unlikely(!dev_xmit_complete(rc))) {
3594 skb->next = next;
3595 goto out;
3596 }
3597
3598 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003599 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003600 rc = NETDEV_TX_BUSY;
3601 break;
3602 }
3603 }
3604
3605out:
3606 *ret = rc;
3607 return skb;
3608}
3609
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003610static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3611 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003612{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003613 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003614 !vlan_hw_offload_capable(features, skb->vlan_proto))
3615 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003616 return skb;
3617}
3618
Davide Caratti43c26a12017-05-18 15:44:41 +02003619int skb_csum_hwoffload_help(struct sk_buff *skb,
3620 const netdev_features_t features)
3621{
3622 if (unlikely(skb->csum_not_inet))
3623 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3624 skb_crc32c_csum_help(skb);
3625
3626 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3627}
3628EXPORT_SYMBOL(skb_csum_hwoffload_help);
3629
Steffen Klassertf53c7232017-12-20 10:41:36 +01003630static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003631{
3632 netdev_features_t features;
3633
David S. Millereae3f882014-08-30 15:17:13 -07003634 features = netif_skb_features(skb);
3635 skb = validate_xmit_vlan(skb, features);
3636 if (unlikely(!skb))
3637 goto out_null;
3638
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003639 skb = sk_validate_xmit_skb(skb, dev);
3640 if (unlikely(!skb))
3641 goto out_null;
3642
Johannes Berg8b86a612015-04-17 15:45:04 +02003643 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003644 struct sk_buff *segs;
3645
3646 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003647 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003648 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003649 } else if (segs) {
3650 consume_skb(skb);
3651 skb = segs;
3652 }
David S. Millereae3f882014-08-30 15:17:13 -07003653 } else {
3654 if (skb_needs_linearize(skb, features) &&
3655 __skb_linearize(skb))
3656 goto out_kfree_skb;
3657
3658 /* If packet is not checksummed and device does not
3659 * support checksumming for this protocol, complete
3660 * checksumming here.
3661 */
3662 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3663 if (skb->encapsulation)
3664 skb_set_inner_transport_header(skb,
3665 skb_checksum_start_offset(skb));
3666 else
3667 skb_set_transport_header(skb,
3668 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003669 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003670 goto out_kfree_skb;
3671 }
3672 }
3673
Steffen Klassertf53c7232017-12-20 10:41:36 +01003674 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003675
David S. Millereae3f882014-08-30 15:17:13 -07003676 return skb;
3677
3678out_kfree_skb:
3679 kfree_skb(skb);
3680out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003681 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003682 return NULL;
3683}
3684
Steffen Klassertf53c7232017-12-20 10:41:36 +01003685struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003686{
3687 struct sk_buff *next, *head = NULL, *tail;
3688
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003689 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003690 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003691 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003692
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003693 /* in case skb wont be segmented, point to itself */
3694 skb->prev = skb;
3695
Steffen Klassertf53c7232017-12-20 10:41:36 +01003696 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003697 if (!skb)
3698 continue;
3699
3700 if (!head)
3701 head = skb;
3702 else
3703 tail->next = skb;
3704 /* If skb was segmented, skb->prev points to
3705 * the last segment. If not, it still contains skb.
3706 */
3707 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003708 }
3709 return head;
3710}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003711EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003712
Eric Dumazet1def9232013-01-10 12:36:42 +00003713static void qdisc_pkt_len_init(struct sk_buff *skb)
3714{
3715 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3716
3717 qdisc_skb_cb(skb)->pkt_len = skb->len;
3718
3719 /* To get more precise estimation of bytes sent on wire,
3720 * we add to pkt_len the headers size of all segments
3721 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003722 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003723 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003724 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003725
Eric Dumazet757b8b12013-01-15 21:14:21 -08003726 /* mac layer + network layer */
3727 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3728
3729 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003730 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3731 const struct tcphdr *th;
3732 struct tcphdr _tcphdr;
3733
3734 th = skb_header_pointer(skb, skb_transport_offset(skb),
3735 sizeof(_tcphdr), &_tcphdr);
3736 if (likely(th))
3737 hdr_len += __tcp_hdrlen(th);
3738 } else {
3739 struct udphdr _udphdr;
3740
3741 if (skb_header_pointer(skb, skb_transport_offset(skb),
3742 sizeof(_udphdr), &_udphdr))
3743 hdr_len += sizeof(struct udphdr);
3744 }
Jason Wang15e5a032013-03-25 20:19:59 +00003745
3746 if (shinfo->gso_type & SKB_GSO_DODGY)
3747 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3748 shinfo->gso_size);
3749
3750 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003751 }
3752}
3753
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003754static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3755 struct net_device *dev,
3756 struct netdev_queue *txq)
3757{
3758 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003759 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003760 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003761 int rc;
3762
Eric Dumazeta2da5702011-01-20 03:48:19 +00003763 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003764
3765 if (q->flags & TCQ_F_NOLOCK) {
Petr Machataac5c66f2020-07-14 20:03:08 +03003766 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Paolo Abeni379349e2020-02-18 18:15:44 +01003767 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003768
3769 if (unlikely(to_free))
3770 kfree_skb_list(to_free);
3771 return rc;
3772 }
3773
Eric Dumazet79640a42010-06-02 05:09:29 -07003774 /*
3775 * Heuristic to force contended enqueues to serialize on a
3776 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003777 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003778 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003779 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003780 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003781 if (unlikely(contended))
3782 spin_lock(&q->busylock);
3783
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003784 spin_lock(root_lock);
3785 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003786 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003787 rc = NET_XMIT_DROP;
3788 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003789 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003790 /*
3791 * This is a work-conserving queue; there are no old skbs
3792 * waiting to be sent out; and the qdisc is not running -
3793 * xmit the skb directly.
3794 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003795
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003796 qdisc_bstats_update(q, skb);
3797
Eric Dumazet55a93b32014-10-03 15:31:07 -07003798 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003799 if (unlikely(contended)) {
3800 spin_unlock(&q->busylock);
3801 contended = false;
3802 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003803 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003804 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003805
John Fastabend6c148182017-12-07 09:54:06 -08003806 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003807 rc = NET_XMIT_SUCCESS;
3808 } else {
Petr Machataac5c66f2020-07-14 20:03:08 +03003809 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003810 if (qdisc_run_begin(q)) {
3811 if (unlikely(contended)) {
3812 spin_unlock(&q->busylock);
3813 contended = false;
3814 }
3815 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003816 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003817 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003818 }
3819 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003820 if (unlikely(to_free))
3821 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003822 if (unlikely(contended))
3823 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003824 return rc;
3825}
3826
Daniel Borkmann86f85152013-12-29 17:27:11 +01003827#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003828static void skb_update_prio(struct sk_buff *skb)
3829{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003830 const struct netprio_map *map;
3831 const struct sock *sk;
3832 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003833
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003834 if (skb->priority)
3835 return;
3836 map = rcu_dereference_bh(skb->dev->priomap);
3837 if (!map)
3838 return;
3839 sk = skb_to_full_sk(skb);
3840 if (!sk)
3841 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003842
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003843 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3844
3845 if (prioidx < map->priomap_len)
3846 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003847}
3848#else
3849#define skb_update_prio(skb)
3850#endif
3851
Dave Jonesd29f7492008-07-22 14:09:06 -07003852/**
Michel Machado95603e22012-06-12 10:16:35 +00003853 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003854 * @net: network namespace this loopback is happening in
3855 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003856 * @skb: buffer to transmit
3857 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003858int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003859{
3860 skb_reset_mac_header(skb);
3861 __skb_pull(skb, skb_network_offset(skb));
3862 skb->pkt_type = PACKET_LOOPBACK;
3863 skb->ip_summed = CHECKSUM_UNNECESSARY;
3864 WARN_ON(!skb_dst(skb));
3865 skb_dst_force(skb);
3866 netif_rx_ni(skb);
3867 return 0;
3868}
3869EXPORT_SYMBOL(dev_loopback_xmit);
3870
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003871#ifdef CONFIG_NET_EGRESS
3872static struct sk_buff *
3873sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3874{
Jiri Pirko46209402017-11-03 11:46:25 +01003875 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003876 struct tcf_result cl_res;
3877
Jiri Pirko46209402017-11-03 11:46:25 +01003878 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003879 return skb;
3880
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003881 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
wenxu496ef462020-11-25 12:01:21 +08003882 qdisc_skb_cb(skb)->mru = 0;
Jiri Pirko46209402017-11-03 11:46:25 +01003883 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003884
Jiri Pirko46209402017-11-03 11:46:25 +01003885 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003886 case TC_ACT_OK:
3887 case TC_ACT_RECLASSIFY:
3888 skb->tc_index = TC_H_MIN(cl_res.classid);
3889 break;
3890 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003891 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003892 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003893 kfree_skb(skb);
3894 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003895 case TC_ACT_STOLEN:
3896 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003897 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003898 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003899 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003900 return NULL;
3901 case TC_ACT_REDIRECT:
3902 /* No need to push/pop skb's mac_header here on egress! */
3903 skb_do_redirect(skb);
3904 *ret = NET_XMIT_SUCCESS;
3905 return NULL;
3906 default:
3907 break;
3908 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01003909
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003910 return skb;
3911}
3912#endif /* CONFIG_NET_EGRESS */
3913
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003914#ifdef CONFIG_XPS
3915static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3916 struct xps_dev_maps *dev_maps, unsigned int tci)
3917{
3918 struct xps_map *map;
3919 int queue_index = -1;
3920
3921 if (dev->num_tc) {
3922 tci *= dev->num_tc;
3923 tci += netdev_get_prio_tc_map(dev, skb->priority);
3924 }
3925
3926 map = rcu_dereference(dev_maps->attr_map[tci]);
3927 if (map) {
3928 if (map->len == 1)
3929 queue_index = map->queues[0];
3930 else
3931 queue_index = map->queues[reciprocal_scale(
3932 skb_get_hash(skb), map->len)];
3933 if (unlikely(queue_index >= dev->real_num_tx_queues))
3934 queue_index = -1;
3935 }
3936 return queue_index;
3937}
3938#endif
3939
Alexander Duyckeadec8772018-07-09 12:19:48 -04003940static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3941 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003942{
3943#ifdef CONFIG_XPS
3944 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003945 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003946 int queue_index = -1;
3947
Amritha Nambiar04157462018-06-29 21:26:46 -07003948 if (!static_key_false(&xps_needed))
3949 return -1;
3950
Jiri Pirko638b2a62015-05-12 14:56:13 +02003951 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003952 if (!static_key_false(&xps_rxqs_needed))
3953 goto get_cpus_map;
3954
Alexander Duyckeadec8772018-07-09 12:19:48 -04003955 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003956 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003957 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003958
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003959 if (tci >= 0 && tci < dev->num_rx_queues)
3960 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3961 tci);
3962 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003963
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003964get_cpus_map:
3965 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003966 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003967 if (dev_maps) {
3968 unsigned int tci = skb->sender_cpu - 1;
3969
3970 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3971 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003972 }
3973 }
3974 rcu_read_unlock();
3975
3976 return queue_index;
3977#else
3978 return -1;
3979#endif
3980}
3981
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003982u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003983 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003984{
3985 return 0;
3986}
3987EXPORT_SYMBOL(dev_pick_tx_zero);
3988
3989u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003990 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003991{
3992 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3993}
3994EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3995
Paolo Abenib71b5832019-03-20 11:02:05 +01003996u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3997 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003998{
3999 struct sock *sk = skb->sk;
4000 int queue_index = sk_tx_queue_get(sk);
4001
Alexander Duyckeadec8772018-07-09 12:19:48 -04004002 sb_dev = sb_dev ? : dev;
4003
Jiri Pirko638b2a62015-05-12 14:56:13 +02004004 if (queue_index < 0 || skb->ooo_okay ||
4005 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04004006 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11004007
Jiri Pirko638b2a62015-05-12 14:56:13 +02004008 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04004009 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004010
4011 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07004012 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02004013 rcu_access_pointer(sk->sk_dst_cache))
4014 sk_tx_queue_set(sk, new_index);
4015
4016 queue_index = new_index;
4017 }
4018
4019 return queue_index;
4020}
Paolo Abenib71b5832019-03-20 11:02:05 +01004021EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004022
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004023struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
4024 struct sk_buff *skb,
4025 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004026{
4027 int queue_index = 0;
4028
4029#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08004030 u32 sender_cpu = skb->sender_cpu - 1;
4031
4032 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02004033 skb->sender_cpu = raw_smp_processor_id() + 1;
4034#endif
4035
4036 if (dev->real_num_tx_queues != 1) {
4037 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11004038
Jiri Pirko638b2a62015-05-12 14:56:13 +02004039 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01004040 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004041 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004042 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004043
Alexander Duyckd5845272017-11-22 10:57:41 -08004044 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02004045 }
4046
4047 skb_set_queue_mapping(skb, queue_index);
4048 return netdev_get_tx_queue(dev, queue_index);
4049}
4050
Michel Machado95603e22012-06-12 10:16:35 +00004051/**
Jason Wang9d08dd32014-01-20 11:25:13 +08004052 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07004053 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04004054 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07004055 *
4056 * Queue a buffer for transmission to a network device. The caller must
4057 * have set the device and priority and built the buffer before calling
4058 * this function. The function can be called from an interrupt.
4059 *
4060 * A negative errno code is returned on a failure. A success does not
4061 * guarantee the frame will be transmitted as it may be dropped due
4062 * to congestion or traffic shaping.
4063 *
4064 * -----------------------------------------------------------------------------------
4065 * I notice this method can also return errors from the queue disciplines,
4066 * including NET_XMIT_DROP, which is a positive value. So, errors can also
4067 * be positive.
4068 *
4069 * Regardless of the return value, the skb is consumed, so it is currently
4070 * difficult to retry a send to this method. (You can bump the ref count
4071 * before sending to hold a reference for retry if you are careful.)
4072 *
4073 * When calling this method, interrupts MUST be enabled. This is because
4074 * the BH enable code must have IRQs enabled so that it will not deadlock.
4075 * --BLG
4076 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04004077static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07004080 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 struct Qdisc *q;
4082 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01004083 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00004085 skb_reset_mac_header(skb);
4086
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004087 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4088 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
4089
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004090 /* Disable soft irqs for various locks below. Also
4091 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004093 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Neil Horman5bc14212011-11-22 05:10:51 +00004095 skb_update_prio(skb);
4096
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004097 qdisc_pkt_len_init(skb);
4098#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004099 skb->tc_at_ingress = 0;
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004100# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004101 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004102 skb = sch_handle_egress(skb, &rc, dev);
4103 if (!skb)
4104 goto out;
4105 }
Daniel Borkmann357b6cc2020-03-18 10:33:22 +01004106# endif
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004107#endif
Eric Dumazet02875872014-10-05 18:38:35 -07004108 /* If device/qdisc don't need skb->dst, release it right now while
4109 * its hot in this cpu cache.
4110 */
4111 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4112 skb_dst_drop(skb);
4113 else
4114 skb_dst_force(skb);
4115
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004116 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08004117 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07004118
Koki Sanagicf66ba52010-08-23 18:45:02 +09004119 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00004121 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07004122 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124
4125 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11004126 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
tchardingeb13da12017-02-09 17:56:06 +11004128 * Really, it is unlikely that netif_tx_lock protection is necessary
4129 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4130 * counters.)
4131 * However, it is possible, that they rely on protection
4132 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
tchardingeb13da12017-02-09 17:56:06 +11004134 * Check this and shot the lock. It is not prone from deadlocks.
4135 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 */
4137 if (dev->flags & IFF_UP) {
4138 int cpu = smp_processor_id(); /* ok because BHs are off */
4139
David S. Millerc773e842008-07-08 23:13:53 -07004140 if (txq->xmit_lock_owner != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004141 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004142 goto recursion_alert;
4143
Steffen Klassertf53c7232017-12-20 10:41:36 +01004144 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004145 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004146 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004147
Willy Tarreau37447412020-08-10 10:27:42 +02004148 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
David S. Millerc773e842008-07-08 23:13:53 -07004149 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Tom Herbert734664982011-11-28 16:32:44 +00004151 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004152 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004153 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004154 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004155 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004156 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 goto out;
4158 }
4159 }
David S. Millerc773e842008-07-08 23:13:53 -07004160 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004161 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4162 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 } else {
4164 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004165 * unfortunately
4166 */
4167recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004168 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4169 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
4171 }
4172
4173 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004174 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Eric Dumazet015f0682014-03-27 08:45:56 -07004176 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004177 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return rc;
4179out:
Herbert Xud4828d82006-06-22 02:28:18 -07004180 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return rc;
4182}
Jason Wangf663dd92014-01-10 16:18:26 +08004183
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004184int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004185{
4186 return __dev_queue_xmit(skb, NULL);
4187}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004188EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Alexander Duyckeadec8772018-07-09 12:19:48 -04004190int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004191{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004192 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004193}
4194EXPORT_SYMBOL(dev_queue_xmit_accel);
4195
Björn Töpel36ccdf82020-11-23 18:56:00 +01004196int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004197{
4198 struct net_device *dev = skb->dev;
4199 struct sk_buff *orig_skb = skb;
4200 struct netdev_queue *txq;
4201 int ret = NETDEV_TX_BUSY;
4202 bool again = false;
4203
4204 if (unlikely(!netif_running(dev) ||
4205 !netif_carrier_ok(dev)))
4206 goto drop;
4207
4208 skb = validate_xmit_skb_list(skb, dev, &again);
4209 if (skb != orig_skb)
4210 goto drop;
4211
4212 skb_set_queue_mapping(skb, queue_id);
4213 txq = skb_get_tx_queue(dev, skb);
Willy Tarreau37447412020-08-10 10:27:42 +02004214 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004215
4216 local_bh_disable();
4217
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004218 dev_xmit_recursion_inc();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004219 HARD_TX_LOCK(dev, txq, smp_processor_id());
4220 if (!netif_xmit_frozen_or_drv_stopped(txq))
4221 ret = netdev_start_xmit(skb, dev, txq, false);
4222 HARD_TX_UNLOCK(dev, txq);
Eric Dumazet0ad6f6e2020-06-17 22:23:25 -07004223 dev_xmit_recursion_dec();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004224
4225 local_bh_enable();
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004226 return ret;
4227drop:
4228 atomic_long_inc(&dev->tx_dropped);
4229 kfree_skb_list(skb);
4230 return NET_XMIT_DROP;
4231}
Björn Töpel36ccdf82020-11-23 18:56:00 +01004232EXPORT_SYMBOL(__dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
tchardingeb13da12017-02-09 17:56:06 +11004234/*************************************************************************
4235 * Receiver routines
4236 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004238int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004239EXPORT_SYMBOL(netdev_max_backlog);
4240
Eric Dumazet3b098e22010-05-15 23:57:10 -07004241int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004242int netdev_budget __read_mostly = 300;
Konstantin Khlebnikova48379802020-04-06 14:39:32 +03004243/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4244unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004245int weight_p __read_mostly = 64; /* old backlog weight */
4246int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4247int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4248int dev_rx_weight __read_mostly = 64;
4249int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004250/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4251int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004253/* Called with irq disabled */
4254static inline void ____napi_schedule(struct softnet_data *sd,
4255 struct napi_struct *napi)
4256{
4257 list_add_tail(&napi->poll_list, &sd->poll_list);
4258 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4259}
4260
Eric Dumazetdf334542010-03-24 19:13:54 +00004261#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004262
4263/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004264struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004265EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004266u32 rps_cpu_mask __read_mostly;
4267EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004268
Eric Dumazetdc053602019-03-22 08:56:38 -07004269struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004270EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004271struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004272EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004273
Ben Hutchingsc4454772011-01-19 11:03:53 +00004274static struct rps_dev_flow *
4275set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4276 struct rps_dev_flow *rflow, u16 next_cpu)
4277{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004278 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004279#ifdef CONFIG_RFS_ACCEL
4280 struct netdev_rx_queue *rxqueue;
4281 struct rps_dev_flow_table *flow_table;
4282 struct rps_dev_flow *old_rflow;
4283 u32 flow_id;
4284 u16 rxq_index;
4285 int rc;
4286
4287 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004288 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4289 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004290 goto out;
4291 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4292 if (rxq_index == skb_get_rx_queue(skb))
4293 goto out;
4294
4295 rxqueue = dev->_rx + rxq_index;
4296 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4297 if (!flow_table)
4298 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004299 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004300 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4301 rxq_index, flow_id);
4302 if (rc < 0)
4303 goto out;
4304 old_rflow = rflow;
4305 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004306 rflow->filter = rc;
4307 if (old_rflow->filter == rflow->filter)
4308 old_rflow->filter = RPS_NO_FILTER;
4309 out:
4310#endif
4311 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004312 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004313 }
4314
Ben Hutchings09994d12011-10-03 04:42:46 +00004315 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004316 return rflow;
4317}
4318
Tom Herbert0a9627f2010-03-16 08:03:29 +00004319/*
4320 * get_rps_cpu is called from netif_receive_skb and returns the target
4321 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004322 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004323 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004324static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4325 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004326{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004327 const struct rps_sock_flow_table *sock_flow_table;
4328 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004329 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004330 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004331 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004332 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004333 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004334
Tom Herbert0a9627f2010-03-16 08:03:29 +00004335 if (skb_rx_queue_recorded(skb)) {
4336 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004337
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004338 if (unlikely(index >= dev->real_num_rx_queues)) {
4339 WARN_ONCE(dev->real_num_rx_queues > 1,
4340 "%s received packet on queue %u, but number "
4341 "of RX queues is %u\n",
4342 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004343 goto done;
4344 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004345 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004346 }
4347
Eric Dumazet567e4b72015-02-06 12:59:01 -08004348 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4349
4350 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4351 map = rcu_dereference(rxqueue->rps_map);
4352 if (!flow_table && !map)
4353 goto done;
4354
Changli Gao2d47b452010-08-17 19:00:56 +00004355 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004356 hash = skb_get_hash(skb);
4357 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004358 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004359
Tom Herbertfec5e652010-04-16 16:01:27 -07004360 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4361 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004362 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004363 u32 next_cpu;
4364 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004365
Eric Dumazet567e4b72015-02-06 12:59:01 -08004366 /* First check into global flow table if there is a match */
4367 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4368 if ((ident ^ hash) & ~rps_cpu_mask)
4369 goto try_rps;
4370
4371 next_cpu = ident & rps_cpu_mask;
4372
4373 /* OK, now we know there is a match,
4374 * we can look at the local (per receive queue) flow table
4375 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004376 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004377 tcpu = rflow->cpu;
4378
Tom Herbertfec5e652010-04-16 16:01:27 -07004379 /*
4380 * If the desired CPU (where last recvmsg was done) is
4381 * different from current CPU (one in the rx-queue flow
4382 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004383 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004384 * - Current CPU is offline.
4385 * - The current CPU's queue tail has advanced beyond the
4386 * last packet that was enqueued using this table entry.
4387 * This guarantees that all previous packets for the flow
4388 * have been dequeued, thus preserving in order delivery.
4389 */
4390 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004391 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004392 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004393 rflow->last_qtail)) >= 0)) {
4394 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004395 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004396 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004397
Eric Dumazeta31196b2015-04-25 09:35:24 -07004398 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004399 *rflowp = rflow;
4400 cpu = tcpu;
4401 goto done;
4402 }
4403 }
4404
Eric Dumazet567e4b72015-02-06 12:59:01 -08004405try_rps:
4406
Tom Herbert0a9627f2010-03-16 08:03:29 +00004407 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004408 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004409 if (cpu_online(tcpu)) {
4410 cpu = tcpu;
4411 goto done;
4412 }
4413 }
4414
4415done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004416 return cpu;
4417}
4418
Ben Hutchingsc4454772011-01-19 11:03:53 +00004419#ifdef CONFIG_RFS_ACCEL
4420
4421/**
4422 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4423 * @dev: Device on which the filter was set
4424 * @rxq_index: RX queue index
4425 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4426 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4427 *
4428 * Drivers that implement ndo_rx_flow_steer() should periodically call
4429 * this function for each installed filter and remove the filters for
4430 * which it returns %true.
4431 */
4432bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4433 u32 flow_id, u16 filter_id)
4434{
4435 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4436 struct rps_dev_flow_table *flow_table;
4437 struct rps_dev_flow *rflow;
4438 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004439 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004440
4441 rcu_read_lock();
4442 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4443 if (flow_table && flow_id <= flow_table->mask) {
4444 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004445 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004446 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004447 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4448 rflow->last_qtail) <
4449 (int)(10 * flow_table->mask)))
4450 expire = false;
4451 }
4452 rcu_read_unlock();
4453 return expire;
4454}
4455EXPORT_SYMBOL(rps_may_expire_flow);
4456
4457#endif /* CONFIG_RFS_ACCEL */
4458
Tom Herbert0a9627f2010-03-16 08:03:29 +00004459/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004460static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004461{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004462 struct softnet_data *sd = data;
4463
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004464 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004465 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004466}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004467
Tom Herbertfec5e652010-04-16 16:01:27 -07004468#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004469
4470/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004471 * Check if this softnet_data structure is another cpu one
4472 * If yes, queue it to our IPI list and return 1
4473 * If no, return 0
4474 */
4475static int rps_ipi_queued(struct softnet_data *sd)
4476{
4477#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004478 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004479
4480 if (sd != mysd) {
4481 sd->rps_ipi_next = mysd->rps_ipi_list;
4482 mysd->rps_ipi_list = sd;
4483
4484 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4485 return 1;
4486 }
4487#endif /* CONFIG_RPS */
4488 return 0;
4489}
4490
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004491#ifdef CONFIG_NET_FLOW_LIMIT
4492int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4493#endif
4494
4495static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4496{
4497#ifdef CONFIG_NET_FLOW_LIMIT
4498 struct sd_flow_limit *fl;
4499 struct softnet_data *sd;
4500 unsigned int old_flow, new_flow;
4501
4502 if (qlen < (netdev_max_backlog >> 1))
4503 return false;
4504
Christoph Lameter903ceff2014-08-17 12:30:35 -05004505 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004506
4507 rcu_read_lock();
4508 fl = rcu_dereference(sd->flow_limit);
4509 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004510 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004511 old_flow = fl->history[fl->history_head];
4512 fl->history[fl->history_head] = new_flow;
4513
4514 fl->history_head++;
4515 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4516
4517 if (likely(fl->buckets[old_flow]))
4518 fl->buckets[old_flow]--;
4519
4520 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4521 fl->count++;
4522 rcu_read_unlock();
4523 return true;
4524 }
4525 }
4526 rcu_read_unlock();
4527#endif
4528 return false;
4529}
4530
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004531/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004532 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4533 * queue (may be a remote CPU queue).
4534 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004535static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4536 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004537{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004538 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004539 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004540 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004541
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004542 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004543
4544 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004545
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004546 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004547 if (!netif_running(skb->dev))
4548 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004549 qlen = skb_queue_len(&sd->input_pkt_queue);
4550 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004551 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004552enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004553 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004554 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004555 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004556 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004557 return NET_RX_SUCCESS;
4558 }
4559
Eric Dumazetebda37c22010-05-06 23:51:21 +00004560 /* Schedule NAPI for backlog device
4561 * We can use non atomic operation since we own the queue lock
4562 */
4563 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004564 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004565 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004566 }
4567 goto enqueue;
4568 }
4569
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004570drop:
Changli Gaodee42872010-05-02 05:42:16 +00004571 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004572 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004573
Tom Herbert0a9627f2010-03-16 08:03:29 +00004574 local_irq_restore(flags);
4575
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004576 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004577 kfree_skb(skb);
4578 return NET_RX_DROP;
4579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004581static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4582{
4583 struct net_device *dev = skb->dev;
4584 struct netdev_rx_queue *rxqueue;
4585
4586 rxqueue = dev->_rx;
4587
4588 if (skb_rx_queue_recorded(skb)) {
4589 u16 index = skb_get_rx_queue(skb);
4590
4591 if (unlikely(index >= dev->real_num_rx_queues)) {
4592 WARN_ONCE(dev->real_num_rx_queues > 1,
4593 "%s received packet on queue %u, but number "
4594 "of RX queues is %u\n",
4595 dev->name, index, dev->real_num_rx_queues);
4596
4597 return rxqueue; /* Return first rxqueue */
4598 }
4599 rxqueue += index;
4600 }
4601 return rxqueue;
4602}
4603
John Fastabendd4455162017-07-17 09:26:45 -07004604static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004605 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004606 struct bpf_prog *xdp_prog)
4607{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004608 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004609 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004610 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004611 __be16 orig_eth_type;
4612 struct ethhdr *eth;
4613 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004614 int hlen, off;
4615 u32 mac_len;
4616
4617 /* Reinjected packets coming from act_mirred or similar should
4618 * not get XDP generic processing.
4619 */
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01004620 if (skb_is_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004621 return XDP_PASS;
4622
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004623 /* XDP packets must be linear and must have sufficient headroom
4624 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4625 * native XDP provides, thus we need to do it here as well.
4626 */
Toke Høiland-Jørgensenad1e03b2020-02-10 17:10:46 +01004627 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004628 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4629 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4630 int troom = skb->tail + skb->data_len - skb->end;
4631
4632 /* In case we have to go down the path and also linearize,
4633 * then lets do the pskb_expand_head() work just once here.
4634 */
4635 if (pskb_expand_head(skb,
4636 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4637 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4638 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004639 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004640 goto do_drop;
4641 }
John Fastabendd4455162017-07-17 09:26:45 -07004642
4643 /* The XDP program wants to see the packet starting at the MAC
4644 * header.
4645 */
4646 mac_len = skb->data - skb_mac_header(skb);
4647 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004648 xdp->data = skb->data - mac_len;
4649 xdp->data_meta = xdp->data;
4650 xdp->data_end = xdp->data + hlen;
4651 xdp->data_hard_start = skb->data - skb_headroom(skb);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004652
4653 /* SKB "head" area always have tailroom for skb_shared_info */
4654 xdp->frame_sz = (void *)skb_end_pointer(skb) - xdp->data_hard_start;
4655 xdp->frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4656
Björn Töpel02671e22018-05-02 13:01:30 +02004657 orig_data_end = xdp->data_end;
4658 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004659 eth = (struct ethhdr *)xdp->data;
4660 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4661 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004662
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004663 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004664 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004665
Björn Töpel02671e22018-05-02 13:01:30 +02004666 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004667
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004668 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004669 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004670 if (off) {
4671 if (off > 0)
4672 __skb_pull(skb, off);
4673 else if (off < 0)
4674 __skb_push(skb, -off);
4675
4676 skb->mac_header += off;
4677 skb_reset_network_header(skb);
4678 }
John Fastabendd4455162017-07-17 09:26:45 -07004679
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004680 /* check if bpf_xdp_adjust_tail was used */
4681 off = xdp->data_end - orig_data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004682 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004683 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Jesper Dangaard Brouera0757672020-05-14 12:49:28 +02004684 skb->len += off; /* positive on grow, negative on shrink */
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004685 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004686
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004687 /* check if XDP changed eth hdr such SKB needs update */
4688 eth = (struct ethhdr *)xdp->data;
4689 if ((orig_eth_type != eth->h_proto) ||
4690 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4691 __skb_push(skb, ETH_HLEN);
4692 skb->protocol = eth_type_trans(skb, skb->dev);
4693 }
4694
John Fastabendd4455162017-07-17 09:26:45 -07004695 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004696 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004697 case XDP_TX:
4698 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004699 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004700 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004701 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004702 if (metalen)
4703 skb_metadata_set(skb, metalen);
4704 break;
John Fastabendd4455162017-07-17 09:26:45 -07004705 default:
4706 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004707 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004708 case XDP_ABORTED:
4709 trace_xdp_exception(skb->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004710 fallthrough;
John Fastabendd4455162017-07-17 09:26:45 -07004711 case XDP_DROP:
4712 do_drop:
4713 kfree_skb(skb);
4714 break;
4715 }
4716
4717 return act;
4718}
4719
4720/* When doing generic XDP we have to bypass the qdisc layer and the
4721 * network taps in order to match in-driver-XDP behavior.
4722 */
Jason Wang7c497472017-08-11 19:41:17 +08004723void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004724{
4725 struct net_device *dev = skb->dev;
4726 struct netdev_queue *txq;
4727 bool free_skb = true;
4728 int cpu, rc;
4729
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004730 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004731 cpu = smp_processor_id();
4732 HARD_TX_LOCK(dev, txq, cpu);
4733 if (!netif_xmit_stopped(txq)) {
4734 rc = netdev_start_xmit(skb, dev, txq, 0);
4735 if (dev_xmit_complete(rc))
4736 free_skb = false;
4737 }
4738 HARD_TX_UNLOCK(dev, txq);
4739 if (free_skb) {
4740 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4741 kfree_skb(skb);
4742 }
4743}
4744
Davidlohr Bueso02786472018-05-08 09:07:02 -07004745static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004746
Jason Wang7c497472017-08-11 19:41:17 +08004747int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004748{
John Fastabendd4455162017-07-17 09:26:45 -07004749 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004750 struct xdp_buff xdp;
4751 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004752 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004753
Björn Töpel02671e22018-05-02 13:01:30 +02004754 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004755 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004756 switch (act) {
4757 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004758 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004759 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004760 if (err)
4761 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004762 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004763 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004764 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004765 break;
4766 }
John Fastabendd4455162017-07-17 09:26:45 -07004767 return XDP_DROP;
4768 }
4769 }
4770 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004771out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004772 kfree_skb(skb);
4773 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004774}
Jason Wang7c497472017-08-11 19:41:17 +08004775EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004776
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004777static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004779 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Eric Dumazet588f0332011-11-15 04:12:55 +00004781 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Koki Sanagicf66ba52010-08-23 18:45:02 +09004783 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004784
Eric Dumazetdf334542010-03-24 19:13:54 +00004785#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004786 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004787 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004788 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Changli Gaocece1942010-08-07 20:35:43 -07004790 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004791 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004792
4793 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004794 if (cpu < 0)
4795 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004796
4797 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4798
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004799 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004800 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004801 } else
4802#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004803 {
4804 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004805
Tom Herbertfec5e652010-04-16 16:01:27 -07004806 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4807 put_cpu();
4808 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004809 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004811
4812/**
4813 * netif_rx - post buffer to the network code
4814 * @skb: buffer to post
4815 *
4816 * This function receives a packet from a device driver and queues it for
4817 * the upper (protocol) levels to process. It always succeeds. The buffer
4818 * may be dropped during processing for congestion control or by the
4819 * protocol layers.
4820 *
4821 * return values:
4822 * NET_RX_SUCCESS (no congestion)
4823 * NET_RX_DROP (packet was dropped)
4824 *
4825 */
4826
4827int netif_rx(struct sk_buff *skb)
4828{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004829 int ret;
4830
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004831 trace_netif_rx_entry(skb);
4832
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004833 ret = netif_rx_internal(skb);
4834 trace_netif_rx_exit(ret);
4835
4836 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004837}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004838EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840int netif_rx_ni(struct sk_buff *skb)
4841{
4842 int err;
4843
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004844 trace_netif_rx_ni_entry(skb);
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004847 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 if (local_softirq_pending())
4849 do_softirq();
4850 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004851 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 return err;
4854}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855EXPORT_SYMBOL(netif_rx_ni);
4856
Sebastian Andrzej Siewiorc11171a2020-09-29 22:25:12 +02004857int netif_rx_any_context(struct sk_buff *skb)
4858{
4859 /*
4860 * If invoked from contexts which do not invoke bottom half
4861 * processing either at return from interrupt or when softrqs are
4862 * reenabled, use netif_rx_ni() which invokes bottomhalf processing
4863 * directly.
4864 */
4865 if (in_interrupt())
4866 return netif_rx(skb);
4867 else
4868 return netif_rx_ni(skb);
4869}
4870EXPORT_SYMBOL(netif_rx_any_context);
4871
Emese Revfy0766f782016-06-20 20:42:34 +02004872static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004874 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 if (sd->completion_queue) {
4877 struct sk_buff *clist;
4878
4879 local_irq_disable();
4880 clist = sd->completion_queue;
4881 sd->completion_queue = NULL;
4882 local_irq_enable();
4883
4884 while (clist) {
4885 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 clist = clist->next;
4888
Reshetova, Elena63354792017-06-30 13:07:58 +03004889 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004890 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4891 trace_consume_skb(skb);
4892 else
4893 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004894
4895 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4896 __kfree_skb(skb);
4897 else
4898 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004900
4901 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
4903
4904 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004905 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 local_irq_disable();
4908 head = sd->output_queue;
4909 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004910 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 local_irq_enable();
4912
4913 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004914 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004915 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 head = head->next_sched;
4918
John Fastabend6b3ba912017-12-07 09:54:25 -08004919 if (!(q->flags & TCQ_F_NOLOCK)) {
4920 root_lock = qdisc_lock(q);
4921 spin_lock(root_lock);
4922 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004923 /* We need to make sure head->next_sched is read
4924 * before clearing __QDISC_STATE_SCHED
4925 */
4926 smp_mb__before_atomic();
4927 clear_bit(__QDISC_STATE_SCHED, &q->state);
4928 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004929 if (root_lock)
4930 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 }
4932 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004933
4934 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004937#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004938/* This hook is defined here for ATM LANE */
4939int (*br_fdb_test_addr_hook)(struct net_device *dev,
4940 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004941EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004944static inline struct sk_buff *
4945sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
Daniel Borkmann9aa12062020-10-11 01:40:02 +02004946 struct net_device *orig_dev, bool *another)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004947{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004948#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004949 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004950 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004951
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004952 /* If there's at least one ingress present somewhere (so
4953 * we get here via enabled static key), remaining devices
4954 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004955 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004956 */
Jiri Pirko46209402017-11-03 11:46:25 +01004957 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004958 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004959
Herbert Xuf697c3e2007-10-14 00:38:47 -07004960 if (*pt_prev) {
4961 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4962 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004963 }
4964
Florian Westphal33654952015-05-14 00:36:28 +02004965 qdisc_skb_cb(skb)->pkt_len = skb->len;
wenxu496ef462020-11-25 12:01:21 +08004966 qdisc_skb_cb(skb)->mru = 0;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004967 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004968 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004969
Paul Blakey7d17c542020-02-16 12:01:22 +02004970 switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list,
4971 &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004972 case TC_ACT_OK:
4973 case TC_ACT_RECLASSIFY:
4974 skb->tc_index = TC_H_MIN(cl_res.classid);
4975 break;
4976 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004977 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004978 kfree_skb(skb);
4979 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004980 case TC_ACT_STOLEN:
4981 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004982 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004983 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004984 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004985 case TC_ACT_REDIRECT:
4986 /* skb_mac_header check was done by cls/act_bpf, so
4987 * we can safely push the L2 header back before
4988 * redirecting to another netdev
4989 */
4990 __skb_push(skb, skb->mac_len);
Daniel Borkmann9aa12062020-10-11 01:40:02 +02004991 if (skb_do_redirect(skb) == -EAGAIN) {
4992 __skb_pull(skb, skb->mac_len);
4993 *another = true;
4994 break;
4995 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004996 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01004997 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02004998 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004999 default:
5000 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07005001 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02005002#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07005003 return skb;
5004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005006/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07005007 * netdev_is_rx_handler_busy - check if receive handler is registered
5008 * @dev: device to check
5009 *
5010 * Check if a receive handler is already registered for a given device.
5011 * Return true if there one.
5012 *
5013 * The caller must hold the rtnl_mutex.
5014 */
5015bool netdev_is_rx_handler_busy(struct net_device *dev)
5016{
5017 ASSERT_RTNL();
5018 return dev && rtnl_dereference(dev->rx_handler);
5019}
5020EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5021
5022/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005023 * netdev_rx_handler_register - register receive handler
5024 * @dev: device to register a handler for
5025 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00005026 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005027 *
Masanari Iidae2278672014-02-18 22:54:36 +09005028 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005029 * called from __netif_receive_skb. A negative errno code is returned
5030 * on a failure.
5031 *
5032 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005033 *
5034 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005035 */
5036int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00005037 rx_handler_func_t *rx_handler,
5038 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005039{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08005040 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005041 return -EBUSY;
5042
Paolo Abenif54262502018-03-09 10:39:24 +01005043 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5044 return -EINVAL;
5045
Eric Dumazet00cfec32013-03-29 03:01:22 +00005046 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00005047 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005048 rcu_assign_pointer(dev->rx_handler, rx_handler);
5049
5050 return 0;
5051}
5052EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5053
5054/**
5055 * netdev_rx_handler_unregister - unregister receive handler
5056 * @dev: device to unregister a handler from
5057 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00005058 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005059 *
5060 * The caller must hold the rtnl_mutex.
5061 */
5062void netdev_rx_handler_unregister(struct net_device *dev)
5063{
5064
5065 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005066 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00005067 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5068 * section has a guarantee to see a non NULL rx_handler_data
5069 * as well.
5070 */
5071 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00005072 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005073}
5074EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5075
Mel Gormanb4b9e352012-07-31 16:44:26 -07005076/*
5077 * Limit the use of PFMEMALLOC reserves to those protocols that implement
5078 * the special handling of PFMEMALLOC skbs.
5079 */
5080static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5081{
5082 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07005083 case htons(ETH_P_ARP):
5084 case htons(ETH_P_IP):
5085 case htons(ETH_P_IPV6):
5086 case htons(ETH_P_8021Q):
5087 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07005088 return true;
5089 default:
5090 return false;
5091 }
5092}
5093
Pablo Neirae687ad62015-05-13 18:19:38 +02005094static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5095 int *ret, struct net_device *orig_dev)
5096{
5097 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04005098 int ingress_retval;
5099
Pablo Neirae687ad62015-05-13 18:19:38 +02005100 if (*pt_prev) {
5101 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5102 *pt_prev = NULL;
5103 }
5104
Aaron Conole2c1e2702016-09-21 11:35:03 -04005105 rcu_read_lock();
5106 ingress_retval = nf_hook_ingress(skb);
5107 rcu_read_unlock();
5108 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02005109 }
5110 return 0;
5111}
Pablo Neirae687ad62015-05-13 18:19:38 +02005112
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005113static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
Edward Cree88eb1942018-07-02 16:13:56 +01005114 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115{
5116 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005117 rx_handler_func_t *rx_handler;
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005118 struct sk_buff *skb = *pskb;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07005119 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005120 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08005122 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Eric Dumazet588f0332011-11-15 04:12:55 +00005124 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07005125
Koki Sanagicf66ba52010-08-23 18:45:02 +09005126 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08005127
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07005128 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00005129
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07005130 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00005131 if (!skb_transport_header_was_set(skb))
5132 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00005133 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
5135 pt_prev = NULL;
5136
David S. Miller63d8ea72011-02-28 10:48:59 -08005137another_round:
David S. Millerb6858172012-07-23 16:27:54 -07005138 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08005139
5140 __this_cpu_inc(softnet_data.processed);
5141
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005142 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5143 int ret2;
5144
5145 preempt_disable();
5146 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5147 preempt_enable();
5148
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005149 if (ret2 != XDP_PASS) {
5150 ret = NET_RX_DROP;
5151 goto out;
5152 }
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07005153 skb_reset_mac_len(skb);
5154 }
5155
Patrick McHardy8ad227f2013-04-19 02:04:31 +00005156 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5157 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005158 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005159 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005160 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00005161 }
5162
Willem de Bruijne7246e12017-01-07 17:06:35 -05005163 if (skb_skip_tc_classify(skb))
5164 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
David S. Miller9754e292013-02-14 15:57:38 -05005166 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005167 goto skip_taps;
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005170 if (pt_prev)
5171 ret = deliver_skb(skb, pt_prev, orig_dev);
5172 pt_prev = ptype;
5173 }
5174
5175 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5176 if (pt_prev)
5177 ret = deliver_skb(skb, pt_prev, orig_dev);
5178 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
5180
Mel Gormanb4b9e352012-07-31 16:44:26 -07005181skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005182#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005183 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann9aa12062020-10-11 01:40:02 +02005184 bool another = false;
5185
5186 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5187 &another);
5188 if (another)
5189 goto another_round;
Daniel Borkmann45771392015-04-10 23:07:54 +02005190 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005191 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005192
5193 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005194 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005195 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005196#endif
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01005197 skb_reset_redirect(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005198skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005199 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005200 goto drop;
5201
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005202 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005203 if (pt_prev) {
5204 ret = deliver_skb(skb, pt_prev, orig_dev);
5205 pt_prev = NULL;
5206 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005207 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005208 goto another_round;
5209 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005210 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005211 }
5212
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005213 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005214 if (rx_handler) {
5215 if (pt_prev) {
5216 ret = deliver_skb(skb, pt_prev, orig_dev);
5217 pt_prev = NULL;
5218 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005219 switch (rx_handler(&skb)) {
5220 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005221 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005222 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005223 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005224 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005225 case RX_HANDLER_EXACT:
5226 deliver_exact = true;
5227 case RX_HANDLER_PASS:
5228 break;
5229 default:
5230 BUG();
5231 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Vladimir Olteanb14a9fc2020-09-12 02:26:07 +03005234 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005235check_vlan_id:
5236 if (skb_vlan_tag_get_id(skb)) {
5237 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5238 * find vlan device.
5239 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005240 skb->pkt_type = PACKET_OTHERHOST;
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005241 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5242 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5243 /* Outer header is 802.1P with vlan 0, inner header is
5244 * 802.1Q or 802.1AD and vlan_do_receive() above could
5245 * not find vlan dev for vlan id 0.
5246 */
5247 __vlan_hwaccel_clear_tag(skb);
5248 skb = skb_vlan_untag(skb);
5249 if (unlikely(!skb))
5250 goto out;
5251 if (vlan_do_receive(&skb))
5252 /* After stripping off 802.1P header with vlan 0
5253 * vlan dev is found for inner header.
5254 */
5255 goto another_round;
5256 else if (unlikely(!skb))
5257 goto out;
5258 else
5259 /* We have stripped outer 802.1P vlan 0 header.
5260 * But could not find vlan dev.
5261 * check again for vlan id to set OTHERHOST.
5262 */
5263 goto check_vlan_id;
5264 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005265 /* Note: we might in the future use prio bits
5266 * and set skb->priority like in vlan_do_receive()
5267 * For the time being, just ignore Priority Code Point
5268 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005269 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005270 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005271
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005273
5274 /* deliver only exact match when indicated */
5275 if (likely(!deliver_exact)) {
5276 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5277 &ptype_base[ntohs(type) &
5278 PTYPE_HASH_MASK]);
5279 }
5280
5281 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5282 &orig_dev->ptype_specific);
5283
5284 if (unlikely(skb->dev != orig_dev)) {
5285 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5286 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 }
5288
5289 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005290 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005291 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005292 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005294drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005295 if (!deliver_exact)
5296 atomic_long_inc(&skb->dev->rx_dropped);
5297 else
5298 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 kfree_skb(skb);
5300 /* Jamal, now you will not able to escape explaining
5301 * me how you were going to use this. :-)
5302 */
5303 ret = NET_RX_DROP;
5304 }
5305
Julian Anastasov2c17d272015-07-09 09:59:10 +03005306out:
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005307 /* The invariant here is that if *ppt_prev is not NULL
5308 * then skb should also be non-NULL.
5309 *
5310 * Apparently *ppt_prev assignment above holds this invariant due to
5311 * skb dereferencing near it.
5312 */
5313 *pskb = skb;
David S. Miller9754e292013-02-14 15:57:38 -05005314 return ret;
5315}
5316
Edward Cree88eb1942018-07-02 16:13:56 +01005317static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5318{
5319 struct net_device *orig_dev = skb->dev;
5320 struct packet_type *pt_prev = NULL;
5321 int ret;
5322
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005323 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree88eb1942018-07-02 16:13:56 +01005324 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005325 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5326 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005327 return ret;
5328}
5329
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005330/**
5331 * netif_receive_skb_core - special purpose version of netif_receive_skb
5332 * @skb: buffer to process
5333 *
5334 * More direct receive version of netif_receive_skb(). It should
5335 * only be used by callers that have a need to skip RPS and Generic XDP.
Mauro Carvalho Chehab2de97802020-03-17 15:54:20 +01005336 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005337 *
5338 * This function may only be called from softirq context and interrupts
5339 * should be enabled.
5340 *
5341 * Return values (usually ignored):
5342 * NET_RX_SUCCESS: no congestion
5343 * NET_RX_DROP: packet was dropped
5344 */
5345int netif_receive_skb_core(struct sk_buff *skb)
5346{
5347 int ret;
5348
5349 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005350 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005351 rcu_read_unlock();
5352
5353 return ret;
5354}
5355EXPORT_SYMBOL(netif_receive_skb_core);
5356
Edward Cree88eb1942018-07-02 16:13:56 +01005357static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5358 struct packet_type *pt_prev,
5359 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005360{
5361 struct sk_buff *skb, *next;
5362
Edward Cree88eb1942018-07-02 16:13:56 +01005363 if (!pt_prev)
5364 return;
5365 if (list_empty(head))
5366 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005367 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005368 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5369 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005370 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005371 list_for_each_entry_safe(skb, next, head, list) {
5372 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005373 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005374 }
Edward Cree88eb1942018-07-02 16:13:56 +01005375}
5376
5377static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5378{
5379 /* Fast-path assumptions:
5380 * - There is no RX handler.
5381 * - Only one packet_type matches.
5382 * If either of these fails, we will end up doing some per-packet
5383 * processing in-line, then handling the 'last ptype' for the whole
5384 * sublist. This can't cause out-of-order delivery to any single ptype,
5385 * because the 'last ptype' must be constant across the sublist, and all
5386 * other ptypes are handled per-packet.
5387 */
5388 /* Current (common) ptype of sublist */
5389 struct packet_type *pt_curr = NULL;
5390 /* Current (common) orig_dev of sublist */
5391 struct net_device *od_curr = NULL;
5392 struct list_head sublist;
5393 struct sk_buff *skb, *next;
5394
Edward Cree9af86f932018-07-09 18:10:19 +01005395 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005396 list_for_each_entry_safe(skb, next, head, list) {
5397 struct net_device *orig_dev = skb->dev;
5398 struct packet_type *pt_prev = NULL;
5399
Edward Cree22f6bbb2018-12-04 17:37:57 +00005400 skb_list_del_init(skb);
Boris Sukholitkoc0bbbdc2020-05-19 10:32:37 +03005401 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
Edward Cree9af86f932018-07-09 18:10:19 +01005402 if (!pt_prev)
5403 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005404 if (pt_curr != pt_prev || od_curr != orig_dev) {
5405 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005406 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5407 /* start new sublist */
Edward Cree9af86f932018-07-09 18:10:19 +01005408 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005409 pt_curr = pt_prev;
5410 od_curr = orig_dev;
5411 }
Edward Cree9af86f932018-07-09 18:10:19 +01005412 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005413 }
5414
5415 /* dispatch final sublist */
Edward Cree9af86f932018-07-09 18:10:19 +01005416 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005417}
5418
David S. Miller9754e292013-02-14 15:57:38 -05005419static int __netif_receive_skb(struct sk_buff *skb)
5420{
5421 int ret;
5422
5423 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005424 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005425
5426 /*
5427 * PFMEMALLOC skbs are special, they should
5428 * - be delivered to SOCK_MEMALLOC sockets only
5429 * - stay away from userspace
5430 * - have bounded memory usage
5431 *
5432 * Use PF_MEMALLOC as this saves us from propagating the allocation
5433 * context down to all allocation sites.
5434 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005435 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005436 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005437 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005438 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005439 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 return ret;
5442}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005443
Edward Cree4ce00172018-07-02 16:13:40 +01005444static void __netif_receive_skb_list(struct list_head *head)
5445{
5446 unsigned long noreclaim_flag = 0;
5447 struct sk_buff *skb, *next;
5448 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5449
5450 list_for_each_entry_safe(skb, next, head, list) {
5451 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5452 struct list_head sublist;
5453
5454 /* Handle the previous sublist */
5455 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005456 if (!list_empty(&sublist))
5457 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005458 pfmemalloc = !pfmemalloc;
5459 /* See comments in __netif_receive_skb */
5460 if (pfmemalloc)
5461 noreclaim_flag = memalloc_noreclaim_save();
5462 else
5463 memalloc_noreclaim_restore(noreclaim_flag);
5464 }
5465 }
5466 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005467 if (!list_empty(head))
5468 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005469 /* Restore pflags */
5470 if (pfmemalloc)
5471 memalloc_noreclaim_restore(noreclaim_flag);
5472}
5473
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005474static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005475{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005476 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005477 struct bpf_prog *new = xdp->prog;
5478 int ret = 0;
5479
David Ahernfbee97f2020-05-29 16:07:13 -06005480 if (new) {
5481 u32 i;
5482
YiFei Zhu984fe942020-09-15 16:45:39 -07005483 mutex_lock(&new->aux->used_maps_mutex);
5484
David Ahernfbee97f2020-05-29 16:07:13 -06005485 /* generic XDP does not work with DEVMAPs that can
5486 * have a bpf_prog installed on an entry
5487 */
5488 for (i = 0; i < new->aux->used_map_cnt; i++) {
YiFei Zhu984fe942020-09-15 16:45:39 -07005489 if (dev_map_can_have_prog(new->aux->used_maps[i]) ||
5490 cpu_map_prog_allowed(new->aux->used_maps[i])) {
5491 mutex_unlock(&new->aux->used_maps_mutex);
David Ahernfbee97f2020-05-29 16:07:13 -06005492 return -EINVAL;
YiFei Zhu984fe942020-09-15 16:45:39 -07005493 }
David Ahernfbee97f2020-05-29 16:07:13 -06005494 }
YiFei Zhu984fe942020-09-15 16:45:39 -07005495
5496 mutex_unlock(&new->aux->used_maps_mutex);
David Ahernfbee97f2020-05-29 16:07:13 -06005497 }
5498
David S. Millerb5cdae32017-04-18 15:36:58 -04005499 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005500 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005501 rcu_assign_pointer(dev->xdp_prog, new);
5502 if (old)
5503 bpf_prog_put(old);
5504
5505 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005506 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005507 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005508 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005509 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005510 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005511 }
5512 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005513
David S. Millerb5cdae32017-04-18 15:36:58 -04005514 default:
5515 ret = -EINVAL;
5516 break;
5517 }
5518
5519 return ret;
5520}
5521
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005522static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005523{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005524 int ret;
5525
Eric Dumazet588f0332011-11-15 04:12:55 +00005526 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005527
Richard Cochranc1f19b52010-07-17 08:49:36 +00005528 if (skb_defer_rx_timestamp(skb))
5529 return NET_RX_SUCCESS;
5530
John Fastabendbbbe2112017-09-08 14:00:30 -07005531 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005532#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005533 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005534 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005535 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005536
Eric Dumazet3b098e22010-05-15 23:57:10 -07005537 if (cpu >= 0) {
5538 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5539 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005540 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005541 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005542 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005543#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005544 ret = __netif_receive_skb(skb);
5545 rcu_read_unlock();
5546 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005547}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005548
Edward Cree7da517a2018-07-02 16:13:24 +01005549static void netif_receive_skb_list_internal(struct list_head *head)
5550{
Edward Cree7da517a2018-07-02 16:13:24 +01005551 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005552 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005553
Edward Cree8c057ef2018-07-09 18:09:54 +01005554 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005555 list_for_each_entry_safe(skb, next, head, list) {
5556 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005557 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005558 if (!skb_defer_rx_timestamp(skb))
5559 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005560 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005561 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005562
Edward Cree7da517a2018-07-02 16:13:24 +01005563 rcu_read_lock();
5564#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005565 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005566 list_for_each_entry_safe(skb, next, head, list) {
5567 struct rps_dev_flow voidflow, *rflow = &voidflow;
5568 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5569
5570 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005571 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005572 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005573 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005574 }
5575 }
5576 }
5577#endif
5578 __netif_receive_skb_list(head);
5579 rcu_read_unlock();
5580}
5581
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005582/**
5583 * netif_receive_skb - process receive buffer from network
5584 * @skb: buffer to process
5585 *
5586 * netif_receive_skb() is the main receive data processing function.
5587 * It always succeeds. The buffer may be dropped during processing
5588 * for congestion control or by the protocol layers.
5589 *
5590 * This function may only be called from softirq context and interrupts
5591 * should be enabled.
5592 *
5593 * Return values (usually ignored):
5594 * NET_RX_SUCCESS: no congestion
5595 * NET_RX_DROP: packet was dropped
5596 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005597int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005598{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005599 int ret;
5600
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005601 trace_netif_receive_skb_entry(skb);
5602
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005603 ret = netif_receive_skb_internal(skb);
5604 trace_netif_receive_skb_exit(ret);
5605
5606 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005607}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005608EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Edward Creef6ad8c12018-07-02 16:12:45 +01005610/**
5611 * netif_receive_skb_list - process many receive buffers from network
5612 * @head: list of skbs to process.
5613 *
Edward Cree7da517a2018-07-02 16:13:24 +01005614 * Since return value of netif_receive_skb() is normally ignored, and
5615 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005616 *
5617 * This function may only be called from softirq context and interrupts
5618 * should be enabled.
5619 */
5620void netif_receive_skb_list(struct list_head *head)
5621{
Edward Cree7da517a2018-07-02 16:13:24 +01005622 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005623
Edward Creeb9f463d2018-07-02 16:14:44 +01005624 if (list_empty(head))
5625 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005626 if (trace_netif_receive_skb_list_entry_enabled()) {
5627 list_for_each_entry(skb, head, list)
5628 trace_netif_receive_skb_list_entry(skb);
5629 }
Edward Cree7da517a2018-07-02 16:13:24 +01005630 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005631 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005632}
5633EXPORT_SYMBOL(netif_receive_skb_list);
5634
Wei Yongjunce1e2a72020-07-13 22:23:44 +08005635static DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005636
5637/* Network device is going away, flush any packets still pending */
5638static void flush_backlog(struct work_struct *work)
5639{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005640 struct sk_buff *skb, *tmp;
5641 struct softnet_data *sd;
5642
5643 local_bh_disable();
5644 sd = this_cpu_ptr(&softnet_data);
5645
5646 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005647 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005648 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005649 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005650 __skb_unlink(skb, &sd->input_pkt_queue);
Subash Abhinov Kasiviswanathan7df5cb752020-07-23 11:31:48 -06005651 dev_kfree_skb_irq(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005652 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005653 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005654 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005655 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005656 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005657
5658 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005659 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005660 __skb_unlink(skb, &sd->process_queue);
5661 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005662 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005663 }
5664 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005665 local_bh_enable();
5666}
5667
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005668static bool flush_required(int cpu)
5669{
5670#if IS_ENABLED(CONFIG_RPS)
5671 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5672 bool do_flush;
5673
5674 local_irq_disable();
5675 rps_lock(sd);
5676
5677 /* as insertion into process_queue happens with the rps lock held,
5678 * process_queue access may race only with dequeue
5679 */
5680 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5681 !skb_queue_empty_lockless(&sd->process_queue);
5682 rps_unlock(sd);
5683 local_irq_enable();
5684
5685 return do_flush;
5686#endif
5687 /* without RPS we can't safely check input_pkt_queue: during a
5688 * concurrent remote skb_queue_splice() we can detect as empty both
5689 * input_pkt_queue and process_queue even if the latter could end-up
5690 * containing a lot of packets.
5691 */
5692 return true;
5693}
5694
Eric Dumazet41852492016-08-26 12:50:39 -07005695static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005696{
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005697 static cpumask_t flush_cpus;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005698 unsigned int cpu;
5699
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005700 /* since we are under rtnl lock protection we can use static data
5701 * for the cpumask and avoid allocating on stack the possibly
5702 * large mask
5703 */
5704 ASSERT_RTNL();
5705
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005706 get_online_cpus();
5707
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005708 cpumask_clear(&flush_cpus);
5709 for_each_online_cpu(cpu) {
5710 if (flush_required(cpu)) {
5711 queue_work_on(cpu, system_highpri_wq,
5712 per_cpu_ptr(&flush_works, cpu));
5713 cpumask_set_cpu(cpu, &flush_cpus);
5714 }
5715 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005716
Paolo Abeni2de79ee2020-09-10 23:33:18 +02005717 /* we can have in flight packet[s] on the cpus we are not flushing,
5718 * synchronize_net() in rollback_registered_many() will take care of
5719 * them
5720 */
5721 for_each_cpu(cpu, &flush_cpus)
Eric Dumazet41852492016-08-26 12:50:39 -07005722 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005723
5724 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005725}
5726
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005727/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5728static void gro_normal_list(struct napi_struct *napi)
5729{
5730 if (!napi->rx_count)
5731 return;
5732 netif_receive_skb_list_internal(&napi->rx_list);
5733 INIT_LIST_HEAD(&napi->rx_list);
5734 napi->rx_count = 0;
5735}
5736
5737/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5738 * pass the whole batch up to the stack.
5739 */
Eric Dumazet9e6ce472021-02-04 13:31:46 -08005740static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs)
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005741{
5742 list_add_tail(&skb->list, &napi->rx_list);
Eric Dumazet9e6ce472021-02-04 13:31:46 -08005743 napi->rx_count += segs;
5744 if (napi->rx_count >= gro_normal_batch)
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005745 gro_normal_list(napi);
5746}
5747
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005748INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5749INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005750static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005751{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005752 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005753 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005754 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005755 int err = -ENOENT;
5756
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005757 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5758
Herbert Xufc59f9a2009-04-14 15:11:06 -07005759 if (NAPI_GRO_CB(skb)->count == 1) {
5760 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005761 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005762 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005763
5764 rcu_read_lock();
5765 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005766 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005767 continue;
5768
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005769 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5770 ipv6_gro_complete, inet_gro_complete,
5771 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005772 break;
5773 }
5774 rcu_read_unlock();
5775
5776 if (err) {
5777 WARN_ON(&ptype->list == head);
5778 kfree_skb(skb);
5779 return NET_RX_SUCCESS;
5780 }
5781
5782out:
Eric Dumazet9e6ce472021-02-04 13:31:46 -08005783 gro_normal_one(napi, skb, NAPI_GRO_CB(skb)->count);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005784 return NET_RX_SUCCESS;
Herbert Xud565b0a2008-12-15 23:38:52 -08005785}
5786
Li RongQing6312fe72018-07-05 14:34:32 +08005787static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005788 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005789{
Li RongQing6312fe72018-07-05 14:34:32 +08005790 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005791 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005792
David Miller07d78362018-06-24 14:14:02 +09005793 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005794 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5795 return;
David S. Miller992cba72018-07-31 15:27:56 -07005796 skb_list_del_init(skb);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005797 napi_gro_complete(napi, skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005798 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005799 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005800
5801 if (!napi->gro_hash[index].count)
5802 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005803}
David Miller07d78362018-06-24 14:14:02 +09005804
Li RongQing6312fe72018-07-05 14:34:32 +08005805/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005806 * youngest packets at the head of it.
5807 * Complete skbs in reverse order to reduce latencies.
5808 */
5809void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5810{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005811 unsigned long bitmask = napi->gro_bitmask;
5812 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005813
Eric Dumazet42519ed2018-11-21 11:39:28 -08005814 while ((i = ffs(bitmask)) != 0) {
5815 bitmask >>= i;
5816 base += i;
5817 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005818 }
David Miller07d78362018-06-24 14:14:02 +09005819}
Eric Dumazet86cac582010-08-31 18:25:32 +00005820EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005821
David Miller07d78362018-06-24 14:14:02 +09005822static struct list_head *gro_list_prepare(struct napi_struct *napi,
5823 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005824{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005825 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005826 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005827 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005828 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005829
Li RongQing6312fe72018-07-05 14:34:32 +08005830 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005831 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005832 unsigned long diffs;
5833
Tom Herbert0b4cec82014-01-15 08:58:06 -08005834 NAPI_GRO_CB(p)->flush = 0;
5835
5836 if (hash != skb_get_hash_raw(p)) {
5837 NAPI_GRO_CB(p)->same_flow = 0;
5838 continue;
5839 }
5840
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005841 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005842 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5843 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005844 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Jesse Grossce87fc62016-01-20 17:59:49 -08005845 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005846 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005847 if (maclen == ETH_HLEN)
5848 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005849 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005850 else if (!diffs)
5851 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005852 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005853 maclen);
5854 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005855 }
David Miller07d78362018-06-24 14:14:02 +09005856
5857 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005858}
5859
Alexander Lobakinda54cc22021-04-19 12:53:06 +00005860static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff)
Jerry Chu299603e82013-12-11 20:53:45 -08005861{
5862 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5863 const skb_frag_t *frag0 = &pinfo->frags[0];
5864
5865 NAPI_GRO_CB(skb)->data_offset = 0;
5866 NAPI_GRO_CB(skb)->frag0 = NULL;
5867 NAPI_GRO_CB(skb)->frag0_len = 0;
5868
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005869 if (!skb_headlen(skb) && pinfo->nr_frags &&
Eric Dumazet91431582021-04-13 05:41:35 -07005870 !PageHighMem(skb_frag_page(frag0)) &&
Alexander Lobakinda54cc22021-04-19 12:53:06 +00005871 (!NET_IP_ALIGN || !((skb_frag_off(frag0) + nhoff) & 3))) {
Jerry Chu299603e82013-12-11 20:53:45 -08005872 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005873 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5874 skb_frag_size(frag0),
5875 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005876 }
5877}
5878
Eric Dumazeta50e2332014-03-29 21:28:21 -07005879static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5880{
5881 struct skb_shared_info *pinfo = skb_shinfo(skb);
5882
5883 BUG_ON(skb->end - skb->tail < grow);
5884
5885 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5886
5887 skb->data_len -= grow;
5888 skb->tail += grow;
5889
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07005890 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005891 skb_frag_size_sub(&pinfo->frags[0], grow);
5892
5893 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5894 skb_frag_unref(skb, 0);
5895 memmove(pinfo->frags, pinfo->frags + 1,
5896 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5897 }
5898}
5899
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005900static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005901{
Li RongQing6312fe72018-07-05 14:34:32 +08005902 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005903
Li RongQing6312fe72018-07-05 14:34:32 +08005904 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005905
Li RongQing6312fe72018-07-05 14:34:32 +08005906 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005907 * impossible.
5908 */
5909 if (WARN_ON_ONCE(!oldest))
5910 return;
5911
Li RongQingd9f37d02018-07-13 14:41:36 +08005912 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5913 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005914 */
David S. Millerece23712018-10-28 10:35:12 -07005915 skb_list_del_init(oldest);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005916 napi_gro_complete(napi, oldest);
David Miller07d78362018-06-24 14:14:02 +09005917}
5918
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005919INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5920 struct sk_buff *));
5921INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5922 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005923static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005924{
Li RongQing6312fe72018-07-05 14:34:32 +08005925 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005926 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005927 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005928 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005929 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005930 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005931 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005932 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005933 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005934
David S. Millerb5cdae32017-04-18 15:36:58 -04005935 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005936 goto normal;
5937
David Miller07d78362018-06-24 14:14:02 +09005938 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005939
Herbert Xud565b0a2008-12-15 23:38:52 -08005940 rcu_read_lock();
5941 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005942 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005943 continue;
5944
Herbert Xu86911732009-01-29 14:19:50 +00005945 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005946 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005947 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005948 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005949 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005950 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005951 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005952 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005953 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005954 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005955
Tom Herbert662880f2014-08-27 21:26:56 -07005956 /* Setup for GRO checksum validation */
5957 switch (skb->ip_summed) {
5958 case CHECKSUM_COMPLETE:
5959 NAPI_GRO_CB(skb)->csum = skb->csum;
5960 NAPI_GRO_CB(skb)->csum_valid = 1;
5961 NAPI_GRO_CB(skb)->csum_cnt = 0;
5962 break;
5963 case CHECKSUM_UNNECESSARY:
5964 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5965 NAPI_GRO_CB(skb)->csum_valid = 0;
5966 break;
5967 default:
5968 NAPI_GRO_CB(skb)->csum_cnt = 0;
5969 NAPI_GRO_CB(skb)->csum_valid = 0;
5970 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005971
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005972 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5973 ipv6_gro_receive, inet_gro_receive,
5974 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005975 break;
5976 }
5977 rcu_read_unlock();
5978
5979 if (&ptype->list == head)
5980 goto normal;
5981
Masahiro Yamada45586c72020-02-03 17:37:45 -08005982 if (PTR_ERR(pp) == -EINPROGRESS) {
Steffen Klassert25393d32017-02-15 09:39:44 +01005983 ret = GRO_CONSUMED;
5984 goto ok;
5985 }
5986
Herbert Xu0da2afd52008-12-26 14:57:42 -08005987 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005988 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005989
Herbert Xud565b0a2008-12-15 23:38:52 -08005990 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005991 skb_list_del_init(pp);
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00005992 napi_gro_complete(napi, pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005993 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005994 }
5995
Herbert Xu0da2afd52008-12-26 14:57:42 -08005996 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005997 goto ok;
5998
Eric Dumazet600adc12014-01-09 14:12:19 -08005999 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08006000 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08006001
Li RongQing6312fe72018-07-05 14:34:32 +08006002 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006003 gro_flush_oldest(napi, gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08006004 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08006005 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08006006 }
Herbert Xud565b0a2008-12-15 23:38:52 -08006007 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00006008 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07006009 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00006010 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09006011 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006012 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08006013
Herbert Xuad0f9902009-02-01 01:24:55 -08006014pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006015 grow = skb_gro_offset(skb) - skb_headlen(skb);
6016 if (grow > 0)
6017 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08006018ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08006019 if (napi->gro_hash[hash].count) {
6020 if (!test_bit(hash, &napi->gro_bitmask))
6021 __set_bit(hash, &napi->gro_bitmask);
6022 } else if (test_bit(hash, &napi->gro_bitmask)) {
6023 __clear_bit(hash, &napi->gro_bitmask);
6024 }
6025
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006026 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006027
6028normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08006029 ret = GRO_NORMAL;
6030 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08006031}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006032
Jerry Chubf5a7552014-01-07 10:23:19 -08006033struct packet_offload *gro_find_receive_by_type(__be16 type)
6034{
6035 struct list_head *offload_head = &offload_base;
6036 struct packet_offload *ptype;
6037
6038 list_for_each_entry_rcu(ptype, offload_head, list) {
6039 if (ptype->type != type || !ptype->callbacks.gro_receive)
6040 continue;
6041 return ptype;
6042 }
6043 return NULL;
6044}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006045EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08006046
6047struct packet_offload *gro_find_complete_by_type(__be16 type)
6048{
6049 struct list_head *offload_head = &offload_base;
6050 struct packet_offload *ptype;
6051
6052 list_for_each_entry_rcu(ptype, offload_head, list) {
6053 if (ptype->type != type || !ptype->callbacks.gro_complete)
6054 continue;
6055 return ptype;
6056 }
6057 return NULL;
6058}
Or Gerlitze27a2f82014-01-20 13:59:20 +02006059EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006060
Michal Kubečeke44699d2017-06-29 11:13:36 +02006061static void napi_skb_free_stolen_head(struct sk_buff *skb)
6062{
6063 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02006064 skb_ext_put(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02006065 kmem_cache_free(skbuff_head_cache, skb);
6066}
6067
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006068static gro_result_t napi_skb_finish(struct napi_struct *napi,
6069 struct sk_buff *skb,
6070 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08006071{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006072 switch (ret) {
6073 case GRO_NORMAL:
Eric Dumazet9e6ce472021-02-04 13:31:46 -08006074 gro_normal_one(napi, skb, 1);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006075 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006076
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006077 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08006078 kfree_skb(skb);
6079 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006080
Eric Dumazetdaa86542012-04-19 07:07:40 +00006081 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02006082 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6083 napi_skb_free_stolen_head(skb);
6084 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00006085 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00006086 break;
6087
Ben Hutchings5b252f02009-10-29 07:17:09 +00006088 case GRO_HELD:
6089 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006090 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006091 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08006092 }
6093
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006094 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006095}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006096
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006097gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006098{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006099 gro_result_t ret;
6100
Eric Dumazet93f93a42015-11-18 06:30:59 -08006101 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006102 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00006103
Alexander Lobakinda54cc22021-04-19 12:53:06 +00006104 skb_gro_reset_offset(skb, 0);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006105
Alexander Lobakin6570bc72019-10-14 11:00:33 +03006106 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006107 trace_napi_gro_receive_exit(ret);
6108
6109 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006110}
6111EXPORT_SYMBOL(napi_gro_receive);
6112
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00006113static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006114{
Eric Dumazet93a35f52014-10-23 06:30:30 -07006115 if (unlikely(skb->pfmemalloc)) {
6116 consume_skb(skb);
6117 return;
6118 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006119 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00006120 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
6121 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01006122 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08006123 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08006124 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08006125
6126 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
6127 skb->pkt_type = PACKET_HOST;
6128
Jerry Chuc3caf112014-07-14 15:54:46 -07006129 skb->encapsulation = 0;
6130 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07006131 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Florian Westphal174e2382019-09-26 20:37:05 +02006132 skb_ext_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006133
6134 napi->skb = skb;
6135}
Herbert Xu96e93ea2009-01-06 10:49:34 -08006136
Herbert Xu76620aa2009-04-16 02:02:07 -07006137struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08006138{
Herbert Xu5d38a072009-01-04 16:13:40 -08006139 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08006140
6141 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08006142 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08006143 if (skb) {
6144 napi->skb = skb;
6145 skb_mark_napi_id(skb, napi);
6146 }
Herbert Xu5d38a072009-01-04 16:13:40 -08006147 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08006148 return skb;
6149}
Herbert Xu76620aa2009-04-16 02:02:07 -07006150EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006151
Eric Dumazeta50e2332014-03-29 21:28:21 -07006152static gro_result_t napi_frags_finish(struct napi_struct *napi,
6153 struct sk_buff *skb,
6154 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006155{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006156 switch (ret) {
6157 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07006158 case GRO_HELD:
6159 __skb_push(skb, ETH_HLEN);
6160 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01006161 if (ret == GRO_NORMAL)
Eric Dumazet9e6ce472021-02-04 13:31:46 -08006162 gro_normal_one(napi, skb, 1);
Herbert Xu86911732009-01-29 14:19:50 +00006163 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006164
6165 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006166 napi_reuse_skb(napi, skb);
6167 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00006168
Michal Kubečeke44699d2017-06-29 11:13:36 +02006169 case GRO_MERGED_FREE:
6170 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6171 napi_skb_free_stolen_head(skb);
6172 else
6173 napi_reuse_skb(napi, skb);
6174 break;
6175
Ben Hutchings5b252f02009-10-29 07:17:09 +00006176 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01006177 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00006178 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006179 }
6180
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006181 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006182}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00006183
Eric Dumazeta50e2332014-03-29 21:28:21 -07006184/* Upper GRO stack assumes network header starts at gro_offset=0
6185 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6186 * We copy ethernet header into skb->data to have a common layout.
6187 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00006188static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08006189{
Herbert Xu76620aa2009-04-16 02:02:07 -07006190 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006191 const struct ethhdr *eth;
6192 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07006193
6194 napi->skb = NULL;
6195
Eric Dumazeta50e2332014-03-29 21:28:21 -07006196 skb_reset_mac_header(skb);
Alexander Lobakinda54cc22021-04-19 12:53:06 +00006197 skb_gro_reset_offset(skb, hlen);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006198
Eric Dumazeta50e2332014-03-29 21:28:21 -07006199 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6200 eth = skb_gro_header_slow(skb, hlen, 0);
6201 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04006202 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6203 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07006204 napi_reuse_skb(napi, skb);
6205 return NULL;
6206 }
6207 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07006208 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07006209 gro_pull_from_frag0(skb, hlen);
6210 NAPI_GRO_CB(skb)->frag0 += hlen;
6211 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07006212 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07006213 __skb_pull(skb, hlen);
6214
6215 /*
6216 * This works because the only protocols we care about don't require
6217 * special handling.
6218 * We'll fix it up properly in napi_frags_finish()
6219 */
6220 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07006221
Herbert Xu76620aa2009-04-16 02:02:07 -07006222 return skb;
6223}
Herbert Xu76620aa2009-04-16 02:02:07 -07006224
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006225gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07006226{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006227 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07006228 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08006229
6230 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07006231 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08006232
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00006233 trace_napi_gro_frags_entry(skb);
6234
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05006235 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6236 trace_napi_gro_frags_exit(ret);
6237
6238 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006239}
6240EXPORT_SYMBOL(napi_gro_frags);
6241
Tom Herbert573e8fc2014-08-22 13:33:47 -07006242/* Compute the checksum from gro_offset and return the folded value
6243 * after adding in any pseudo checksum.
6244 */
6245__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6246{
6247 __wsum wsum;
6248 __sum16 sum;
6249
6250 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6251
6252 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6253 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006254 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006255 if (likely(!sum)) {
6256 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6257 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006258 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006259 }
6260
6261 NAPI_GRO_CB(skb)->csum = wsum;
6262 NAPI_GRO_CB(skb)->csum_valid = 1;
6263
6264 return sum;
6265}
6266EXPORT_SYMBOL(__skb_gro_checksum_complete);
6267
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306268static void net_rps_send_ipi(struct softnet_data *remsd)
6269{
6270#ifdef CONFIG_RPS
6271 while (remsd) {
6272 struct softnet_data *next = remsd->rps_ipi_next;
6273
6274 if (cpu_online(remsd->cpu))
6275 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6276 remsd = next;
6277 }
6278#endif
6279}
6280
Eric Dumazete326bed2010-04-22 00:22:45 -07006281/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006282 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006283 * Note: called with local irq disabled, but exits with local irq enabled.
6284 */
6285static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6286{
6287#ifdef CONFIG_RPS
6288 struct softnet_data *remsd = sd->rps_ipi_list;
6289
6290 if (remsd) {
6291 sd->rps_ipi_list = NULL;
6292
6293 local_irq_enable();
6294
6295 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306296 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006297 } else
6298#endif
6299 local_irq_enable();
6300}
6301
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006302static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6303{
6304#ifdef CONFIG_RPS
6305 return sd->rps_ipi_list != NULL;
6306#else
6307 return false;
6308#endif
6309}
6310
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006311static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006313 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006314 bool again = true;
6315 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
Eric Dumazete326bed2010-04-22 00:22:45 -07006317 /* Check if we have pending ipi, its better to send them now,
6318 * not waiting net_rx_action() end.
6319 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006320 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006321 local_irq_disable();
6322 net_rps_action_and_irq_enable(sd);
6323 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006324
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006325 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006326 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
Changli Gao6e7676c2010-04-27 15:07:33 -07006329 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006330 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006331 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006332 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006333 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006334 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006335 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006336
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006339 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006340 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006341 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006342 /*
6343 * Inline a custom version of __napi_complete().
6344 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006345 * and NAPI_STATE_SCHED is the only possible flag set
6346 * on backlog.
6347 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006348 * and we dont need an smp_mb() memory barrier.
6349 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006350 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006351 again = false;
6352 } else {
6353 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6354 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006355 }
6356 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006357 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006360 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361}
6362
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006363/**
6364 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006365 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006366 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006367 * The entry's receive function will be scheduled to run.
6368 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006369 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006370void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006371{
6372 unsigned long flags;
6373
6374 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006375 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006376 local_irq_restore(flags);
6377}
6378EXPORT_SYMBOL(__napi_schedule);
6379
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006380/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006381 * napi_schedule_prep - check if napi can be scheduled
6382 * @n: napi context
6383 *
6384 * Test if NAPI routine is already running, and if not mark
Jonathan Neuschäferee1a4c82020-09-05 20:32:18 +02006385 * it as running. This is used as a condition variable to
Eric Dumazet39e6c822017-02-28 10:34:50 -08006386 * insure only one NAPI poll instance runs. We also make
6387 * sure there is no pending NAPI disable.
6388 */
6389bool napi_schedule_prep(struct napi_struct *n)
6390{
6391 unsigned long val, new;
6392
6393 do {
6394 val = READ_ONCE(n->state);
6395 if (unlikely(val & NAPIF_STATE_DISABLE))
6396 return false;
6397 new = val | NAPIF_STATE_SCHED;
6398
6399 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6400 * This was suggested by Alexander Duyck, as compiler
6401 * emits better code than :
6402 * if (val & NAPIF_STATE_SCHED)
6403 * new |= NAPIF_STATE_MISSED;
6404 */
6405 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6406 NAPIF_STATE_MISSED;
6407 } while (cmpxchg(&n->state, val, new) != val);
6408
6409 return !(val & NAPIF_STATE_SCHED);
6410}
6411EXPORT_SYMBOL(napi_schedule_prep);
6412
6413/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006414 * __napi_schedule_irqoff - schedule for receive
6415 * @n: entry to schedule
6416 *
6417 * Variant of __napi_schedule() assuming hard irqs are masked
6418 */
6419void __napi_schedule_irqoff(struct napi_struct *n)
6420{
6421 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6422}
6423EXPORT_SYMBOL(__napi_schedule_irqoff);
6424
Eric Dumazet364b6052016-11-15 10:15:13 -08006425bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006426{
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006427 unsigned long flags, val, new, timeout = 0;
6428 bool ret = true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006429
6430 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006431 * 1) Don't let napi dequeue from the cpu poll list
6432 * just in case its running on a different cpu.
6433 * 2) If we are busy polling, do nothing here, we have
6434 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006435 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006436 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6437 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006438 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006439
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006440 if (work_done) {
6441 if (n->gro_bitmask)
Eric Dumazet7e417a62020-04-22 09:13:28 -07006442 timeout = READ_ONCE(n->dev->gro_flush_timeout);
6443 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006444 }
6445 if (n->defer_hard_irqs_count > 0) {
6446 n->defer_hard_irqs_count--;
Eric Dumazet7e417a62020-04-22 09:13:28 -07006447 timeout = READ_ONCE(n->dev->gro_flush_timeout);
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006448 if (timeout)
6449 ret = false;
6450 }
6451 if (n->gro_bitmask) {
Paolo Abeni605108a2018-11-21 18:21:35 +01006452 /* When the NAPI instance uses a timeout and keeps postponing
6453 * it, we need to bound somehow the time packets are kept in
6454 * the GRO layer
6455 */
6456 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006457 }
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006458
6459 gro_normal_list(n);
6460
Eric Dumazet02c16022017-02-04 15:25:02 -08006461 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006462 /* If n->poll_list is not empty, we need to mask irqs */
6463 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006464 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006465 local_irq_restore(flags);
6466 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006467
6468 do {
6469 val = READ_ONCE(n->state);
6470
6471 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6472
6473 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6474
6475 /* If STATE_MISSED was set, leave STATE_SCHED set,
6476 * because we will call napi->poll() one more time.
6477 * This C code was suggested by Alexander Duyck to help gcc.
6478 */
6479 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6480 NAPIF_STATE_SCHED;
6481 } while (cmpxchg(&n->state, val, new) != val);
6482
6483 if (unlikely(val & NAPIF_STATE_MISSED)) {
6484 __napi_schedule(n);
6485 return false;
6486 }
6487
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006488 if (timeout)
6489 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6490 HRTIMER_MODE_REL_PINNED);
6491 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08006492}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006493EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006494
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006495/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006496static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006497{
6498 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6499 struct napi_struct *napi;
6500
6501 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6502 if (napi->napi_id == napi_id)
6503 return napi;
6504
6505 return NULL;
6506}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006507
6508#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006509
Eric Dumazetce6aea92015-11-18 06:30:54 -08006510#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006511
6512static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6513{
6514 int rc;
6515
Eric Dumazet39e6c822017-02-28 10:34:50 -08006516 /* Busy polling means there is a high chance device driver hard irq
6517 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6518 * set in napi_schedule_prep().
6519 * Since we are about to call napi->poll() once more, we can safely
6520 * clear NAPI_STATE_MISSED.
6521 *
6522 * Note: x86 could use a single "lock and ..." instruction
6523 * to perform these two clear_bit()
6524 */
6525 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006526 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6527
6528 local_bh_disable();
6529
6530 /* All we really want here is to re-enable device interrupts.
6531 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6532 */
6533 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006534 /* We can't gro_normal_list() here, because napi->poll() might have
6535 * rearmed the napi (napi_complete_done()) in which case it could
6536 * already be running on another CPU.
6537 */
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006538 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006539 netpoll_poll_unlock(have_poll_lock);
Edward Cree323ebb62019-08-06 14:53:55 +01006540 if (rc == BUSY_POLL_BUDGET) {
6541 /* As the whole budget was spent, we still own the napi so can
6542 * safely handle the rx_list.
6543 */
6544 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006545 __napi_schedule(napi);
Edward Cree323ebb62019-08-06 14:53:55 +01006546 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006547 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006548}
6549
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006550void napi_busy_loop(unsigned int napi_id,
6551 bool (*loop_end)(void *, unsigned long),
6552 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006553{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006554 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006555 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006556 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006557 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006558
6559restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006560 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006561
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006562 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006563
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006564 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006565 if (!napi)
6566 goto out;
6567
Eric Dumazet217f6972016-11-15 10:15:11 -08006568 preempt_disable();
6569 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006570 int work = 0;
6571
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006572 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006573 if (!napi_poll) {
6574 unsigned long val = READ_ONCE(napi->state);
6575
6576 /* If multiple threads are competing for this napi,
6577 * we avoid dirtying napi->state as much as we can.
6578 */
6579 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6580 NAPIF_STATE_IN_BUSY_POLL))
6581 goto count;
6582 if (cmpxchg(&napi->state, val,
6583 val | NAPIF_STATE_IN_BUSY_POLL |
6584 NAPIF_STATE_SCHED) != val)
6585 goto count;
6586 have_poll_lock = netpoll_poll_lock(napi);
6587 napi_poll = napi->poll;
6588 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006589 work = napi_poll(napi, BUSY_POLL_BUDGET);
6590 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006591 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006592count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006593 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006594 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006595 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006596 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006597
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006598 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006599 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006600
Eric Dumazet217f6972016-11-15 10:15:11 -08006601 if (unlikely(need_resched())) {
6602 if (napi_poll)
6603 busy_poll_stop(napi, have_poll_lock);
6604 preempt_enable();
6605 rcu_read_unlock();
6606 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006607 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006608 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006609 goto restart;
6610 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006611 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006612 }
6613 if (napi_poll)
6614 busy_poll_stop(napi, have_poll_lock);
6615 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006616out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006617 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006618}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006619EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006620
6621#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006622
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006623static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006624{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006625 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006626 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006627
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006628 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006629
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006630 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006631 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006632 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6633 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006634 } while (napi_by_id(napi_gen_id));
6635 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006636
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006637 hlist_add_head_rcu(&napi->napi_hash_node,
6638 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006639
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006640 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006641}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006642
6643/* Warning : caller is responsible to make sure rcu grace period
6644 * is respected before freeing memory containing @napi
6645 */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006646static void napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006647{
6648 spin_lock(&napi_hash_lock);
6649
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006650 hlist_del_init_rcu(&napi->napi_hash_node);
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006651
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006652 spin_unlock(&napi_hash_lock);
6653}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006654
Eric Dumazet3b47d302014-11-06 21:09:44 -08006655static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6656{
6657 struct napi_struct *napi;
6658
6659 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006660
6661 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6662 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6663 */
Eric Dumazet6f8b12d2020-04-22 09:13:27 -07006664 if (!napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006665 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6666 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006667
6668 return HRTIMER_NORESTART;
6669}
6670
David S. Miller7c4ec742018-07-20 23:37:55 -07006671static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006672{
David Miller07d78362018-06-24 14:14:02 +09006673 int i;
6674
Li RongQing6312fe72018-07-05 14:34:32 +08006675 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6676 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6677 napi->gro_hash[i].count = 0;
6678 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006679 napi->gro_bitmask = 0;
6680}
6681
6682void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6683 int (*poll)(struct napi_struct *, int), int weight)
6684{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006685 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6686 return;
6687
David S. Miller7c4ec742018-07-20 23:37:55 -07006688 INIT_LIST_HEAD(&napi->poll_list);
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006689 INIT_HLIST_NODE(&napi->napi_hash_node);
David S. Miller7c4ec742018-07-20 23:37:55 -07006690 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6691 napi->timer.function = napi_watchdog;
6692 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006693 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006694 INIT_LIST_HEAD(&napi->rx_list);
6695 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006696 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006697 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006698 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6699 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006700 napi->weight = weight;
Herbert Xud565b0a2008-12-15 23:38:52 -08006701 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006702#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006703 napi->poll_owner = -1;
6704#endif
6705 set_bit(NAPI_STATE_SCHED, &napi->state);
Jakub Kicinski96e97bc2020-08-26 12:40:06 -07006706 set_bit(NAPI_STATE_NPSVC, &napi->state);
6707 list_add_rcu(&napi->dev_list, &dev->napi_list);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006708 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006709}
6710EXPORT_SYMBOL(netif_napi_add);
6711
Eric Dumazet3b47d302014-11-06 21:09:44 -08006712void napi_disable(struct napi_struct *n)
6713{
6714 might_sleep();
6715 set_bit(NAPI_STATE_DISABLE, &n->state);
6716
6717 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6718 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006719 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6720 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006721
6722 hrtimer_cancel(&n->timer);
6723
6724 clear_bit(NAPI_STATE_DISABLE, &n->state);
6725}
6726EXPORT_SYMBOL(napi_disable);
6727
David Miller07d78362018-06-24 14:14:02 +09006728static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006729{
David Miller07d78362018-06-24 14:14:02 +09006730 int i;
David Millerd4546c22018-06-24 14:13:49 +09006731
David Miller07d78362018-06-24 14:14:02 +09006732 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6733 struct sk_buff *skb, *n;
6734
Li RongQing6312fe72018-07-05 14:34:32 +08006735 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006736 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006737 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006738 }
David Millerd4546c22018-06-24 14:13:49 +09006739}
6740
Eric Dumazet93d05d42015-11-18 06:31:03 -08006741/* Must be called in process context */
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006742void __netif_napi_del(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006743{
Jakub Kicinski4d092dd2020-09-09 10:37:52 -07006744 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6745 return;
6746
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006747 napi_hash_del(napi);
Jakub Kicinski5251ef82020-09-09 10:37:53 -07006748 list_del_rcu(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006749 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006750
David Miller07d78362018-06-24 14:14:02 +09006751 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006752 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006753}
Jakub Kicinski5198d5452020-09-09 10:37:51 -07006754EXPORT_SYMBOL(__netif_napi_del);
Herbert Xud565b0a2008-12-15 23:38:52 -08006755
Herbert Xu726ce702014-12-21 07:16:21 +11006756static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6757{
6758 void *have;
6759 int work, weight;
6760
6761 list_del_init(&n->poll_list);
6762
6763 have = netpoll_poll_lock(n);
6764
6765 weight = n->weight;
6766
6767 /* This NAPI_STATE_SCHED test is for avoiding a race
6768 * with netpoll's poll_napi(). Only the entity which
6769 * obtains the lock and sees NAPI_STATE_SCHED set will
6770 * actually make the ->poll() call. Therefore we avoid
6771 * accidentally calling ->poll() when NAPI is not scheduled.
6772 */
6773 work = 0;
6774 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6775 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006776 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006777 }
6778
Eric Dumazet427d5832020-06-17 09:40:51 -07006779 if (unlikely(work > weight))
6780 pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6781 n->poll, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006782
6783 if (likely(work < weight))
6784 goto out_unlock;
6785
6786 /* Drivers must not modify the NAPI state if they
6787 * consume the entire weight. In such cases this code
6788 * still "owns" the NAPI instance and therefore can
6789 * move the instance around on the list at-will.
6790 */
6791 if (unlikely(napi_disable_pending(n))) {
6792 napi_complete(n);
6793 goto out_unlock;
6794 }
6795
Li RongQingd9f37d02018-07-13 14:41:36 +08006796 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006797 /* flush too old packets
6798 * If HZ < 1000, flush all packets.
6799 */
6800 napi_gro_flush(n, HZ >= 1000);
6801 }
6802
Maxim Mikityanskiyc8079432020-01-21 15:09:40 +00006803 gro_normal_list(n);
6804
Herbert Xu001ce542014-12-21 07:16:22 +11006805 /* Some drivers may have called napi_schedule
6806 * prior to exhausting their budget.
6807 */
6808 if (unlikely(!list_empty(&n->poll_list))) {
6809 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6810 n->dev ? n->dev->name : "backlog");
6811 goto out_unlock;
6812 }
6813
Herbert Xu726ce702014-12-21 07:16:21 +11006814 list_add_tail(&n->poll_list, repoll);
6815
6816out_unlock:
6817 netpoll_poll_unlock(have);
6818
6819 return work;
6820}
6821
Emese Revfy0766f782016-06-20 20:42:34 +02006822static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006824 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006825 unsigned long time_limit = jiffies +
6826 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006827 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006828 LIST_HEAD(list);
6829 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006830
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006832 list_splice_init(&sd->poll_list, &list);
6833 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006835 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006836 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006838 if (list_empty(&list)) {
6839 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006840 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006841 break;
6842 }
6843
Herbert Xu6bd373e2014-12-21 07:16:24 +11006844 n = list_first_entry(&list, struct napi_struct, poll_list);
6845 budget -= napi_poll(n, &repoll);
6846
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006847 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006848 * Allow this to run for 2 jiffies since which will allow
6849 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006850 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006851 if (unlikely(budget <= 0 ||
6852 time_after_eq(jiffies, time_limit))) {
6853 sd->time_squeeze++;
6854 break;
6855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006857
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006858 local_irq_disable();
6859
6860 list_splice_tail_init(&sd->poll_list, &list);
6861 list_splice_tail(&repoll, &list);
6862 list_splice(&list, &sd->poll_list);
6863 if (!list_empty(&sd->poll_list))
6864 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6865
Eric Dumazete326bed2010-04-22 00:22:45 -07006866 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006867out:
6868 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869}
6870
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006871struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006872 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006873
6874 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006875 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006876
Taehee Yoo32b6d342019-10-21 18:47:56 +00006877 /* lookup ignore flag */
6878 bool ignore;
6879
Veaceslav Falico5d261912013-08-28 23:25:05 +02006880 /* counter for the number of times this device was added to us */
6881 u16 ref_nr;
6882
Veaceslav Falico402dae92013-09-25 09:20:09 +02006883 /* private field for the users */
6884 void *private;
6885
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006886 struct list_head list;
6887 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006888};
6889
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006890static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006891 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006892{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006893 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006894
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006895 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006896 if (adj->dev == adj_dev)
6897 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006898 }
6899 return NULL;
6900}
6901
Taehee Yooeff74232020-09-25 18:13:12 +00006902static int ____netdev_has_upper_dev(struct net_device *upper_dev,
6903 struct netdev_nested_priv *priv)
David Ahernf1170fd2016-10-17 19:15:51 -07006904{
Taehee Yooeff74232020-09-25 18:13:12 +00006905 struct net_device *dev = (struct net_device *)priv->data;
David Ahernf1170fd2016-10-17 19:15:51 -07006906
6907 return upper_dev == dev;
6908}
6909
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006910/**
6911 * netdev_has_upper_dev - Check if device is linked to an upper device
6912 * @dev: device
6913 * @upper_dev: upper device to check
6914 *
6915 * Find out if a device is linked to specified upper device and return true
6916 * in case it is. Note that this checks only immediate upper device,
6917 * not through a complete stack of devices. The caller must hold the RTNL lock.
6918 */
6919bool netdev_has_upper_dev(struct net_device *dev,
6920 struct net_device *upper_dev)
6921{
Taehee Yooeff74232020-09-25 18:13:12 +00006922 struct netdev_nested_priv priv = {
6923 .data = (void *)upper_dev,
6924 };
6925
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006926 ASSERT_RTNL();
6927
Taehee Yoo32b6d342019-10-21 18:47:56 +00006928 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006929 &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006930}
6931EXPORT_SYMBOL(netdev_has_upper_dev);
6932
6933/**
David Ahern1a3f0602016-10-17 19:15:44 -07006934 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6935 * @dev: device
6936 * @upper_dev: upper device to check
6937 *
6938 * Find out if a device is linked to specified upper device and return true
6939 * in case it is. Note that this checks the entire upper device chain.
6940 * The caller must hold rcu lock.
6941 */
6942
David Ahern1a3f0602016-10-17 19:15:44 -07006943bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6944 struct net_device *upper_dev)
6945{
Taehee Yooeff74232020-09-25 18:13:12 +00006946 struct netdev_nested_priv priv = {
6947 .data = (void *)upper_dev,
6948 };
6949
Taehee Yoo32b6d342019-10-21 18:47:56 +00006950 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00006951 &priv);
David Ahern1a3f0602016-10-17 19:15:44 -07006952}
6953EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6954
6955/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006956 * netdev_has_any_upper_dev - Check if device is linked to some device
6957 * @dev: device
6958 *
6959 * Find out if a device is linked to an upper device and return true in case
6960 * it is. The caller must hold the RTNL lock.
6961 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006962bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006963{
6964 ASSERT_RTNL();
6965
David Ahernf1170fd2016-10-17 19:15:51 -07006966 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006967}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006968EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006969
6970/**
6971 * netdev_master_upper_dev_get - Get master upper device
6972 * @dev: device
6973 *
6974 * Find a master upper device and return pointer to it or NULL in case
6975 * it's not there. The caller must hold the RTNL lock.
6976 */
6977struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6978{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006979 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006980
6981 ASSERT_RTNL();
6982
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006983 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006984 return NULL;
6985
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006986 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006987 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006988 if (likely(upper->master))
6989 return upper->dev;
6990 return NULL;
6991}
6992EXPORT_SYMBOL(netdev_master_upper_dev_get);
6993
Taehee Yoo32b6d342019-10-21 18:47:56 +00006994static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6995{
6996 struct netdev_adjacent *upper;
6997
6998 ASSERT_RTNL();
6999
7000 if (list_empty(&dev->adj_list.upper))
7001 return NULL;
7002
7003 upper = list_first_entry(&dev->adj_list.upper,
7004 struct netdev_adjacent, list);
7005 if (likely(upper->master) && !upper->ignore)
7006 return upper->dev;
7007 return NULL;
7008}
7009
David Ahern0f524a82016-10-17 19:15:52 -07007010/**
7011 * netdev_has_any_lower_dev - Check if device is linked to some device
7012 * @dev: device
7013 *
7014 * Find out if a device is linked to a lower device and return true in case
7015 * it is. The caller must hold the RTNL lock.
7016 */
7017static bool netdev_has_any_lower_dev(struct net_device *dev)
7018{
7019 ASSERT_RTNL();
7020
7021 return !list_empty(&dev->adj_list.lower);
7022}
7023
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02007024void *netdev_adjacent_get_private(struct list_head *adj_list)
7025{
7026 struct netdev_adjacent *adj;
7027
7028 adj = list_entry(adj_list, struct netdev_adjacent, list);
7029
7030 return adj->private;
7031}
7032EXPORT_SYMBOL(netdev_adjacent_get_private);
7033
Veaceslav Falico31088a12013-09-25 09:20:12 +02007034/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04007035 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
7036 * @dev: device
7037 * @iter: list_head ** of the current position
7038 *
7039 * Gets the next device from the dev's upper list, starting from iter
7040 * position. The caller must hold RCU read lock.
7041 */
7042struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
7043 struct list_head **iter)
7044{
7045 struct netdev_adjacent *upper;
7046
7047 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7048
7049 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7050
7051 if (&upper->list == &dev->adj_list.upper)
7052 return NULL;
7053
7054 *iter = &upper->list;
7055
7056 return upper->dev;
7057}
7058EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
7059
Taehee Yoo32b6d342019-10-21 18:47:56 +00007060static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
7061 struct list_head **iter,
7062 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00007063{
7064 struct netdev_adjacent *upper;
7065
7066 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
7067
7068 if (&upper->list == &dev->adj_list.upper)
7069 return NULL;
7070
7071 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007072 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007073
7074 return upper->dev;
7075}
7076
David Ahern1a3f0602016-10-17 19:15:44 -07007077static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
7078 struct list_head **iter)
7079{
7080 struct netdev_adjacent *upper;
7081
7082 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7083
7084 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7085
7086 if (&upper->list == &dev->adj_list.upper)
7087 return NULL;
7088
7089 *iter = &upper->list;
7090
7091 return upper->dev;
7092}
7093
Taehee Yoo32b6d342019-10-21 18:47:56 +00007094static int __netdev_walk_all_upper_dev(struct net_device *dev,
7095 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007096 struct netdev_nested_priv *priv),
7097 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007098{
7099 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7100 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7101 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007102 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007103
7104 now = dev;
7105 iter = &dev->adj_list.upper;
7106
7107 while (1) {
7108 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007109 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007110 if (ret)
7111 return ret;
7112 }
7113
7114 next = NULL;
7115 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007116 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007117 if (!udev)
7118 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007119 if (ignore)
7120 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007121
7122 next = udev;
7123 niter = &udev->adj_list.upper;
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;
7138 }
7139
7140 return 0;
7141}
7142
David Ahern1a3f0602016-10-17 19:15:44 -07007143int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
7144 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007145 struct netdev_nested_priv *priv),
7146 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007147{
Taehee Yoo5343da42019-10-21 18:47:50 +00007148 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7149 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7150 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007151
Taehee Yoo5343da42019-10-21 18:47:50 +00007152 now = dev;
7153 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07007154
Taehee Yoo5343da42019-10-21 18:47:50 +00007155 while (1) {
7156 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007157 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007158 if (ret)
7159 return ret;
7160 }
7161
7162 next = NULL;
7163 while (1) {
7164 udev = netdev_next_upper_dev_rcu(now, &iter);
7165 if (!udev)
7166 break;
7167
7168 next = udev;
7169 niter = &udev->adj_list.upper;
7170 dev_stack[cur] = now;
7171 iter_stack[cur++] = iter;
7172 break;
7173 }
7174
7175 if (!next) {
7176 if (!cur)
7177 return 0;
7178 next = dev_stack[--cur];
7179 niter = iter_stack[cur];
7180 }
7181
7182 now = next;
7183 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007184 }
7185
7186 return 0;
7187}
7188EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
7189
Taehee Yoo32b6d342019-10-21 18:47:56 +00007190static bool __netdev_has_upper_dev(struct net_device *dev,
7191 struct net_device *upper_dev)
7192{
Taehee Yooeff74232020-09-25 18:13:12 +00007193 struct netdev_nested_priv priv = {
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007194 .flags = 0,
Taehee Yooeff74232020-09-25 18:13:12 +00007195 .data = (void *)upper_dev,
7196 };
7197
Taehee Yoo32b6d342019-10-21 18:47:56 +00007198 ASSERT_RTNL();
7199
7200 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007201 &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007202}
7203
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007204/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02007205 * netdev_lower_get_next_private - Get the next ->private from the
7206 * lower neighbour list
7207 * @dev: device
7208 * @iter: list_head ** of the current position
7209 *
7210 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7211 * list, starting from iter position. The caller must hold either hold the
7212 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007213 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02007214 */
7215void *netdev_lower_get_next_private(struct net_device *dev,
7216 struct list_head **iter)
7217{
7218 struct netdev_adjacent *lower;
7219
7220 lower = list_entry(*iter, struct netdev_adjacent, list);
7221
7222 if (&lower->list == &dev->adj_list.lower)
7223 return NULL;
7224
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007225 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007226
7227 return lower->private;
7228}
7229EXPORT_SYMBOL(netdev_lower_get_next_private);
7230
7231/**
7232 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7233 * lower neighbour list, RCU
7234 * variant
7235 * @dev: device
7236 * @iter: list_head ** of the current position
7237 *
7238 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7239 * list, starting from iter position. The caller must hold RCU read lock.
7240 */
7241void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7242 struct list_head **iter)
7243{
7244 struct netdev_adjacent *lower;
7245
7246 WARN_ON_ONCE(!rcu_read_lock_held());
7247
7248 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7249
7250 if (&lower->list == &dev->adj_list.lower)
7251 return NULL;
7252
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02007253 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02007254
7255 return lower->private;
7256}
7257EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7258
7259/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007260 * netdev_lower_get_next - Get the next device from the lower neighbour
7261 * list
7262 * @dev: device
7263 * @iter: list_head ** of the current position
7264 *
7265 * Gets the next netdev_adjacent from the dev's lower neighbour
7266 * list, starting from iter position. The caller must hold RTNL lock or
7267 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007268 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007269 */
7270void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7271{
7272 struct netdev_adjacent *lower;
7273
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007274 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007275
7276 if (&lower->list == &dev->adj_list.lower)
7277 return NULL;
7278
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007279 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007280
7281 return lower->dev;
7282}
7283EXPORT_SYMBOL(netdev_lower_get_next);
7284
David Ahern1a3f0602016-10-17 19:15:44 -07007285static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7286 struct list_head **iter)
7287{
7288 struct netdev_adjacent *lower;
7289
David Ahern46b5ab12016-10-26 13:21:33 -07007290 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007291
7292 if (&lower->list == &dev->adj_list.lower)
7293 return NULL;
7294
David Ahern46b5ab12016-10-26 13:21:33 -07007295 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007296
7297 return lower->dev;
7298}
7299
Taehee Yoo32b6d342019-10-21 18:47:56 +00007300static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7301 struct list_head **iter,
7302 bool *ignore)
7303{
7304 struct netdev_adjacent *lower;
7305
7306 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7307
7308 if (&lower->list == &dev->adj_list.lower)
7309 return NULL;
7310
7311 *iter = &lower->list;
7312 *ignore = lower->ignore;
7313
7314 return lower->dev;
7315}
7316
David Ahern1a3f0602016-10-17 19:15:44 -07007317int netdev_walk_all_lower_dev(struct net_device *dev,
7318 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007319 struct netdev_nested_priv *priv),
7320 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007321{
Taehee Yoo5343da42019-10-21 18:47:50 +00007322 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7323 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7324 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007325
Taehee Yoo5343da42019-10-21 18:47:50 +00007326 now = dev;
7327 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007328
Taehee Yoo5343da42019-10-21 18:47:50 +00007329 while (1) {
7330 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007331 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007332 if (ret)
7333 return ret;
7334 }
7335
7336 next = NULL;
7337 while (1) {
7338 ldev = netdev_next_lower_dev(now, &iter);
7339 if (!ldev)
7340 break;
7341
7342 next = ldev;
7343 niter = &ldev->adj_list.lower;
7344 dev_stack[cur] = now;
7345 iter_stack[cur++] = iter;
7346 break;
7347 }
7348
7349 if (!next) {
7350 if (!cur)
7351 return 0;
7352 next = dev_stack[--cur];
7353 niter = iter_stack[cur];
7354 }
7355
7356 now = next;
7357 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007358 }
7359
7360 return 0;
7361}
7362EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7363
Taehee Yoo32b6d342019-10-21 18:47:56 +00007364static int __netdev_walk_all_lower_dev(struct net_device *dev,
7365 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007366 struct netdev_nested_priv *priv),
7367 struct netdev_nested_priv *priv)
Taehee Yoo32b6d342019-10-21 18:47:56 +00007368{
7369 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7370 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7371 int ret, cur = 0;
7372 bool ignore;
7373
7374 now = dev;
7375 iter = &dev->adj_list.lower;
7376
7377 while (1) {
7378 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007379 ret = fn(now, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007380 if (ret)
7381 return ret;
7382 }
7383
7384 next = NULL;
7385 while (1) {
7386 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7387 if (!ldev)
7388 break;
7389 if (ignore)
7390 continue;
7391
7392 next = ldev;
7393 niter = &ldev->adj_list.lower;
7394 dev_stack[cur] = now;
7395 iter_stack[cur++] = iter;
7396 break;
7397 }
7398
7399 if (!next) {
7400 if (!cur)
7401 return 0;
7402 next = dev_stack[--cur];
7403 niter = iter_stack[cur];
7404 }
7405
7406 now = next;
7407 iter = niter;
7408 }
7409
7410 return 0;
7411}
7412
Taehee Yoo7151aff2020-02-15 10:50:21 +00007413struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7414 struct list_head **iter)
David Ahern1a3f0602016-10-17 19:15:44 -07007415{
7416 struct netdev_adjacent *lower;
7417
7418 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7419 if (&lower->list == &dev->adj_list.lower)
7420 return NULL;
7421
7422 *iter = &lower->list;
7423
7424 return lower->dev;
7425}
Taehee Yoo7151aff2020-02-15 10:50:21 +00007426EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
David Ahern1a3f0602016-10-17 19:15:44 -07007427
Taehee Yoo5343da42019-10-21 18:47:50 +00007428static u8 __netdev_upper_depth(struct net_device *dev)
7429{
7430 struct net_device *udev;
7431 struct list_head *iter;
7432 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007433 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007434
7435 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007436 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007437 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007438 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7439 if (ignore)
7440 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007441 if (max_depth < udev->upper_level)
7442 max_depth = udev->upper_level;
7443 }
7444
7445 return max_depth;
7446}
7447
7448static u8 __netdev_lower_depth(struct net_device *dev)
7449{
7450 struct net_device *ldev;
7451 struct list_head *iter;
7452 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007453 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007454
7455 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007456 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007457 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007458 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7459 if (ignore)
7460 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007461 if (max_depth < ldev->lower_level)
7462 max_depth = ldev->lower_level;
7463 }
7464
7465 return max_depth;
7466}
7467
Taehee Yooeff74232020-09-25 18:13:12 +00007468static int __netdev_update_upper_level(struct net_device *dev,
7469 struct netdev_nested_priv *__unused)
Taehee Yoo5343da42019-10-21 18:47:50 +00007470{
7471 dev->upper_level = __netdev_upper_depth(dev) + 1;
7472 return 0;
7473}
7474
Taehee Yooeff74232020-09-25 18:13:12 +00007475static int __netdev_update_lower_level(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007476 struct netdev_nested_priv *priv)
Taehee Yoo5343da42019-10-21 18:47:50 +00007477{
7478 dev->lower_level = __netdev_lower_depth(dev) + 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007479
7480#ifdef CONFIG_LOCKDEP
7481 if (!priv)
7482 return 0;
7483
7484 if (priv->flags & NESTED_SYNC_IMM)
7485 dev->nested_level = dev->lower_level - 1;
7486 if (priv->flags & NESTED_SYNC_TODO)
7487 net_unlink_todo(dev);
7488#endif
Taehee Yoo5343da42019-10-21 18:47:50 +00007489 return 0;
7490}
7491
David Ahern1a3f0602016-10-17 19:15:44 -07007492int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7493 int (*fn)(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00007494 struct netdev_nested_priv *priv),
7495 struct netdev_nested_priv *priv)
David Ahern1a3f0602016-10-17 19:15:44 -07007496{
Taehee Yoo5343da42019-10-21 18:47:50 +00007497 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7498 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7499 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007500
Taehee Yoo5343da42019-10-21 18:47:50 +00007501 now = dev;
7502 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007503
Taehee Yoo5343da42019-10-21 18:47:50 +00007504 while (1) {
7505 if (now != dev) {
Taehee Yooeff74232020-09-25 18:13:12 +00007506 ret = fn(now, priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007507 if (ret)
7508 return ret;
7509 }
7510
7511 next = NULL;
7512 while (1) {
7513 ldev = netdev_next_lower_dev_rcu(now, &iter);
7514 if (!ldev)
7515 break;
7516
7517 next = ldev;
7518 niter = &ldev->adj_list.lower;
7519 dev_stack[cur] = now;
7520 iter_stack[cur++] = iter;
7521 break;
7522 }
7523
7524 if (!next) {
7525 if (!cur)
7526 return 0;
7527 next = dev_stack[--cur];
7528 niter = iter_stack[cur];
7529 }
7530
7531 now = next;
7532 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007533 }
7534
7535 return 0;
7536}
7537EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7538
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007539/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007540 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7541 * lower neighbour list, RCU
7542 * variant
7543 * @dev: device
7544 *
7545 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7546 * list. The caller must hold RCU read lock.
7547 */
7548void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7549{
7550 struct netdev_adjacent *lower;
7551
7552 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7553 struct netdev_adjacent, list);
7554 if (lower)
7555 return lower->private;
7556 return NULL;
7557}
7558EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7559
7560/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007561 * netdev_master_upper_dev_get_rcu - Get master upper device
7562 * @dev: device
7563 *
7564 * Find a master upper device and return pointer to it or NULL in case
7565 * it's not there. The caller must hold the RCU read lock.
7566 */
7567struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7568{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007569 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007570
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007571 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007572 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007573 if (upper && likely(upper->master))
7574 return upper->dev;
7575 return NULL;
7576}
7577EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7578
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307579static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007580 struct net_device *adj_dev,
7581 struct list_head *dev_list)
7582{
7583 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007584
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007585 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7586 "upper_%s" : "lower_%s", adj_dev->name);
7587 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7588 linkname);
7589}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307590static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007591 char *name,
7592 struct list_head *dev_list)
7593{
7594 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007595
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007596 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7597 "upper_%s" : "lower_%s", name);
7598 sysfs_remove_link(&(dev->dev.kobj), linkname);
7599}
7600
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007601static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7602 struct net_device *adj_dev,
7603 struct list_head *dev_list)
7604{
7605 return (dev_list == &dev->adj_list.upper ||
7606 dev_list == &dev->adj_list.lower) &&
7607 net_eq(dev_net(dev), dev_net(adj_dev));
7608}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007609
Veaceslav Falico5d261912013-08-28 23:25:05 +02007610static int __netdev_adjacent_dev_insert(struct net_device *dev,
7611 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007612 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007613 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007614{
7615 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007616 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007617
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007618 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007619
7620 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007621 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007622 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7623 dev->name, adj_dev->name, adj->ref_nr);
7624
Veaceslav Falico5d261912013-08-28 23:25:05 +02007625 return 0;
7626 }
7627
7628 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7629 if (!adj)
7630 return -ENOMEM;
7631
7632 adj->dev = adj_dev;
7633 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007634 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007635 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007636 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007637 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007638
David Ahern67b62f92016-10-17 19:15:53 -07007639 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7640 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007641
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007642 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007643 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007644 if (ret)
7645 goto free_adj;
7646 }
7647
Veaceslav Falico7863c052013-09-25 09:20:06 +02007648 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007649 if (master) {
7650 ret = sysfs_create_link(&(dev->dev.kobj),
7651 &(adj_dev->dev.kobj), "master");
7652 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007653 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007654
Veaceslav Falico7863c052013-09-25 09:20:06 +02007655 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007656 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007657 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007658 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007659
7660 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007661
Veaceslav Falico5831d662013-09-25 09:20:32 +02007662remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007663 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007664 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007665free_adj:
7666 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007667 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007668
7669 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007670}
7671
stephen hemminger1d143d92013-12-29 14:01:29 -08007672static void __netdev_adjacent_dev_remove(struct net_device *dev,
7673 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007674 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007675 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007676{
7677 struct netdev_adjacent *adj;
7678
David Ahern67b62f92016-10-17 19:15:53 -07007679 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7680 dev->name, adj_dev->name, ref_nr);
7681
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007682 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007683
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007684 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007685 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007686 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007687 WARN_ON(1);
7688 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007689 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007690
Andrew Collins93409032016-10-03 13:43:02 -06007691 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007692 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7693 dev->name, adj_dev->name, ref_nr,
7694 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007695 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007696 return;
7697 }
7698
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007699 if (adj->master)
7700 sysfs_remove_link(&(dev->dev.kobj), "master");
7701
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007702 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007703 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007704
Veaceslav Falico5d261912013-08-28 23:25:05 +02007705 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007706 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007707 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007708 dev_put(adj_dev);
7709 kfree_rcu(adj, rcu);
7710}
7711
stephen hemminger1d143d92013-12-29 14:01:29 -08007712static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7713 struct net_device *upper_dev,
7714 struct list_head *up_list,
7715 struct list_head *down_list,
7716 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007717{
7718 int ret;
7719
David Ahern790510d2016-10-17 19:15:43 -07007720 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007721 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007722 if (ret)
7723 return ret;
7724
David Ahern790510d2016-10-17 19:15:43 -07007725 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007726 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007727 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007728 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007729 return ret;
7730 }
7731
7732 return 0;
7733}
7734
stephen hemminger1d143d92013-12-29 14:01:29 -08007735static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7736 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007737 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007738 struct list_head *up_list,
7739 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007740{
Andrew Collins93409032016-10-03 13:43:02 -06007741 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7742 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007743}
7744
stephen hemminger1d143d92013-12-29 14:01:29 -08007745static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7746 struct net_device *upper_dev,
7747 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007748{
David Ahernf1170fd2016-10-17 19:15:51 -07007749 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7750 &dev->adj_list.upper,
7751 &upper_dev->adj_list.lower,
7752 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007753}
7754
stephen hemminger1d143d92013-12-29 14:01:29 -08007755static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7756 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007757{
Andrew Collins93409032016-10-03 13:43:02 -06007758 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007759 &dev->adj_list.upper,
7760 &upper_dev->adj_list.lower);
7761}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007762
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007763static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007764 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007765 void *upper_priv, void *upper_info,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007766 struct netdev_nested_priv *priv,
David Ahern42ab19e2017-10-04 17:48:47 -07007767 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007768{
David Ahern51d0c0472017-10-04 17:48:45 -07007769 struct netdev_notifier_changeupper_info changeupper_info = {
7770 .info = {
7771 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007772 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007773 },
7774 .upper_dev = upper_dev,
7775 .master = master,
7776 .linking = true,
7777 .upper_info = upper_info,
7778 };
Mike Manning50d629e2018-02-26 23:49:30 +00007779 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007780 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007781
7782 ASSERT_RTNL();
7783
7784 if (dev == upper_dev)
7785 return -EBUSY;
7786
7787 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007788 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007789 return -EBUSY;
7790
Taehee Yoo5343da42019-10-21 18:47:50 +00007791 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7792 return -EMLINK;
7793
Mike Manning50d629e2018-02-26 23:49:30 +00007794 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007795 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007796 return -EEXIST;
7797 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007798 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007799 if (master_dev)
7800 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7801 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007802
David Ahern51d0c0472017-10-04 17:48:45 -07007803 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007804 &changeupper_info.info);
7805 ret = notifier_to_errno(ret);
7806 if (ret)
7807 return ret;
7808
Jiri Pirko6dffb042015-12-03 12:12:10 +01007809 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007810 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007811 if (ret)
7812 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007813
David Ahern51d0c0472017-10-04 17:48:45 -07007814 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007815 &changeupper_info.info);
7816 ret = notifier_to_errno(ret);
7817 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007818 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007819
Taehee Yoo5343da42019-10-21 18:47:50 +00007820 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007821 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007822
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007823 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007824 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007825 priv);
Taehee Yoo5343da42019-10-21 18:47:50 +00007826
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007827 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007828
David Ahernf1170fd2016-10-17 19:15:51 -07007829rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007830 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007831
7832 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007833}
7834
7835/**
7836 * netdev_upper_dev_link - Add a link to the upper device
7837 * @dev: device
7838 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007839 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007840 *
7841 * Adds a link to device which is upper to this one. The caller must hold
7842 * the RTNL lock. On a failure a negative errno code is returned.
7843 * On success the reference counts are adjusted and the function
7844 * returns zero.
7845 */
7846int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007847 struct net_device *upper_dev,
7848 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007849{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007850 struct netdev_nested_priv priv = {
7851 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7852 .data = NULL,
7853 };
7854
David Ahern42ab19e2017-10-04 17:48:47 -07007855 return __netdev_upper_dev_link(dev, upper_dev, false,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007856 NULL, NULL, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007857}
7858EXPORT_SYMBOL(netdev_upper_dev_link);
7859
7860/**
7861 * netdev_master_upper_dev_link - Add a master link to the upper device
7862 * @dev: device
7863 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007864 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007865 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007866 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007867 *
7868 * Adds a link to device which is upper to this one. In this case, only
7869 * one master upper device can be linked, although other non-master devices
7870 * might be linked as well. The caller must hold the RTNL lock.
7871 * On a failure a negative errno code is returned. On success the reference
7872 * counts are adjusted and the function returns zero.
7873 */
7874int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007875 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007876 void *upper_priv, void *upper_info,
7877 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007878{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007879 struct netdev_nested_priv priv = {
7880 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7881 .data = NULL,
7882 };
7883
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007884 return __netdev_upper_dev_link(dev, upper_dev, true,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007885 upper_priv, upper_info, &priv, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007886}
7887EXPORT_SYMBOL(netdev_master_upper_dev_link);
7888
Taehee Yoofe8300f2020-09-25 18:13:02 +00007889static void __netdev_upper_dev_unlink(struct net_device *dev,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007890 struct net_device *upper_dev,
7891 struct netdev_nested_priv *priv)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007892{
David Ahern51d0c0472017-10-04 17:48:45 -07007893 struct netdev_notifier_changeupper_info changeupper_info = {
7894 .info = {
7895 .dev = dev,
7896 },
7897 .upper_dev = upper_dev,
7898 .linking = false,
7899 };
tchardingf4563a72017-02-09 17:56:07 +11007900
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007901 ASSERT_RTNL();
7902
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007903 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007904
David Ahern51d0c0472017-10-04 17:48:45 -07007905 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007906 &changeupper_info.info);
7907
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007908 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007909
David Ahern51d0c0472017-10-04 17:48:45 -07007910 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007911 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007912
7913 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007914 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007915
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007916 __netdev_update_lower_level(upper_dev, priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007917 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007918 priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007919}
Taehee Yoofe8300f2020-09-25 18:13:02 +00007920
7921/**
7922 * netdev_upper_dev_unlink - Removes a link to upper device
7923 * @dev: device
7924 * @upper_dev: new upper device
7925 *
7926 * Removes a link to device which is upper to this one. The caller must hold
7927 * the RTNL lock.
7928 */
7929void netdev_upper_dev_unlink(struct net_device *dev,
7930 struct net_device *upper_dev)
7931{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007932 struct netdev_nested_priv priv = {
7933 .flags = NESTED_SYNC_TODO,
7934 .data = NULL,
7935 };
7936
7937 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007938}
7939EXPORT_SYMBOL(netdev_upper_dev_unlink);
7940
Taehee Yoo32b6d342019-10-21 18:47:56 +00007941static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7942 struct net_device *lower_dev,
7943 bool val)
7944{
7945 struct netdev_adjacent *adj;
7946
7947 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7948 if (adj)
7949 adj->ignore = val;
7950
7951 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7952 if (adj)
7953 adj->ignore = val;
7954}
7955
7956static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7957 struct net_device *lower_dev)
7958{
7959 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7960}
7961
7962static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7963 struct net_device *lower_dev)
7964{
7965 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7966}
7967
7968int netdev_adjacent_change_prepare(struct net_device *old_dev,
7969 struct net_device *new_dev,
7970 struct net_device *dev,
7971 struct netlink_ext_ack *extack)
7972{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007973 struct netdev_nested_priv priv = {
7974 .flags = 0,
7975 .data = NULL,
7976 };
Taehee Yoo32b6d342019-10-21 18:47:56 +00007977 int err;
7978
7979 if (!new_dev)
7980 return 0;
7981
7982 if (old_dev && new_dev != old_dev)
7983 netdev_adjacent_dev_disable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00007984 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
7985 extack);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007986 if (err) {
7987 if (old_dev && new_dev != old_dev)
7988 netdev_adjacent_dev_enable(dev, old_dev);
7989 return err;
7990 }
7991
7992 return 0;
7993}
7994EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7995
7996void netdev_adjacent_change_commit(struct net_device *old_dev,
7997 struct net_device *new_dev,
7998 struct net_device *dev)
7999{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008000 struct netdev_nested_priv priv = {
8001 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8002 .data = NULL,
8003 };
8004
Taehee Yoo32b6d342019-10-21 18:47:56 +00008005 if (!new_dev || !old_dev)
8006 return;
8007
8008 if (new_dev == old_dev)
8009 return;
8010
8011 netdev_adjacent_dev_enable(dev, old_dev);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008012 __netdev_upper_dev_unlink(old_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008013}
8014EXPORT_SYMBOL(netdev_adjacent_change_commit);
8015
8016void netdev_adjacent_change_abort(struct net_device *old_dev,
8017 struct net_device *new_dev,
8018 struct net_device *dev)
8019{
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008020 struct netdev_nested_priv priv = {
8021 .flags = 0,
8022 .data = NULL,
8023 };
8024
Taehee Yoo32b6d342019-10-21 18:47:56 +00008025 if (!new_dev)
8026 return;
8027
8028 if (old_dev && new_dev != old_dev)
8029 netdev_adjacent_dev_enable(dev, old_dev);
8030
Taehee Yoo1fc70ed2020-09-25 18:13:29 +00008031 __netdev_upper_dev_unlink(new_dev, dev, &priv);
Taehee Yoo32b6d342019-10-21 18:47:56 +00008032}
8033EXPORT_SYMBOL(netdev_adjacent_change_abort);
8034
Moni Shoua61bd3852015-02-03 16:48:29 +02008035/**
8036 * netdev_bonding_info_change - Dispatch event about slave change
8037 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09008038 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02008039 *
8040 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
8041 * The caller must hold the RTNL lock.
8042 */
8043void netdev_bonding_info_change(struct net_device *dev,
8044 struct netdev_bonding_info *bonding_info)
8045{
David Ahern51d0c0472017-10-04 17:48:45 -07008046 struct netdev_notifier_bonding_info info = {
8047 .info.dev = dev,
8048 };
Moni Shoua61bd3852015-02-03 16:48:29 +02008049
8050 memcpy(&info.bonding_info, bonding_info,
8051 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07008052 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02008053 &info.info);
8054}
8055EXPORT_SYMBOL(netdev_bonding_info_change);
8056
Maor Gottliebcff9f122020-04-30 22:21:31 +03008057/**
8058 * netdev_get_xmit_slave - Get the xmit slave of master device
Andrew Lunn88425002020-07-13 01:14:59 +02008059 * @dev: device
Maor Gottliebcff9f122020-04-30 22:21:31 +03008060 * @skb: The packet
8061 * @all_slaves: assume all the slaves are active
8062 *
8063 * The reference counters are not incremented so the caller must be
8064 * careful with locks. The caller must hold RCU lock.
8065 * %NULL is returned if no slave is found.
8066 */
8067
8068struct net_device *netdev_get_xmit_slave(struct net_device *dev,
8069 struct sk_buff *skb,
8070 bool all_slaves)
8071{
8072 const struct net_device_ops *ops = dev->netdev_ops;
8073
8074 if (!ops->ndo_get_xmit_slave)
8075 return NULL;
8076 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
8077}
8078EXPORT_SYMBOL(netdev_get_xmit_slave);
8079
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008080static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008081{
8082 struct netdev_adjacent *iter;
8083
8084 struct net *net = dev_net(dev);
8085
8086 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008087 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008088 continue;
8089 netdev_adjacent_sysfs_add(iter->dev, dev,
8090 &iter->dev->adj_list.lower);
8091 netdev_adjacent_sysfs_add(dev, iter->dev,
8092 &dev->adj_list.upper);
8093 }
8094
8095 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008096 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008097 continue;
8098 netdev_adjacent_sysfs_add(iter->dev, dev,
8099 &iter->dev->adj_list.upper);
8100 netdev_adjacent_sysfs_add(dev, iter->dev,
8101 &dev->adj_list.lower);
8102 }
8103}
8104
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008105static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008106{
8107 struct netdev_adjacent *iter;
8108
8109 struct net *net = dev_net(dev);
8110
8111 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008112 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008113 continue;
8114 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8115 &iter->dev->adj_list.lower);
8116 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8117 &dev->adj_list.upper);
8118 }
8119
8120 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008121 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008122 continue;
8123 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8124 &iter->dev->adj_list.upper);
8125 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8126 &dev->adj_list.lower);
8127 }
8128}
8129
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008130void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02008131{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008132 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02008133
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008134 struct net *net = dev_net(dev);
8135
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008136 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008137 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008138 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008139 netdev_adjacent_sysfs_del(iter->dev, oldname,
8140 &iter->dev->adj_list.lower);
8141 netdev_adjacent_sysfs_add(iter->dev, dev,
8142 &iter->dev->adj_list.lower);
8143 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008144
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008145 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08008146 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008147 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01008148 netdev_adjacent_sysfs_del(iter->dev, oldname,
8149 &iter->dev->adj_list.upper);
8150 netdev_adjacent_sysfs_add(iter->dev, dev,
8151 &iter->dev->adj_list.upper);
8152 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02008153}
Veaceslav Falico402dae92013-09-25 09:20:09 +02008154
8155void *netdev_lower_dev_get_private(struct net_device *dev,
8156 struct net_device *lower_dev)
8157{
8158 struct netdev_adjacent *lower;
8159
8160 if (!lower_dev)
8161 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02008162 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02008163 if (!lower)
8164 return NULL;
8165
8166 return lower->private;
8167}
8168EXPORT_SYMBOL(netdev_lower_dev_get_private);
8169
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04008170
Jiri Pirko04d48262015-12-03 12:12:15 +01008171/**
8172 * netdev_lower_change - Dispatch event about lower device state change
8173 * @lower_dev: device
8174 * @lower_state_info: state to dispatch
8175 *
8176 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
8177 * The caller must hold the RTNL lock.
8178 */
8179void netdev_lower_state_changed(struct net_device *lower_dev,
8180 void *lower_state_info)
8181{
David Ahern51d0c0472017-10-04 17:48:45 -07008182 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
8183 .info.dev = lower_dev,
8184 };
Jiri Pirko04d48262015-12-03 12:12:15 +01008185
8186 ASSERT_RTNL();
8187 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07008188 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01008189 &changelowerstate_info.info);
8190}
8191EXPORT_SYMBOL(netdev_lower_state_changed);
8192
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008193static void dev_change_rx_flags(struct net_device *dev, int flags)
8194{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008195 const struct net_device_ops *ops = dev->netdev_ops;
8196
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05008197 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008198 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008199}
8200
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008201static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07008202{
Eric Dumazetb536db92011-11-30 21:42:26 +00008203 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06008204 kuid_t uid;
8205 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07008206
Patrick McHardy24023452007-07-14 18:51:31 -07008207 ASSERT_RTNL();
8208
Wang Chendad9b332008-06-18 01:48:28 -07008209 dev->flags |= IFF_PROMISC;
8210 dev->promiscuity += inc;
8211 if (dev->promiscuity == 0) {
8212 /*
8213 * Avoid overflow.
8214 * If inc causes overflow, untouch promisc and return error.
8215 */
8216 if (inc < 0)
8217 dev->flags &= ~IFF_PROMISC;
8218 else {
8219 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008220 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
8221 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008222 return -EOVERFLOW;
8223 }
8224 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008225 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008226 pr_info("device %s %s promiscuous mode\n",
8227 dev->name,
8228 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11008229 if (audit_enabled) {
8230 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04008231 audit_log(audit_context(), GFP_ATOMIC,
8232 AUDIT_ANOM_PROMISCUOUS,
8233 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
8234 dev->name, (dev->flags & IFF_PROMISC),
8235 (old_flags & IFF_PROMISC),
8236 from_kuid(&init_user_ns, audit_get_loginuid(current)),
8237 from_kuid(&init_user_ns, uid),
8238 from_kgid(&init_user_ns, gid),
8239 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11008240 }
Patrick McHardy24023452007-07-14 18:51:31 -07008241
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008242 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07008243 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008244 if (notify)
8245 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07008246 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008247}
8248
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249/**
8250 * dev_set_promiscuity - update promiscuity count on a device
8251 * @dev: device
8252 * @inc: modifier
8253 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07008254 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 * remains above zero the interface remains promiscuous. Once it hits zero
8256 * the device reverts back to normal filtering operation. A negative inc
8257 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07008258 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 */
Wang Chendad9b332008-06-18 01:48:28 -07008260int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261{
Eric Dumazetb536db92011-11-30 21:42:26 +00008262 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07008263 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008265 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07008266 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07008267 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07008268 if (dev->flags != old_flags)
8269 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07008270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008272EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008274static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008276 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277
Patrick McHardy24023452007-07-14 18:51:31 -07008278 ASSERT_RTNL();
8279
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07008281 dev->allmulti += inc;
8282 if (dev->allmulti == 0) {
8283 /*
8284 * Avoid overflow.
8285 * If inc causes overflow, untouch allmulti and return error.
8286 */
8287 if (inc < 0)
8288 dev->flags &= ~IFF_ALLMULTI;
8289 else {
8290 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008291 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8292 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07008293 return -EOVERFLOW;
8294 }
8295 }
Patrick McHardy24023452007-07-14 18:51:31 -07008296 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008297 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07008298 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008299 if (notify)
8300 __dev_notify_flags(dev, old_flags,
8301 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07008302 }
Wang Chendad9b332008-06-18 01:48:28 -07008303 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07008304}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008305
8306/**
8307 * dev_set_allmulti - update allmulti count on a device
8308 * @dev: device
8309 * @inc: modifier
8310 *
8311 * Add or remove reception of all multicast frames to a device. While the
8312 * count in the device remains above zero the interface remains listening
8313 * to all interfaces. Once it hits zero the device reverts back to normal
8314 * filtering operation. A negative @inc value is used to drop the counter
8315 * when releasing a resource needing all multicasts.
8316 * Return 0 if successful or a negative errno code on error.
8317 */
8318
8319int dev_set_allmulti(struct net_device *dev, int inc)
8320{
8321 return __dev_set_allmulti(dev, inc, true);
8322}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008323EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07008324
8325/*
8326 * Upload unicast and multicast address lists to device and
8327 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08008328 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07008329 * are present.
8330 */
8331void __dev_set_rx_mode(struct net_device *dev)
8332{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008333 const struct net_device_ops *ops = dev->netdev_ops;
8334
Patrick McHardy4417da62007-06-27 01:28:10 -07008335 /* dev_open will call this function so the list will stay sane. */
8336 if (!(dev->flags&IFF_UP))
8337 return;
8338
8339 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008340 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008341
Jiri Pirko01789342011-08-16 06:29:00 +00008342 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008343 /* Unicast addresses changes may only happen under the rtnl,
8344 * therefore calling __dev_set_promiscuity here is safe.
8345 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008346 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008347 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008348 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008349 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008350 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008351 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008352 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008353 }
Jiri Pirko01789342011-08-16 06:29:00 +00008354
8355 if (ops->ndo_set_rx_mode)
8356 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008357}
8358
8359void dev_set_rx_mode(struct net_device *dev)
8360{
David S. Millerb9e40852008-07-15 00:15:08 -07008361 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008362 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008363 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364}
8365
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008366/**
8367 * dev_get_flags - get flags reported to userspace
8368 * @dev: device
8369 *
8370 * Get the combination of flag bits exported through APIs to userspace.
8371 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008372unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373{
Eric Dumazet95c96172012-04-15 05:58:06 +00008374 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375
8376 flags = (dev->flags & ~(IFF_PROMISC |
8377 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008378 IFF_RUNNING |
8379 IFF_LOWER_UP |
8380 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 (dev->gflags & (IFF_PROMISC |
8382 IFF_ALLMULTI));
8383
Stefan Rompfb00055a2006-03-20 17:09:11 -08008384 if (netif_running(dev)) {
8385 if (netif_oper_up(dev))
8386 flags |= IFF_RUNNING;
8387 if (netif_carrier_ok(dev))
8388 flags |= IFF_LOWER_UP;
8389 if (netif_dormant(dev))
8390 flags |= IFF_DORMANT;
8391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392
8393 return flags;
8394}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008395EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396
Petr Machata6d040322018-12-06 17:05:43 +00008397int __dev_change_flags(struct net_device *dev, unsigned int flags,
8398 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399{
Eric Dumazetb536db92011-11-30 21:42:26 +00008400 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008401 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402
Patrick McHardy24023452007-07-14 18:51:31 -07008403 ASSERT_RTNL();
8404
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 /*
8406 * Set the flags on our device.
8407 */
8408
8409 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8410 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8411 IFF_AUTOMEDIA)) |
8412 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8413 IFF_ALLMULTI));
8414
8415 /*
8416 * Load in the correct multicast list now the flags have changed.
8417 */
8418
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008419 if ((old_flags ^ flags) & IFF_MULTICAST)
8420 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008421
Patrick McHardy4417da62007-06-27 01:28:10 -07008422 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423
8424 /*
8425 * Have we downed the interface. We handle IFF_UP ourselves
8426 * according to user attempts to set it, rather than blindly
8427 * setting it.
8428 */
8429
8430 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008431 if ((old_flags ^ flags) & IFF_UP) {
8432 if (old_flags & IFF_UP)
8433 __dev_close(dev);
8434 else
Petr Machata40c900a2018-12-06 17:05:47 +00008435 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008439 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008440 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008441
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008443
8444 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8445 if (dev->flags != old_flags)
8446 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 }
8448
8449 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008450 * is important. Some (broken) drivers set IFF_PROMISC, when
8451 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 */
8453 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008454 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8455
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008457 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 }
8459
Patrick McHardybd380812010-02-26 06:34:53 +00008460 return ret;
8461}
8462
Nicolas Dichtela528c212013-09-25 12:02:44 +02008463void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8464 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008465{
8466 unsigned int changes = dev->flags ^ old_flags;
8467
Nicolas Dichtela528c212013-09-25 12:02:44 +02008468 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008469 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008470
Patrick McHardybd380812010-02-26 06:34:53 +00008471 if (changes & IFF_UP) {
8472 if (dev->flags & IFF_UP)
8473 call_netdevice_notifiers(NETDEV_UP, dev);
8474 else
8475 call_netdevice_notifiers(NETDEV_DOWN, dev);
8476 }
8477
8478 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008479 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008480 struct netdev_notifier_change_info change_info = {
8481 .info = {
8482 .dev = dev,
8483 },
8484 .flags_changed = changes,
8485 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008486
David Ahern51d0c0472017-10-04 17:48:45 -07008487 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008488 }
Patrick McHardybd380812010-02-26 06:34:53 +00008489}
8490
8491/**
8492 * dev_change_flags - change device settings
8493 * @dev: device
8494 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008495 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008496 *
8497 * Change settings on device based state flags. The flags are
8498 * in the userspace exported format.
8499 */
Petr Machata567c5e12018-12-06 17:05:42 +00008500int dev_change_flags(struct net_device *dev, unsigned int flags,
8501 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008502{
Eric Dumazetb536db92011-11-30 21:42:26 +00008503 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008504 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008505
Petr Machata6d040322018-12-06 17:05:43 +00008506 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008507 if (ret < 0)
8508 return ret;
8509
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008510 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008511 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 return ret;
8513}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008514EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515
WANG Congf51048c2017-07-06 15:01:57 -07008516int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008517{
8518 const struct net_device_ops *ops = dev->netdev_ops;
8519
8520 if (ops->ndo_change_mtu)
8521 return ops->ndo_change_mtu(dev, new_mtu);
8522
Eric Dumazet501a90c2019-12-05 20:43:46 -08008523 /* Pairs with all the lockless reads of dev->mtu in the stack */
8524 WRITE_ONCE(dev->mtu, new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008525 return 0;
8526}
WANG Congf51048c2017-07-06 15:01:57 -07008527EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008528
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008529int dev_validate_mtu(struct net_device *dev, int new_mtu,
8530 struct netlink_ext_ack *extack)
8531{
8532 /* MTU must be positive, and in range */
8533 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8534 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8535 return -EINVAL;
8536 }
8537
8538 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8539 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8540 return -EINVAL;
8541 }
8542 return 0;
8543}
8544
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008545/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008546 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008547 * @dev: device
8548 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008549 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008550 *
8551 * Change the maximum transfer size of the network device.
8552 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008553int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8554 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008556 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557
8558 if (new_mtu == dev->mtu)
8559 return 0;
8560
Eric Dumazetd836f5c2020-01-21 22:47:29 -08008561 err = dev_validate_mtu(dev, new_mtu, extack);
8562 if (err)
8563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564
8565 if (!netif_device_present(dev))
8566 return -ENODEV;
8567
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008568 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8569 err = notifier_to_errno(err);
8570 if (err)
8571 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008572
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008573 orig_mtu = dev->mtu;
8574 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008576 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008577 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8578 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008579 err = notifier_to_errno(err);
8580 if (err) {
8581 /* setting mtu back and notifying everyone again,
8582 * so that they have a chance to revert changes.
8583 */
8584 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008585 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8586 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008587 }
8588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589 return err;
8590}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008591
8592int dev_set_mtu(struct net_device *dev, int new_mtu)
8593{
8594 struct netlink_ext_ack extack;
8595 int err;
8596
Li RongQinga6bcfc82018-08-03 15:45:21 +08008597 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008598 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008599 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008600 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8601 return err;
8602}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008603EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008605/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008606 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8607 * @dev: device
8608 * @new_len: new tx queue length
8609 */
8610int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8611{
8612 unsigned int orig_len = dev->tx_queue_len;
8613 int res;
8614
8615 if (new_len != (unsigned int)new_len)
8616 return -ERANGE;
8617
8618 if (new_len != orig_len) {
8619 dev->tx_queue_len = new_len;
8620 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8621 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008622 if (res)
8623 goto err_rollback;
8624 res = dev_qdisc_change_tx_queue_len(dev);
8625 if (res)
8626 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008627 }
8628
8629 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008630
8631err_rollback:
8632 netdev_err(dev, "refused to change device tx_queue_len\n");
8633 dev->tx_queue_len = orig_len;
8634 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008635}
8636
8637/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008638 * dev_set_group - Change group this device belongs to
8639 * @dev: device
8640 * @new_group: group this device should belong to
8641 */
8642void dev_set_group(struct net_device *dev, int new_group)
8643{
8644 dev->group = new_group;
8645}
8646EXPORT_SYMBOL(dev_set_group);
8647
8648/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008649 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8650 * @dev: device
8651 * @addr: new address
8652 * @extack: netlink extended ack
8653 */
8654int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8655 struct netlink_ext_ack *extack)
8656{
8657 struct netdev_notifier_pre_changeaddr_info info = {
8658 .info.dev = dev,
8659 .info.extack = extack,
8660 .dev_addr = addr,
8661 };
8662 int rc;
8663
8664 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8665 return notifier_to_errno(rc);
8666}
8667EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8668
8669/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008670 * dev_set_mac_address - Change Media Access Control Address
8671 * @dev: device
8672 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008673 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008674 *
8675 * Change the hardware (MAC) address of the device
8676 */
Petr Machata3a37a962018-12-13 11:54:30 +00008677int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8678 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008680 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 int err;
8682
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008683 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 return -EOPNOTSUPP;
8685 if (sa->sa_family != dev->type)
8686 return -EINVAL;
8687 if (!netif_device_present(dev))
8688 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008689 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8690 if (err)
8691 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008692 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008693 if (err)
8694 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008695 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008696 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008697 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008700EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701
Cong Wang1fc205d2021-02-11 11:34:10 -08008702static DECLARE_RWSEM(dev_addr_sem);
8703
8704int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
8705 struct netlink_ext_ack *extack)
8706{
8707 int ret;
8708
8709 down_write(&dev_addr_sem);
8710 ret = dev_set_mac_address(dev, sa, extack);
8711 up_write(&dev_addr_sem);
8712 return ret;
8713}
8714EXPORT_SYMBOL(dev_set_mac_address_user);
8715
8716int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
8717{
8718 size_t size = sizeof(sa->sa_data);
8719 struct net_device *dev;
8720 int ret = 0;
8721
8722 down_read(&dev_addr_sem);
8723 rcu_read_lock();
8724
8725 dev = dev_get_by_name_rcu(net, dev_name);
8726 if (!dev) {
8727 ret = -ENODEV;
8728 goto unlock;
8729 }
8730 if (!dev->addr_len)
8731 memset(sa->sa_data, 0, size);
8732 else
8733 memcpy(sa->sa_data, dev->dev_addr,
8734 min_t(size_t, size, dev->addr_len));
8735 sa->sa_family = dev->type;
8736
8737unlock:
8738 rcu_read_unlock();
8739 up_read(&dev_addr_sem);
8740 return ret;
8741}
8742EXPORT_SYMBOL(dev_get_mac_address);
8743
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008744/**
8745 * dev_change_carrier - Change device carrier
8746 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008747 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008748 *
8749 * Change device carrier
8750 */
8751int dev_change_carrier(struct net_device *dev, bool new_carrier)
8752{
8753 const struct net_device_ops *ops = dev->netdev_ops;
8754
8755 if (!ops->ndo_change_carrier)
8756 return -EOPNOTSUPP;
8757 if (!netif_device_present(dev))
8758 return -ENODEV;
8759 return ops->ndo_change_carrier(dev, new_carrier);
8760}
8761EXPORT_SYMBOL(dev_change_carrier);
8762
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008764 * dev_get_phys_port_id - Get device physical port ID
8765 * @dev: device
8766 * @ppid: port ID
8767 *
8768 * Get device physical port ID
8769 */
8770int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008771 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008772{
8773 const struct net_device_ops *ops = dev->netdev_ops;
8774
8775 if (!ops->ndo_get_phys_port_id)
8776 return -EOPNOTSUPP;
8777 return ops->ndo_get_phys_port_id(dev, ppid);
8778}
8779EXPORT_SYMBOL(dev_get_phys_port_id);
8780
8781/**
David Aherndb24a902015-03-17 20:23:15 -06008782 * dev_get_phys_port_name - Get device physical port name
8783 * @dev: device
8784 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008785 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008786 *
8787 * Get device physical port name
8788 */
8789int dev_get_phys_port_name(struct net_device *dev,
8790 char *name, size_t len)
8791{
8792 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008793 int err;
David Aherndb24a902015-03-17 20:23:15 -06008794
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008795 if (ops->ndo_get_phys_port_name) {
8796 err = ops->ndo_get_phys_port_name(dev, name, len);
8797 if (err != -EOPNOTSUPP)
8798 return err;
8799 }
8800 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008801}
8802EXPORT_SYMBOL(dev_get_phys_port_name);
8803
8804/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008805 * dev_get_port_parent_id - Get the device's port parent identifier
8806 * @dev: network device
8807 * @ppid: pointer to a storage for the port's parent identifier
8808 * @recurse: allow/disallow recursion to lower devices
8809 *
8810 * Get the devices's port parent identifier
8811 */
8812int dev_get_port_parent_id(struct net_device *dev,
8813 struct netdev_phys_item_id *ppid,
8814 bool recurse)
8815{
8816 const struct net_device_ops *ops = dev->netdev_ops;
8817 struct netdev_phys_item_id first = { };
8818 struct net_device *lower_dev;
8819 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008820 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008821
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008822 if (ops->ndo_get_port_parent_id) {
8823 err = ops->ndo_get_port_parent_id(dev, ppid);
8824 if (err != -EOPNOTSUPP)
8825 return err;
8826 }
8827
8828 err = devlink_compat_switch_id_get(dev, ppid);
8829 if (!err || err != -EOPNOTSUPP)
8830 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008831
8832 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008833 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008834
8835 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8836 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8837 if (err)
8838 break;
8839 if (!first.id_len)
8840 first = *ppid;
8841 else if (memcmp(&first, ppid, sizeof(*ppid)))
Ido Schimmele1b9efe2020-09-10 14:01:26 +03008842 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008843 }
8844
8845 return err;
8846}
8847EXPORT_SYMBOL(dev_get_port_parent_id);
8848
8849/**
8850 * netdev_port_same_parent_id - Indicate if two network devices have
8851 * the same port parent identifier
8852 * @a: first network device
8853 * @b: second network device
8854 */
8855bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8856{
8857 struct netdev_phys_item_id a_id = { };
8858 struct netdev_phys_item_id b_id = { };
8859
8860 if (dev_get_port_parent_id(a, &a_id, true) ||
8861 dev_get_port_parent_id(b, &b_id, true))
8862 return false;
8863
8864 return netdev_phys_item_id_same(&a_id, &b_id);
8865}
8866EXPORT_SYMBOL(netdev_port_same_parent_id);
8867
8868/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008869 * dev_change_proto_down - update protocol port state information
8870 * @dev: device
8871 * @proto_down: new value
8872 *
8873 * This info can be used by switch drivers to set the phys state of the
8874 * port.
8875 */
8876int dev_change_proto_down(struct net_device *dev, bool proto_down)
8877{
8878 const struct net_device_ops *ops = dev->netdev_ops;
8879
8880 if (!ops->ndo_change_proto_down)
8881 return -EOPNOTSUPP;
8882 if (!netif_device_present(dev))
8883 return -ENODEV;
8884 return ops->ndo_change_proto_down(dev, proto_down);
8885}
8886EXPORT_SYMBOL(dev_change_proto_down);
8887
Andy Roulinb5899672019-02-22 18:06:36 +00008888/**
8889 * dev_change_proto_down_generic - generic implementation for
8890 * ndo_change_proto_down that sets carrier according to
8891 * proto_down.
8892 *
8893 * @dev: device
8894 * @proto_down: new value
8895 */
8896int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8897{
8898 if (proto_down)
8899 netif_carrier_off(dev);
8900 else
8901 netif_carrier_on(dev);
8902 dev->proto_down = proto_down;
8903 return 0;
8904}
8905EXPORT_SYMBOL(dev_change_proto_down_generic);
8906
Roopa Prabhu829eb202020-07-31 17:34:01 -07008907/**
8908 * dev_change_proto_down_reason - proto down reason
8909 *
8910 * @dev: device
8911 * @mask: proto down mask
8912 * @value: proto down value
8913 */
8914void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
8915 u32 value)
8916{
8917 int b;
8918
8919 if (!mask) {
8920 dev->proto_down_reason = value;
8921 } else {
8922 for_each_set_bit(b, &mask, 32) {
8923 if (value & (1 << b))
8924 dev->proto_down_reason |= BIT(b);
8925 else
8926 dev->proto_down_reason &= ~BIT(b);
8927 }
8928 }
8929}
8930EXPORT_SYMBOL(dev_change_proto_down_reason);
8931
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008932struct bpf_xdp_link {
8933 struct bpf_link link;
8934 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
8935 int flags;
8936};
8937
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008938static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008939{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008940 if (flags & XDP_FLAGS_HW_MODE)
8941 return XDP_MODE_HW;
8942 if (flags & XDP_FLAGS_DRV_MODE)
8943 return XDP_MODE_DRV;
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07008944 if (flags & XDP_FLAGS_SKB_MODE)
8945 return XDP_MODE_SKB;
8946 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008947}
8948
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008949static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008950{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008951 switch (mode) {
8952 case XDP_MODE_SKB:
8953 return generic_xdp_install;
8954 case XDP_MODE_DRV:
8955 case XDP_MODE_HW:
8956 return dev->netdev_ops->ndo_bpf;
8957 default:
8958 return NULL;
8959 };
8960}
8961
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008962static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
8963 enum bpf_xdp_mode mode)
8964{
8965 return dev->xdp_state[mode].link;
8966}
8967
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008968static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
8969 enum bpf_xdp_mode mode)
8970{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008971 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
8972
8973 if (link)
8974 return link->link.prog;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008975 return dev->xdp_state[mode].prog;
8976}
8977
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01008978static u8 dev_xdp_prog_count(struct net_device *dev)
8979{
8980 u8 count = 0;
8981 int i;
8982
8983 for (i = 0; i < __MAX_XDP_MODE; i++)
8984 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
8985 count++;
8986 return count;
8987}
8988
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07008989u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
8990{
8991 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
8992
8993 return prog ? prog->aux->id : 0;
8994}
8995
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07008996static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
8997 struct bpf_xdp_link *link)
8998{
8999 dev->xdp_state[mode].link = link;
9000 dev->xdp_state[mode].prog = NULL;
9001}
9002
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009003static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
9004 struct bpf_prog *prog)
9005{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009006 dev->xdp_state[mode].link = NULL;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009007 dev->xdp_state[mode].prog = prog;
9008}
9009
9010static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
9011 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
9012 u32 flags, struct bpf_prog *prog)
9013{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07009014 struct netdev_bpf xdp;
Björn Töpel7e6897f2019-12-13 18:51:09 +01009015 int err;
9016
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009017 memset(&xdp, 0, sizeof(xdp));
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009018 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009019 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07009020 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009021 xdp.prog = prog;
9022
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009023 /* Drivers assume refcnt is already incremented (i.e, prog pointer is
9024 * "moved" into driver), so they don't increment it on their own, but
9025 * they do decrement refcnt when program is detached or replaced.
9026 * Given net_device also owns link/prog, we need to bump refcnt here
9027 * to prevent drivers from underflowing it.
9028 */
9029 if (prog)
9030 bpf_prog_inc(prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009031 err = bpf_op(dev, &xdp);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009032 if (err) {
9033 if (prog)
9034 bpf_prog_put(prog);
9035 return err;
9036 }
Björn Töpel7e6897f2019-12-13 18:51:09 +01009037
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009038 if (mode != XDP_MODE_HW)
9039 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
Björn Töpel7e6897f2019-12-13 18:51:09 +01009040
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009041 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02009042}
9043
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009044static void dev_xdp_uninstall(struct net_device *dev)
9045{
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009046 struct bpf_xdp_link *link;
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009047 struct bpf_prog *prog;
9048 enum bpf_xdp_mode mode;
9049 bpf_op_t bpf_op;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009050
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009051 ASSERT_RTNL();
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009052
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009053 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
9054 prog = dev_xdp_prog(dev, mode);
9055 if (!prog)
9056 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009057
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009058 bpf_op = dev_xdp_bpf_op(dev, mode);
9059 if (!bpf_op)
9060 continue;
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009061
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009062 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9063
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009064 /* auto-detach link from net device */
9065 link = dev_xdp_link(dev, mode);
9066 if (link)
9067 link->dev = NULL;
9068 else
9069 bpf_prog_put(prog);
9070
9071 dev_xdp_set_link(dev, mode, NULL);
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009072 }
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009073}
9074
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009075static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009076 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
9077 struct bpf_prog *old_prog, u32 flags)
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009078{
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009079 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009080 struct bpf_prog *cur_prog;
9081 enum bpf_xdp_mode mode;
9082 bpf_op_t bpf_op;
9083 int err;
9084
9085 ASSERT_RTNL();
9086
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009087 /* either link or prog attachment, never both */
9088 if (link && (new_prog || old_prog))
9089 return -EINVAL;
9090 /* link supports only XDP mode flags */
9091 if (link && (flags & ~XDP_FLAGS_MODES)) {
9092 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
9093 return -EINVAL;
9094 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009095 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
9096 if (num_modes > 1) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009097 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
9098 return -EINVAL;
9099 }
Toke Høiland-Jørgensen998f1722020-12-09 14:57:37 +01009100 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
9101 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
9102 NL_SET_ERR_MSG(extack,
9103 "More than one program loaded, unset mode is ambiguous");
9104 return -EINVAL;
9105 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009106 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
9107 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
9108 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
9109 return -EINVAL;
9110 }
9111
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009112 mode = dev_xdp_mode(dev, flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009113 /* can't replace attached link */
9114 if (dev_xdp_link(dev, mode)) {
9115 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
9116 return -EBUSY;
9117 }
9118
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009119 cur_prog = dev_xdp_prog(dev, mode);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009120 /* can't replace attached prog with link */
9121 if (link && cur_prog) {
9122 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
9123 return -EBUSY;
9124 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009125 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
9126 NL_SET_ERR_MSG(extack, "Active program does not match expected");
9127 return -EEXIST;
9128 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009129
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009130 /* put effective new program into new_prog */
9131 if (link)
9132 new_prog = link->link.prog;
9133
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009134 if (new_prog) {
9135 bool offload = mode == XDP_MODE_HW;
9136 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
9137 ? XDP_MODE_DRV : XDP_MODE_SKB;
9138
Andrii Nakryiko068d9d12020-08-11 19:29:23 -07009139 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
9140 NL_SET_ERR_MSG(extack, "XDP program already attached");
9141 return -EBUSY;
9142 }
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009143 if (!offload && dev_xdp_prog(dev, other_mode)) {
9144 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
9145 return -EEXIST;
9146 }
9147 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
9148 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
9149 return -EINVAL;
9150 }
9151 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
9152 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
9153 return -EINVAL;
9154 }
9155 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
9156 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
9157 return -EINVAL;
9158 }
9159 }
9160
9161 /* don't call drivers if the effective program didn't change */
9162 if (new_prog != cur_prog) {
9163 bpf_op = dev_xdp_bpf_op(dev, mode);
9164 if (!bpf_op) {
9165 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
9166 return -EOPNOTSUPP;
9167 }
9168
9169 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
9170 if (err)
9171 return err;
9172 }
9173
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009174 if (link)
9175 dev_xdp_set_link(dev, mode, link);
9176 else
9177 dev_xdp_set_prog(dev, mode, new_prog);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009178 if (cur_prog)
9179 bpf_prog_put(cur_prog);
9180
9181 return 0;
9182}
9183
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009184static int dev_xdp_attach_link(struct net_device *dev,
9185 struct netlink_ext_ack *extack,
9186 struct bpf_xdp_link *link)
9187{
9188 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
9189}
9190
9191static int dev_xdp_detach_link(struct net_device *dev,
9192 struct netlink_ext_ack *extack,
9193 struct bpf_xdp_link *link)
9194{
9195 enum bpf_xdp_mode mode;
9196 bpf_op_t bpf_op;
9197
9198 ASSERT_RTNL();
9199
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009200 mode = dev_xdp_mode(dev, link->flags);
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009201 if (dev_xdp_link(dev, mode) != link)
9202 return -EINVAL;
9203
9204 bpf_op = dev_xdp_bpf_op(dev, mode);
9205 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9206 dev_xdp_set_link(dev, mode, NULL);
9207 return 0;
9208}
9209
9210static void bpf_xdp_link_release(struct bpf_link *link)
9211{
9212 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9213
9214 rtnl_lock();
9215
9216 /* if racing with net_device's tear down, xdp_link->dev might be
9217 * already NULL, in which case link was already auto-detached
9218 */
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009219 if (xdp_link->dev) {
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009220 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009221 xdp_link->dev = NULL;
9222 }
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009223
9224 rtnl_unlock();
9225}
9226
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009227static int bpf_xdp_link_detach(struct bpf_link *link)
9228{
9229 bpf_xdp_link_release(link);
9230 return 0;
9231}
9232
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009233static void bpf_xdp_link_dealloc(struct bpf_link *link)
9234{
9235 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9236
9237 kfree(xdp_link);
9238}
9239
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009240static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
9241 struct seq_file *seq)
9242{
9243 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9244 u32 ifindex = 0;
9245
9246 rtnl_lock();
9247 if (xdp_link->dev)
9248 ifindex = xdp_link->dev->ifindex;
9249 rtnl_unlock();
9250
9251 seq_printf(seq, "ifindex:\t%u\n", ifindex);
9252}
9253
9254static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
9255 struct bpf_link_info *info)
9256{
9257 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9258 u32 ifindex = 0;
9259
9260 rtnl_lock();
9261 if (xdp_link->dev)
9262 ifindex = xdp_link->dev->ifindex;
9263 rtnl_unlock();
9264
9265 info->xdp.ifindex = ifindex;
9266 return 0;
9267}
9268
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009269static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9270 struct bpf_prog *old_prog)
9271{
9272 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9273 enum bpf_xdp_mode mode;
9274 bpf_op_t bpf_op;
9275 int err = 0;
9276
9277 rtnl_lock();
9278
9279 /* link might have been auto-released already, so fail */
9280 if (!xdp_link->dev) {
9281 err = -ENOLINK;
9282 goto out_unlock;
9283 }
9284
9285 if (old_prog && link->prog != old_prog) {
9286 err = -EPERM;
9287 goto out_unlock;
9288 }
9289 old_prog = link->prog;
9290 if (old_prog == new_prog) {
9291 /* no-op, don't disturb drivers */
9292 bpf_prog_put(new_prog);
9293 goto out_unlock;
9294 }
9295
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009296 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009297 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9298 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9299 xdp_link->flags, new_prog);
9300 if (err)
9301 goto out_unlock;
9302
9303 old_prog = xchg(&link->prog, new_prog);
9304 bpf_prog_put(old_prog);
9305
9306out_unlock:
9307 rtnl_unlock();
9308 return err;
9309}
9310
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009311static const struct bpf_link_ops bpf_xdp_link_lops = {
9312 .release = bpf_xdp_link_release,
9313 .dealloc = bpf_xdp_link_dealloc,
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07009314 .detach = bpf_xdp_link_detach,
Andrii Nakryikoc1931c92020-07-21 23:45:59 -07009315 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9316 .fill_link_info = bpf_xdp_link_fill_link_info,
Andrii Nakryiko026a4c22020-07-21 23:45:58 -07009317 .update_prog = bpf_xdp_link_update,
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009318};
9319
9320int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9321{
9322 struct net *net = current->nsproxy->net_ns;
9323 struct bpf_link_primer link_primer;
9324 struct bpf_xdp_link *link;
9325 struct net_device *dev;
9326 int err, fd;
9327
9328 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
9329 if (!dev)
9330 return -EINVAL;
9331
9332 link = kzalloc(sizeof(*link), GFP_USER);
9333 if (!link) {
9334 err = -ENOMEM;
9335 goto out_put_dev;
9336 }
9337
9338 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9339 link->dev = dev;
9340 link->flags = attr->link_create.flags;
9341
9342 err = bpf_link_prime(&link->link, &link_primer);
9343 if (err) {
9344 kfree(link);
9345 goto out_put_dev;
9346 }
9347
9348 rtnl_lock();
9349 err = dev_xdp_attach_link(dev, NULL, link);
9350 rtnl_unlock();
9351
9352 if (err) {
9353 bpf_link_cleanup(&link_primer);
9354 goto out_put_dev;
9355 }
9356
9357 fd = bpf_link_settle(&link_primer);
9358 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
9359 dev_put(dev);
9360 return fd;
9361
9362out_put_dev:
9363 dev_put(dev);
9364 return err;
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07009365}
9366
9367/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07009368 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
9369 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07009370 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07009371 * @fd: new program fd or negative value to clear
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009372 * @expected_fd: old program fd that userspace expects to replace or clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01009373 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07009374 *
9375 * Set or clear a bpf program for a device
9376 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07009377int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009378 int fd, int expected_fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07009379{
Andrii Nakryikoc8a36f12020-08-19 22:28:41 -07009380 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009381 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
Brenden Blancoa7862b42016-07-19 12:16:48 -07009382 int err;
9383
Daniel Borkmann85de8572016-11-28 23:16:54 +01009384 ASSERT_RTNL();
9385
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01009386 if (fd >= 0) {
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009387 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9388 mode != XDP_MODE_SKB);
9389 if (IS_ERR(new_prog))
9390 return PTR_ERR(new_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009391 }
9392
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009393 if (expected_fd >= 0) {
9394 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9395 mode != XDP_MODE_SKB);
9396 if (IS_ERR(old_prog)) {
9397 err = PTR_ERR(old_prog);
9398 old_prog = NULL;
9399 goto err_out;
9400 }
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07009401 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07009402
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07009403 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
Andrii Nakryikod4baa932020-07-21 23:45:56 -07009404
9405err_out:
9406 if (err && new_prog)
9407 bpf_prog_put(new_prog);
9408 if (old_prog)
9409 bpf_prog_put(old_prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07009410 return err;
9411}
Brenden Blancoa7862b42016-07-19 12:16:48 -07009412
9413/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07009415 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 *
9417 * Returns a suitable unique value for a new device interface
9418 * number. The caller must hold the rtnl semaphore or the
9419 * dev_base_lock to be sure it remains unique.
9420 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07009421static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009423 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11009424
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425 for (;;) {
9426 if (++ifindex <= 0)
9427 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009428 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00009429 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430 }
9431}
9432
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08009434static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07009435DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436
Stephen Hemminger6f05f622007-03-08 20:46:03 -08009437static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009440 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441}
9442
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009443static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009444{
Krishna Kumare93737b2009-12-08 22:26:02 +00009445 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009446 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009447
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009448 BUG_ON(dev_boot_phase);
9449 ASSERT_RTNL();
9450
Krishna Kumare93737b2009-12-08 22:26:02 +00009451 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009452 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00009453 * for initialization unwind. Remove those
9454 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009455 */
9456 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009457 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
9458 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009459
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009460 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00009461 list_del(&dev->unreg_list);
9462 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009463 }
Eric Dumazet449f4542011-05-19 12:24:16 +00009464 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009465 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00009466 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009467
Octavian Purdila44345722010-12-13 12:44:07 +00009468 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009469 list_for_each_entry(dev, head, unreg_list)
9470 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04009471 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009472
Octavian Purdila44345722010-12-13 12:44:07 +00009473 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009474 /* And unlink it from device chain. */
9475 unlist_netdevice(dev);
9476
9477 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009478 }
Eric Dumazet41852492016-08-26 12:50:39 -07009479 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009480
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009481 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009482
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009483 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009484 struct sk_buff *skb = NULL;
9485
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009486 /* Shutdown queueing discipline. */
9487 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009488
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08009489 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009490
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009491 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11009492 * this device. They should clean all the things.
9493 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009494 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
9495
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009496 if (!dev->rtnl_link_ops ||
9497 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04009498 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009499 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009500
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009501 /*
9502 * Flush the unicast and multicast chains
9503 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009504 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00009505 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009506
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02009507 netdev_name_node_alt_flush(dev);
Jiri Pirkoff927412019-09-30 11:48:15 +02009508 netdev_name_node_free(dev->name_node);
9509
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009510 if (dev->netdev_ops->ndo_uninit)
9511 dev->netdev_ops->ndo_uninit(dev);
9512
Mahesh Bandewar395eea62014-12-03 13:46:24 -08009513 if (skb)
9514 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07009515
Jiri Pirko9ff162a2013-01-03 22:48:49 +00009516 /* Notifier chain MUST detach us all upper devices. */
9517 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07009518 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009519
9520 /* Remove entries from kobject tree */
9521 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00009522#ifdef CONFIG_XPS
9523 /* Remove XPS queueing entries */
9524 netif_reset_xps_queues_gt(dev, 0);
9525#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009526 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009527
Eric W. Biederman850a5452011-10-13 22:25:23 +00009528 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009529
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00009530 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009531 dev_put(dev);
9532}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009533
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009534static void rollback_registered(struct net_device *dev)
9535{
9536 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009537
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009538 list_add(&dev->unreg_list, &single);
9539 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00009540 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009541}
9542
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009543static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9544 struct net_device *upper, netdev_features_t features)
9545{
9546 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9547 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009548 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009549
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009550 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009551 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009552 if (!(upper->wanted_features & feature)
9553 && (features & feature)) {
9554 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9555 &feature, upper->name);
9556 features &= ~feature;
9557 }
9558 }
9559
9560 return features;
9561}
9562
9563static void netdev_sync_lower_features(struct net_device *upper,
9564 struct net_device *lower, netdev_features_t features)
9565{
9566 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9567 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009568 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009569
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01009570 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05009571 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009572 if (!(features & feature) && (lower->features & feature)) {
9573 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9574 &feature, lower->name);
9575 lower->wanted_features &= ~feature;
Cong Wangdd912302020-05-07 12:19:03 -07009576 __netdev_update_features(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009577
9578 if (unlikely(lower->features & feature))
9579 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9580 &feature, lower->name);
Cong Wangdd912302020-05-07 12:19:03 -07009581 else
9582 netdev_features_change(lower);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009583 }
9584 }
9585}
9586
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009587static netdev_features_t netdev_fix_features(struct net_device *dev,
9588 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07009589{
Michał Mirosław57422dc2011-01-22 12:14:12 +00009590 /* Fix illegal checksum combinations */
9591 if ((features & NETIF_F_HW_CSUM) &&
9592 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009593 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00009594 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9595 }
9596
Herbert Xub63365a2008-10-23 01:11:29 -07009597 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009598 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009599 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00009600 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07009601 }
9602
Pravin B Shelarec5f0612013-03-07 09:28:01 +00009603 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9604 !(features & NETIF_F_IP_CSUM)) {
9605 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9606 features &= ~NETIF_F_TSO;
9607 features &= ~NETIF_F_TSO_ECN;
9608 }
9609
9610 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9611 !(features & NETIF_F_IPV6_CSUM)) {
9612 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9613 features &= ~NETIF_F_TSO6;
9614 }
9615
Alexander Duyckb1dc4972016-05-02 09:38:24 -07009616 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
9617 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9618 features &= ~NETIF_F_TSO_MANGLEID;
9619
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00009620 /* TSO ECN requires that TSO is present as well. */
9621 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9622 features &= ~NETIF_F_TSO_ECN;
9623
Michał Mirosław212b5732011-02-15 16:59:16 +00009624 /* Software GSO depends on SG. */
9625 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04009626 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00009627 features &= ~NETIF_F_GSO;
9628 }
9629
Alexander Duyck802ab552016-04-10 21:45:03 -04009630 /* GSO partial features require GSO partial be set */
9631 if ((features & dev->gso_partial_features) &&
9632 !(features & NETIF_F_GSO_PARTIAL)) {
9633 netdev_dbg(dev,
9634 "Dropping partially supported GSO features since no GSO partial.\n");
9635 features &= ~dev->gso_partial_features;
9636 }
9637
Michael Chanfb1f5f72017-12-16 03:09:40 -05009638 if (!(features & NETIF_F_RXCSUM)) {
9639 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
9640 * successfully merged by hardware must also have the
9641 * checksum verified by hardware. If the user does not
9642 * want to enable RXCSUM, logically, we should disable GRO_HW.
9643 */
9644 if (features & NETIF_F_GRO_HW) {
9645 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9646 features &= ~NETIF_F_GRO_HW;
9647 }
9648 }
9649
Gal Pressmande8d5ab2018-03-12 11:48:49 +02009650 /* LRO/HW-GRO features cannot be combined with RX-FCS */
9651 if (features & NETIF_F_RXFCS) {
9652 if (features & NETIF_F_LRO) {
9653 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9654 features &= ~NETIF_F_LRO;
9655 }
9656
9657 if (features & NETIF_F_GRO_HW) {
9658 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9659 features &= ~NETIF_F_GRO_HW;
9660 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02009661 }
9662
Tariq Toukanf0f3d3e2021-01-17 17:15:38 +02009663 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
9664 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
9665 features &= ~NETIF_F_HW_TLS_RX;
9666 }
9667
Herbert Xub63365a2008-10-23 01:11:29 -07009668 return features;
9669}
Herbert Xub63365a2008-10-23 01:11:29 -07009670
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009671int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00009672{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009673 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009674 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009675 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05009676 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00009677
Michał Mirosław87267482011-04-12 09:56:38 +00009678 ASSERT_RTNL();
9679
Michał Mirosław5455c692011-02-15 16:59:17 +00009680 features = netdev_get_wanted_features(dev);
9681
9682 if (dev->netdev_ops->ndo_fix_features)
9683 features = dev->netdev_ops->ndo_fix_features(dev, features);
9684
9685 /* driver might be less strict about feature dependencies */
9686 features = netdev_fix_features(dev, features);
9687
Randy Dunlap4250b752020-09-17 21:35:15 -07009688 /* some features can't be enabled if they're off on an upper device */
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009689 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9690 features = netdev_sync_upper_features(dev, upper, features);
9691
Michał Mirosław5455c692011-02-15 16:59:17 +00009692 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05009693 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00009694
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009695 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9696 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00009697
9698 if (dev->netdev_ops->ndo_set_features)
9699 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01009700 else
9701 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00009702
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009703 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00009704 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009705 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9706 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01009707 /* return non-0 since some features might have changed and
9708 * it's better to fire a spurious notification than miss it
9709 */
9710 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009711 }
9712
Jarod Wilsone7868a82015-11-03 23:09:32 -05009713sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05009714 /* some features must be disabled on lower devices when disabled
9715 * on an upper device (think: bonding master or bridge)
9716 */
9717 netdev_for_each_lower_dev(dev, lower, iter)
9718 netdev_sync_lower_features(dev, lower, features);
9719
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009720 if (!err) {
9721 netdev_features_t diff = features ^ dev->features;
9722
9723 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9724 /* udp_tunnel_{get,drop}_rx_info both need
9725 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9726 * device, or they won't do anything.
9727 * Thus we need to update dev->features
9728 * *before* calling udp_tunnel_get_rx_info,
9729 * but *after* calling udp_tunnel_drop_rx_info.
9730 */
9731 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9732 dev->features = features;
9733 udp_tunnel_get_rx_info(dev);
9734 } else {
9735 udp_tunnel_drop_rx_info(dev);
9736 }
9737 }
9738
Gal Pressman9daae9b2018-03-28 17:46:54 +03009739 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9740 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9741 dev->features = features;
9742 err |= vlan_get_rx_ctag_filter_info(dev);
9743 } else {
9744 vlan_drop_rx_ctag_filter_info(dev);
9745 }
9746 }
9747
9748 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9749 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9750 dev->features = features;
9751 err |= vlan_get_rx_stag_filter_info(dev);
9752 } else {
9753 vlan_drop_rx_stag_filter_info(dev);
9754 }
9755 }
9756
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009757 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009758 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009759
Jarod Wilsone7868a82015-11-03 23:09:32 -05009760 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009761}
9762
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009763/**
9764 * netdev_update_features - recalculate device features
9765 * @dev: the device to check
9766 *
9767 * Recalculate dev->features set and send notifications if it
9768 * has changed. Should be called after driver or hardware dependent
9769 * conditions might have changed that influence the features.
9770 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009771void netdev_update_features(struct net_device *dev)
9772{
9773 if (__netdev_update_features(dev))
9774 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009775}
9776EXPORT_SYMBOL(netdev_update_features);
9777
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009779 * netdev_change_features - recalculate device features
9780 * @dev: the device to check
9781 *
9782 * Recalculate dev->features set and send notifications even
9783 * if they have not changed. Should be called instead of
9784 * netdev_update_features() if also dev->vlan_features might
9785 * have changed to allow the changes to be propagated to stacked
9786 * VLAN devices.
9787 */
9788void netdev_change_features(struct net_device *dev)
9789{
9790 __netdev_update_features(dev);
9791 netdev_features_change(dev);
9792}
9793EXPORT_SYMBOL(netdev_change_features);
9794
9795/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009796 * netif_stacked_transfer_operstate - transfer operstate
9797 * @rootdev: the root or lower level device to transfer state from
9798 * @dev: the device to transfer operstate to
9799 *
9800 * Transfer operational state from root to device. This is normally
9801 * called when a stacking relationship exists between the root
9802 * device and the device(a leaf device).
9803 */
9804void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9805 struct net_device *dev)
9806{
9807 if (rootdev->operstate == IF_OPER_DORMANT)
9808 netif_dormant_on(dev);
9809 else
9810 netif_dormant_off(dev);
9811
Andrew Lunneec517cd2020-04-20 00:11:50 +02009812 if (rootdev->operstate == IF_OPER_TESTING)
9813 netif_testing_on(dev);
9814 else
9815 netif_testing_off(dev);
9816
Zhang Shengju0575c862017-04-26 17:49:38 +08009817 if (netif_carrier_ok(rootdev))
9818 netif_carrier_on(dev);
9819 else
9820 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009821}
9822EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9823
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009824static int netif_alloc_rx_queues(struct net_device *dev)
9825{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009826 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009827 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309828 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009829 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009830
Tom Herbertbd25fa72010-10-18 18:00:16 +00009831 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009832
Michal Hockodcda9b02017-07-12 14:36:45 -07009833 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009834 if (!rx)
9835 return -ENOMEM;
9836
Tom Herbertbd25fa72010-10-18 18:00:16 +00009837 dev->_rx = rx;
9838
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009839 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009840 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009841
9842 /* XDP RX-queue setup */
9843 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
9844 if (err < 0)
9845 goto err_rxq_info;
9846 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009847 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009848
9849err_rxq_info:
9850 /* Rollback successful reg's and free other resources */
9851 while (i--)
9852 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009853 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009854 dev->_rx = NULL;
9855 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009856}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009857
9858static void netif_free_rx_queues(struct net_device *dev)
9859{
9860 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009861
9862 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9863 if (!dev->_rx)
9864 return;
9865
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009866 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009867 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9868
9869 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009870}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009871
Changli Gaoaa942102010-12-04 02:31:41 +00009872static void netdev_init_one_queue(struct net_device *dev,
9873 struct netdev_queue *queue, void *_unused)
9874{
9875 /* Initialize queue lock */
9876 spin_lock_init(&queue->_xmit_lock);
Cong Wang1a33e102020-05-02 22:22:19 -07009877 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
Changli Gaoaa942102010-12-04 02:31:41 +00009878 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009879 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009880 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009881#ifdef CONFIG_BQL
9882 dql_init(&queue->dql, HZ);
9883#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009884}
9885
Eric Dumazet60877a32013-06-20 01:15:51 -07009886static void netif_free_tx_queues(struct net_device *dev)
9887{
WANG Cong4cb28972014-06-02 15:55:22 -07009888 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009889}
9890
Tom Herberte6484932010-10-18 18:04:39 +00009891static int netif_alloc_netdev_queues(struct net_device *dev)
9892{
9893 unsigned int count = dev->num_tx_queues;
9894 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009895 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009896
Eric Dumazetd3397272015-07-06 17:13:26 +02009897 if (count < 1 || count > 0xffff)
9898 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009899
Michal Hockodcda9b02017-07-12 14:36:45 -07009900 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009901 if (!tx)
9902 return -ENOMEM;
9903
Tom Herberte6484932010-10-18 18:04:39 +00009904 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009905
Tom Herberte6484932010-10-18 18:04:39 +00009906 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9907 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009908
9909 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009910}
9911
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009912void netif_tx_stop_all_queues(struct net_device *dev)
9913{
9914 unsigned int i;
9915
9916 for (i = 0; i < dev->num_tx_queues; i++) {
9917 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009918
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009919 netif_tx_stop_queue(txq);
9920 }
9921}
9922EXPORT_SYMBOL(netif_tx_stop_all_queues);
9923
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009924/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925 * register_netdevice - register a network device
9926 * @dev: device to register
9927 *
9928 * Take a completed network device structure and add it to the kernel
9929 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9930 * chain. 0 is returned on success. A negative errno code is returned
9931 * on a failure to set up the device, or if the name is a duplicate.
9932 *
9933 * Callers must hold the rtnl semaphore. You may want
9934 * register_netdev() instead of this.
9935 *
9936 * BUGS:
9937 * The locking appears insufficient to guarantee two parallel registers
9938 * will not get the same name.
9939 */
9940
9941int register_netdevice(struct net_device *dev)
9942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009944 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009945
Florian Fainellie283de32018-04-30 14:20:05 -07009946 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9947 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948 BUG_ON(dev_boot_phase);
9949 ASSERT_RTNL();
9950
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009951 might_sleep();
9952
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 /* When net_device's are persistent, this will be fatal. */
9954 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009955 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956
Jakub Kicinski9000edb2020-03-16 13:47:12 -07009957 ret = ethtool_check_ops(dev->ethtool_ops);
9958 if (ret)
9959 return ret;
9960
David S. Millerf1f28aa2008-07-15 00:08:33 -07009961 spin_lock_init(&dev->addr_list_lock);
Cong Wang845e0eb2020-06-08 14:53:01 -07009962 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963
Gao feng828de4f2012-09-13 20:58:27 +00009964 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00009965 if (ret < 0)
9966 goto out;
9967
Eric Dumazet9077f052019-10-03 08:59:24 -07009968 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +02009969 dev->name_node = netdev_name_node_head_alloc(dev);
9970 if (!dev->name_node)
9971 goto out;
9972
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009974 if (dev->netdev_ops->ndo_init) {
9975 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009976 if (ret) {
9977 if (ret > 0)
9978 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009979 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 }
9981 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009982
Patrick McHardyf6469682013-04-19 02:04:27 +00009983 if (((dev->hw_features | dev->features) &
9984 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00009985 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9986 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9987 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9988 ret = -EINVAL;
9989 goto err_uninit;
9990 }
9991
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00009992 ret = -EBUSY;
9993 if (!dev->ifindex)
9994 dev->ifindex = dev_new_index(net);
9995 else if (__dev_get_by_index(net, dev->ifindex))
9996 goto err_uninit;
9997
Michał Mirosław5455c692011-02-15 16:59:17 +00009998 /* Transfer changeable features to wanted_features and enable
9999 * software offloads (GSO and GRO).
10000 */
Steffen Klassert1a3c9982020-01-25 11:26:43 +010010001 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
Michał Mirosław14d12322011-02-22 16:52:28 +000010002 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +020010003
10004 if (dev->netdev_ops->ndo_udp_tunnel_add) {
10005 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10006 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10007 }
10008
Michał Mirosław14d12322011-02-22 16:52:28 +000010009 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010011 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +000010012 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010013
Alexander Duyck7f348a62016-04-20 16:51:00 -040010014 /* If IPv4 TCP segmentation offload is supported we should also
10015 * allow the device to enable segmenting the frame with the option
10016 * of ignoring a static IP ID value. This doesn't enable the
10017 * feature itself but allows the user to enable it later.
10018 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -040010019 if (dev->hw_features & NETIF_F_TSO)
10020 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -040010021 if (dev->vlan_features & NETIF_F_TSO)
10022 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
10023 if (dev->mpls_features & NETIF_F_TSO)
10024 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
10025 if (dev->hw_enc_features & NETIF_F_TSO)
10026 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -070010027
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010028 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +000010029 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -070010030 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +000010031
Pravin B Shelaree579672013-03-07 09:28:08 +000010032 /* Make NETIF_F_SG inheritable to tunnel devices.
10033 */
Alexander Duyck802ab552016-04-10 21:45:03 -040010034 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +000010035
Simon Horman0d89d202013-05-23 21:02:52 +000010036 /* Make NETIF_F_SG inheritable to MPLS.
10037 */
10038 dev->mpls_features |= NETIF_F_SG;
10039
Johannes Berg7ffbe3f2009-10-02 05:15:27 +000010040 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
10041 ret = notifier_to_errno(ret);
10042 if (ret)
10043 goto err_uninit;
10044
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010045 ret = netdev_register_kobject(dev);
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010046 if (ret) {
10047 dev->reg_state = NETREG_UNREGISTERED;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010048 goto err_uninit;
Jouni Hogandercb626bf2020-01-20 09:51:03 +020010049 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010050 dev->reg_state = NETREG_REGISTERED;
10051
Michał Mirosław6cb6a272011-04-02 22:48:47 -070010052 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +000010053
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054 /*
10055 * Default initial state at registry is that the
10056 * device is present.
10057 */
10058
10059 set_bit(__LINK_STATE_PRESENT, &dev->state);
10060
Ben Hutchings8f4cccb2012-08-20 22:16:51 +010010061 linkwatch_init_dev(dev);
10062
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010065 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -040010066 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067
Jiri Pirko948b3372013-01-08 01:38:25 +000010068 /* If the device has permanent device address, driver should
10069 * set dev_addr and also addr_assign_type should be set to
10070 * NET_ADDR_PERM (default value).
10071 */
10072 if (dev->addr_assign_type == NET_ADDR_PERM)
10073 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
10074
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010076 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -070010077 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010078 if (ret) {
10079 rollback_registered(dev);
Subash Abhinov Kasiviswanathan10cc5142019-09-10 14:02:57 -060010080 rcu_barrier();
10081
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010082 dev->reg_state = NETREG_UNREGISTERED;
Yang Yingliang814152a2020-06-16 09:39:21 +000010083 /* We should put the kobject that hold in
10084 * netdev_unregister_kobject(), otherwise
10085 * the net device cannot be freed when
10086 * driver calls free_netdev(), because the
10087 * kobject is being hold.
10088 */
10089 kobject_put(&dev->dev.kobj);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -070010090 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010091 /*
10092 * Prevent userspace races by waiting until the network
10093 * device is fully setup before sending notifications.
10094 */
Patrick McHardya2835762010-02-26 06:34:51 +000010095 if (!dev->rtnl_link_ops ||
10096 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010097 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
10099out:
10100 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010101
10102err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -080010103 if (dev->netdev_ops->ndo_uninit)
10104 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -040010105 if (dev->priv_destructor)
10106 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +030010107err_free_name:
10108 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -070010109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010111EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112
10113/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010114 * init_dummy_netdev - init a dummy network device for NAPI
10115 * @dev: device to init
10116 *
10117 * This takes a network device structure and initialize the minimum
10118 * amount of fields so it can be used to schedule NAPI polls without
10119 * registering a full blown interface. This is to be used by drivers
10120 * that need to tie several hardware interfaces to a single NAPI
10121 * poll scheduler due to HW limitations.
10122 */
10123int init_dummy_netdev(struct net_device *dev)
10124{
10125 /* Clear everything. Note we don't initialize spinlocks
10126 * are they aren't supposed to be taken by any of the
10127 * NAPI code and this dummy netdev is supposed to be
10128 * only ever used for NAPI polls
10129 */
10130 memset(dev, 0, sizeof(struct net_device));
10131
10132 /* make sure we BUG if trying to hit standard
10133 * register/unregister code path
10134 */
10135 dev->reg_state = NETREG_DUMMY;
10136
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010137 /* NAPI wants this */
10138 INIT_LIST_HEAD(&dev->napi_list);
10139
10140 /* a dummy interface is started by default */
10141 set_bit(__LINK_STATE_PRESENT, &dev->state);
10142 set_bit(__LINK_STATE_START, &dev->state);
10143
Josh Elsasser35edfdc2019-01-26 14:38:33 -080010144 /* napi_busy_loop stats accounting wants this */
10145 dev_net_set(dev, &init_net);
10146
Eric Dumazet29b44332010-10-11 10:22:12 +000010147 /* Note : We dont allocate pcpu_refcnt for dummy devices,
10148 * because users of this 'device' dont need to change
10149 * its refcount.
10150 */
10151
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -080010152 return 0;
10153}
10154EXPORT_SYMBOL_GPL(init_dummy_netdev);
10155
10156
10157/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 * register_netdev - register a network device
10159 * @dev: device to register
10160 *
10161 * Take a completed network device structure and add it to the kernel
10162 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10163 * chain. 0 is returned on success. A negative errno code is returned
10164 * on a failure to set up the device, or if the name is a duplicate.
10165 *
Borislav Petkov38b4da32007-04-20 22:14:10 -070010166 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 * and expands the device name if you passed a format string to
10168 * alloc_netdev.
10169 */
10170int register_netdev(struct net_device *dev)
10171{
10172 int err;
10173
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +030010174 if (rtnl_lock_killable())
10175 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 rtnl_unlock();
10178 return err;
10179}
10180EXPORT_SYMBOL(register_netdev);
10181
Eric Dumazet29b44332010-10-11 10:22:12 +000010182int netdev_refcnt_read(const struct net_device *dev)
10183{
10184 int i, refcnt = 0;
10185
10186 for_each_possible_cpu(i)
10187 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
10188 return refcnt;
10189}
10190EXPORT_SYMBOL(netdev_refcnt_read);
10191
Mauro Carvalho Chehabde2b5412020-09-22 13:22:52 +020010192#define WAIT_REFS_MIN_MSECS 1
10193#define WAIT_REFS_MAX_MSECS 250
Ben Hutchings2c530402012-07-10 10:55:09 +000010194/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +000010196 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 *
10198 * This is called when unregistering network devices.
10199 *
10200 * Any protocol or device that holds a reference should register
10201 * for netdevice notification, and cleanup and put back the
10202 * reference if they receive an UNREGISTER event.
10203 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010204 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 */
10206static void netdev_wait_allrefs(struct net_device *dev)
10207{
10208 unsigned long rebroadcast_time, warning_time;
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010209 int wait = 0, refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210
Eric Dumazete014deb2009-11-17 05:59:21 +000010211 linkwatch_forget_dev(dev);
10212
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +000010214 refcnt = netdev_refcnt_read(dev);
10215
10216 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010218 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219
10220 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -070010221 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222
Eric Dumazet748e2d92012-08-22 21:50:59 +000010223 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010224 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +000010225 rtnl_lock();
10226
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
10228 &dev->state)) {
10229 /* We must not have linkwatch events
10230 * pending on unregister. If this
10231 * happens, we simply run the queue
10232 * unscheduled, resulting in a noop
10233 * for this device.
10234 */
10235 linkwatch_run_queue();
10236 }
10237
Stephen Hemminger6756ae42006-03-20 22:23:58 -080010238 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239
10240 rebroadcast_time = jiffies;
10241 }
10242
Francesco Ruggeri0e4be9e2020-09-18 13:19:01 -070010243 if (!wait) {
10244 rcu_barrier();
10245 wait = WAIT_REFS_MIN_MSECS;
10246 } else {
10247 msleep(wait);
10248 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
10249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010250
Eric Dumazet29b44332010-10-11 10:22:12 +000010251 refcnt = netdev_refcnt_read(dev);
10252
Eric Dumazetd7c04b02019-05-16 08:09:57 -070010253 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010254 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
10255 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010256 warning_time = jiffies;
10257 }
10258 }
10259}
10260
10261/* The sequence is:
10262 *
10263 * rtnl_lock();
10264 * ...
10265 * register_netdevice(x1);
10266 * register_netdevice(x2);
10267 * ...
10268 * unregister_netdevice(y1);
10269 * unregister_netdevice(y2);
10270 * ...
10271 * rtnl_unlock();
10272 * free_netdev(y1);
10273 * free_netdev(y2);
10274 *
Herbert Xu58ec3b42008-10-07 15:50:03 -070010275 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010277 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 * without deadlocking with linkwatch via keventd.
10279 * 2) Since we run with the RTNL semaphore not held, we can sleep
10280 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -070010281 *
10282 * We must not return until all unregister events added during
10283 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285void netdev_run_todo(void)
10286{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010287 struct list_head list;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010288#ifdef CONFIG_LOCKDEP
10289 struct list_head unlink_list;
10290
10291 list_replace_init(&net_unlink_list, &unlink_list);
10292
10293 while (!list_empty(&unlink_list)) {
10294 struct net_device *dev = list_first_entry(&unlink_list,
10295 struct net_device,
10296 unlink_list);
Taehee Yoo0e8b8d62020-10-15 16:26:06 +000010297 list_del_init(&dev->unlink_list);
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010298 dev->nested_level = dev->lower_level - 1;
10299 }
10300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010303 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -070010304
10305 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070010306
Eric Dumazet0115e8e2012-08-22 17:19:46 +000010307
10308 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +000010309 if (!list_empty(&list))
10310 rcu_barrier();
10311
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 while (!list_empty(&list)) {
10313 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +000010314 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315 list_del(&dev->todo_list);
10316
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010317 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010318 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010320 dump_stack();
10321 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010322 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010323
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -070010324 dev->reg_state = NETREG_UNREGISTERED;
10325
10326 netdev_wait_allrefs(dev);
10327
10328 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +000010329 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -080010330 BUG_ON(!list_empty(&dev->ptype_all));
10331 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +000010332 WARN_ON(rcu_access_pointer(dev->ip_ptr));
10333 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -080010334#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -070010335 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -080010336#endif
David S. Millercf124db2017-05-08 12:52:56 -040010337 if (dev->priv_destructor)
10338 dev->priv_destructor(dev);
10339 if (dev->needs_free_netdev)
10340 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010341
Eric W. Biederman50624c92013-09-23 21:19:49 -070010342 /* Report a network device has been unregistered */
10343 rtnl_lock();
10344 dev_net(dev)->dev_unreg_count--;
10345 __rtnl_unlock();
10346 wake_up(&netdev_unregistering_wq);
10347
Stephen Hemminger9093bbb2007-05-19 15:39:25 -070010348 /* Free network device */
10349 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351}
10352
Jarod Wilson92566452016-02-01 18:51:04 -050010353/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
10354 * all the same fields in the same order as net_device_stats, with only
10355 * the type differing, but rtnl_link_stats64 may have additional fields
10356 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +000010357 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010358void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
10359 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +000010360{
10361#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -050010362 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +020010363 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -050010364 /* zero out counters that only exist in rtnl_link_stats64 */
10365 memset((char *)stats64 + sizeof(*netdev_stats), 0,
10366 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010367#else
Jarod Wilson92566452016-02-01 18:51:04 -050010368 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010369 const unsigned long *src = (const unsigned long *)netdev_stats;
10370 u64 *dst = (u64 *)stats64;
10371
Jarod Wilson92566452016-02-01 18:51:04 -050010372 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010373 for (i = 0; i < n; i++)
10374 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -050010375 /* zero out counters that only exist in rtnl_link_stats64 */
10376 memset((char *)stats64 + n * sizeof(u64), 0,
10377 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +000010378#endif
10379}
Eric Dumazet77a1abf2012-03-05 04:50:09 +000010380EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +000010381
Eric Dumazetd83345a2009-11-16 03:36:51 +000010382/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010383 * dev_get_stats - get network device statistics
10384 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -070010385 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010386 *
Ben Hutchingsd7753512010-07-09 09:12:41 +000010387 * Get network statistics from device. Return @storage.
10388 * The device driver may provide its own method by setting
10389 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
10390 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010391 */
Ben Hutchingsd7753512010-07-09 09:12:41 +000010392struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10393 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +000010394{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010395 const struct net_device_ops *ops = dev->netdev_ops;
10396
Eric Dumazet28172732010-07-07 14:58:56 -070010397 if (ops->ndo_get_stats64) {
10398 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010399 ops->ndo_get_stats64(dev, storage);
10400 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +000010401 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +000010402 } else {
10403 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -070010404 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -070010405 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10406 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10407 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -070010408 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -070010409}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080010410EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -070010411
Heiner Kallweit44fa32f2020-10-12 10:01:27 +020010412/**
10413 * dev_fetch_sw_netstats - get per-cpu network device statistics
10414 * @s: place to store stats
10415 * @netstats: per-cpu network stats to read from
10416 *
10417 * Read per-cpu network statistics and populate the related fields in @s.
10418 */
10419void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10420 const struct pcpu_sw_netstats __percpu *netstats)
10421{
10422 int cpu;
10423
10424 for_each_possible_cpu(cpu) {
10425 const struct pcpu_sw_netstats *stats;
10426 struct pcpu_sw_netstats tmp;
10427 unsigned int start;
10428
10429 stats = per_cpu_ptr(netstats, cpu);
10430 do {
10431 start = u64_stats_fetch_begin_irq(&stats->syncp);
10432 tmp.rx_packets = stats->rx_packets;
10433 tmp.rx_bytes = stats->rx_bytes;
10434 tmp.tx_packets = stats->tx_packets;
10435 tmp.tx_bytes = stats->tx_bytes;
10436 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10437
10438 s->rx_packets += tmp.rx_packets;
10439 s->rx_bytes += tmp.rx_bytes;
10440 s->tx_packets += tmp.tx_packets;
10441 s->tx_bytes += tmp.tx_bytes;
10442 }
10443}
10444EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10445
Eric Dumazet24824a02010-10-02 06:11:55 +000010446struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -070010447{
Eric Dumazet24824a02010-10-02 06:11:55 +000010448 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -070010449
Eric Dumazet24824a02010-10-02 06:11:55 +000010450#ifdef CONFIG_NET_CLS_ACT
10451 if (queue)
10452 return queue;
10453 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10454 if (!queue)
10455 return NULL;
10456 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -080010457 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +000010458 queue->qdisc_sleeping = &noop_qdisc;
10459 rcu_assign_pointer(dev->ingress_queue, queue);
10460#endif
10461 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -070010462}
10463
Eric Dumazet2c60db02012-09-16 09:17:26 +000010464static const struct ethtool_ops default_ethtool_ops;
10465
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +000010466void netdev_set_default_ethtool_ops(struct net_device *dev,
10467 const struct ethtool_ops *ops)
10468{
10469 if (dev->ethtool_ops == &default_ethtool_ops)
10470 dev->ethtool_ops = ops;
10471}
10472EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10473
Eric Dumazet74d332c2013-10-30 13:10:44 -070010474void netdev_freemem(struct net_device *dev)
10475{
10476 char *addr = (char *)dev - dev->padded;
10477
WANG Cong4cb28972014-06-02 15:55:22 -070010478 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010479}
10480
Linus Torvalds1da177e2005-04-16 15:20:36 -070010481/**
tcharding722c9a02017-02-09 17:56:04 +110010482 * alloc_netdev_mqs - allocate network device
10483 * @sizeof_priv: size of private data to allocate space for
10484 * @name: device name format string
10485 * @name_assign_type: origin of device name
10486 * @setup: callback to initialize device
10487 * @txqs: the number of TX subqueues to allocate
10488 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 *
tcharding722c9a02017-02-09 17:56:04 +110010490 * Allocates a struct net_device with private data area for driver use
10491 * and performs basic initialization. Also allocates subqueue structs
10492 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493 */
Tom Herbert36909ea2011-01-09 19:36:31 +000010494struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +020010495 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +000010496 void (*setup)(struct net_device *),
10497 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010498{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +030010500 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010501 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010502
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -070010503 BUG_ON(strlen(name) >= sizeof(dev->name));
10504
Tom Herbert36909ea2011-01-09 19:36:31 +000010505 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010506 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +000010507 return NULL;
10508 }
10509
Tom Herbert36909ea2011-01-09 19:36:31 +000010510 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010511 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +000010512 return NULL;
10513 }
Tom Herbert36909ea2011-01-09 19:36:31 +000010514
David S. Millerfd2ea0a2008-07-17 01:56:23 -070010515 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010516 if (sizeof_priv) {
10517 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010518 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -070010519 alloc_size += sizeof_priv;
10520 }
10521 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010522 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010523
Michal Hockodcda9b02017-07-12 14:36:45 -070010524 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +000010525 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +000010528 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010530
Eric Dumazet29b44332010-10-11 10:22:12 +000010531 dev->pcpu_refcnt = alloc_percpu(int);
10532 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -070010533 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010534
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +000010536 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010537
Jiri Pirko22bedad32010-04-01 21:22:57 +000010538 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010539 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +000010540
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010541 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -070010543 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +000010544 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +000010545 dev->upper_level = 1;
10546 dev->lower_level = 1;
Taehee Yoo1fc70ed2020-09-25 18:13:29 +000010547#ifdef CONFIG_LOCKDEP
10548 dev->nested_level = 0;
10549 INIT_LIST_HEAD(&dev->unlink_list);
10550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551
Herbert Xud565b0a2008-12-15 23:38:52 -080010552 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010553 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -070010554 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +000010555 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +020010556 INIT_LIST_HEAD(&dev->adj_list.upper);
10557 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -080010558 INIT_LIST_HEAD(&dev->ptype_all);
10559 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Pirko93642e12020-01-25 12:17:08 +010010560 INIT_LIST_HEAD(&dev->net_notifier_list);
Jiri Kosina59cc1f62016-08-10 11:05:15 +020010561#ifdef CONFIG_NET_SCHED
10562 hash_init(dev->qdisc_hash);
10563#endif
Eric Dumazet02875872014-10-05 18:38:35 -070010564 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010566
Phil Suttera8131042016-02-17 15:37:43 +010010567 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +020010568 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +010010569 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +010010570 }
Phil Sutter906470c2015-08-18 10:30:48 +020010571
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010572 dev->num_tx_queues = txqs;
10573 dev->real_num_tx_queues = txqs;
10574 if (netif_alloc_netdev_queues(dev))
10575 goto free_all;
10576
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010577 dev->num_rx_queues = rxqs;
10578 dev->real_num_rx_queues = rxqs;
10579 if (netif_alloc_rx_queues(dev))
10580 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010581
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +020010583 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +000010584 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +000010585 if (!dev->ethtool_ops)
10586 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +020010587
Daniel Borkmann357b6cc2020-03-18 10:33:22 +010010588 nf_hook_ingress_init(dev);
Pablo Neirae687ad62015-05-13 18:19:38 +020010589
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010591
David S. Miller8d3bdbd2011-02-08 15:02:50 -080010592free_all:
10593 free_netdev(dev);
10594 return NULL;
10595
Eric Dumazet29b44332010-10-11 10:22:12 +000010596free_pcpu:
10597 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -070010598free_dev:
10599 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +000010600 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601}
Tom Herbert36909ea2011-01-09 19:36:31 +000010602EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010603
10604/**
tcharding722c9a02017-02-09 17:56:04 +110010605 * free_netdev - free network device
10606 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -070010607 *
tcharding722c9a02017-02-09 17:56:04 +110010608 * This function does the last stage of destroying an allocated device
10609 * interface. The reference to the device object is released. If this
10610 * is the last reference then it will be freed.Must be called in process
10611 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010612 */
10613void free_netdev(struct net_device *dev)
10614{
Herbert Xud565b0a2008-12-15 23:38:52 -080010615 struct napi_struct *p, *n;
10616
Eric Dumazet93d05d42015-11-18 06:31:03 -080010617 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -070010618 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +010010619 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -070010620
Eric Dumazet33d480c2011-08-11 19:30:52 +000010621 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +000010622
Jiri Pirkof001fde2009-05-05 02:48:28 +000010623 /* Flush device addresses */
10624 dev_addr_flush(dev);
10625
Herbert Xud565b0a2008-12-15 23:38:52 -080010626 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10627 netif_napi_del(p);
10628
Eric Dumazet29b44332010-10-11 10:22:12 +000010629 free_percpu(dev->pcpu_refcnt);
10630 dev->pcpu_refcnt = NULL;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010010631 free_percpu(dev->xdp_bulkq);
10632 dev->xdp_bulkq = NULL;
Eric Dumazet29b44332010-10-11 10:22:12 +000010633
Stephen Hemminger3041a062006-05-26 13:25:24 -070010634 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010635 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -070010636 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010637 return;
10638 }
10639
10640 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10641 dev->reg_state = NETREG_RELEASED;
10642
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070010643 /* will free via device release */
10644 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010646EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010647
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010648/**
10649 * synchronize_net - Synchronize with packet receive processing
10650 *
10651 * Wait for packets currently being received to be done.
10652 * Does not block later packets from starting.
10653 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090010654void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010655{
10656 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +000010657 if (rtnl_is_locked())
10658 synchronize_rcu_expedited();
10659 else
10660 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010661}
Eric Dumazetd1b19df2009-09-03 01:29:39 -070010662EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010663
10664/**
Eric Dumazet44a08732009-10-27 07:03:04 +000010665 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070010666 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +000010667 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -080010668 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010669 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010670 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +000010671 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010672 *
10673 * Callers must hold the rtnl semaphore. You may want
10674 * unregister_netdev() instead of this.
10675 */
10676
Eric Dumazet44a08732009-10-27 07:03:04 +000010677void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678{
Herbert Xua6620712007-12-12 19:21:56 -080010679 ASSERT_RTNL();
10680
Eric Dumazet44a08732009-10-27 07:03:04 +000010681 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +000010682 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +000010683 } else {
10684 rollback_registered(dev);
10685 /* Finish processing unregister after unlock */
10686 net_set_todo(dev);
10687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010688}
Eric Dumazet44a08732009-10-27 07:03:04 +000010689EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010690
10691/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010692 * unregister_netdevice_many - unregister many devices
10693 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -070010694 *
10695 * Note: As most callers use a stack allocated list_head,
10696 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010697 */
10698void unregister_netdevice_many(struct list_head *head)
10699{
10700 struct net_device *dev;
10701
10702 if (!list_empty(head)) {
10703 rollback_registered_many(head);
10704 list_for_each_entry(dev, head, unreg_list)
10705 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -070010706 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010707 }
10708}
Eric Dumazet63c80992009-10-27 07:06:49 +000010709EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +000010710
10711/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712 * unregister_netdev - remove device from the kernel
10713 * @dev: device
10714 *
10715 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -080010716 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717 *
10718 * This is just a wrapper for unregister_netdevice that takes
10719 * the rtnl semaphore. In general you want to use this and not
10720 * unregister_netdevice.
10721 */
10722void unregister_netdev(struct net_device *dev)
10723{
10724 rtnl_lock();
10725 unregister_netdevice(dev);
10726 rtnl_unlock();
10727}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728EXPORT_SYMBOL(unregister_netdev);
10729
Eric W. Biedermance286d32007-09-12 13:53:49 +020010730/**
10731 * dev_change_net_namespace - move device to different nethost namespace
10732 * @dev: device
10733 * @net: network namespace
10734 * @pat: If not NULL name pattern to try if the current device name
10735 * is already taken in the destination network namespace.
10736 *
10737 * This function shuts down a device interface and moves it
10738 * to a new network namespace. On success 0 is returned, on
10739 * a failure a netagive errno code is returned.
10740 *
10741 * Callers must hold the rtnl semaphore.
10742 */
10743
10744int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10745{
Christian Brauneref6a4c82020-02-27 04:37:19 +010010746 struct net *net_old = dev_net(dev);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010747 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010748
10749 ASSERT_RTNL();
10750
10751 /* Don't allow namespace local devices to be moved. */
10752 err = -EINVAL;
10753 if (dev->features & NETIF_F_NETNS_LOCAL)
10754 goto out;
10755
10756 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010757 if (dev->reg_state != NETREG_REGISTERED)
10758 goto out;
10759
10760 /* Get out if there is nothing todo */
10761 err = 0;
Christian Brauneref6a4c82020-02-27 04:37:19 +010010762 if (net_eq(net_old, net))
Eric W. Biedermance286d32007-09-12 13:53:49 +020010763 goto out;
10764
10765 /* Pick the destination device name, and ensure
10766 * we can use it in the destination network namespace.
10767 */
10768 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +000010769 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010770 /* We get here if we can't use the current device name */
10771 if (!pat)
10772 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +080010773 err = dev_get_valid_name(net, dev, pat);
10774 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010775 goto out;
10776 }
10777
10778 /*
10779 * And now a mini version of register_netdevice unregister_netdevice.
10780 */
10781
10782 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -070010783 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010784
10785 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010786 unlist_netdevice(dev);
10787
10788 synchronize_net();
10789
10790 /* Shutdown queueing discipline. */
10791 dev_shutdown(dev);
10792
10793 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +110010794 * this device. They should clean all the things.
10795 *
10796 * Note that dev->reg_state stays at NETREG_REGISTERED.
10797 * This is wanted because this way 8021q and macvlan know
10798 * the device is just moving and can keep their slaves up.
10799 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010800 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010801 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010802
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010803 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010804 /* If there is an ifindex conflict assign a new one */
10805 if (__dev_get_by_index(net, dev->ifindex))
10806 new_ifindex = dev_new_index(net);
10807 else
10808 new_ifindex = dev->ifindex;
10809
10810 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10811 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010812
10813 /*
10814 * Flush the unicast and multicast chains
10815 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010816 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010817 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010818
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010819 /* Send a netdev-removed uevent to the old namespace */
10820 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010821 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010822
Jiri Pirko93642e12020-01-25 12:17:08 +010010823 /* Move per-net netdevice notifiers that are following the netdevice */
10824 move_netdevice_notifiers_dev_net(dev, net);
10825
Eric W. Biedermance286d32007-09-12 13:53:49 +020010826 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010827 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010828 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010829
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010830 /* Send a netdev-add uevent to the new namespace */
10831 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010832 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010833
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010834 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010835 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010836 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010837
Christian Brauneref6a4c82020-02-27 04:37:19 +010010838 /* Adapt owner in case owning user namespace of target network
10839 * namespace is different from the original one.
10840 */
10841 err = netdev_change_owner(dev, net_old, net);
10842 WARN_ON(err);
10843
Eric W. Biedermance286d32007-09-12 13:53:49 +020010844 /* Add the device back in the hashes */
10845 list_netdevice(dev);
10846
10847 /* Notify protocols, that a new device appeared. */
10848 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10849
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010850 /*
10851 * Prevent userspace races by waiting until the network
10852 * device is fully setup before sending notifications.
10853 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010854 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010855
Eric W. Biedermance286d32007-09-12 13:53:49 +020010856 synchronize_net();
10857 err = 0;
10858out:
10859 return err;
10860}
Johannes Berg463d0182009-07-14 00:33:35 +020010861EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010862
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010863static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010864{
10865 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010866 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010867 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010868 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010869
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870 local_irq_disable();
10871 cpu = smp_processor_id();
10872 sd = &per_cpu(softnet_data, cpu);
10873 oldsd = &per_cpu(softnet_data, oldcpu);
10874
10875 /* Find end of our completion_queue. */
10876 list_skb = &sd->completion_queue;
10877 while (*list_skb)
10878 list_skb = &(*list_skb)->next;
10879 /* Append completion queue from offline CPU. */
10880 *list_skb = oldsd->completion_queue;
10881 oldsd->completion_queue = NULL;
10882
Linus Torvalds1da177e2005-04-16 15:20:36 -070010883 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010884 if (oldsd->output_queue) {
10885 *sd->output_queue_tailp = oldsd->output_queue;
10886 sd->output_queue_tailp = oldsd->output_queue_tailp;
10887 oldsd->output_queue = NULL;
10888 oldsd->output_queue_tailp = &oldsd->output_queue;
10889 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010890 /* Append NAPI poll list from offline CPU, with one exception :
10891 * process_backlog() must be called by cpu owning percpu backlog.
10892 * We properly handle process_queue & input_pkt_queue later.
10893 */
10894 while (!list_empty(&oldsd->poll_list)) {
10895 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10896 struct napi_struct,
10897 poll_list);
10898
10899 list_del_init(&napi->poll_list);
10900 if (napi->poll == process_backlog)
10901 napi->state = 0;
10902 else
10903 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010905
10906 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10907 local_irq_enable();
10908
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010909#ifdef CONFIG_RPS
10910 remsd = oldsd->rps_ipi_list;
10911 oldsd->rps_ipi_list = NULL;
10912#endif
10913 /* send out pending IPI's on offline CPU */
10914 net_rps_send_ipi(remsd);
10915
Linus Torvalds1da177e2005-04-16 15:20:36 -070010916 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010917 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010918 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010919 input_queue_head_incr(oldsd);
10920 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010921 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010922 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010923 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010927}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010928
Herbert Xu7f353bf2007-08-10 15:47:58 -070010929/**
Herbert Xub63365a2008-10-23 01:11:29 -070010930 * netdev_increment_features - increment feature set by one
10931 * @all: current feature set
10932 * @one: new feature set
10933 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010934 *
10935 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010936 * @one to the master device with current feature set @all. Will not
10937 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010938 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010939netdev_features_t netdev_increment_features(netdev_features_t all,
10940 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010941{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010942 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010943 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010944 mask |= NETIF_F_VLAN_CHALLENGED;
10945
Tom Herberta1882222015-12-14 11:19:43 -080010946 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010947 all &= one | ~NETIF_F_ALL_FOR_ALL;
10948
Michał Mirosław1742f182011-04-22 06:31:16 +000010949 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010950 if (all & NETIF_F_HW_CSUM)
10951 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010952
10953 return all;
10954}
Herbert Xub63365a2008-10-23 01:11:29 -070010955EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010956
Baruch Siach430f03c2013-06-02 20:43:55 +000010957static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010958{
10959 int i;
10960 struct hlist_head *hash;
10961
Kees Cook6da2ec52018-06-12 13:55:00 -070010962 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010963 if (hash != NULL)
10964 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10965 INIT_HLIST_HEAD(&hash[i]);
10966
10967 return hash;
10968}
10969
Eric W. Biederman881d9662007-09-17 11:56:21 -070010970/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070010971static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010972{
Li RongQingd9f37d02018-07-13 14:41:36 +080010973 BUILD_BUG_ON(GRO_HASH_BUCKETS >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080010974 8 * sizeof_field(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080010975
Rustad, Mark D734b6542012-07-18 09:06:07 +000010976 if (net != &init_net)
10977 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070010978
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010979 net->dev_name_head = netdev_create_hash();
10980 if (net->dev_name_head == NULL)
10981 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010982
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010983 net->dev_index_head = netdev_create_hash();
10984 if (net->dev_index_head == NULL)
10985 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010986
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020010987 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10988
Eric W. Biederman881d9662007-09-17 11:56:21 -070010989 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010990
10991err_idx:
10992 kfree(net->dev_name_head);
10993err_name:
10994 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010995}
10996
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010997/**
10998 * netdev_drivername - network driver for the device
10999 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070011000 *
11001 * Determine network driver for device.
11002 */
David S. Miller3019de12011-06-06 16:41:33 -070011003const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070011004{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070011005 const struct device_driver *driver;
11006 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070011007 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070011008
11009 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011010 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070011011 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011012
11013 driver = parent->driver;
11014 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070011015 return driver->name;
11016 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070011017}
11018
Joe Perches6ea754e2014-09-22 11:10:50 -070011019static void __netdev_printk(const char *level, const struct net_device *dev,
11020 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000011021{
Joe Perchesb004ff42012-09-12 20:12:19 -070011022 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011023 dev_printk_emit(level[1] - '0',
11024 dev->dev.parent,
11025 "%s %s %s%s: %pV",
11026 dev_driver_string(dev->dev.parent),
11027 dev_name(dev->dev.parent),
11028 netdev_name(dev), netdev_reg_state(dev),
11029 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011030 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070011031 printk("%s%s%s: %pV",
11032 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011033 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070011034 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011035 }
Joe Perches256df2f2010-06-27 01:02:35 +000011036}
11037
Joe Perches6ea754e2014-09-22 11:10:50 -070011038void netdev_printk(const char *level, const struct net_device *dev,
11039 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000011040{
11041 struct va_format vaf;
11042 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000011043
11044 va_start(args, format);
11045
11046 vaf.fmt = format;
11047 vaf.va = &args;
11048
Joe Perches6ea754e2014-09-22 11:10:50 -070011049 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070011050
Joe Perches256df2f2010-06-27 01:02:35 +000011051 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000011052}
11053EXPORT_SYMBOL(netdev_printk);
11054
11055#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070011056void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000011057{ \
Joe Perches256df2f2010-06-27 01:02:35 +000011058 struct va_format vaf; \
11059 va_list args; \
11060 \
11061 va_start(args, fmt); \
11062 \
11063 vaf.fmt = fmt; \
11064 vaf.va = &args; \
11065 \
Joe Perches6ea754e2014-09-22 11:10:50 -070011066 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070011067 \
Joe Perches256df2f2010-06-27 01:02:35 +000011068 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000011069} \
11070EXPORT_SYMBOL(func);
11071
11072define_netdev_printk_level(netdev_emerg, KERN_EMERG);
11073define_netdev_printk_level(netdev_alert, KERN_ALERT);
11074define_netdev_printk_level(netdev_crit, KERN_CRIT);
11075define_netdev_printk_level(netdev_err, KERN_ERR);
11076define_netdev_printk_level(netdev_warn, KERN_WARNING);
11077define_netdev_printk_level(netdev_notice, KERN_NOTICE);
11078define_netdev_printk_level(netdev_info, KERN_INFO);
11079
Pavel Emelyanov46650792007-10-08 20:38:39 -070011080static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070011081{
11082 kfree(net->dev_name_head);
11083 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030011084 if (net != &init_net)
11085 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070011086}
11087
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011088static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070011089 .init = netdev_init,
11090 .exit = netdev_exit,
11091};
11092
Pavel Emelyanov46650792007-10-08 20:38:39 -070011093static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020011094{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011095 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020011096 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011097 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020011098 * initial network namespace
11099 */
11100 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011101 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011102 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011103 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020011104
11105 /* Ignore unmoveable devices (i.e. loopback) */
11106 if (dev->features & NETIF_F_NETNS_LOCAL)
11107 continue;
11108
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011109 /* Leave virtual devices for the generic cleanup */
Martin Willi8dc08a22021-03-02 13:24:23 +010011110 if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000011111 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080011112
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011113 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011114 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020011115 if (__dev_get_by_name(&init_net, fb_name))
11116 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011117 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020011118 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000011119 pr_emerg("%s: failed to move %s to init_net: %d\n",
11120 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070011121 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020011122 }
11123 }
11124 rtnl_unlock();
11125}
11126
Eric W. Biederman50624c92013-09-23 21:19:49 -070011127static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
11128{
11129 /* Return with the rtnl_lock held when there are no network
11130 * devices unregistering in any network namespace in net_list.
11131 */
11132 struct net *net;
11133 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010011134 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011135
Peter Zijlstraff960a72014-10-29 17:04:56 +010011136 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011137 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070011138 unregistering = false;
11139 rtnl_lock();
11140 list_for_each_entry(net, net_list, exit_list) {
11141 if (net->dev_unreg_count > 0) {
11142 unregistering = true;
11143 break;
11144 }
11145 }
11146 if (!unregistering)
11147 break;
11148 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010011149
11150 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011151 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010011152 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070011153}
11154
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011155static void __net_exit default_device_exit_batch(struct list_head *net_list)
11156{
11157 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011158 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011159 * Do this across as many network namespaces as possible to
11160 * improve batching efficiency.
11161 */
11162 struct net_device *dev;
11163 struct net *net;
11164 LIST_HEAD(dev_kill_list);
11165
Eric W. Biederman50624c92013-09-23 21:19:49 -070011166 /* To prevent network device cleanup code from dereferencing
11167 * loopback devices or network devices that have been freed
11168 * wait here for all pending unregistrations to complete,
11169 * before unregistring the loopback device and allowing the
11170 * network namespace be freed.
11171 *
11172 * The netdev todo list containing all network devices
11173 * unregistrations that happen in default_device_exit_batch
11174 * will run in the rtnl_unlock() at the end of
11175 * default_device_exit_batch.
11176 */
11177 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011178 list_for_each_entry(net, net_list, exit_list) {
11179 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020011180 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011181 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
11182 else
11183 unregister_netdevice_queue(dev, &dev_kill_list);
11184 }
11185 }
11186 unregister_netdevice_many(&dev_kill_list);
11187 rtnl_unlock();
11188}
11189
Denis V. Lunev022cbae2007-11-13 03:23:50 -080011190static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020011191 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000011192 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020011193};
11194
Linus Torvalds1da177e2005-04-16 15:20:36 -070011195/*
11196 * Initialize the DEV module. At boot time this walks the device list and
11197 * unhooks any devices that fail to initialise (normally hardware not
11198 * present) and leaves us with a valid list of present and active devices.
11199 *
11200 */
11201
11202/*
11203 * This is called single threaded during boot, so no need
11204 * to take the rtnl semaphore.
11205 */
11206static int __init net_dev_init(void)
11207{
11208 int i, rc = -ENOMEM;
11209
11210 BUG_ON(!dev_boot_phase);
11211
Linus Torvalds1da177e2005-04-16 15:20:36 -070011212 if (dev_proc_init())
11213 goto out;
11214
Eric W. Biederman8b41d182007-09-26 22:02:53 -070011215 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070011216 goto out;
11217
11218 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080011219 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011220 INIT_LIST_HEAD(&ptype_base[i]);
11221
Vlad Yasevich62532da2012-11-15 08:49:10 +000011222 INIT_LIST_HEAD(&offload_base);
11223
Eric W. Biederman881d9662007-09-17 11:56:21 -070011224 if (register_pernet_subsys(&netdev_net_ops))
11225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011226
11227 /*
11228 * Initialise the packet receive queues.
11229 */
11230
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070011231 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070011232 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011233 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011234
Eric Dumazet41852492016-08-26 12:50:39 -070011235 INIT_WORK(flush, flush_backlog);
11236
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011237 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070011238 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010011239#ifdef CONFIG_XFRM_OFFLOAD
11240 skb_queue_head_init(&sd->xfrm_backlog);
11241#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011242 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000011243 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000011244#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011245 sd->csd.func = rps_trigger_softirq;
11246 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011247 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070011248#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000011249
David S. Miller7c4ec742018-07-20 23:37:55 -070011250 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000011251 sd->backlog.poll = process_backlog;
11252 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011253 }
11254
Linus Torvalds1da177e2005-04-16 15:20:36 -070011255 dev_boot_phase = 0;
11256
Eric W. Biederman505d4f72008-11-07 22:54:20 -080011257 /* The loopback device is special if any other network devices
11258 * is present in a network namespace the loopback device must
11259 * be present. Since we now dynamically allocate and free the
11260 * loopback device ensure this invariant is maintained by
11261 * keeping the loopback device as the first device on the
11262 * list of network devices. Ensuring the loopback devices
11263 * is the first device that appears and the last network device
11264 * that disappears.
11265 */
11266 if (register_pernet_device(&loopback_net_ops))
11267 goto out;
11268
11269 if (register_pernet_device(&default_device_ops))
11270 goto out;
11271
Carlos R. Mafra962cf362008-05-15 11:15:37 -030011272 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11273 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011274
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010011275 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11276 NULL, dev_cpu_dead);
11277 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011278 rc = 0;
11279out:
11280 return rc;
11281}
11282
11283subsys_initcall(net_dev_init);