blob: e7c027fb48084f4ff32a7d5b4d69100716ddabd9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
tcharding722c9a02017-02-09 17:56:04 +11003 * NET3 Protocol independent device support routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Derived from the non IP parts of dev.c 1.0.19
tcharding722c9a02017-02-09 17:56:04 +11006 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
9 *
10 * Additional Authors:
11 * Florian la Roche <rzsfl@rz.uni-sb.de>
12 * Alan Cox <gw4pts@gw4pts.ampr.org>
13 * David Hinds <dahinds@users.sourceforge.net>
14 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
15 * Adam Sulmicki <adam@cfar.umd.edu>
16 * Pekka Riikonen <priikone@poesidon.pspt.fi>
17 *
18 * Changes:
19 * D.J. Barrow : Fixed bug where dev->refcnt gets set
tcharding722c9a02017-02-09 17:56:04 +110020 * to 2 if register_netdev gets called
21 * before net_dev_init & also removed a
22 * few lines of code in the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Alan Cox : device private ioctl copies fields back.
24 * Alan Cox : Transmit queue code does relevant
25 * stunts to keep the queue safe.
26 * Alan Cox : Fixed double lock.
27 * Alan Cox : Fixed promisc NULL pointer trap
28 * ???????? : Support the full private ioctl range
29 * Alan Cox : Moved ioctl permission check into
30 * drivers
31 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
32 * Alan Cox : 100 backlog just doesn't cut it when
33 * you start doing multicast video 8)
34 * Alan Cox : Rewrote net_bh and list manager.
tcharding722c9a02017-02-09 17:56:04 +110035 * Alan Cox : Fix ETH_P_ALL echoback lengths.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * Alan Cox : Took out transmit every packet pass
37 * Saved a few bytes in the ioctl handler
38 * Alan Cox : Network driver sets packet type before
39 * calling netif_rx. Saves a function
40 * call a packet.
41 * Alan Cox : Hashed net_bh()
42 * Richard Kooijman: Timestamp fixes.
43 * Alan Cox : Wrong field in SIOCGIFDSTADDR
44 * Alan Cox : Device lock protection.
tcharding722c9a02017-02-09 17:56:04 +110045 * Alan Cox : Fixed nasty side effect of device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * changes.
47 * Rudi Cilibrasi : Pass the right thing to
48 * set_mac_address()
49 * Dave Miller : 32bit quantity for the device lock to
50 * make it work out on a Sparc.
51 * Bjorn Ekwall : Added KERNELD hack.
52 * Alan Cox : Cleaned up the backlog initialise.
53 * Craig Metz : SIOCGIFCONF fix if space for under
54 * 1 device.
55 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
56 * is no device open function.
57 * Andi Kleen : Fix error reporting for SIOCGIFCONF
58 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
59 * Cyrus Durgin : Cleaned for KMOD
60 * Adam Sulmicki : Bug Fix : Network Device Unload
61 * A network device unload needs to purge
62 * the backlog queue.
63 * Paul Rusty Russell : SIOCSIFNAME
64 * Pekka Riikonen : Netdev boot-time settings code
65 * Andrew Morton : Make unregister_netdevice wait
tcharding722c9a02017-02-09 17:56:04 +110066 * indefinitely on dev->refcnt
67 * J Hadi Salim : - Backlog queue sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * - netif_rx() feedback
69 */
70
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080073#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/cpu.h>
75#include <linux/types.h>
76#include <linux/kernel.h>
stephen hemminger08e98972009-11-10 07:20:34 +000077#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090078#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -070080#include <linux/sched/mm.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080081#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/string.h>
83#include <linux/mm.h>
84#include <linux/socket.h>
85#include <linux/sockios.h>
86#include <linux/errno.h>
87#include <linux/interrupt.h>
88#include <linux/if_ether.h>
89#include <linux/netdevice.h>
90#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070091#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/skbuff.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070093#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040094#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020095#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -080097#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700101#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200103#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000105#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <linux/highmem.h>
107#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <linux/netpoll.h>
110#include <linux/rcupdate.h>
111#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500114#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700115#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700116#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700117#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700118#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700119#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700120#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700121#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700122#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700123#include <linux/ipv6.h>
124#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700125#include <linux/jhash.h>
126#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700127#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900128#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900129#include <trace/events/skb.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700130#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000131#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100132#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300133#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700134#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100135#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400136#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800137#include <linux/hrtimer.h>
Pablo Neirae687ad62015-05-13 18:19:38 +0200138#include <linux/netfilter_ingress.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530139#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200140#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200141#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200142#include <linux/net_namespace.h>
Paolo Abeniaaa5d902018-12-14 11:51:58 +0100143#include <linux/indirect_call_wrapper.h>
Jiri Pirkoaf3836d2019-03-28 13:56:37 +0100144#include <net/devlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700146#include "net-sysfs.h"
147
Herbert Xud565b0a2008-12-15 23:38:52 -0800148#define MAX_GRO_SKBS 8
Taehee Yoo5343da42019-10-21 18:47:50 +0000149#define MAX_NEST_DEV 8
Herbert Xud565b0a2008-12-15 23:38:52 -0800150
Herbert Xu5d38a072009-01-04 16:13:40 -0800151/* This should be increased if a protocol with a bigger head is added. */
152#define GRO_MAX_HEAD (MAX_HEADER + 128)
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000155static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000156struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
157struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000158static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000160static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700161static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700162 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000163static int call_netdevice_notifiers_extack(unsigned long val,
164 struct net_device *dev,
165 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200166static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700169 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * semaphore.
171 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800172 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700175 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * actual updates. This allows pure readers to access the list even
177 * while a writer is preparing to update it.
178 *
179 * To put it another way, dev_base_lock is held for writing only to
180 * protect against pure readers; the rtnl semaphore provides the
181 * protection against other writers.
182 *
183 * See, for example usages, register_netdevice() and
184 * unregister_netdevice(), which must be called with the rtnl
185 * semaphore held.
186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188EXPORT_SYMBOL(dev_base_lock);
189
Florian Westphal6c557002017-10-02 23:50:05 +0200190static DEFINE_MUTEX(ifalias_mutex);
191
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300192/* protects napi_hash addition/deletion and napi_gen_id */
193static DEFINE_SPINLOCK(napi_hash_lock);
194
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800195static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800196static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300197
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200198static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000199
Thomas Graf4e985ad2011-06-21 03:11:20 +0000200static inline void dev_base_seq_inc(struct net *net)
201{
tcharding643aa9c2017-02-09 17:56:05 +1100202 while (++net->dev_base_seq == 0)
203 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000204}
205
Eric W. Biederman881d9662007-09-17 11:56:21 -0700206static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700208 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000209
stephen hemminger08e98972009-11-10 07:20:34 +0000210 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Eric W. Biederman881d9662007-09-17 11:56:21 -0700213static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700215 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000218static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000219{
220#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000221 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000222#endif
223}
224
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000225static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000226{
227#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000228 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000229#endif
230}
231
Jiri Pirkoff927412019-09-30 11:48:15 +0200232static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
233 const char *name)
234{
235 struct netdev_name_node *name_node;
236
237 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
238 if (!name_node)
239 return NULL;
240 INIT_HLIST_NODE(&name_node->hlist);
241 name_node->dev = dev;
242 name_node->name = name;
243 return name_node;
244}
245
246static struct netdev_name_node *
247netdev_name_node_head_alloc(struct net_device *dev)
248{
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200249 struct netdev_name_node *name_node;
250
251 name_node = netdev_name_node_alloc(dev, dev->name);
252 if (!name_node)
253 return NULL;
254 INIT_LIST_HEAD(&name_node->list);
255 return name_node;
Jiri Pirkoff927412019-09-30 11:48:15 +0200256}
257
258static void netdev_name_node_free(struct netdev_name_node *name_node)
259{
260 kfree(name_node);
261}
262
263static void netdev_name_node_add(struct net *net,
264 struct netdev_name_node *name_node)
265{
266 hlist_add_head_rcu(&name_node->hlist,
267 dev_name_hash(net, name_node->name));
268}
269
270static void netdev_name_node_del(struct netdev_name_node *name_node)
271{
272 hlist_del_rcu(&name_node->hlist);
273}
274
275static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
276 const char *name)
277{
278 struct hlist_head *head = dev_name_hash(net, name);
279 struct netdev_name_node *name_node;
280
281 hlist_for_each_entry(name_node, head, hlist)
282 if (!strcmp(name_node->name, name))
283 return name_node;
284 return NULL;
285}
286
287static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
288 const char *name)
289{
290 struct hlist_head *head = dev_name_hash(net, name);
291 struct netdev_name_node *name_node;
292
293 hlist_for_each_entry_rcu(name_node, head, hlist)
294 if (!strcmp(name_node->name, name))
295 return name_node;
296 return NULL;
297}
298
Jiri Pirko36fbf1e2019-09-30 11:48:16 +0200299int netdev_name_node_alt_create(struct net_device *dev, const char *name)
300{
301 struct netdev_name_node *name_node;
302 struct net *net = dev_net(dev);
303
304 name_node = netdev_name_node_lookup(net, name);
305 if (name_node)
306 return -EEXIST;
307 name_node = netdev_name_node_alloc(dev, name);
308 if (!name_node)
309 return -ENOMEM;
310 netdev_name_node_add(net, name_node);
311 /* The node that holds dev->name acts as a head of per-device list. */
312 list_add_tail(&name_node->list, &dev->name_node->list);
313
314 return 0;
315}
316EXPORT_SYMBOL(netdev_name_node_alt_create);
317
318static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
319{
320 list_del(&name_node->list);
321 netdev_name_node_del(name_node);
322 kfree(name_node->name);
323 netdev_name_node_free(name_node);
324}
325
326int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
327{
328 struct netdev_name_node *name_node;
329 struct net *net = dev_net(dev);
330
331 name_node = netdev_name_node_lookup(net, name);
332 if (!name_node)
333 return -ENOENT;
334 __netdev_name_node_alt_destroy(name_node);
335
336 return 0;
337}
338EXPORT_SYMBOL(netdev_name_node_alt_destroy);
339
340static void netdev_name_node_alt_flush(struct net_device *dev)
341{
342 struct netdev_name_node *name_node, *tmp;
343
344 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
345 __netdev_name_node_alt_destroy(name_node);
346}
347
Eric W. Biedermance286d32007-09-12 13:53:49 +0200348/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000349static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200350{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900351 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200352
353 ASSERT_RTNL();
354
355 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800356 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Jiri Pirkoff927412019-09-30 11:48:15 +0200357 netdev_name_node_add(net, dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000358 hlist_add_head_rcu(&dev->index_hlist,
359 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200360 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000361
362 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200363}
364
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000365/* Device list removal
366 * caller must respect a RCU grace period before freeing/reusing dev
367 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200368static void unlist_netdevice(struct net_device *dev)
369{
370 ASSERT_RTNL();
371
372 /* Unlink dev from the device chain */
373 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800374 list_del_rcu(&dev->dev_list);
Jiri Pirkoff927412019-09-30 11:48:15 +0200375 netdev_name_node_del(dev->name_node);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000376 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200377 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000378
379 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * Our notifier list
384 */
385
Alan Sternf07d5b92006-05-09 15:23:03 -0700386static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/*
389 * Device drivers call our routines to queue packets here. We empty the
390 * queue in the local softnet handler.
391 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700392
Eric Dumazet9958da02010-04-17 04:17:02 +0000393DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700394EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100397 *
398 * Protocol management and registration routines
399 *
400 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * Add a protocol ID to the list. Now that the input handler is
405 * smarter we can dispense with all the messy stuff that used to be
406 * here.
407 *
408 * BEWARE!!! Protocol handlers, mangling input packets,
409 * MUST BE last in hash buckets and checking protocol handlers
410 * MUST start from promiscuous ptype_all chain in net_bh.
411 * It is true now, do not change it.
412 * Explanation follows: if protocol handler, mangling packet, will
413 * be the first on list, it is not able to sense, that packet
414 * is cloned and should be copied-on-write, so that it will
415 * change it and subsequent readers will get broken packet.
416 * --ANK (980803)
417 */
418
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000419static inline struct list_head *ptype_head(const struct packet_type *pt)
420{
421 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800422 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000423 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800424 return pt->dev ? &pt->dev->ptype_specific :
425 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/**
429 * dev_add_pack - add packet handler
430 * @pt: packet type declaration
431 *
432 * Add a protocol handler to the networking stack. The passed &packet_type
433 * is linked into kernel lists and may not be freed until it has been
434 * removed from the kernel lists.
435 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900436 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * guarantee all CPU's that are in middle of receiving packets
438 * will see the new packet type (until the next received packet).
439 */
440
441void dev_add_pack(struct packet_type *pt)
442{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000443 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000445 spin_lock(&ptype_lock);
446 list_add_rcu(&pt->list, head);
447 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700449EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * __dev_remove_pack - remove packet handler
453 * @pt: packet type declaration
454 *
455 * Remove a protocol handler that was previously added to the kernel
456 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
457 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900458 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *
460 * The packet type might still be in use by receivers
461 * and must not be freed until after all the CPU's have gone
462 * through a quiescent state.
463 */
464void __dev_remove_pack(struct packet_type *pt)
465{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000466 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct packet_type *pt1;
468
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000469 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 list_for_each_entry(pt1, head, list) {
472 if (pt == pt1) {
473 list_del_rcu(&pt->list);
474 goto out;
475 }
476 }
477
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000478 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000480 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700482EXPORT_SYMBOL(__dev_remove_pack);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/**
485 * dev_remove_pack - remove packet handler
486 * @pt: packet type declaration
487 *
488 * Remove a protocol handler that was previously added to the kernel
489 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
490 * from the kernel lists and can be freed or reused once this function
491 * returns.
492 *
493 * This call sleeps to guarantee that no CPU is looking at the packet
494 * type after return.
495 */
496void dev_remove_pack(struct packet_type *pt)
497{
498 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 synchronize_net();
501}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700502EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Vlad Yasevich62532da2012-11-15 08:49:10 +0000504
505/**
506 * dev_add_offload - register offload handlers
507 * @po: protocol offload declaration
508 *
509 * Add protocol offload handlers to the networking stack. The passed
510 * &proto_offload is linked into kernel lists and may not be freed until
511 * it has been removed from the kernel lists.
512 *
513 * This call does not sleep therefore it can not
514 * guarantee all CPU's that are in middle of receiving packets
515 * will see the new offload handlers (until the next received packet).
516 */
517void dev_add_offload(struct packet_offload *po)
518{
David S. Millerbdef7de2015-06-01 14:56:09 -0700519 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000520
521 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700522 list_for_each_entry(elem, &offload_base, list) {
523 if (po->priority < elem->priority)
524 break;
525 }
526 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000527 spin_unlock(&offload_lock);
528}
529EXPORT_SYMBOL(dev_add_offload);
530
531/**
532 * __dev_remove_offload - remove offload handler
533 * @po: packet offload declaration
534 *
535 * Remove a protocol offload handler that was previously added to the
536 * kernel offload handlers by dev_add_offload(). The passed &offload_type
537 * is removed from the kernel lists and can be freed or reused once this
538 * function returns.
539 *
540 * The packet type might still be in use by receivers
541 * and must not be freed until after all the CPU's have gone
542 * through a quiescent state.
543 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800544static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000545{
546 struct list_head *head = &offload_base;
547 struct packet_offload *po1;
548
Eric Dumazetc53aa502012-11-16 08:08:23 +0000549 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000550
551 list_for_each_entry(po1, head, list) {
552 if (po == po1) {
553 list_del_rcu(&po->list);
554 goto out;
555 }
556 }
557
558 pr_warn("dev_remove_offload: %p not found\n", po);
559out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000560 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000561}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000562
563/**
564 * dev_remove_offload - remove packet offload handler
565 * @po: packet offload declaration
566 *
567 * Remove a packet offload handler that was previously added to the kernel
568 * offload handlers by dev_add_offload(). The passed &offload_type is
569 * removed from the kernel lists and can be freed or reused once this
570 * function returns.
571 *
572 * This call sleeps to guarantee that no CPU is looking at the packet
573 * type after return.
574 */
575void dev_remove_offload(struct packet_offload *po)
576{
577 __dev_remove_offload(po);
578
579 synchronize_net();
580}
581EXPORT_SYMBOL(dev_remove_offload);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100584 *
585 * Device Boot-time Settings Routines
586 *
587 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/* Boot time configuration table */
590static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
591
592/**
593 * netdev_boot_setup_add - add new setup entry
594 * @name: name of the device
595 * @map: configured settings for the device
596 *
597 * Adds new setup entry to the dev_boot_setup list. The function
598 * returns 0 on error and 1 on success. This is a generic routine to
599 * all netdevices.
600 */
601static int netdev_boot_setup_add(char *name, struct ifmap *map)
602{
603 struct netdev_boot_setup *s;
604 int i;
605
606 s = dev_boot_setup;
607 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
608 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
609 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700610 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 memcpy(&s[i].map, map, sizeof(s[i].map));
612 break;
613 }
614 }
615
616 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
617}
618
619/**
tcharding722c9a02017-02-09 17:56:04 +1100620 * netdev_boot_setup_check - check boot time settings
621 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
tcharding722c9a02017-02-09 17:56:04 +1100623 * Check boot time settings for the device.
624 * The found settings are set for the device to be used
625 * later in the device probing.
626 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628int netdev_boot_setup_check(struct net_device *dev)
629{
630 struct netdev_boot_setup *s = dev_boot_setup;
631 int i;
632
633 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
634 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700635 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100636 dev->irq = s[i].map.irq;
637 dev->base_addr = s[i].map.base_addr;
638 dev->mem_start = s[i].map.mem_start;
639 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 1;
641 }
642 }
643 return 0;
644}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700645EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647
648/**
tcharding722c9a02017-02-09 17:56:04 +1100649 * netdev_boot_base - get address from boot time settings
650 * @prefix: prefix for network device
651 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *
tcharding722c9a02017-02-09 17:56:04 +1100653 * Check boot time settings for the base address of device.
654 * The found settings are set for the device to be used
655 * later in the device probing.
656 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658unsigned long netdev_boot_base(const char *prefix, int unit)
659{
660 const struct netdev_boot_setup *s = dev_boot_setup;
661 char name[IFNAMSIZ];
662 int i;
663
664 sprintf(name, "%s%d", prefix, unit);
665
666 /*
667 * If device already registered then return base of 1
668 * to indicate not to probe for this interface
669 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700670 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 1;
672
673 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
674 if (!strcmp(name, s[i].name))
675 return s[i].map.base_addr;
676 return 0;
677}
678
679/*
680 * Saves at boot time configured settings for any netdevice.
681 */
682int __init netdev_boot_setup(char *str)
683{
684 int ints[5];
685 struct ifmap map;
686
687 str = get_options(str, ARRAY_SIZE(ints), ints);
688 if (!str || !*str)
689 return 0;
690
691 /* Save settings */
692 memset(&map, 0, sizeof(map));
693 if (ints[0] > 0)
694 map.irq = ints[1];
695 if (ints[0] > 1)
696 map.base_addr = ints[2];
697 if (ints[0] > 2)
698 map.mem_start = ints[3];
699 if (ints[0] > 3)
700 map.mem_end = ints[4];
701
702 /* Add new entry to the list */
703 return netdev_boot_setup_add(str, &map);
704}
705
706__setup("netdev=", netdev_boot_setup);
707
708/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100709 *
710 * Device Interface Subroutines
711 *
712 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200715 * dev_get_iflink - get 'iflink' value of a interface
716 * @dev: targeted interface
717 *
718 * Indicates the ifindex the interface is linked to.
719 * Physical interfaces have the same 'ifindex' and 'iflink' values.
720 */
721
722int dev_get_iflink(const struct net_device *dev)
723{
724 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
725 return dev->netdev_ops->ndo_get_iflink(dev);
726
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200727 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200728}
729EXPORT_SYMBOL(dev_get_iflink);
730
731/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700732 * dev_fill_metadata_dst - Retrieve tunnel egress information.
733 * @dev: targeted interface
734 * @skb: The packet.
735 *
736 * For better visibility of tunnel traffic OVS needs to retrieve
737 * egress tunnel information for a packet. Following API allows
738 * user to get this info.
739 */
740int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
741{
742 struct ip_tunnel_info *info;
743
744 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
745 return -EINVAL;
746
747 info = skb_tunnel_info_unclone(skb);
748 if (!info)
749 return -ENOMEM;
750 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
751 return -EINVAL;
752
753 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
754}
755EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
756
757/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700759 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * @name: name to find
761 *
762 * Find an interface by name. Must be called under RTNL semaphore
763 * or @dev_base_lock. If the name is found a pointer to the device
764 * is returned. If the name is not found then %NULL is returned. The
765 * reference counters are not incremented so the caller must be
766 * careful with locks.
767 */
768
Eric W. Biederman881d9662007-09-17 11:56:21 -0700769struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Jiri Pirkoff927412019-09-30 11:48:15 +0200771 struct netdev_name_node *node_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jiri Pirkoff927412019-09-30 11:48:15 +0200773 node_name = netdev_name_node_lookup(net, name);
774 return node_name ? node_name->dev : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700776EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778/**
tcharding722c9a02017-02-09 17:56:04 +1100779 * dev_get_by_name_rcu - find a device by its name
780 * @net: the applicable net namespace
781 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000782 *
tcharding722c9a02017-02-09 17:56:04 +1100783 * Find an interface by name.
784 * If the name is found a pointer to the device is returned.
785 * If the name is not found then %NULL is returned.
786 * The reference counters are not incremented so the caller must be
787 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000788 */
789
790struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
791{
Jiri Pirkoff927412019-09-30 11:48:15 +0200792 struct netdev_name_node *node_name;
Eric Dumazet72c95282009-10-30 07:11:27 +0000793
Jiri Pirkoff927412019-09-30 11:48:15 +0200794 node_name = netdev_name_node_lookup_rcu(net, name);
795 return node_name ? node_name->dev : NULL;
Eric Dumazet72c95282009-10-30 07:11:27 +0000796}
797EXPORT_SYMBOL(dev_get_by_name_rcu);
798
799/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700801 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * @name: name to find
803 *
804 * Find an interface by name. This can be called from any
805 * context and does its own locking. The returned handle has
806 * the usage count incremented and the caller must use dev_put() to
807 * release it when it is no longer needed. %NULL is returned if no
808 * matching device is found.
809 */
810
Eric W. Biederman881d9662007-09-17 11:56:21 -0700811struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct net_device *dev;
814
Eric Dumazet72c95282009-10-30 07:11:27 +0000815 rcu_read_lock();
816 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (dev)
818 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000819 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return dev;
821}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700822EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824/**
825 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700826 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * @ifindex: index of device
828 *
829 * Search for an interface by index. Returns %NULL if the device
830 * is not found or a pointer to the device. The device has not
831 * had its reference counter increased so the caller must be careful
832 * about locking. The caller must hold either the RTNL semaphore
833 * or @dev_base_lock.
834 */
835
Eric W. Biederman881d9662007-09-17 11:56:21 -0700836struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700838 struct net_device *dev;
839 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Sasha Levinb67bfe02013-02-27 17:06:00 -0800841 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (dev->ifindex == ifindex)
843 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return NULL;
846}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700847EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000849/**
850 * dev_get_by_index_rcu - find a device by its ifindex
851 * @net: the applicable net namespace
852 * @ifindex: index of device
853 *
854 * Search for an interface by index. Returns %NULL if the device
855 * is not found or a pointer to the device. The device has not
856 * had its reference counter increased so the caller must be careful
857 * about locking. The caller must hold RCU lock.
858 */
859
860struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
861{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000862 struct net_device *dev;
863 struct hlist_head *head = dev_index_hash(net, ifindex);
864
Sasha Levinb67bfe02013-02-27 17:06:00 -0800865 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000866 if (dev->ifindex == ifindex)
867 return dev;
868
869 return NULL;
870}
871EXPORT_SYMBOL(dev_get_by_index_rcu);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874/**
875 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700876 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * @ifindex: index of device
878 *
879 * Search for an interface by index. Returns NULL if the device
880 * is not found or a pointer to the device. The device returned has
881 * had a reference added and the pointer is safe until the user calls
882 * dev_put to indicate they have finished with it.
883 */
884
Eric W. Biederman881d9662007-09-17 11:56:21 -0700885struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct net_device *dev;
888
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000889 rcu_read_lock();
890 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (dev)
892 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000893 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return dev;
895}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700896EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200899 * dev_get_by_napi_id - find a device by napi_id
900 * @napi_id: ID of the NAPI struct
901 *
902 * Search for an interface by NAPI ID. Returns %NULL if the device
903 * is not found or a pointer to the device. The device has not had
904 * its reference counter increased so the caller must be careful
905 * about locking. The caller must hold RCU lock.
906 */
907
908struct net_device *dev_get_by_napi_id(unsigned int napi_id)
909{
910 struct napi_struct *napi;
911
912 WARN_ON_ONCE(!rcu_read_lock_held());
913
914 if (napi_id < MIN_NAPI_ID)
915 return NULL;
916
917 napi = napi_by_id(napi_id);
918
919 return napi ? napi->dev : NULL;
920}
921EXPORT_SYMBOL(dev_get_by_napi_id);
922
923/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200924 * netdev_get_name - get a netdevice name, knowing its ifindex.
925 * @net: network namespace
926 * @name: a pointer to the buffer where the name will be stored.
927 * @ifindex: the ifindex of the interface to get the name from.
928 *
929 * The use of raw_seqcount_begin() and cond_resched() before
930 * retrying is required as we want to give the writers a chance
931 * to complete when CONFIG_PREEMPT is not set.
932 */
933int netdev_get_name(struct net *net, char *name, int ifindex)
934{
935 struct net_device *dev;
936 unsigned int seq;
937
938retry:
939 seq = raw_seqcount_begin(&devnet_rename_seq);
940 rcu_read_lock();
941 dev = dev_get_by_index_rcu(net, ifindex);
942 if (!dev) {
943 rcu_read_unlock();
944 return -ENODEV;
945 }
946
947 strcpy(name, dev->name);
948 rcu_read_unlock();
949 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
950 cond_resched();
951 goto retry;
952 }
953
954 return 0;
955}
956
957/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000958 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700959 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * @type: media type of device
961 * @ha: hardware address
962 *
963 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800964 * is not found or a pointer to the device.
965 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000966 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * and the caller must therefore be careful about locking
968 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
970
Eric Dumazet941666c2010-12-05 01:23:53 +0000971struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
972 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct net_device *dev;
975
Eric Dumazet941666c2010-12-05 01:23:53 +0000976 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (dev->type == type &&
978 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700979 return dev;
980
981 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
Eric Dumazet941666c2010-12-05 01:23:53 +0000983EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300984
Eric W. Biederman881d9662007-09-17 11:56:21 -0700985struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700986{
987 struct net_device *dev;
988
989 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700990 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700991 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700992 return dev;
993
994 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700995}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700996EXPORT_SYMBOL(__dev_getfirstbyhwtype);
997
Eric W. Biederman881d9662007-09-17 11:56:21 -0700998struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001000 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Eric Dumazet99fe3c32010-03-18 11:27:25 +00001002 rcu_read_lock();
1003 for_each_netdev_rcu(net, dev)
1004 if (dev->type == type) {
1005 dev_hold(dev);
1006 ret = dev;
1007 break;
1008 }
1009 rcu_read_unlock();
1010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012EXPORT_SYMBOL(dev_getfirstbyhwtype);
1013
1014/**
WANG Cong6c555492014-09-11 15:35:09 -07001015 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001016 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * @if_flags: IFF_* values
1018 * @mask: bitmask of bits in if_flags to check
1019 *
1020 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +00001021 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -07001022 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
1024
WANG Cong6c555492014-09-11 15:35:09 -07001025struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1026 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001028 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
WANG Cong6c555492014-09-11 15:35:09 -07001030 ASSERT_RTNL();
1031
Pavel Emelianov7562f872007-05-03 15:13:45 -07001032 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001033 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001035 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037 }
1038 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
WANG Cong6c555492014-09-11 15:35:09 -07001041EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043/**
1044 * dev_valid_name - check if name is okay for network device
1045 * @name: name string
1046 *
1047 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -07001048 * to allow sysfs to work. We also disallow any kind of
1049 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
David S. Miller95f050b2012-03-06 16:12:15 -05001051bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001053 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001054 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001055 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001056 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001057 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001058 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001059
1060 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001061 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001062 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001063 name++;
1064 }
David S. Miller95f050b2012-03-06 16:12:15 -05001065 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001067EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001070 * __dev_alloc_name - allocate a name for a device
1071 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001073 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
1075 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001076 * id. It scans list of devices to build up a free map, then chooses
1077 * the first empty slot. The caller must hold the dev_base or rtnl lock
1078 * while allocating the name and adding the device in order to avoid
1079 * duplicates.
1080 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1081 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 */
1083
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001084static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 const char *p;
1088 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001089 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct net_device *d;
1091
Rasmus Villemoes93809102017-11-13 00:15:08 +01001092 if (!dev_valid_name(name))
1093 return -EINVAL;
1094
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001095 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (p) {
1097 /*
1098 * Verify the string as this thing may have come from
1099 * the user. There must be either one "%d" and no other "%"
1100 * characters.
1101 */
1102 if (p[1] != 'd' || strchr(p + 2, '%'))
1103 return -EINVAL;
1104
1105 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001106 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!inuse)
1108 return -ENOMEM;
1109
Eric W. Biederman881d9662007-09-17 11:56:21 -07001110 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!sscanf(d->name, name, &i))
1112 continue;
1113 if (i < 0 || i >= max_netdevices)
1114 continue;
1115
1116 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001117 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!strncmp(buf, d->name, IFNAMSIZ))
1119 set_bit(i, inuse);
1120 }
1121
1122 i = find_first_zero_bit(inuse, max_netdevices);
1123 free_page((unsigned long) inuse);
1124 }
1125
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001126 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001127 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* It is possible to run out of possible slots
1131 * when the name is long and there isn't enough space left
1132 * for the digits, or if all bits are used.
1133 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001134 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001137static int dev_alloc_name_ns(struct net *net,
1138 struct net_device *dev,
1139 const char *name)
1140{
1141 char buf[IFNAMSIZ];
1142 int ret;
1143
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001144 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001145 ret = __dev_alloc_name(net, name, buf);
1146 if (ret >= 0)
1147 strlcpy(dev->name, buf, IFNAMSIZ);
1148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001151/**
1152 * dev_alloc_name - allocate a name for a device
1153 * @dev: device
1154 * @name: name format string
1155 *
1156 * Passed a format string - eg "lt%d" it will try and find a suitable
1157 * id. It scans list of devices to build up a free map, then chooses
1158 * the first empty slot. The caller must hold the dev_base or rtnl lock
1159 * while allocating the name and adding the device in order to avoid
1160 * duplicates.
1161 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1162 * Returns the number of the unit assigned or a negative errno code.
1163 */
1164
1165int dev_alloc_name(struct net_device *dev, const char *name)
1166{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001167 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001168}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001169EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001170
Eric Dumazetbacb7e12019-10-08 14:20:34 -07001171static int dev_get_valid_name(struct net *net, struct net_device *dev,
1172 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001173{
David S. Miller55a5ec92018-01-02 11:45:07 -05001174 BUG_ON(!net);
1175
1176 if (!dev_valid_name(name))
1177 return -EINVAL;
1178
1179 if (strchr(name, '%'))
1180 return dev_alloc_name_ns(net, dev, name);
1181 else if (__dev_get_by_name(net, name))
1182 return -EEXIST;
1183 else if (dev->name != name)
1184 strlcpy(dev->name, name, IFNAMSIZ);
1185
1186 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189/**
1190 * dev_change_name - change name of a device
1191 * @dev: device
1192 * @newname: name (or format string) must be at least IFNAMSIZ
1193 *
1194 * Change name of a device, can pass format strings "eth%d".
1195 * for wildcarding.
1196 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001197int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Tom Gundersen238fa362014-07-14 16:37:23 +02001199 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001200 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001202 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001203 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001206 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001208 net = dev_net(dev);
Si-Wei Liu8065a772019-04-08 19:45:27 -04001209
1210 /* Some auto-enslaved devices e.g. failover slaves are
1211 * special, as userspace might rename the device after
1212 * the interface had been brought up and running since
1213 * the point kernel initiated auto-enslavement. Allow
1214 * live name change even when these slave devices are
1215 * up and running.
1216 *
1217 * Typically, users of these auto-enslaving devices
1218 * don't actually care about slave name change, as
1219 * they are supposed to operate on master interface
1220 * directly.
1221 */
1222 if (dev->flags & IFF_UP &&
1223 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return -EBUSY;
1225
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001226 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001227
1228 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001229 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001230 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001231 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001232
Herbert Xufcc5a032007-07-30 17:03:38 -07001233 memcpy(oldname, dev->name, IFNAMSIZ);
1234
Gao feng828de4f2012-09-13 20:58:27 +00001235 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001236 if (err < 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001237 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001238 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001241 if (oldname[0] && !strchr(oldname, '%'))
1242 netdev_info(dev, "renamed from %s\n", oldname);
1243
Tom Gundersen238fa362014-07-14 16:37:23 +02001244 old_assign_type = dev->name_assign_type;
1245 dev->name_assign_type = NET_NAME_RENAMED;
1246
Herbert Xufcc5a032007-07-30 17:03:38 -07001247rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001248 ret = device_rename(&dev->dev, dev->name);
1249 if (ret) {
1250 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001251 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001252 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001253 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001254 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001255
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001256 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001257
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001258 netdev_adjacent_rename_links(dev, oldname);
1259
Herbert Xu7f988ea2007-07-30 16:35:46 -07001260 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001261 netdev_name_node_del(dev->name_node);
Eric Dumazet72c95282009-10-30 07:11:27 +00001262 write_unlock_bh(&dev_base_lock);
1263
1264 synchronize_rcu();
1265
1266 write_lock_bh(&dev_base_lock);
Jiri Pirkoff927412019-09-30 11:48:15 +02001267 netdev_name_node_add(net, dev->name_node);
Herbert Xu7f988ea2007-07-30 16:35:46 -07001268 write_unlock_bh(&dev_base_lock);
1269
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001270 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001271 ret = notifier_to_errno(ret);
1272
1273 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001274 /* err >= 0 after dev_alloc_name() or stores the first errno */
1275 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001276 err = ret;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001277 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001278 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001279 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001280 dev->name_assign_type = old_assign_type;
1281 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001282 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001283 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001284 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001285 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001286 }
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 return err;
1290}
1291
1292/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001293 * dev_set_alias - change ifalias of a device
1294 * @dev: device
1295 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001296 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001297 *
1298 * Set ifalias for a device,
1299 */
1300int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1301{
Florian Westphal6c557002017-10-02 23:50:05 +02001302 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001303
1304 if (len >= IFALIASZ)
1305 return -EINVAL;
1306
Florian Westphal6c557002017-10-02 23:50:05 +02001307 if (len) {
1308 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1309 if (!new_alias)
1310 return -ENOMEM;
1311
1312 memcpy(new_alias->ifalias, alias, len);
1313 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001314 }
1315
Florian Westphal6c557002017-10-02 23:50:05 +02001316 mutex_lock(&ifalias_mutex);
Paul E. McKenneye3f0d762019-09-23 15:42:28 -07001317 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1318 mutex_is_locked(&ifalias_mutex));
Florian Westphal6c557002017-10-02 23:50:05 +02001319 mutex_unlock(&ifalias_mutex);
1320
1321 if (new_alias)
1322 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001323
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001324 return len;
1325}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001326EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001327
Florian Westphal6c557002017-10-02 23:50:05 +02001328/**
1329 * dev_get_alias - get ifalias of a device
1330 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001331 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001332 * @len: size of buffer
1333 *
1334 * get ifalias for a device. Caller must make sure dev cannot go
1335 * away, e.g. rcu read lock or own a reference count to device.
1336 */
1337int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1338{
1339 const struct dev_ifalias *alias;
1340 int ret = 0;
1341
1342 rcu_read_lock();
1343 alias = rcu_dereference(dev->ifalias);
1344 if (alias)
1345 ret = snprintf(name, len, "%s", alias->ifalias);
1346 rcu_read_unlock();
1347
1348 return ret;
1349}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001350
1351/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001352 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001353 * @dev: device to cause notification
1354 *
1355 * Called to indicate a device has changed features.
1356 */
1357void netdev_features_change(struct net_device *dev)
1358{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001359 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001360}
1361EXPORT_SYMBOL(netdev_features_change);
1362
1363/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * netdev_state_change - device changes state
1365 * @dev: device to cause notification
1366 *
1367 * Called to indicate a device has changed state. This function calls
1368 * the notifier chains for netdev_chain and sends a NEWLINK message
1369 * to the routing socket.
1370 */
1371void netdev_state_change(struct net_device *dev)
1372{
1373 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001374 struct netdev_notifier_change_info change_info = {
1375 .info.dev = dev,
1376 };
Loic Prylli54951192014-07-01 21:39:43 -07001377
David Ahern51d0c0472017-10-04 17:48:45 -07001378 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001379 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001380 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001383EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Amerigo Wangee89bab2012-08-09 22:14:56 +00001385/**
tcharding722c9a02017-02-09 17:56:04 +11001386 * netdev_notify_peers - notify network peers about existence of @dev
1387 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001388 *
1389 * Generate traffic such that interested network peers are aware of
1390 * @dev, such as by generating a gratuitous ARP. This may be used when
1391 * a device wants to inform the rest of the network about some sort of
1392 * reconfiguration such as a failover event or virtual machine
1393 * migration.
1394 */
1395void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001396{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001397 rtnl_lock();
1398 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001399 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001400 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001401}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001402EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001403
Petr Machata40c900a2018-12-06 17:05:47 +00001404static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001406 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001407 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001409 ASSERT_RTNL();
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (!netif_device_present(dev))
1412 return -ENODEV;
1413
Neil Hormanca99ca12013-02-05 08:05:43 +00001414 /* Block netpoll from trying to do any rx path servicing.
1415 * If we don't do this there is a chance ndo_poll_controller
1416 * or ndo_poll may be running while we open the device
1417 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001418 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001419
Petr Machata40c900a2018-12-06 17:05:47 +00001420 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001421 ret = notifier_to_errno(ret);
1422 if (ret)
1423 return ret;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001426
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001427 if (ops->ndo_validate_addr)
1428 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001429
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001430 if (!ret && ops->ndo_open)
1431 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Eric W. Biederman66b55522014-03-27 15:39:03 -07001433 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001434
Jeff Garzikbada3392007-10-23 20:19:37 -07001435 if (ret)
1436 clear_bit(__LINK_STATE_START, &dev->state);
1437 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001439 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001441 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return ret;
1445}
Patrick McHardybd380812010-02-26 06:34:53 +00001446
1447/**
1448 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001449 * @dev: device to open
1450 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001451 *
1452 * Takes a device from down to up state. The device's private open
1453 * function is invoked and then the multicast lists are loaded. Finally
1454 * the device is moved into the up state and a %NETDEV_UP message is
1455 * sent to the netdev notifier chain.
1456 *
1457 * Calling this function on an active interface is a nop. On a failure
1458 * a negative errno code is returned.
1459 */
Petr Machata00f54e62018-12-06 17:05:36 +00001460int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001461{
1462 int ret;
1463
Patrick McHardybd380812010-02-26 06:34:53 +00001464 if (dev->flags & IFF_UP)
1465 return 0;
1466
Petr Machata40c900a2018-12-06 17:05:47 +00001467 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001468 if (ret < 0)
1469 return ret;
1470
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001471 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001472 call_netdevice_notifiers(NETDEV_UP, dev);
1473
1474 return ret;
1475}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001476EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
stephen hemminger7051b882017-07-18 15:59:27 -07001478static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Octavian Purdila44345722010-12-13 12:44:07 +00001480 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001481
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001482 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001483 might_sleep();
1484
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001485 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001486 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001487 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001488
Octavian Purdila44345722010-12-13 12:44:07 +00001489 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Octavian Purdila44345722010-12-13 12:44:07 +00001491 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Octavian Purdila44345722010-12-13 12:44:07 +00001493 /* Synchronize to scheduled poll. We cannot touch poll list, it
1494 * can be even on different cpu. So just clear netif_running().
1495 *
1496 * dev->stop() will invoke napi_disable() on all of it's
1497 * napi_struct instances on this device.
1498 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001499 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Octavian Purdila44345722010-12-13 12:44:07 +00001502 dev_deactivate_many(head);
1503
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001504 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001505 const struct net_device_ops *ops = dev->netdev_ops;
1506
1507 /*
1508 * Call the device specific close. This cannot fail.
1509 * Only if device is UP
1510 *
1511 * We allow it to be called even after a DETACH hot-plug
1512 * event.
1513 */
1514 if (ops->ndo_stop)
1515 ops->ndo_stop(dev);
1516
Octavian Purdila44345722010-12-13 12:44:07 +00001517 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001518 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001519 }
Octavian Purdila44345722010-12-13 12:44:07 +00001520}
1521
stephen hemminger7051b882017-07-18 15:59:27 -07001522static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001523{
1524 LIST_HEAD(single);
1525
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001526 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001527 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001528 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001529}
1530
stephen hemminger7051b882017-07-18 15:59:27 -07001531void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001532{
1533 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001534
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001535 /* Remove the devices that don't need to be closed */
1536 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001537 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001538 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001539
1540 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001541
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001542 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001543 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001544 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001545 if (unlink)
1546 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
David S. Miller99c4a262015-03-18 22:52:33 -04001549EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001550
1551/**
1552 * dev_close - shutdown an interface.
1553 * @dev: device to shutdown
1554 *
1555 * This function moves an active device into down state. A
1556 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1557 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1558 * chain.
1559 */
stephen hemminger7051b882017-07-18 15:59:27 -07001560void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001561{
Eric Dumazete14a5992011-05-10 12:26:06 -07001562 if (dev->flags & IFF_UP) {
1563 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001564
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001565 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001566 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001567 list_del(&single);
1568 }
Patrick McHardybd380812010-02-26 06:34:53 +00001569}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001570EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001573/**
1574 * dev_disable_lro - disable Large Receive Offload on a device
1575 * @dev: device
1576 *
1577 * Disable Large Receive Offload (LRO) on a net device. Must be
1578 * called under RTNL. This is needed if received packets may be
1579 * forwarded to another interface.
1580 */
1581void dev_disable_lro(struct net_device *dev)
1582{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001583 struct net_device *lower_dev;
1584 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001585
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001586 dev->wanted_features &= ~NETIF_F_LRO;
1587 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001588
Michał Mirosław22d59692011-04-21 12:42:15 +00001589 if (unlikely(dev->features & NETIF_F_LRO))
1590 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001591
1592 netdev_for_each_lower_dev(dev, lower_dev, iter)
1593 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001594}
1595EXPORT_SYMBOL(dev_disable_lro);
1596
Michael Chan56f5aa72017-12-16 03:09:41 -05001597/**
1598 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1599 * @dev: device
1600 *
1601 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1602 * called under RTNL. This is needed if Generic XDP is installed on
1603 * the device.
1604 */
1605static void dev_disable_gro_hw(struct net_device *dev)
1606{
1607 dev->wanted_features &= ~NETIF_F_GRO_HW;
1608 netdev_update_features(dev);
1609
1610 if (unlikely(dev->features & NETIF_F_GRO_HW))
1611 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1612}
1613
Kirill Tkhaiede27622018-03-23 19:47:19 +03001614const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1615{
1616#define N(val) \
1617 case NETDEV_##val: \
1618 return "NETDEV_" __stringify(val);
1619 switch (cmd) {
1620 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1621 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1622 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1623 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1624 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1625 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1626 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001627 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1628 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001629 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001630 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001631#undef N
1632 return "UNKNOWN_NETDEV_EVENT";
1633}
1634EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1635
Jiri Pirko351638e2013-05-28 01:30:21 +00001636static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1637 struct net_device *dev)
1638{
David Ahern51d0c0472017-10-04 17:48:45 -07001639 struct netdev_notifier_info info = {
1640 .dev = dev,
1641 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001642
Jiri Pirko351638e2013-05-28 01:30:21 +00001643 return nb->notifier_call(nb, val, &info);
1644}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001645
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001646static int call_netdevice_register_notifiers(struct notifier_block *nb,
1647 struct net_device *dev)
1648{
1649 int err;
1650
1651 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1652 err = notifier_to_errno(err);
1653 if (err)
1654 return err;
1655
1656 if (!(dev->flags & IFF_UP))
1657 return 0;
1658
1659 call_netdevice_notifier(nb, NETDEV_UP, dev);
1660 return 0;
1661}
1662
1663static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1664 struct net_device *dev)
1665{
1666 if (dev->flags & IFF_UP) {
1667 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1668 dev);
1669 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1670 }
1671 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1672}
1673
1674static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1675 struct net *net)
1676{
1677 struct net_device *dev;
1678 int err;
1679
1680 for_each_netdev(net, dev) {
1681 err = call_netdevice_register_notifiers(nb, dev);
1682 if (err)
1683 goto rollback;
1684 }
1685 return 0;
1686
1687rollback:
1688 for_each_netdev_continue_reverse(net, dev)
1689 call_netdevice_unregister_notifiers(nb, dev);
1690 return err;
1691}
1692
1693static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1694 struct net *net)
1695{
1696 struct net_device *dev;
1697
1698 for_each_netdev(net, dev)
1699 call_netdevice_unregister_notifiers(nb, dev);
1700}
1701
Eric W. Biederman881d9662007-09-17 11:56:21 -07001702static int dev_boot_phase = 1;
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704/**
tcharding722c9a02017-02-09 17:56:04 +11001705 * register_netdevice_notifier - register a network notifier block
1706 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 *
tcharding722c9a02017-02-09 17:56:04 +11001708 * Register a notifier to be called when network device events occur.
1709 * The notifier passed is linked into the kernel structures and must
1710 * not be reused until it has been unregistered. A negative errno code
1711 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *
tcharding722c9a02017-02-09 17:56:04 +11001713 * When registered all registration and up events are replayed
1714 * to the new notifier to allow device to have a race free
1715 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
1717
1718int register_netdevice_notifier(struct notifier_block *nb)
1719{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001720 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int err;
1722
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001723 /* Close race with setup_net() and cleanup_net() */
1724 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001726 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001727 if (err)
1728 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001729 if (dev_boot_phase)
1730 goto unlock;
1731 for_each_net(net) {
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001732 err = call_netdevice_register_net_notifiers(nb, net);
1733 if (err)
1734 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001736
1737unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001739 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001741
1742rollback:
Jiri Pirkoafa0df52019-09-30 10:15:09 +02001743 for_each_net_continue_reverse(net)
1744 call_netdevice_unregister_net_notifiers(nb, net);
Herbert Xufcc5a032007-07-30 17:03:38 -07001745
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001746 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001747 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001749EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751/**
tcharding722c9a02017-02-09 17:56:04 +11001752 * unregister_netdevice_notifier - unregister a network notifier block
1753 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 *
tcharding722c9a02017-02-09 17:56:04 +11001755 * Unregister a notifier previously registered by
1756 * register_netdevice_notifier(). The notifier is unlinked into the
1757 * kernel structures and may then be reused. A negative errno code
1758 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001759 *
tcharding722c9a02017-02-09 17:56:04 +11001760 * After unregistering unregister and down device events are synthesized
1761 * for all devices on the device list to the removed notifier to remove
1762 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
1764
1765int unregister_netdevice_notifier(struct notifier_block *nb)
1766{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001767 struct net_device *dev;
1768 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001769 int err;
1770
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001771 /* Close race with setup_net() and cleanup_net() */
1772 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001773 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001774 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001775 if (err)
1776 goto unlock;
1777
1778 for_each_net(net) {
1779 for_each_netdev(net, dev) {
1780 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001781 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1782 dev);
1783 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001784 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001785 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001786 }
1787 }
1788unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001789 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001790 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001793EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795/**
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001796 * register_netdevice_notifier_net - register a per-netns network notifier block
1797 * @net: network namespace
1798 * @nb: notifier
1799 *
1800 * Register a notifier to be called when network device events occur.
1801 * The notifier passed is linked into the kernel structures and must
1802 * not be reused until it has been unregistered. A negative errno code
1803 * is returned on a failure.
1804 *
1805 * When registered all registration and up events are replayed
1806 * to the new notifier to allow device to have a race free
1807 * view of the network device list.
1808 */
1809
1810int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1811{
1812 int err;
1813
1814 rtnl_lock();
1815 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1816 if (err)
1817 goto unlock;
1818 if (dev_boot_phase)
1819 goto unlock;
1820
1821 err = call_netdevice_register_net_notifiers(nb, net);
1822 if (err)
1823 goto chain_unregister;
1824
1825unlock:
1826 rtnl_unlock();
1827 return err;
1828
1829chain_unregister:
1830 raw_notifier_chain_unregister(&netdev_chain, nb);
1831 goto unlock;
1832}
1833EXPORT_SYMBOL(register_netdevice_notifier_net);
1834
1835/**
1836 * unregister_netdevice_notifier_net - unregister a per-netns
1837 * network notifier block
1838 * @net: network namespace
1839 * @nb: notifier
1840 *
1841 * Unregister a notifier previously registered by
1842 * register_netdevice_notifier(). The notifier is unlinked into the
1843 * kernel structures and may then be reused. A negative errno code
1844 * is returned on a failure.
1845 *
1846 * After unregistering unregister and down device events are synthesized
1847 * for all devices on the device list to the removed notifier to remove
1848 * the need for special case cleanup code.
1849 */
1850
1851int unregister_netdevice_notifier_net(struct net *net,
1852 struct notifier_block *nb)
1853{
1854 int err;
1855
1856 rtnl_lock();
1857 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1858 if (err)
1859 goto unlock;
1860
1861 call_netdevice_unregister_net_notifiers(nb, net);
1862
1863unlock:
1864 rtnl_unlock();
1865 return err;
1866}
1867EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1868
1869/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001870 * call_netdevice_notifiers_info - call all network notifier blocks
1871 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00001872 * @info: notifier information data
1873 *
1874 * Call all network notifier blocks. Parameters and return value
1875 * are as for raw_notifier_call_chain().
1876 */
1877
stephen hemminger1d143d92013-12-29 14:01:29 -08001878static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001879 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001880{
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001881 struct net *net = dev_net(info->dev);
1882 int ret;
1883
Jiri Pirko351638e2013-05-28 01:30:21 +00001884 ASSERT_RTNL();
Jiri Pirkoa30c7b42019-09-30 10:15:10 +02001885
1886 /* Run per-netns notifier block chain first, then run the global one.
1887 * Hopefully, one day, the global one is going to be removed after
1888 * all notifier block registrators get converted to be per-netns.
1889 */
1890 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1891 if (ret & NOTIFY_STOP_MASK)
1892 return ret;
Jiri Pirko351638e2013-05-28 01:30:21 +00001893 return raw_notifier_call_chain(&netdev_chain, val, info);
1894}
Jiri Pirko351638e2013-05-28 01:30:21 +00001895
Petr Machata26372602018-12-06 17:05:45 +00001896static int call_netdevice_notifiers_extack(unsigned long val,
1897 struct net_device *dev,
1898 struct netlink_ext_ack *extack)
1899{
1900 struct netdev_notifier_info info = {
1901 .dev = dev,
1902 .extack = extack,
1903 };
1904
1905 return call_netdevice_notifiers_info(val, &info);
1906}
1907
Jiri Pirko351638e2013-05-28 01:30:21 +00001908/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 * call_netdevice_notifiers - call all network notifier blocks
1910 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001911 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *
1913 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001914 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
1916
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001917int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Petr Machata26372602018-12-06 17:05:45 +00001919 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001921EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001923/**
1924 * call_netdevice_notifiers_mtu - call all network notifier blocks
1925 * @val: value passed unmodified to notifier function
1926 * @dev: net_device pointer passed unmodified to notifier function
1927 * @arg: additional u32 argument passed to the notifier function
1928 *
1929 * Call all network notifier blocks. Parameters and return value
1930 * are as for raw_notifier_call_chain().
1931 */
1932static int call_netdevice_notifiers_mtu(unsigned long val,
1933 struct net_device *dev, u32 arg)
1934{
1935 struct netdev_notifier_info_ext info = {
1936 .info.dev = dev,
1937 .ext.mtu = arg,
1938 };
1939
1940 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1941
1942 return call_netdevice_notifiers_info(val, &info.info);
1943}
1944
Pablo Neira1cf519002015-05-13 18:19:37 +02001945#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001946static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001947
1948void net_inc_ingress_queue(void)
1949{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001950 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001951}
1952EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1953
1954void net_dec_ingress_queue(void)
1955{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001956 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001957}
1958EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1959#endif
1960
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001961#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001962static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001963
1964void net_inc_egress_queue(void)
1965{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001966 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001967}
1968EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1969
1970void net_dec_egress_queue(void)
1971{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001972 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001973}
1974EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1975#endif
1976
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001977static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001978#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00001979static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08001980static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001981static void netstamp_clear(struct work_struct *work)
1982{
1983 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08001984 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001985
Eric Dumazet13baa002017-03-01 14:28:39 -08001986 wanted = atomic_add_return(deferred, &netstamp_wanted);
1987 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001988 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08001989 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001990 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001991}
1992static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995void net_enable_timestamp(void)
1996{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001997#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08001998 int wanted;
1999
2000 while (1) {
2001 wanted = atomic_read(&netstamp_wanted);
2002 if (wanted <= 0)
2003 break;
2004 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2005 return;
2006 }
2007 atomic_inc(&netstamp_needed_deferred);
2008 schedule_work(&netstamp_work);
2009#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002010 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08002011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002013EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015void net_disable_timestamp(void)
2016{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09002017#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08002018 int wanted;
2019
2020 while (1) {
2021 wanted = atomic_read(&netstamp_wanted);
2022 if (wanted <= 1)
2023 break;
2024 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2025 return;
2026 }
2027 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002028 schedule_work(&netstamp_work);
2029#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002030 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08002031#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002033EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Eric Dumazet3b098e22010-05-15 23:57:10 -07002035static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Thomas Gleixner2456e852016-12-25 11:38:40 +01002037 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002038 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002039 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Davidlohr Bueso39e83922018-05-08 09:07:01 -07002042#define net_timestamp_check(COND, SKB) \
2043 if (static_branch_unlikely(&netstamp_needed_key)) { \
2044 if ((COND) && !(SKB)->tstamp) \
2045 __net_timestamp(SKB); \
2046 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07002047
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02002048bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002049{
2050 unsigned int len;
2051
2052 if (!(dev->flags & IFF_UP))
2053 return false;
2054
2055 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2056 if (skb->len <= len)
2057 return true;
2058
2059 /* if TSO is enabled, we don't care about the length as the packet
2060 * could be forwarded without being segmented before
2061 */
2062 if (skb_is_gso(skb))
2063 return true;
2064
2065 return false;
2066}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04002067EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07002068
Herbert Xua0265d22014-04-17 13:45:03 +08002069int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2070{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002071 int ret = ____dev_forward_skb(dev, skb);
2072
2073 if (likely(!ret)) {
2074 skb->protocol = eth_type_trans(skb, dev);
2075 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08002076 }
2077
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08002078 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08002079}
2080EXPORT_SYMBOL_GPL(__dev_forward_skb);
2081
Arnd Bergmann44540962009-11-26 06:07:08 +00002082/**
2083 * dev_forward_skb - loopback an skb to another netif
2084 *
2085 * @dev: destination network device
2086 * @skb: buffer to forward
2087 *
2088 * return values:
2089 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07002090 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00002091 *
2092 * dev_forward_skb can be used for injecting an skb from the
2093 * start_xmit function of one device into the receive queue
2094 * of another device.
2095 *
2096 * The receiving device may be in another namespace, so
2097 * we have to clear all information in the skb that could
2098 * impact namespace isolation.
2099 */
2100int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2101{
Herbert Xua0265d22014-04-17 13:45:03 +08002102 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00002103}
2104EXPORT_SYMBOL_GPL(dev_forward_skb);
2105
Changli Gao71d9dec2010-12-15 19:57:25 +00002106static inline int deliver_skb(struct sk_buff *skb,
2107 struct packet_type *pt_prev,
2108 struct net_device *orig_dev)
2109{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002110 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00002111 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03002112 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00002113 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2114}
2115
Salam Noureddine7866a622015-01-27 11:35:48 -08002116static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2117 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002118 struct net_device *orig_dev,
2119 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08002120 struct list_head *ptype_list)
2121{
2122 struct packet_type *ptype, *pt_prev = *pt;
2123
2124 list_for_each_entry_rcu(ptype, ptype_list, list) {
2125 if (ptype->type != type)
2126 continue;
2127 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02002128 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08002129 pt_prev = ptype;
2130 }
2131 *pt = pt_prev;
2132}
2133
Eric Leblondc0de08d2012-08-16 22:02:58 +00002134static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2135{
Eric Leblonda3d744e2012-11-06 02:10:10 +00002136 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00002137 return false;
2138
2139 if (ptype->id_match)
2140 return ptype->id_match(ptype, skb->sk);
2141 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2142 return true;
2143
2144 return false;
2145}
2146
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01002147/**
2148 * dev_nit_active - return true if any network interface taps are in use
2149 *
2150 * @dev: network device to check for the presence of taps
2151 */
2152bool dev_nit_active(struct net_device *dev)
2153{
2154 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2155}
2156EXPORT_SYMBOL_GPL(dev_nit_active);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158/*
2159 * Support routine. Sends outgoing frames to any network
2160 * taps currently in use.
2161 */
2162
David Ahern74b20582016-05-10 11:19:50 -07002163void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002166 struct sk_buff *skb2 = NULL;
2167 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002168 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002171again:
2172 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002173 if (ptype->ignore_outgoing)
2174 continue;
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /* Never send packets back to the socket
2177 * they originated from - MvS (miquels@drinkel.ow.org)
2178 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002179 if (skb_loop_sk(ptype, skb))
2180 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002181
Salam Noureddine7866a622015-01-27 11:35:48 -08002182 if (pt_prev) {
2183 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002184 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002185 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002187
2188 /* need to clone skb, done only once */
2189 skb2 = skb_clone(skb, GFP_ATOMIC);
2190 if (!skb2)
2191 goto out_unlock;
2192
2193 net_timestamp_set(skb2);
2194
2195 /* skb->nh should be correctly
2196 * set by sender, so that the second statement is
2197 * just protection against buggy protocols.
2198 */
2199 skb_reset_mac_header(skb2);
2200
2201 if (skb_network_header(skb2) < skb2->data ||
2202 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2203 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2204 ntohs(skb2->protocol),
2205 dev->name);
2206 skb_reset_network_header(skb2);
2207 }
2208
2209 skb2->transport_header = skb2->network_header;
2210 skb2->pkt_type = PACKET_OUTGOING;
2211 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002213
2214 if (ptype_list == &ptype_all) {
2215 ptype_list = &dev->ptype_all;
2216 goto again;
2217 }
2218out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002219 if (pt_prev) {
2220 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2221 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2222 else
2223 kfree_skb(skb2);
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 rcu_read_unlock();
2226}
David Ahern74b20582016-05-10 11:19:50 -07002227EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Ben Hutchings2c530402012-07-10 10:55:09 +00002229/**
2230 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002231 * @dev: Network device
2232 * @txq: number of queues available
2233 *
2234 * If real_num_tx_queues is changed the tc mappings may no longer be
2235 * valid. To resolve this verify the tc mapping remains valid and if
2236 * not NULL the mapping. With no priorities mapping to this
2237 * offset/count pair it will no longer be used. In the worst case TC0
2238 * is invalid nothing can be done so disable priority mappings. If is
2239 * expected that drivers will fix this mapping if they can before
2240 * calling netif_set_real_num_tx_queues.
2241 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002242static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002243{
2244 int i;
2245 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2246
2247 /* If TC0 is invalidated disable TC mapping */
2248 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002249 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002250 dev->num_tc = 0;
2251 return;
2252 }
2253
2254 /* Invalidated prio to tc mappings set to TC0 */
2255 for (i = 1; i < TC_BITMASK + 1; i++) {
2256 int q = netdev_get_prio_tc_map(dev, i);
2257
2258 tc = &dev->tc_to_txq[q];
2259 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002260 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2261 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002262 netdev_set_prio_tc_map(dev, i, 0);
2263 }
2264 }
2265}
2266
Alexander Duyck8d059b02016-10-28 11:43:49 -04002267int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2268{
2269 if (dev->num_tc) {
2270 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2271 int i;
2272
Alexander Duyckffcfe252018-07-09 12:19:38 -04002273 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002274 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2275 if ((txq - tc->offset) < tc->count)
2276 return i;
2277 }
2278
Alexander Duyckffcfe252018-07-09 12:19:38 -04002279 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002280 return -1;
2281 }
2282
2283 return 0;
2284}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002285EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002286
Alexander Duyck537c00d2013-01-10 08:57:02 +00002287#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002288struct static_key xps_needed __read_mostly;
2289EXPORT_SYMBOL(xps_needed);
2290struct static_key xps_rxqs_needed __read_mostly;
2291EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002292static DEFINE_MUTEX(xps_map_mutex);
2293#define xmap_dereference(P) \
2294 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2295
Alexander Duyck6234f872016-10-28 11:46:49 -04002296static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2297 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002298{
2299 struct xps_map *map = NULL;
2300 int pos;
2301
2302 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002303 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002304 if (!map)
2305 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002306
Alexander Duyck6234f872016-10-28 11:46:49 -04002307 for (pos = map->len; pos--;) {
2308 if (map->queues[pos] != index)
2309 continue;
2310
2311 if (map->len > 1) {
2312 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002313 break;
2314 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002315
Amritha Nambiar80d19662018-06-29 21:26:41 -07002316 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002317 kfree_rcu(map, rcu);
2318 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002319 }
2320
Alexander Duyck6234f872016-10-28 11:46:49 -04002321 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002322}
2323
Alexander Duyck6234f872016-10-28 11:46:49 -04002324static bool remove_xps_queue_cpu(struct net_device *dev,
2325 struct xps_dev_maps *dev_maps,
2326 int cpu, u16 offset, u16 count)
2327{
Alexander Duyck184c4492016-10-28 11:50:13 -04002328 int num_tc = dev->num_tc ? : 1;
2329 bool active = false;
2330 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002331
Alexander Duyck184c4492016-10-28 11:50:13 -04002332 for (tci = cpu * num_tc; num_tc--; tci++) {
2333 int i, j;
2334
2335 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002336 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002337 break;
2338 }
2339
2340 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002341 }
2342
Alexander Duyck184c4492016-10-28 11:50:13 -04002343 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002344}
2345
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002346static void reset_xps_maps(struct net_device *dev,
2347 struct xps_dev_maps *dev_maps,
2348 bool is_rxqs_map)
2349{
2350 if (is_rxqs_map) {
2351 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2352 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2353 } else {
2354 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2355 }
2356 static_key_slow_dec_cpuslocked(&xps_needed);
2357 kfree_rcu(dev_maps, rcu);
2358}
2359
Amritha Nambiar80d19662018-06-29 21:26:41 -07002360static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2361 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2362 u16 offset, u16 count, bool is_rxqs_map)
2363{
2364 bool active = false;
2365 int i, j;
2366
2367 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2368 j < nr_ids;)
2369 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2370 count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002371 if (!active)
2372 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002373
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002374 if (!is_rxqs_map) {
2375 for (i = offset + (count - 1); count--; i--) {
2376 netdev_queue_numa_node_write(
2377 netdev_get_tx_queue(dev, i),
2378 NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002379 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002380 }
2381}
2382
Alexander Duyck6234f872016-10-28 11:46:49 -04002383static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2384 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002385{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002386 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002387 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002388 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002389
Amritha Nambiar04157462018-06-29 21:26:46 -07002390 if (!static_key_false(&xps_needed))
2391 return;
2392
Andrei Vagin4d99f662018-08-08 20:07:35 -07002393 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002394 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002395
Amritha Nambiar04157462018-06-29 21:26:46 -07002396 if (static_key_false(&xps_rxqs_needed)) {
2397 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2398 if (dev_maps) {
2399 nr_ids = dev->num_rx_queues;
2400 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2401 offset, count, true);
2402 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002403 }
2404
2405 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002406 if (!dev_maps)
2407 goto out_no_maps;
2408
Amritha Nambiar80d19662018-06-29 21:26:41 -07002409 if (num_possible_cpus() > 1)
2410 possible_mask = cpumask_bits(cpu_possible_mask);
2411 nr_ids = nr_cpu_ids;
2412 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2413 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002414
Alexander Duyck537c00d2013-01-10 08:57:02 +00002415out_no_maps:
2416 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002417 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002418}
2419
Alexander Duyck6234f872016-10-28 11:46:49 -04002420static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2421{
2422 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2423}
2424
Amritha Nambiar80d19662018-06-29 21:26:41 -07002425static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2426 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002427{
2428 struct xps_map *new_map;
2429 int alloc_len = XPS_MIN_MAP_ALLOC;
2430 int i, pos;
2431
2432 for (pos = 0; map && pos < map->len; pos++) {
2433 if (map->queues[pos] != index)
2434 continue;
2435 return map;
2436 }
2437
Amritha Nambiar80d19662018-06-29 21:26:41 -07002438 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002439 if (map) {
2440 if (pos < map->alloc_len)
2441 return map;
2442
2443 alloc_len = map->alloc_len * 2;
2444 }
2445
Amritha Nambiar80d19662018-06-29 21:26:41 -07002446 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2447 * map
2448 */
2449 if (is_rxqs_map)
2450 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2451 else
2452 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2453 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002454 if (!new_map)
2455 return NULL;
2456
2457 for (i = 0; i < pos; i++)
2458 new_map->queues[i] = map->queues[i];
2459 new_map->alloc_len = alloc_len;
2460 new_map->len = pos;
2461
2462 return new_map;
2463}
2464
Andrei Vagin4d99f662018-08-08 20:07:35 -07002465/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002466int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2467 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002468{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002469 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002470 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002471 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002472 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002473 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002474 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002475 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002476
Alexander Duyck184c4492016-10-28 11:50:13 -04002477 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002478 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002479 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002480 if (num_tc < 0)
2481 return -EINVAL;
2482
2483 /* If queue belongs to subordinate dev use its map */
2484 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2485
Alexander Duyck184c4492016-10-28 11:50:13 -04002486 tc = netdev_txq_to_tc(dev, index);
2487 if (tc < 0)
2488 return -EINVAL;
2489 }
2490
Amritha Nambiar80d19662018-06-29 21:26:41 -07002491 mutex_lock(&xps_map_mutex);
2492 if (is_rxqs_map) {
2493 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2494 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2495 nr_ids = dev->num_rx_queues;
2496 } else {
2497 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2498 if (num_possible_cpus() > 1) {
2499 online_mask = cpumask_bits(cpu_online_mask);
2500 possible_mask = cpumask_bits(cpu_possible_mask);
2501 }
2502 dev_maps = xmap_dereference(dev->xps_cpus_map);
2503 nr_ids = nr_cpu_ids;
2504 }
2505
Alexander Duyck184c4492016-10-28 11:50:13 -04002506 if (maps_sz < L1_CACHE_BYTES)
2507 maps_sz = L1_CACHE_BYTES;
2508
Alexander Duyck01c5f862013-01-10 08:57:35 +00002509 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002510 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2511 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002512 if (!new_dev_maps)
2513 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002514 if (!new_dev_maps) {
2515 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002516 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002517 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002518
Amritha Nambiar80d19662018-06-29 21:26:41 -07002519 tci = j * num_tc + tc;
2520 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002521 NULL;
2522
Amritha Nambiar80d19662018-06-29 21:26:41 -07002523 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002524 if (!map)
2525 goto error;
2526
Amritha Nambiar80d19662018-06-29 21:26:41 -07002527 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002528 }
2529
2530 if (!new_dev_maps)
2531 goto out_no_new_maps;
2532
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002533 if (!dev_maps) {
2534 /* Increment static keys at most once per type */
2535 static_key_slow_inc_cpuslocked(&xps_needed);
2536 if (is_rxqs_map)
2537 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2538 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002539
Amritha Nambiar80d19662018-06-29 21:26:41 -07002540 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2541 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002542 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002543 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002544 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002545 map = xmap_dereference(dev_maps->attr_map[tci]);
2546 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002547 }
2548
2549 /* We need to explicitly update tci as prevous loop
2550 * could break out early if dev_maps is NULL.
2551 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002552 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002553
Amritha Nambiar80d19662018-06-29 21:26:41 -07002554 if (netif_attr_test_mask(j, mask, nr_ids) &&
2555 netif_attr_test_online(j, online_mask, nr_ids)) {
2556 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002557 int pos = 0;
2558
Amritha Nambiar80d19662018-06-29 21:26:41 -07002559 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002560 while ((pos < map->len) && (map->queues[pos] != index))
2561 pos++;
2562
2563 if (pos == map->len)
2564 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002565#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002566 if (!is_rxqs_map) {
2567 if (numa_node_id == -2)
2568 numa_node_id = cpu_to_node(j);
2569 else if (numa_node_id != cpu_to_node(j))
2570 numa_node_id = -1;
2571 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002572#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002573 } else if (dev_maps) {
2574 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002575 map = xmap_dereference(dev_maps->attr_map[tci]);
2576 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002577 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002578
Alexander Duyck184c4492016-10-28 11:50:13 -04002579 /* copy maps belonging to foreign traffic classes */
2580 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2581 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002582 map = xmap_dereference(dev_maps->attr_map[tci]);
2583 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002584 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002585 }
2586
Amritha Nambiar80d19662018-06-29 21:26:41 -07002587 if (is_rxqs_map)
2588 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2589 else
2590 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002591
Alexander Duyck537c00d2013-01-10 08:57:02 +00002592 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002593 if (!dev_maps)
2594 goto out_no_old_maps;
2595
Amritha Nambiar80d19662018-06-29 21:26:41 -07002596 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2597 j < nr_ids;) {
2598 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2599 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2600 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002601 if (map && map != new_map)
2602 kfree_rcu(map, rcu);
2603 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002604 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002605
Alexander Duyck184c4492016-10-28 11:50:13 -04002606 kfree_rcu(dev_maps, rcu);
2607
2608out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002609 dev_maps = new_dev_maps;
2610 active = true;
2611
2612out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002613 if (!is_rxqs_map) {
2614 /* update Tx queue numa node */
2615 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2616 (numa_node_id >= 0) ?
2617 numa_node_id : NUMA_NO_NODE);
2618 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002619
Alexander Duyck01c5f862013-01-10 08:57:35 +00002620 if (!dev_maps)
2621 goto out_no_maps;
2622
Amritha Nambiar80d19662018-06-29 21:26:41 -07002623 /* removes tx-queue from unused CPUs/rx-queues */
2624 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2625 j < nr_ids;) {
2626 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002627 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002628 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2629 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002630 active |= remove_xps_queue(dev_maps, tci, index);
2631 for (i = num_tc - tc, tci++; --i; tci++)
2632 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002633 }
2634
2635 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002636 if (!active)
2637 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002638
2639out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002640 mutex_unlock(&xps_map_mutex);
2641
2642 return 0;
2643error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002644 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002645 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2646 j < nr_ids;) {
2647 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2648 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002649 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002650 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002651 NULL;
2652 if (new_map && new_map != map)
2653 kfree(new_map);
2654 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002655 }
2656
Alexander Duyck537c00d2013-01-10 08:57:02 +00002657 mutex_unlock(&xps_map_mutex);
2658
Alexander Duyck537c00d2013-01-10 08:57:02 +00002659 kfree(new_dev_maps);
2660 return -ENOMEM;
2661}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002662EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002663
2664int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2665 u16 index)
2666{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002667 int ret;
2668
2669 cpus_read_lock();
2670 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2671 cpus_read_unlock();
2672
2673 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002674}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002675EXPORT_SYMBOL(netif_set_xps_queue);
2676
2677#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002678static void netdev_unbind_all_sb_channels(struct net_device *dev)
2679{
2680 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2681
2682 /* Unbind any subordinate channels */
2683 while (txq-- != &dev->_tx[0]) {
2684 if (txq->sb_dev)
2685 netdev_unbind_sb_channel(dev, txq->sb_dev);
2686 }
2687}
2688
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002689void netdev_reset_tc(struct net_device *dev)
2690{
Alexander Duyck6234f872016-10-28 11:46:49 -04002691#ifdef CONFIG_XPS
2692 netif_reset_xps_queues_gt(dev, 0);
2693#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002694 netdev_unbind_all_sb_channels(dev);
2695
2696 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002697 dev->num_tc = 0;
2698 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2699 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2700}
2701EXPORT_SYMBOL(netdev_reset_tc);
2702
2703int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2704{
2705 if (tc >= dev->num_tc)
2706 return -EINVAL;
2707
Alexander Duyck6234f872016-10-28 11:46:49 -04002708#ifdef CONFIG_XPS
2709 netif_reset_xps_queues(dev, offset, count);
2710#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002711 dev->tc_to_txq[tc].count = count;
2712 dev->tc_to_txq[tc].offset = offset;
2713 return 0;
2714}
2715EXPORT_SYMBOL(netdev_set_tc_queue);
2716
2717int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2718{
2719 if (num_tc > TC_MAX_QUEUE)
2720 return -EINVAL;
2721
Alexander Duyck6234f872016-10-28 11:46:49 -04002722#ifdef CONFIG_XPS
2723 netif_reset_xps_queues_gt(dev, 0);
2724#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002725 netdev_unbind_all_sb_channels(dev);
2726
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002727 dev->num_tc = num_tc;
2728 return 0;
2729}
2730EXPORT_SYMBOL(netdev_set_num_tc);
2731
Alexander Duyckffcfe252018-07-09 12:19:38 -04002732void netdev_unbind_sb_channel(struct net_device *dev,
2733 struct net_device *sb_dev)
2734{
2735 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2736
2737#ifdef CONFIG_XPS
2738 netif_reset_xps_queues_gt(sb_dev, 0);
2739#endif
2740 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2741 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2742
2743 while (txq-- != &dev->_tx[0]) {
2744 if (txq->sb_dev == sb_dev)
2745 txq->sb_dev = NULL;
2746 }
2747}
2748EXPORT_SYMBOL(netdev_unbind_sb_channel);
2749
2750int netdev_bind_sb_channel_queue(struct net_device *dev,
2751 struct net_device *sb_dev,
2752 u8 tc, u16 count, u16 offset)
2753{
2754 /* Make certain the sb_dev and dev are already configured */
2755 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2756 return -EINVAL;
2757
2758 /* We cannot hand out queues we don't have */
2759 if ((offset + count) > dev->real_num_tx_queues)
2760 return -EINVAL;
2761
2762 /* Record the mapping */
2763 sb_dev->tc_to_txq[tc].count = count;
2764 sb_dev->tc_to_txq[tc].offset = offset;
2765
2766 /* Provide a way for Tx queue to find the tc_to_txq map or
2767 * XPS map for itself.
2768 */
2769 while (count--)
2770 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2771
2772 return 0;
2773}
2774EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2775
2776int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2777{
2778 /* Do not use a multiqueue device to represent a subordinate channel */
2779 if (netif_is_multiqueue(dev))
2780 return -ENODEV;
2781
2782 /* We allow channels 1 - 32767 to be used for subordinate channels.
2783 * Channel 0 is meant to be "native" mode and used only to represent
2784 * the main root device. We allow writing 0 to reset the device back
2785 * to normal mode after being used as a subordinate channel.
2786 */
2787 if (channel > S16_MAX)
2788 return -EINVAL;
2789
2790 dev->num_tc = -channel;
2791
2792 return 0;
2793}
2794EXPORT_SYMBOL(netdev_set_sb_channel);
2795
John Fastabendf0796d52010-07-01 13:21:57 +00002796/*
2797 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002798 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002799 */
Tom Herberte6484932010-10-18 18:04:39 +00002800int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002801{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002802 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002803 int rc;
2804
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002805 disabling = txq < dev->real_num_tx_queues;
2806
Tom Herberte6484932010-10-18 18:04:39 +00002807 if (txq < 1 || txq > dev->num_tx_queues)
2808 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002809
Ben Hutchings5c565802011-02-15 19:39:21 +00002810 if (dev->reg_state == NETREG_REGISTERED ||
2811 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002812 ASSERT_RTNL();
2813
Tom Herbert1d24eb42010-11-21 13:17:27 +00002814 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2815 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002816 if (rc)
2817 return rc;
2818
John Fastabend4f57c082011-01-17 08:06:04 +00002819 if (dev->num_tc)
2820 netif_setup_tc(dev, txq);
2821
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002822 dev->real_num_tx_queues = txq;
2823
2824 if (disabling) {
2825 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002826 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002827#ifdef CONFIG_XPS
2828 netif_reset_xps_queues_gt(dev, txq);
2829#endif
2830 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002831 } else {
2832 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002833 }
Tom Herberte6484932010-10-18 18:04:39 +00002834
Tom Herberte6484932010-10-18 18:04:39 +00002835 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002836}
2837EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002838
Michael Daltona953be52014-01-16 22:23:28 -08002839#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002840/**
2841 * netif_set_real_num_rx_queues - set actual number of RX queues used
2842 * @dev: Network device
2843 * @rxq: Actual number of RX queues
2844 *
2845 * This must be called either with the rtnl_lock held or before
2846 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002847 * negative error code. If called before registration, it always
2848 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002849 */
2850int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2851{
2852 int rc;
2853
Tom Herbertbd25fa72010-10-18 18:00:16 +00002854 if (rxq < 1 || rxq > dev->num_rx_queues)
2855 return -EINVAL;
2856
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002857 if (dev->reg_state == NETREG_REGISTERED) {
2858 ASSERT_RTNL();
2859
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002860 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2861 rxq);
2862 if (rc)
2863 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002864 }
2865
2866 dev->real_num_rx_queues = rxq;
2867 return 0;
2868}
2869EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2870#endif
2871
Ben Hutchings2c530402012-07-10 10:55:09 +00002872/**
2873 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002874 *
2875 * This routine should set an upper limit on the number of RSS queues
2876 * used by default by multiqueue devices.
2877 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002878int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002879{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302880 return is_kdump_kernel() ?
2881 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002882}
2883EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2884
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002885static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002886{
2887 struct softnet_data *sd;
2888 unsigned long flags;
2889
2890 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002891 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002892 q->next_sched = NULL;
2893 *sd->output_queue_tailp = q;
2894 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002895 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2896 local_irq_restore(flags);
2897}
2898
David S. Miller37437bb2008-07-16 02:15:04 -07002899void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002900{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002901 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2902 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002903}
2904EXPORT_SYMBOL(__netif_schedule);
2905
Eric Dumazete6247022013-12-05 04:45:08 -08002906struct dev_kfree_skb_cb {
2907 enum skb_free_reason reason;
2908};
2909
2910static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002911{
Eric Dumazete6247022013-12-05 04:45:08 -08002912 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002913}
Denis Vlasenko56079432006-03-29 15:57:29 -08002914
John Fastabend46e5da40a2014-09-12 20:04:52 -07002915void netif_schedule_queue(struct netdev_queue *txq)
2916{
2917 rcu_read_lock();
Julio Faracco5be55152019-10-01 11:39:04 -03002918 if (!netif_xmit_stopped(txq)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -07002919 struct Qdisc *q = rcu_dereference(txq->qdisc);
2920
2921 __netif_schedule(q);
2922 }
2923 rcu_read_unlock();
2924}
2925EXPORT_SYMBOL(netif_schedule_queue);
2926
John Fastabend46e5da40a2014-09-12 20:04:52 -07002927void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2928{
2929 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2930 struct Qdisc *q;
2931
2932 rcu_read_lock();
2933 q = rcu_dereference(dev_queue->qdisc);
2934 __netif_schedule(q);
2935 rcu_read_unlock();
2936 }
2937}
2938EXPORT_SYMBOL(netif_tx_wake_queue);
2939
Eric Dumazete6247022013-12-05 04:45:08 -08002940void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2941{
2942 unsigned long flags;
2943
Myungho Jung98998862017-04-25 11:58:15 -07002944 if (unlikely(!skb))
2945 return;
2946
Reshetova, Elena63354792017-06-30 13:07:58 +03002947 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08002948 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03002949 refcount_set(&skb->users, 0);
2950 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08002951 return;
2952 }
2953 get_kfree_skb_cb(skb)->reason = reason;
2954 local_irq_save(flags);
2955 skb->next = __this_cpu_read(softnet_data.completion_queue);
2956 __this_cpu_write(softnet_data.completion_queue, skb);
2957 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2958 local_irq_restore(flags);
2959}
2960EXPORT_SYMBOL(__dev_kfree_skb_irq);
2961
2962void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08002963{
2964 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08002965 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08002966 else
2967 dev_kfree_skb(skb);
2968}
Eric Dumazete6247022013-12-05 04:45:08 -08002969EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08002970
2971
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002972/**
2973 * netif_device_detach - mark device as removed
2974 * @dev: network device
2975 *
2976 * Mark device as removed from system and therefore no longer available.
2977 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002978void netif_device_detach(struct net_device *dev)
2979{
2980 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2981 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002982 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002983 }
2984}
2985EXPORT_SYMBOL(netif_device_detach);
2986
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002987/**
2988 * netif_device_attach - mark device as attached
2989 * @dev: network device
2990 *
2991 * Mark device as attached from system and restart if needed.
2992 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002993void netif_device_attach(struct net_device *dev)
2994{
2995 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2996 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002997 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002998 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002999 }
3000}
3001EXPORT_SYMBOL(netif_device_attach);
3002
Jiri Pirko5605c762015-05-12 14:56:12 +02003003/*
3004 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3005 * to be used as a distribution range.
3006 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003007static u16 skb_tx_hash(const struct net_device *dev,
3008 const struct net_device *sb_dev,
3009 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02003010{
3011 u32 hash;
3012 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04003013 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02003014
Alexander Duyckeadec8772018-07-09 12:19:48 -04003015 if (dev->num_tc) {
3016 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3017
3018 qoffset = sb_dev->tc_to_txq[tc].offset;
3019 qcount = sb_dev->tc_to_txq[tc].count;
3020 }
3021
Jiri Pirko5605c762015-05-12 14:56:12 +02003022 if (skb_rx_queue_recorded(skb)) {
3023 hash = skb_get_rx_queue(skb);
Alexander Duyck1b837d42018-04-27 14:06:53 -04003024 while (unlikely(hash >= qcount))
3025 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04003026 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02003027 }
3028
3029 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3030}
Jiri Pirko5605c762015-05-12 14:56:12 +02003031
Ben Hutchings36c92472012-01-17 07:57:56 +00003032static void skb_warn_bad_offload(const struct sk_buff *skb)
3033{
Wei Tang84d15ae2016-06-16 21:17:49 +08003034 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00003035 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01003036 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00003037
Ben Greearc846ad92013-04-19 10:45:52 +00003038 if (!net_ratelimit())
3039 return;
3040
Bjørn Mork88ad4172015-11-16 19:16:40 +01003041 if (dev) {
3042 if (dev->dev.parent)
3043 name = dev_driver_string(dev->dev.parent);
3044 else
3045 name = netdev_name(dev);
3046 }
Willem de Bruijn64131392019-07-07 05:51:55 -04003047 skb_dump(KERN_WARNING, skb, false);
3048 WARN(1, "%s: caps=(%pNF, %pNF)\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01003049 name, dev ? &dev->features : &null_features,
Willem de Bruijn64131392019-07-07 05:51:55 -04003050 skb->sk ? &skb->sk->sk_route_caps : &null_features);
Ben Hutchings36c92472012-01-17 07:57:56 +00003051}
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053/*
3054 * Invalidate hardware checksum when packet is to be mangled, and
3055 * complete checksum manually on outgoing path.
3056 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07003057int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Al Virod3bc23e2006-11-14 21:24:49 -08003059 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07003060 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Patrick McHardy84fa7932006-08-29 16:44:56 -07003062 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07003063 goto out_set_summed;
3064
3065 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00003066 skb_warn_bad_offload(skb);
3067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069
Eric Dumazetcef401d2013-01-25 20:34:37 +00003070 /* Before computing a checksum, we should make sure no frag could
3071 * be modified by an external entity : checksum could be wrong.
3072 */
3073 if (skb_has_shared_frag(skb)) {
3074 ret = __skb_linearize(skb);
3075 if (ret)
3076 goto out;
3077 }
3078
Michał Mirosław55508d62010-12-14 15:24:08 +00003079 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07003080 BUG_ON(offset >= skb_headlen(skb));
3081 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3082
3083 offset += skb->csum_offset;
3084 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3085
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003086 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3087 if (ret)
3088 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07003090 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07003091out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return ret;
3095}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003096EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Davide Carattib72b5bf2017-05-18 15:44:38 +02003098int skb_crc32c_csum_help(struct sk_buff *skb)
3099{
3100 __le32 crc32c_csum;
3101 int ret = 0, offset, start;
3102
3103 if (skb->ip_summed != CHECKSUM_PARTIAL)
3104 goto out;
3105
3106 if (unlikely(skb_is_gso(skb)))
3107 goto out;
3108
3109 /* Before computing a checksum, we should make sure no frag could
3110 * be modified by an external entity : checksum could be wrong.
3111 */
3112 if (unlikely(skb_has_shared_frag(skb))) {
3113 ret = __skb_linearize(skb);
3114 if (ret)
3115 goto out;
3116 }
3117 start = skb_checksum_start_offset(skb);
3118 offset = start + offsetof(struct sctphdr, checksum);
3119 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3120 ret = -EINVAL;
3121 goto out;
3122 }
Heiner Kallweit8211fbf2019-10-06 18:52:43 +02003123
3124 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3125 if (ret)
3126 goto out;
3127
Davide Carattib72b5bf2017-05-18 15:44:38 +02003128 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3129 skb->len - start, ~(__u32)0,
3130 crc32c_csum_stub));
3131 *(__le32 *)(skb->data + offset) = crc32c_csum;
3132 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02003133 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02003134out:
3135 return ret;
3136}
3137
Vlad Yasevich53d64712014-03-27 17:26:18 -04003138__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003139{
3140 __be16 type = skb->protocol;
3141
Pravin B Shelar19acc322013-05-07 20:41:07 +00003142 /* Tunnel gso handlers can set protocol to ethernet. */
3143 if (type == htons(ETH_P_TEB)) {
3144 struct ethhdr *eth;
3145
3146 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3147 return 0;
3148
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003149 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003150 type = eth->h_proto;
3151 }
3152
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003153 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003154}
3155
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003156/**
3157 * skb_mac_gso_segment - mac layer segmentation handler.
3158 * @skb: buffer to segment
3159 * @features: features for the output path (see dev->features)
3160 */
3161struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3162 netdev_features_t features)
3163{
3164 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3165 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003166 int vlan_depth = skb->mac_len;
3167 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003168
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003169 if (unlikely(!type))
3170 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003171
Vlad Yasevich53d64712014-03-27 17:26:18 -04003172 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003173
3174 rcu_read_lock();
3175 list_for_each_entry_rcu(ptype, &offload_base, list) {
3176 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003177 segs = ptype->callbacks.gso_segment(skb, features);
3178 break;
3179 }
3180 }
3181 rcu_read_unlock();
3182
3183 __skb_push(skb, skb->data - skb_mac_header(skb));
3184
3185 return segs;
3186}
3187EXPORT_SYMBOL(skb_mac_gso_segment);
3188
3189
Cong Wang12b00042013-02-05 16:36:38 +00003190/* openvswitch calls this on rx path, so we need a different check.
3191 */
3192static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3193{
3194 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003195 return skb->ip_summed != CHECKSUM_PARTIAL &&
3196 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003197
3198 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003199}
3200
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003201/**
Cong Wang12b00042013-02-05 16:36:38 +00003202 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003203 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003204 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003205 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003206 *
3207 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003208 *
3209 * It may return NULL if the skb requires no segmentation. This is
3210 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003211 *
3212 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003213 */
Cong Wang12b00042013-02-05 16:36:38 +00003214struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3215 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003216{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003217 struct sk_buff *segs;
3218
Cong Wang12b00042013-02-05 16:36:38 +00003219 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003220 int err;
3221
Eric Dumazetb2504a52017-01-31 10:20:32 -08003222 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003223 err = skb_cow_head(skb, 0);
3224 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003225 return ERR_PTR(err);
3226 }
3227
Alexander Duyck802ab552016-04-10 21:45:03 -04003228 /* Only report GSO partial support if it will enable us to
3229 * support segmentation on this frame without needing additional
3230 * work.
3231 */
3232 if (features & NETIF_F_GSO_PARTIAL) {
3233 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3234 struct net_device *dev = skb->dev;
3235
3236 partial_features |= dev->features & dev->gso_partial_features;
3237 if (!skb_gso_ok(skb, features | partial_features))
3238 features &= ~NETIF_F_GSO_PARTIAL;
3239 }
3240
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003241 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3242 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3243
Pravin B Shelar68c33162013-02-14 14:02:41 +00003244 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003245 SKB_GSO_CB(skb)->encap_level = 0;
3246
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003247 skb_reset_mac_header(skb);
3248 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003249
Eric Dumazetb2504a52017-01-31 10:20:32 -08003250 segs = skb_mac_gso_segment(skb, features);
3251
Willem de Bruijn8d74e9f2017-12-12 11:39:04 -05003252 if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003253 skb_warn_bad_offload(skb);
3254
3255 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003256}
Cong Wang12b00042013-02-05 16:36:38 +00003257EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003258
Herbert Xufb286bb2005-11-10 13:01:24 -08003259/* Take action when hardware reception checksum errors are detected. */
3260#ifdef CONFIG_BUG
Cong Wang7fe50ac2018-11-12 14:47:18 -08003261void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003262{
3263 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003264 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Willem de Bruijn64131392019-07-07 05:51:55 -04003265 skb_dump(KERN_ERR, skb, true);
Herbert Xufb286bb2005-11-10 13:01:24 -08003266 dump_stack();
3267 }
3268}
3269EXPORT_SYMBOL(netdev_rx_csum_fault);
3270#endif
3271
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003272/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003273static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003275#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 int i;
tchardingf4563a72017-02-09 17:56:07 +11003277
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003278 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003279 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3280 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003281
Ian Campbellea2ab692011-08-22 23:44:58 +00003282 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003283 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003284 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003285 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return 0;
3288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Simon Horman3b392dd2014-06-04 08:53:17 +09003290/* If MPLS offload request, verify we are testing hardware MPLS features
3291 * instead of standard features for the netdev.
3292 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003293#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003294static netdev_features_t net_mpls_features(struct sk_buff *skb,
3295 netdev_features_t features,
3296 __be16 type)
3297{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003298 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003299 features &= skb->dev->mpls_features;
3300
3301 return features;
3302}
3303#else
3304static netdev_features_t net_mpls_features(struct sk_buff *skb,
3305 netdev_features_t features,
3306 __be16 type)
3307{
3308 return features;
3309}
3310#endif
3311
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003312static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003313 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003314{
Vlad Yasevich53d64712014-03-27 17:26:18 -04003315 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09003316 __be16 type;
3317
3318 type = skb_network_protocol(skb, &tmp);
3319 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003320
Ed Cashinc0d680e2012-09-19 15:49:00 +00003321 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003322 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003323 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003324 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003325 if (illegal_highdma(skb->dev, skb))
3326 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003327
3328 return features;
3329}
3330
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003331netdev_features_t passthru_features_check(struct sk_buff *skb,
3332 struct net_device *dev,
3333 netdev_features_t features)
3334{
3335 return features;
3336}
3337EXPORT_SYMBOL(passthru_features_check);
3338
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003339static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003340 struct net_device *dev,
3341 netdev_features_t features)
3342{
3343 return vlan_features_check(skb, features);
3344}
3345
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003346static netdev_features_t gso_features_check(const struct sk_buff *skb,
3347 struct net_device *dev,
3348 netdev_features_t features)
3349{
3350 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3351
3352 if (gso_segs > dev->gso_max_segs)
3353 return features & ~NETIF_F_GSO_MASK;
3354
Alexander Duyck802ab552016-04-10 21:45:03 -04003355 /* Support for GSO partial features requires software
3356 * intervention before we can actually process the packets
3357 * so we need to strip support for any partial features now
3358 * and we can pull them back in after we have partially
3359 * segmented the frame.
3360 */
3361 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3362 features &= ~dev->gso_partial_features;
3363
3364 /* Make sure to clear the IPv4 ID mangling feature if the
3365 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003366 */
3367 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3368 struct iphdr *iph = skb->encapsulation ?
3369 inner_ip_hdr(skb) : ip_hdr(skb);
3370
3371 if (!(iph->frag_off & htons(IP_DF)))
3372 features &= ~NETIF_F_TSO_MANGLEID;
3373 }
3374
3375 return features;
3376}
3377
Florian Westphalc1e756b2014-05-05 15:00:44 +02003378netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003379{
Jesse Gross5f352272014-12-23 22:37:26 -08003380 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003381 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003382
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003383 if (skb_is_gso(skb))
3384 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003385
Jesse Gross5f352272014-12-23 22:37:26 -08003386 /* If encapsulation offload request, verify we are testing
3387 * hardware encapsulation features instead of standard
3388 * features for the netdev
3389 */
3390 if (skb->encapsulation)
3391 features &= dev->hw_enc_features;
3392
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003393 if (skb_vlan_tagged(skb))
3394 features = netdev_intersect_features(features,
3395 dev->vlan_features |
3396 NETIF_F_HW_VLAN_CTAG_TX |
3397 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003398
Jesse Gross5f352272014-12-23 22:37:26 -08003399 if (dev->netdev_ops->ndo_features_check)
3400 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3401 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003402 else
3403 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003404
Florian Westphalc1e756b2014-05-05 15:00:44 +02003405 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003406}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003407EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003408
David S. Miller2ea25512014-08-29 21:10:01 -07003409static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003410 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003411{
David S. Miller2ea25512014-08-29 21:10:01 -07003412 unsigned int len;
3413 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003414
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003415 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003416 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003417
David S. Miller2ea25512014-08-29 21:10:01 -07003418 len = skb->len;
3419 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003420 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003421 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003422
Patrick McHardy572a9d72009-11-10 06:14:14 +00003423 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003424}
David S. Miller2ea25512014-08-29 21:10:01 -07003425
David S. Miller8dcda222014-09-01 15:06:40 -07003426struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3427 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003428{
3429 struct sk_buff *skb = first;
3430 int rc = NETDEV_TX_OK;
3431
3432 while (skb) {
3433 struct sk_buff *next = skb->next;
3434
David S. Millera8305bf2018-07-29 20:42:53 -07003435 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003436 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003437 if (unlikely(!dev_xmit_complete(rc))) {
3438 skb->next = next;
3439 goto out;
3440 }
3441
3442 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003443 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003444 rc = NETDEV_TX_BUSY;
3445 break;
3446 }
3447 }
3448
3449out:
3450 *ret = rc;
3451 return skb;
3452}
3453
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003454static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3455 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003456{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003457 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003458 !vlan_hw_offload_capable(features, skb->vlan_proto))
3459 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003460 return skb;
3461}
3462
Davide Caratti43c26a12017-05-18 15:44:41 +02003463int skb_csum_hwoffload_help(struct sk_buff *skb,
3464 const netdev_features_t features)
3465{
3466 if (unlikely(skb->csum_not_inet))
3467 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3468 skb_crc32c_csum_help(skb);
3469
3470 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3471}
3472EXPORT_SYMBOL(skb_csum_hwoffload_help);
3473
Steffen Klassertf53c7232017-12-20 10:41:36 +01003474static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003475{
3476 netdev_features_t features;
3477
David S. Millereae3f882014-08-30 15:17:13 -07003478 features = netif_skb_features(skb);
3479 skb = validate_xmit_vlan(skb, features);
3480 if (unlikely(!skb))
3481 goto out_null;
3482
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003483 skb = sk_validate_xmit_skb(skb, dev);
3484 if (unlikely(!skb))
3485 goto out_null;
3486
Johannes Berg8b86a612015-04-17 15:45:04 +02003487 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003488 struct sk_buff *segs;
3489
3490 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003491 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003492 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003493 } else if (segs) {
3494 consume_skb(skb);
3495 skb = segs;
3496 }
David S. Millereae3f882014-08-30 15:17:13 -07003497 } else {
3498 if (skb_needs_linearize(skb, features) &&
3499 __skb_linearize(skb))
3500 goto out_kfree_skb;
3501
3502 /* If packet is not checksummed and device does not
3503 * support checksumming for this protocol, complete
3504 * checksumming here.
3505 */
3506 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3507 if (skb->encapsulation)
3508 skb_set_inner_transport_header(skb,
3509 skb_checksum_start_offset(skb));
3510 else
3511 skb_set_transport_header(skb,
3512 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003513 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003514 goto out_kfree_skb;
3515 }
3516 }
3517
Steffen Klassertf53c7232017-12-20 10:41:36 +01003518 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003519
David S. Millereae3f882014-08-30 15:17:13 -07003520 return skb;
3521
3522out_kfree_skb:
3523 kfree_skb(skb);
3524out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003525 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003526 return NULL;
3527}
3528
Steffen Klassertf53c7232017-12-20 10:41:36 +01003529struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003530{
3531 struct sk_buff *next, *head = NULL, *tail;
3532
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003533 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003534 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003535 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003536
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003537 /* in case skb wont be segmented, point to itself */
3538 skb->prev = skb;
3539
Steffen Klassertf53c7232017-12-20 10:41:36 +01003540 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003541 if (!skb)
3542 continue;
3543
3544 if (!head)
3545 head = skb;
3546 else
3547 tail->next = skb;
3548 /* If skb was segmented, skb->prev points to
3549 * the last segment. If not, it still contains skb.
3550 */
3551 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003552 }
3553 return head;
3554}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003555EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003556
Eric Dumazet1def9232013-01-10 12:36:42 +00003557static void qdisc_pkt_len_init(struct sk_buff *skb)
3558{
3559 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3560
3561 qdisc_skb_cb(skb)->pkt_len = skb->len;
3562
3563 /* To get more precise estimation of bytes sent on wire,
3564 * we add to pkt_len the headers size of all segments
3565 */
Maxim Mikityanskiya0dce872019-02-22 12:55:22 +00003566 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003567 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003568 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003569
Eric Dumazet757b8b12013-01-15 21:14:21 -08003570 /* mac layer + network layer */
3571 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3572
3573 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003574 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3575 const struct tcphdr *th;
3576 struct tcphdr _tcphdr;
3577
3578 th = skb_header_pointer(skb, skb_transport_offset(skb),
3579 sizeof(_tcphdr), &_tcphdr);
3580 if (likely(th))
3581 hdr_len += __tcp_hdrlen(th);
3582 } else {
3583 struct udphdr _udphdr;
3584
3585 if (skb_header_pointer(skb, skb_transport_offset(skb),
3586 sizeof(_udphdr), &_udphdr))
3587 hdr_len += sizeof(struct udphdr);
3588 }
Jason Wang15e5a032013-03-25 20:19:59 +00003589
3590 if (shinfo->gso_type & SKB_GSO_DODGY)
3591 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3592 shinfo->gso_size);
3593
3594 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003595 }
3596}
3597
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003598static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3599 struct net_device *dev,
3600 struct netdev_queue *txq)
3601{
3602 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003603 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003604 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003605 int rc;
3606
Eric Dumazeta2da5702011-01-20 03:48:19 +00003607 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003608
3609 if (q->flags & TCQ_F_NOLOCK) {
Eric Dumazet90b2be22019-11-08 08:45:23 -08003610 if ((q->flags & TCQ_F_CAN_BYPASS) && READ_ONCE(q->empty) &&
Paolo Abenid518d2e2019-09-12 12:02:42 +02003611 qdisc_run_begin(q)) {
3612 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
3613 &q->state))) {
3614 __qdisc_drop(skb, &to_free);
3615 rc = NET_XMIT_DROP;
3616 goto end_run;
3617 }
Paolo Abeniba27b4c2019-03-22 16:01:56 +01003618 qdisc_bstats_cpu_update(q, skb);
3619
Paolo Abenid518d2e2019-09-12 12:02:42 +02003620 rc = NET_XMIT_SUCCESS;
Paolo Abeniba27b4c2019-03-22 16:01:56 +01003621 if (sch_direct_xmit(skb, q, dev, txq, NULL, true))
3622 __qdisc_run(q);
3623
Paolo Abenid518d2e2019-09-12 12:02:42 +02003624end_run:
Paolo Abeniba27b4c2019-03-22 16:01:56 +01003625 qdisc_run_end(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003626 } else {
3627 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Paolo Abeni32f7b442018-05-15 10:50:31 +02003628 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003629 }
3630
3631 if (unlikely(to_free))
3632 kfree_skb_list(to_free);
3633 return rc;
3634 }
3635
Eric Dumazet79640a42010-06-02 05:09:29 -07003636 /*
3637 * Heuristic to force contended enqueues to serialize on a
3638 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003639 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003640 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003641 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003642 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003643 if (unlikely(contended))
3644 spin_lock(&q->busylock);
3645
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003646 spin_lock(root_lock);
3647 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003648 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003649 rc = NET_XMIT_DROP;
3650 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003651 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003652 /*
3653 * This is a work-conserving queue; there are no old skbs
3654 * waiting to be sent out; and the qdisc is not running -
3655 * xmit the skb directly.
3656 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003657
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003658 qdisc_bstats_update(q, skb);
3659
Eric Dumazet55a93b32014-10-03 15:31:07 -07003660 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003661 if (unlikely(contended)) {
3662 spin_unlock(&q->busylock);
3663 contended = false;
3664 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003665 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003666 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003667
John Fastabend6c148182017-12-07 09:54:06 -08003668 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003669 rc = NET_XMIT_SUCCESS;
3670 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003671 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003672 if (qdisc_run_begin(q)) {
3673 if (unlikely(contended)) {
3674 spin_unlock(&q->busylock);
3675 contended = false;
3676 }
3677 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003678 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003679 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003680 }
3681 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003682 if (unlikely(to_free))
3683 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003684 if (unlikely(contended))
3685 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003686 return rc;
3687}
3688
Daniel Borkmann86f85152013-12-29 17:27:11 +01003689#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003690static void skb_update_prio(struct sk_buff *skb)
3691{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003692 const struct netprio_map *map;
3693 const struct sock *sk;
3694 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003695
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003696 if (skb->priority)
3697 return;
3698 map = rcu_dereference_bh(skb->dev->priomap);
3699 if (!map)
3700 return;
3701 sk = skb_to_full_sk(skb);
3702 if (!sk)
3703 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003704
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003705 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3706
3707 if (prioidx < map->priomap_len)
3708 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003709}
3710#else
3711#define skb_update_prio(skb)
3712#endif
3713
Dave Jonesd29f7492008-07-22 14:09:06 -07003714/**
Michel Machado95603e22012-06-12 10:16:35 +00003715 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003716 * @net: network namespace this loopback is happening in
3717 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003718 * @skb: buffer to transmit
3719 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003720int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003721{
3722 skb_reset_mac_header(skb);
3723 __skb_pull(skb, skb_network_offset(skb));
3724 skb->pkt_type = PACKET_LOOPBACK;
3725 skb->ip_summed = CHECKSUM_UNNECESSARY;
3726 WARN_ON(!skb_dst(skb));
3727 skb_dst_force(skb);
3728 netif_rx_ni(skb);
3729 return 0;
3730}
3731EXPORT_SYMBOL(dev_loopback_xmit);
3732
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003733#ifdef CONFIG_NET_EGRESS
3734static struct sk_buff *
3735sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3736{
Jiri Pirko46209402017-11-03 11:46:25 +01003737 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003738 struct tcf_result cl_res;
3739
Jiri Pirko46209402017-11-03 11:46:25 +01003740 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003741 return skb;
3742
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003743 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Jiri Pirko46209402017-11-03 11:46:25 +01003744 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003745
Jiri Pirko46209402017-11-03 11:46:25 +01003746 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003747 case TC_ACT_OK:
3748 case TC_ACT_RECLASSIFY:
3749 skb->tc_index = TC_H_MIN(cl_res.classid);
3750 break;
3751 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003752 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003753 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003754 kfree_skb(skb);
3755 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003756 case TC_ACT_STOLEN:
3757 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003758 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003759 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003760 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003761 return NULL;
3762 case TC_ACT_REDIRECT:
3763 /* No need to push/pop skb's mac_header here on egress! */
3764 skb_do_redirect(skb);
3765 *ret = NET_XMIT_SUCCESS;
3766 return NULL;
3767 default:
3768 break;
3769 }
3770
3771 return skb;
3772}
3773#endif /* CONFIG_NET_EGRESS */
3774
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003775#ifdef CONFIG_XPS
3776static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3777 struct xps_dev_maps *dev_maps, unsigned int tci)
3778{
3779 struct xps_map *map;
3780 int queue_index = -1;
3781
3782 if (dev->num_tc) {
3783 tci *= dev->num_tc;
3784 tci += netdev_get_prio_tc_map(dev, skb->priority);
3785 }
3786
3787 map = rcu_dereference(dev_maps->attr_map[tci]);
3788 if (map) {
3789 if (map->len == 1)
3790 queue_index = map->queues[0];
3791 else
3792 queue_index = map->queues[reciprocal_scale(
3793 skb_get_hash(skb), map->len)];
3794 if (unlikely(queue_index >= dev->real_num_tx_queues))
3795 queue_index = -1;
3796 }
3797 return queue_index;
3798}
3799#endif
3800
Alexander Duyckeadec8772018-07-09 12:19:48 -04003801static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3802 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003803{
3804#ifdef CONFIG_XPS
3805 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003806 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003807 int queue_index = -1;
3808
Amritha Nambiar04157462018-06-29 21:26:46 -07003809 if (!static_key_false(&xps_needed))
3810 return -1;
3811
Jiri Pirko638b2a62015-05-12 14:56:13 +02003812 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003813 if (!static_key_false(&xps_rxqs_needed))
3814 goto get_cpus_map;
3815
Alexander Duyckeadec8772018-07-09 12:19:48 -04003816 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003817 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003818 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003819
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003820 if (tci >= 0 && tci < dev->num_rx_queues)
3821 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3822 tci);
3823 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003824
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003825get_cpus_map:
3826 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003827 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003828 if (dev_maps) {
3829 unsigned int tci = skb->sender_cpu - 1;
3830
3831 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3832 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003833 }
3834 }
3835 rcu_read_unlock();
3836
3837 return queue_index;
3838#else
3839 return -1;
3840#endif
3841}
3842
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003843u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003844 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003845{
3846 return 0;
3847}
3848EXPORT_SYMBOL(dev_pick_tx_zero);
3849
3850u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01003851 struct net_device *sb_dev)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003852{
3853 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3854}
3855EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3856
Paolo Abenib71b5832019-03-20 11:02:05 +01003857u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3858 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003859{
3860 struct sock *sk = skb->sk;
3861 int queue_index = sk_tx_queue_get(sk);
3862
Alexander Duyckeadec8772018-07-09 12:19:48 -04003863 sb_dev = sb_dev ? : dev;
3864
Jiri Pirko638b2a62015-05-12 14:56:13 +02003865 if (queue_index < 0 || skb->ooo_okay ||
3866 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003867 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003868
Jiri Pirko638b2a62015-05-12 14:56:13 +02003869 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003870 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003871
3872 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003873 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003874 rcu_access_pointer(sk->sk_dst_cache))
3875 sk_tx_queue_set(sk, new_index);
3876
3877 queue_index = new_index;
3878 }
3879
3880 return queue_index;
3881}
Paolo Abenib71b5832019-03-20 11:02:05 +01003882EXPORT_SYMBOL(netdev_pick_tx);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003883
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003884struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
3885 struct sk_buff *skb,
3886 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003887{
3888 int queue_index = 0;
3889
3890#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003891 u32 sender_cpu = skb->sender_cpu - 1;
3892
3893 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003894 skb->sender_cpu = raw_smp_processor_id() + 1;
3895#endif
3896
3897 if (dev->real_num_tx_queues != 1) {
3898 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003899
Jiri Pirko638b2a62015-05-12 14:56:13 +02003900 if (ops->ndo_select_queue)
Paolo Abenia350ecc2019-03-20 11:02:06 +01003901 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003902 else
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003903 queue_index = netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003904
Alexander Duyckd5845272017-11-22 10:57:41 -08003905 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003906 }
3907
3908 skb_set_queue_mapping(skb, queue_index);
3909 return netdev_get_tx_queue(dev, queue_index);
3910}
3911
Michel Machado95603e22012-06-12 10:16:35 +00003912/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003913 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003914 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04003915 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003916 *
3917 * Queue a buffer for transmission to a network device. The caller must
3918 * have set the device and priority and built the buffer before calling
3919 * this function. The function can be called from an interrupt.
3920 *
3921 * A negative errno code is returned on a failure. A success does not
3922 * guarantee the frame will be transmitted as it may be dropped due
3923 * to congestion or traffic shaping.
3924 *
3925 * -----------------------------------------------------------------------------------
3926 * I notice this method can also return errors from the queue disciplines,
3927 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3928 * be positive.
3929 *
3930 * Regardless of the return value, the skb is consumed, so it is currently
3931 * difficult to retry a send to this method. (You can bump the ref count
3932 * before sending to hold a reference for retry if you are careful.)
3933 *
3934 * When calling this method, interrupts MUST be enabled. This is because
3935 * the BH enable code must have IRQs enabled so that it will not deadlock.
3936 * --BLG
3937 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003938static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
3940 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003941 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct Qdisc *q;
3943 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01003944 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003946 skb_reset_mac_header(skb);
3947
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003948 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3949 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3950
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003951 /* Disable soft irqs for various locks below. Also
3952 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003954 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Neil Horman5bc14212011-11-22 05:10:51 +00003956 skb_update_prio(skb);
3957
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003958 qdisc_pkt_len_init(skb);
3959#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003960 skb->tc_at_ingress = 0;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003961# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07003962 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003963 skb = sch_handle_egress(skb, &rc, dev);
3964 if (!skb)
3965 goto out;
3966 }
3967# endif
3968#endif
Eric Dumazet02875872014-10-05 18:38:35 -07003969 /* If device/qdisc don't need skb->dst, release it right now while
3970 * its hot in this cpu cache.
3971 */
3972 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3973 skb_dst_drop(skb);
3974 else
3975 skb_dst_force(skb);
3976
Paolo Abeni4bd97d52019-03-20 11:02:04 +01003977 txq = netdev_core_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08003978 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07003979
Koki Sanagicf66ba52010-08-23 18:45:02 +09003980 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003982 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07003983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
3985
3986 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11003987 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
tchardingeb13da12017-02-09 17:56:06 +11003989 * Really, it is unlikely that netif_tx_lock protection is necessary
3990 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3991 * counters.)
3992 * However, it is possible, that they rely on protection
3993 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
tchardingeb13da12017-02-09 17:56:06 +11003995 * Check this and shot the lock. It is not prone from deadlocks.
3996 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 */
3998 if (dev->flags & IFF_UP) {
3999 int cpu = smp_processor_id(); /* ok because BHs are off */
4000
David S. Millerc773e842008-07-08 23:13:53 -07004001 if (txq->xmit_lock_owner != cpu) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004002 if (dev_xmit_recursion())
Eric Dumazet745e20f2010-09-29 13:23:09 -07004003 goto recursion_alert;
4004
Steffen Klassertf53c7232017-12-20 10:41:36 +01004005 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004006 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07004007 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004008
David S. Millerc773e842008-07-08 23:13:53 -07004009 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Tom Herbert734664982011-11-28 16:32:44 +00004011 if (!netif_xmit_stopped(txq)) {
Florian Westphal97cdcf32019-04-01 16:42:13 +02004012 dev_xmit_recursion_inc();
David S. Millerce937182014-08-30 19:22:20 -07004013 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Florian Westphal97cdcf32019-04-01 16:42:13 +02004014 dev_xmit_recursion_dec();
Patrick McHardy572a9d72009-11-10 06:14:14 +00004015 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07004016 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 goto out;
4018 }
4019 }
David S. Millerc773e842008-07-08 23:13:53 -07004020 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00004021 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4022 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 } else {
4024 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07004025 * unfortunately
4026 */
4027recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00004028 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4029 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
4031 }
4032
4033 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07004034 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Eric Dumazet015f0682014-03-27 08:45:56 -07004036 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02004037 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return rc;
4039out:
Herbert Xud4828d82006-06-22 02:28:18 -07004040 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 return rc;
4042}
Jason Wangf663dd92014-01-10 16:18:26 +08004043
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004044int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08004045{
4046 return __dev_queue_xmit(skb, NULL);
4047}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05004048EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Alexander Duyckeadec8772018-07-09 12:19:48 -04004050int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08004051{
Alexander Duyckeadec8772018-07-09 12:19:48 -04004052 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08004053}
4054EXPORT_SYMBOL(dev_queue_xmit_accel);
4055
Magnus Karlsson865b03f2018-05-02 13:01:33 +02004056int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4057{
4058 struct net_device *dev = skb->dev;
4059 struct sk_buff *orig_skb = skb;
4060 struct netdev_queue *txq;
4061 int ret = NETDEV_TX_BUSY;
4062 bool again = false;
4063
4064 if (unlikely(!netif_running(dev) ||
4065 !netif_carrier_ok(dev)))
4066 goto drop;
4067
4068 skb = validate_xmit_skb_list(skb, dev, &again);
4069 if (skb != orig_skb)
4070 goto drop;
4071
4072 skb_set_queue_mapping(skb, queue_id);
4073 txq = skb_get_tx_queue(dev, skb);
4074
4075 local_bh_disable();
4076
4077 HARD_TX_LOCK(dev, txq, smp_processor_id());
4078 if (!netif_xmit_frozen_or_drv_stopped(txq))
4079 ret = netdev_start_xmit(skb, dev, txq, false);
4080 HARD_TX_UNLOCK(dev, txq);
4081
4082 local_bh_enable();
4083
4084 if (!dev_xmit_complete(ret))
4085 kfree_skb(skb);
4086
4087 return ret;
4088drop:
4089 atomic_long_inc(&dev->tx_dropped);
4090 kfree_skb_list(skb);
4091 return NET_XMIT_DROP;
4092}
4093EXPORT_SYMBOL(dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
tchardingeb13da12017-02-09 17:56:06 +11004095/*************************************************************************
4096 * Receiver routines
4097 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004099int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00004100EXPORT_SYMBOL(netdev_max_backlog);
4101
Eric Dumazet3b098e22010-05-15 23:57:10 -07004102int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07004103int netdev_budget __read_mostly = 300;
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04004104unsigned int __read_mostly netdev_budget_usecs = 2000;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01004105int weight_p __read_mostly = 64; /* old backlog weight */
4106int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4107int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4108int dev_rx_weight __read_mostly = 64;
4109int dev_tx_weight __read_mostly = 64;
Edward Cree323ebb62019-08-06 14:53:55 +01004110/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4111int gro_normal_batch __read_mostly = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004113/* Called with irq disabled */
4114static inline void ____napi_schedule(struct softnet_data *sd,
4115 struct napi_struct *napi)
4116{
4117 list_add_tail(&napi->poll_list, &sd->poll_list);
4118 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4119}
4120
Eric Dumazetdf334542010-03-24 19:13:54 +00004121#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07004122
4123/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00004124struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07004125EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004126u32 rps_cpu_mask __read_mostly;
4127EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07004128
Eric Dumazetdc053602019-03-22 08:56:38 -07004129struct static_key_false rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04004130EXPORT_SYMBOL(rps_needed);
Eric Dumazetdc053602019-03-22 08:56:38 -07004131struct static_key_false rfs_needed __read_mostly;
Eric Dumazet13bfff22016-12-07 08:29:10 -08004132EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00004133
Ben Hutchingsc4454772011-01-19 11:03:53 +00004134static struct rps_dev_flow *
4135set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4136 struct rps_dev_flow *rflow, u16 next_cpu)
4137{
Eric Dumazeta31196b2015-04-25 09:35:24 -07004138 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00004139#ifdef CONFIG_RFS_ACCEL
4140 struct netdev_rx_queue *rxqueue;
4141 struct rps_dev_flow_table *flow_table;
4142 struct rps_dev_flow *old_rflow;
4143 u32 flow_id;
4144 u16 rxq_index;
4145 int rc;
4146
4147 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00004148 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4149 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00004150 goto out;
4151 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4152 if (rxq_index == skb_get_rx_queue(skb))
4153 goto out;
4154
4155 rxqueue = dev->_rx + rxq_index;
4156 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4157 if (!flow_table)
4158 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004159 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004160 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4161 rxq_index, flow_id);
4162 if (rc < 0)
4163 goto out;
4164 old_rflow = rflow;
4165 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004166 rflow->filter = rc;
4167 if (old_rflow->filter == rflow->filter)
4168 old_rflow->filter = RPS_NO_FILTER;
4169 out:
4170#endif
4171 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004172 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004173 }
4174
Ben Hutchings09994d12011-10-03 04:42:46 +00004175 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004176 return rflow;
4177}
4178
Tom Herbert0a9627f2010-03-16 08:03:29 +00004179/*
4180 * get_rps_cpu is called from netif_receive_skb and returns the target
4181 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004182 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004183 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004184static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4185 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004186{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004187 const struct rps_sock_flow_table *sock_flow_table;
4188 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004189 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004190 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004191 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004192 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004193 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004194
Tom Herbert0a9627f2010-03-16 08:03:29 +00004195 if (skb_rx_queue_recorded(skb)) {
4196 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004197
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004198 if (unlikely(index >= dev->real_num_rx_queues)) {
4199 WARN_ONCE(dev->real_num_rx_queues > 1,
4200 "%s received packet on queue %u, but number "
4201 "of RX queues is %u\n",
4202 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004203 goto done;
4204 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004205 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004206 }
4207
Eric Dumazet567e4b72015-02-06 12:59:01 -08004208 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4209
4210 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4211 map = rcu_dereference(rxqueue->rps_map);
4212 if (!flow_table && !map)
4213 goto done;
4214
Changli Gao2d47b452010-08-17 19:00:56 +00004215 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004216 hash = skb_get_hash(skb);
4217 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004218 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004219
Tom Herbertfec5e652010-04-16 16:01:27 -07004220 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4221 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004222 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004223 u32 next_cpu;
4224 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004225
Eric Dumazet567e4b72015-02-06 12:59:01 -08004226 /* First check into global flow table if there is a match */
4227 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4228 if ((ident ^ hash) & ~rps_cpu_mask)
4229 goto try_rps;
4230
4231 next_cpu = ident & rps_cpu_mask;
4232
4233 /* OK, now we know there is a match,
4234 * we can look at the local (per receive queue) flow table
4235 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004236 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004237 tcpu = rflow->cpu;
4238
Tom Herbertfec5e652010-04-16 16:01:27 -07004239 /*
4240 * If the desired CPU (where last recvmsg was done) is
4241 * different from current CPU (one in the rx-queue flow
4242 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004243 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004244 * - Current CPU is offline.
4245 * - The current CPU's queue tail has advanced beyond the
4246 * last packet that was enqueued using this table entry.
4247 * This guarantees that all previous packets for the flow
4248 * have been dequeued, thus preserving in order delivery.
4249 */
4250 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004251 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004252 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004253 rflow->last_qtail)) >= 0)) {
4254 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004255 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004256 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004257
Eric Dumazeta31196b2015-04-25 09:35:24 -07004258 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004259 *rflowp = rflow;
4260 cpu = tcpu;
4261 goto done;
4262 }
4263 }
4264
Eric Dumazet567e4b72015-02-06 12:59:01 -08004265try_rps:
4266
Tom Herbert0a9627f2010-03-16 08:03:29 +00004267 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004268 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004269 if (cpu_online(tcpu)) {
4270 cpu = tcpu;
4271 goto done;
4272 }
4273 }
4274
4275done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004276 return cpu;
4277}
4278
Ben Hutchingsc4454772011-01-19 11:03:53 +00004279#ifdef CONFIG_RFS_ACCEL
4280
4281/**
4282 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4283 * @dev: Device on which the filter was set
4284 * @rxq_index: RX queue index
4285 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4286 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4287 *
4288 * Drivers that implement ndo_rx_flow_steer() should periodically call
4289 * this function for each installed filter and remove the filters for
4290 * which it returns %true.
4291 */
4292bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4293 u32 flow_id, u16 filter_id)
4294{
4295 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4296 struct rps_dev_flow_table *flow_table;
4297 struct rps_dev_flow *rflow;
4298 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004299 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004300
4301 rcu_read_lock();
4302 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4303 if (flow_table && flow_id <= flow_table->mask) {
4304 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004305 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004306 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004307 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4308 rflow->last_qtail) <
4309 (int)(10 * flow_table->mask)))
4310 expire = false;
4311 }
4312 rcu_read_unlock();
4313 return expire;
4314}
4315EXPORT_SYMBOL(rps_may_expire_flow);
4316
4317#endif /* CONFIG_RFS_ACCEL */
4318
Tom Herbert0a9627f2010-03-16 08:03:29 +00004319/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004320static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004321{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004322 struct softnet_data *sd = data;
4323
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004324 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004325 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004326}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004327
Tom Herbertfec5e652010-04-16 16:01:27 -07004328#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004329
4330/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004331 * Check if this softnet_data structure is another cpu one
4332 * If yes, queue it to our IPI list and return 1
4333 * If no, return 0
4334 */
4335static int rps_ipi_queued(struct softnet_data *sd)
4336{
4337#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004338 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004339
4340 if (sd != mysd) {
4341 sd->rps_ipi_next = mysd->rps_ipi_list;
4342 mysd->rps_ipi_list = sd;
4343
4344 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4345 return 1;
4346 }
4347#endif /* CONFIG_RPS */
4348 return 0;
4349}
4350
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004351#ifdef CONFIG_NET_FLOW_LIMIT
4352int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4353#endif
4354
4355static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4356{
4357#ifdef CONFIG_NET_FLOW_LIMIT
4358 struct sd_flow_limit *fl;
4359 struct softnet_data *sd;
4360 unsigned int old_flow, new_flow;
4361
4362 if (qlen < (netdev_max_backlog >> 1))
4363 return false;
4364
Christoph Lameter903ceff2014-08-17 12:30:35 -05004365 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004366
4367 rcu_read_lock();
4368 fl = rcu_dereference(sd->flow_limit);
4369 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004370 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004371 old_flow = fl->history[fl->history_head];
4372 fl->history[fl->history_head] = new_flow;
4373
4374 fl->history_head++;
4375 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4376
4377 if (likely(fl->buckets[old_flow]))
4378 fl->buckets[old_flow]--;
4379
4380 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4381 fl->count++;
4382 rcu_read_unlock();
4383 return true;
4384 }
4385 }
4386 rcu_read_unlock();
4387#endif
4388 return false;
4389}
4390
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004391/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004392 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4393 * queue (may be a remote CPU queue).
4394 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004395static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4396 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004397{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004398 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004399 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004400 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004401
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004402 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004403
4404 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004405
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004406 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004407 if (!netif_running(skb->dev))
4408 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004409 qlen = skb_queue_len(&sd->input_pkt_queue);
4410 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004411 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004412enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004413 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004414 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004415 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004416 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004417 return NET_RX_SUCCESS;
4418 }
4419
Eric Dumazetebda37c22010-05-06 23:51:21 +00004420 /* Schedule NAPI for backlog device
4421 * We can use non atomic operation since we own the queue lock
4422 */
4423 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004424 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004425 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004426 }
4427 goto enqueue;
4428 }
4429
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004430drop:
Changli Gaodee42872010-05-02 05:42:16 +00004431 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004432 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004433
Tom Herbert0a9627f2010-03-16 08:03:29 +00004434 local_irq_restore(flags);
4435
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004436 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004437 kfree_skb(skb);
4438 return NET_RX_DROP;
4439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004441static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4442{
4443 struct net_device *dev = skb->dev;
4444 struct netdev_rx_queue *rxqueue;
4445
4446 rxqueue = dev->_rx;
4447
4448 if (skb_rx_queue_recorded(skb)) {
4449 u16 index = skb_get_rx_queue(skb);
4450
4451 if (unlikely(index >= dev->real_num_rx_queues)) {
4452 WARN_ONCE(dev->real_num_rx_queues > 1,
4453 "%s received packet on queue %u, but number "
4454 "of RX queues is %u\n",
4455 dev->name, index, dev->real_num_rx_queues);
4456
4457 return rxqueue; /* Return first rxqueue */
4458 }
4459 rxqueue += index;
4460 }
4461 return rxqueue;
4462}
4463
John Fastabendd4455162017-07-17 09:26:45 -07004464static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004465 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004466 struct bpf_prog *xdp_prog)
4467{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004468 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004469 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004470 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004471 __be16 orig_eth_type;
4472 struct ethhdr *eth;
4473 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004474 int hlen, off;
4475 u32 mac_len;
4476
4477 /* Reinjected packets coming from act_mirred or similar should
4478 * not get XDP generic processing.
4479 */
Paolo Abenicd11b1642018-07-30 14:30:44 +02004480 if (skb_cloned(skb) || skb_is_tc_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004481 return XDP_PASS;
4482
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004483 /* XDP packets must be linear and must have sufficient headroom
4484 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4485 * native XDP provides, thus we need to do it here as well.
4486 */
4487 if (skb_is_nonlinear(skb) ||
4488 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4489 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4490 int troom = skb->tail + skb->data_len - skb->end;
4491
4492 /* In case we have to go down the path and also linearize,
4493 * then lets do the pskb_expand_head() work just once here.
4494 */
4495 if (pskb_expand_head(skb,
4496 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4497 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4498 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004499 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004500 goto do_drop;
4501 }
John Fastabendd4455162017-07-17 09:26:45 -07004502
4503 /* The XDP program wants to see the packet starting at the MAC
4504 * header.
4505 */
4506 mac_len = skb->data - skb_mac_header(skb);
4507 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004508 xdp->data = skb->data - mac_len;
4509 xdp->data_meta = xdp->data;
4510 xdp->data_end = xdp->data + hlen;
4511 xdp->data_hard_start = skb->data - skb_headroom(skb);
4512 orig_data_end = xdp->data_end;
4513 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004514 eth = (struct ethhdr *)xdp->data;
4515 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4516 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004517
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004518 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004519 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004520
Björn Töpel02671e22018-05-02 13:01:30 +02004521 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004522
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004523 /* check if bpf_xdp_adjust_head was used */
Björn Töpel02671e22018-05-02 13:01:30 +02004524 off = xdp->data - orig_data;
Jesper Dangaard Brouer065af352019-08-01 20:00:31 +02004525 if (off) {
4526 if (off > 0)
4527 __skb_pull(skb, off);
4528 else if (off < 0)
4529 __skb_push(skb, -off);
4530
4531 skb->mac_header += off;
4532 skb_reset_network_header(skb);
4533 }
John Fastabendd4455162017-07-17 09:26:45 -07004534
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004535 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4536 * pckt.
4537 */
Björn Töpel02671e22018-05-02 13:01:30 +02004538 off = orig_data_end - xdp->data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004539 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004540 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004541 skb->len -= off;
Björn Töpel02671e22018-05-02 13:01:30 +02004542
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004543 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004544
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004545 /* check if XDP changed eth hdr such SKB needs update */
4546 eth = (struct ethhdr *)xdp->data;
4547 if ((orig_eth_type != eth->h_proto) ||
4548 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4549 __skb_push(skb, ETH_HLEN);
4550 skb->protocol = eth_type_trans(skb, skb->dev);
4551 }
4552
John Fastabendd4455162017-07-17 09:26:45 -07004553 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004554 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004555 case XDP_TX:
4556 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004557 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004558 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004559 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004560 if (metalen)
4561 skb_metadata_set(skb, metalen);
4562 break;
John Fastabendd4455162017-07-17 09:26:45 -07004563 default:
4564 bpf_warn_invalid_xdp_action(act);
4565 /* fall through */
4566 case XDP_ABORTED:
4567 trace_xdp_exception(skb->dev, xdp_prog, act);
4568 /* fall through */
4569 case XDP_DROP:
4570 do_drop:
4571 kfree_skb(skb);
4572 break;
4573 }
4574
4575 return act;
4576}
4577
4578/* When doing generic XDP we have to bypass the qdisc layer and the
4579 * network taps in order to match in-driver-XDP behavior.
4580 */
Jason Wang7c497472017-08-11 19:41:17 +08004581void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004582{
4583 struct net_device *dev = skb->dev;
4584 struct netdev_queue *txq;
4585 bool free_skb = true;
4586 int cpu, rc;
4587
Paolo Abeni4bd97d52019-03-20 11:02:04 +01004588 txq = netdev_core_pick_tx(dev, skb, NULL);
John Fastabendd4455162017-07-17 09:26:45 -07004589 cpu = smp_processor_id();
4590 HARD_TX_LOCK(dev, txq, cpu);
4591 if (!netif_xmit_stopped(txq)) {
4592 rc = netdev_start_xmit(skb, dev, txq, 0);
4593 if (dev_xmit_complete(rc))
4594 free_skb = false;
4595 }
4596 HARD_TX_UNLOCK(dev, txq);
4597 if (free_skb) {
4598 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4599 kfree_skb(skb);
4600 }
4601}
Jason Wang7c497472017-08-11 19:41:17 +08004602EXPORT_SYMBOL_GPL(generic_xdp_tx);
John Fastabendd4455162017-07-17 09:26:45 -07004603
Davidlohr Bueso02786472018-05-08 09:07:02 -07004604static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004605
Jason Wang7c497472017-08-11 19:41:17 +08004606int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004607{
John Fastabendd4455162017-07-17 09:26:45 -07004608 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004609 struct xdp_buff xdp;
4610 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004611 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004612
Björn Töpel02671e22018-05-02 13:01:30 +02004613 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004614 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004615 switch (act) {
4616 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004617 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004618 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004619 if (err)
4620 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004621 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004622 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004623 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004624 break;
4625 }
John Fastabendd4455162017-07-17 09:26:45 -07004626 return XDP_DROP;
4627 }
4628 }
4629 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004630out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004631 kfree_skb(skb);
4632 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004633}
Jason Wang7c497472017-08-11 19:41:17 +08004634EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004635
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004636static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004638 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Eric Dumazet588f0332011-11-15 04:12:55 +00004640 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Koki Sanagicf66ba52010-08-23 18:45:02 +09004642 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004643
Eric Dumazetdf334542010-03-24 19:13:54 +00004644#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07004645 if (static_branch_unlikely(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004646 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004647 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Changli Gaocece1942010-08-07 20:35:43 -07004649 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004650 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004651
4652 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004653 if (cpu < 0)
4654 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004655
4656 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4657
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004658 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004659 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004660 } else
4661#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004662 {
4663 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004664
Tom Herbertfec5e652010-04-16 16:01:27 -07004665 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4666 put_cpu();
4667 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004668 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004670
4671/**
4672 * netif_rx - post buffer to the network code
4673 * @skb: buffer to post
4674 *
4675 * This function receives a packet from a device driver and queues it for
4676 * the upper (protocol) levels to process. It always succeeds. The buffer
4677 * may be dropped during processing for congestion control or by the
4678 * protocol layers.
4679 *
4680 * return values:
4681 * NET_RX_SUCCESS (no congestion)
4682 * NET_RX_DROP (packet was dropped)
4683 *
4684 */
4685
4686int netif_rx(struct sk_buff *skb)
4687{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004688 int ret;
4689
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004690 trace_netif_rx_entry(skb);
4691
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004692 ret = netif_rx_internal(skb);
4693 trace_netif_rx_exit(ret);
4694
4695 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004696}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004697EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699int netif_rx_ni(struct sk_buff *skb)
4700{
4701 int err;
4702
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004703 trace_netif_rx_ni_entry(skb);
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004706 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 if (local_softirq_pending())
4708 do_softirq();
4709 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004710 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712 return err;
4713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714EXPORT_SYMBOL(netif_rx_ni);
4715
Emese Revfy0766f782016-06-20 20:42:34 +02004716static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004718 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 if (sd->completion_queue) {
4721 struct sk_buff *clist;
4722
4723 local_irq_disable();
4724 clist = sd->completion_queue;
4725 sd->completion_queue = NULL;
4726 local_irq_enable();
4727
4728 while (clist) {
4729 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 clist = clist->next;
4732
Reshetova, Elena63354792017-06-30 13:07:58 +03004733 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004734 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4735 trace_consume_skb(skb);
4736 else
4737 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004738
4739 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4740 __kfree_skb(skb);
4741 else
4742 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004744
4745 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 }
4747
4748 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004749 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 local_irq_disable();
4752 head = sd->output_queue;
4753 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004754 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 local_irq_enable();
4756
4757 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004758 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004759 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 head = head->next_sched;
4762
John Fastabend6b3ba912017-12-07 09:54:25 -08004763 if (!(q->flags & TCQ_F_NOLOCK)) {
4764 root_lock = qdisc_lock(q);
4765 spin_lock(root_lock);
4766 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004767 /* We need to make sure head->next_sched is read
4768 * before clearing __QDISC_STATE_SCHED
4769 */
4770 smp_mb__before_atomic();
4771 clear_bit(__QDISC_STATE_SCHED, &q->state);
4772 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004773 if (root_lock)
4774 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
4776 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004777
4778 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004781#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004782/* This hook is defined here for ATM LANE */
4783int (*br_fdb_test_addr_hook)(struct net_device *dev,
4784 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004785EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004788static inline struct sk_buff *
4789sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4790 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004791{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004792#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004793 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004794 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004795
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004796 /* If there's at least one ingress present somewhere (so
4797 * we get here via enabled static key), remaining devices
4798 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004799 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004800 */
Jiri Pirko46209402017-11-03 11:46:25 +01004801 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004802 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004803
Herbert Xuf697c3e2007-10-14 00:38:47 -07004804 if (*pt_prev) {
4805 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4806 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004807 }
4808
Florian Westphal33654952015-05-14 00:36:28 +02004809 qdisc_skb_cb(skb)->pkt_len = skb->len;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004810 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004811 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004812
Jiri Pirko46209402017-11-03 11:46:25 +01004813 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004814 case TC_ACT_OK:
4815 case TC_ACT_RECLASSIFY:
4816 skb->tc_index = TC_H_MIN(cl_res.classid);
4817 break;
4818 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004819 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004820 kfree_skb(skb);
4821 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004822 case TC_ACT_STOLEN:
4823 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004824 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004825 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004826 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004827 case TC_ACT_REDIRECT:
4828 /* skb_mac_header check was done by cls/act_bpf, so
4829 * we can safely push the L2 header back before
4830 * redirecting to another netdev
4831 */
4832 __skb_push(skb, skb->mac_len);
4833 skb_do_redirect(skb);
4834 return NULL;
John Hurley720f22f2019-06-24 23:13:35 +01004835 case TC_ACT_CONSUMED:
Paolo Abenicd11b1642018-07-30 14:30:44 +02004836 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004837 default:
4838 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004839 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004840#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004841 return skb;
4842}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004844/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004845 * netdev_is_rx_handler_busy - check if receive handler is registered
4846 * @dev: device to check
4847 *
4848 * Check if a receive handler is already registered for a given device.
4849 * Return true if there one.
4850 *
4851 * The caller must hold the rtnl_mutex.
4852 */
4853bool netdev_is_rx_handler_busy(struct net_device *dev)
4854{
4855 ASSERT_RTNL();
4856 return dev && rtnl_dereference(dev->rx_handler);
4857}
4858EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4859
4860/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004861 * netdev_rx_handler_register - register receive handler
4862 * @dev: device to register a handler for
4863 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004864 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004865 *
Masanari Iidae2278672014-02-18 22:54:36 +09004866 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004867 * called from __netif_receive_skb. A negative errno code is returned
4868 * on a failure.
4869 *
4870 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004871 *
4872 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004873 */
4874int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004875 rx_handler_func_t *rx_handler,
4876 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004877{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004878 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004879 return -EBUSY;
4880
Paolo Abenif54262502018-03-09 10:39:24 +01004881 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4882 return -EINVAL;
4883
Eric Dumazet00cfec32013-03-29 03:01:22 +00004884 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004885 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004886 rcu_assign_pointer(dev->rx_handler, rx_handler);
4887
4888 return 0;
4889}
4890EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4891
4892/**
4893 * netdev_rx_handler_unregister - unregister receive handler
4894 * @dev: device to unregister a handler from
4895 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004896 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004897 *
4898 * The caller must hold the rtnl_mutex.
4899 */
4900void netdev_rx_handler_unregister(struct net_device *dev)
4901{
4902
4903 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004904 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004905 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4906 * section has a guarantee to see a non NULL rx_handler_data
4907 * as well.
4908 */
4909 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004910 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004911}
4912EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4913
Mel Gormanb4b9e352012-07-31 16:44:26 -07004914/*
4915 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4916 * the special handling of PFMEMALLOC skbs.
4917 */
4918static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4919{
4920 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004921 case htons(ETH_P_ARP):
4922 case htons(ETH_P_IP):
4923 case htons(ETH_P_IPV6):
4924 case htons(ETH_P_8021Q):
4925 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004926 return true;
4927 default:
4928 return false;
4929 }
4930}
4931
Pablo Neirae687ad62015-05-13 18:19:38 +02004932static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4933 int *ret, struct net_device *orig_dev)
4934{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004935#ifdef CONFIG_NETFILTER_INGRESS
Pablo Neirae687ad62015-05-13 18:19:38 +02004936 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004937 int ingress_retval;
4938
Pablo Neirae687ad62015-05-13 18:19:38 +02004939 if (*pt_prev) {
4940 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4941 *pt_prev = NULL;
4942 }
4943
Aaron Conole2c1e2702016-09-21 11:35:03 -04004944 rcu_read_lock();
4945 ingress_retval = nf_hook_ingress(skb);
4946 rcu_read_unlock();
4947 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004948 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004949#endif /* CONFIG_NETFILTER_INGRESS */
Pablo Neirae687ad62015-05-13 18:19:38 +02004950 return 0;
4951}
Pablo Neirae687ad62015-05-13 18:19:38 +02004952
Edward Cree88eb1942018-07-02 16:13:56 +01004953static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
4954 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
4956 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004957 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004958 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004959 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004961 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Eric Dumazet588f0332011-11-15 04:12:55 +00004963 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07004964
Koki Sanagicf66ba52010-08-23 18:45:02 +09004965 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08004966
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07004967 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00004968
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07004969 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00004970 if (!skb_transport_header_was_set(skb))
4971 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00004972 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 pt_prev = NULL;
4975
David S. Miller63d8ea72011-02-28 10:48:59 -08004976another_round:
David S. Millerb6858172012-07-23 16:27:54 -07004977 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08004978
4979 __this_cpu_inc(softnet_data.processed);
4980
Stephen Hemminger458bf2f2019-05-28 11:47:31 -07004981 if (static_branch_unlikely(&generic_xdp_needed_key)) {
4982 int ret2;
4983
4984 preempt_disable();
4985 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4986 preempt_enable();
4987
4988 if (ret2 != XDP_PASS)
4989 return NET_RX_DROP;
4990 skb_reset_mac_len(skb);
4991 }
4992
Patrick McHardy8ad227f2013-04-19 02:04:31 +00004993 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4994 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004995 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004996 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004997 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004998 }
4999
Willem de Bruijne7246e12017-01-07 17:06:35 -05005000 if (skb_skip_tc_classify(skb))
5001 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
David S. Miller9754e292013-02-14 15:57:38 -05005003 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07005004 goto skip_taps;
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08005007 if (pt_prev)
5008 ret = deliver_skb(skb, pt_prev, orig_dev);
5009 pt_prev = ptype;
5010 }
5011
5012 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5013 if (pt_prev)
5014 ret = deliver_skb(skb, pt_prev, orig_dev);
5015 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
5017
Mel Gormanb4b9e352012-07-31 16:44:26 -07005018skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02005019#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07005020 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01005021 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02005022 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005023 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02005024
5025 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03005026 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02005027 }
Pablo Neira1cf519002015-05-13 18:19:37 +02005028#endif
Willem de Bruijna5135bc2017-01-07 17:06:36 -05005029 skb_reset_tc(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05005030skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05005031 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07005032 goto drop;
5033
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005034 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00005035 if (pt_prev) {
5036 ret = deliver_skb(skb, pt_prev, orig_dev);
5037 pt_prev = NULL;
5038 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005039 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00005040 goto another_round;
5041 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03005042 goto out;
John Fastabend24257172011-10-10 09:16:41 +00005043 }
5044
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005045 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005046 if (rx_handler) {
5047 if (pt_prev) {
5048 ret = deliver_skb(skb, pt_prev, orig_dev);
5049 pt_prev = NULL;
5050 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005051 switch (rx_handler(&skb)) {
5052 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00005053 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005054 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005055 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08005056 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00005057 case RX_HANDLER_EXACT:
5058 deliver_exact = true;
5059 case RX_HANDLER_PASS:
5060 break;
5061 default:
5062 BUG();
5063 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00005064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005066 if (unlikely(skb_vlan_tag_present(skb))) {
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005067check_vlan_id:
5068 if (skb_vlan_tag_get_id(skb)) {
5069 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5070 * find vlan device.
5071 */
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005072 skb->pkt_type = PACKET_OTHERHOST;
Govindarajulu Varadarajan36b2f612019-06-14 06:13:54 -07005073 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5074 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5075 /* Outer header is 802.1P with vlan 0, inner header is
5076 * 802.1Q or 802.1AD and vlan_do_receive() above could
5077 * not find vlan dev for vlan id 0.
5078 */
5079 __vlan_hwaccel_clear_tag(skb);
5080 skb = skb_vlan_untag(skb);
5081 if (unlikely(!skb))
5082 goto out;
5083 if (vlan_do_receive(&skb))
5084 /* After stripping off 802.1P header with vlan 0
5085 * vlan dev is found for inner header.
5086 */
5087 goto another_round;
5088 else if (unlikely(!skb))
5089 goto out;
5090 else
5091 /* We have stripped outer 802.1P vlan 0 header.
5092 * But could not find vlan dev.
5093 * check again for vlan id to set OTHERHOST.
5094 */
5095 goto check_vlan_id;
5096 }
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005097 /* Note: we might in the future use prio bits
5098 * and set skb->priority like in vlan_do_receive()
5099 * For the time being, just ignore Priority Code Point
5100 */
Michał Mirosławb18175242018-11-09 00:18:02 +01005101 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07005102 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00005103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08005105
5106 /* deliver only exact match when indicated */
5107 if (likely(!deliver_exact)) {
5108 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5109 &ptype_base[ntohs(type) &
5110 PTYPE_HASH_MASK]);
5111 }
5112
5113 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5114 &orig_dev->ptype_specific);
5115
5116 if (unlikely(skb->dev != orig_dev)) {
5117 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5118 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
5120
5121 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005122 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00005123 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01005124 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07005126drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05005127 if (!deliver_exact)
5128 atomic_long_inc(&skb->dev->rx_dropped);
5129 else
5130 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 kfree_skb(skb);
5132 /* Jamal, now you will not able to escape explaining
5133 * me how you were going to use this. :-)
5134 */
5135 ret = NET_RX_DROP;
5136 }
5137
Julian Anastasov2c17d272015-07-09 09:59:10 +03005138out:
David S. Miller9754e292013-02-14 15:57:38 -05005139 return ret;
5140}
5141
Edward Cree88eb1942018-07-02 16:13:56 +01005142static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5143{
5144 struct net_device *orig_dev = skb->dev;
5145 struct packet_type *pt_prev = NULL;
5146 int ret;
5147
5148 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
5149 if (pt_prev)
Paolo Abenif5737cb2019-05-03 17:01:36 +02005150 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5151 skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005152 return ret;
5153}
5154
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005155/**
5156 * netif_receive_skb_core - special purpose version of netif_receive_skb
5157 * @skb: buffer to process
5158 *
5159 * More direct receive version of netif_receive_skb(). It should
5160 * only be used by callers that have a need to skip RPS and Generic XDP.
5161 * Caller must also take care of handling if (page_is_)pfmemalloc.
5162 *
5163 * This function may only be called from softirq context and interrupts
5164 * should be enabled.
5165 *
5166 * Return values (usually ignored):
5167 * NET_RX_SUCCESS: no congestion
5168 * NET_RX_DROP: packet was dropped
5169 */
5170int netif_receive_skb_core(struct sk_buff *skb)
5171{
5172 int ret;
5173
5174 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01005175 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02005176 rcu_read_unlock();
5177
5178 return ret;
5179}
5180EXPORT_SYMBOL(netif_receive_skb_core);
5181
Edward Cree88eb1942018-07-02 16:13:56 +01005182static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5183 struct packet_type *pt_prev,
5184 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005185{
5186 struct sk_buff *skb, *next;
5187
Edward Cree88eb1942018-07-02 16:13:56 +01005188 if (!pt_prev)
5189 return;
5190 if (list_empty(head))
5191 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005192 if (pt_prev->list_func != NULL)
Paolo Abenifdf71422019-06-04 11:44:06 +02005193 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5194 ip_list_rcv, head, pt_prev, orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +01005195 else
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005196 list_for_each_entry_safe(skb, next, head, list) {
5197 skb_list_del_init(skb);
Paolo Abenifdf71422019-06-04 11:44:06 +02005198 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Alexander Lobakin9a5a90d2019-03-28 18:23:04 +03005199 }
Edward Cree88eb1942018-07-02 16:13:56 +01005200}
5201
5202static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5203{
5204 /* Fast-path assumptions:
5205 * - There is no RX handler.
5206 * - Only one packet_type matches.
5207 * If either of these fails, we will end up doing some per-packet
5208 * processing in-line, then handling the 'last ptype' for the whole
5209 * sublist. This can't cause out-of-order delivery to any single ptype,
5210 * because the 'last ptype' must be constant across the sublist, and all
5211 * other ptypes are handled per-packet.
5212 */
5213 /* Current (common) ptype of sublist */
5214 struct packet_type *pt_curr = NULL;
5215 /* Current (common) orig_dev of sublist */
5216 struct net_device *od_curr = NULL;
5217 struct list_head sublist;
5218 struct sk_buff *skb, *next;
5219
Edward Cree9af86f932018-07-09 18:10:19 +01005220 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005221 list_for_each_entry_safe(skb, next, head, list) {
5222 struct net_device *orig_dev = skb->dev;
5223 struct packet_type *pt_prev = NULL;
5224
Edward Cree22f6bbb2018-12-04 17:37:57 +00005225 skb_list_del_init(skb);
Edward Cree88eb1942018-07-02 16:13:56 +01005226 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
Edward Cree9af86f932018-07-09 18:10:19 +01005227 if (!pt_prev)
5228 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005229 if (pt_curr != pt_prev || od_curr != orig_dev) {
5230 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005231 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5232 /* start new sublist */
Edward Cree9af86f932018-07-09 18:10:19 +01005233 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005234 pt_curr = pt_prev;
5235 od_curr = orig_dev;
5236 }
Edward Cree9af86f932018-07-09 18:10:19 +01005237 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005238 }
5239
5240 /* dispatch final sublist */
Edward Cree9af86f932018-07-09 18:10:19 +01005241 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005242}
5243
David S. Miller9754e292013-02-14 15:57:38 -05005244static int __netif_receive_skb(struct sk_buff *skb)
5245{
5246 int ret;
5247
5248 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005249 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005250
5251 /*
5252 * PFMEMALLOC skbs are special, they should
5253 * - be delivered to SOCK_MEMALLOC sockets only
5254 * - stay away from userspace
5255 * - have bounded memory usage
5256 *
5257 * Use PF_MEMALLOC as this saves us from propagating the allocation
5258 * context down to all allocation sites.
5259 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005260 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005261 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005262 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005263 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005264 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 return ret;
5267}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005268
Edward Cree4ce00172018-07-02 16:13:40 +01005269static void __netif_receive_skb_list(struct list_head *head)
5270{
5271 unsigned long noreclaim_flag = 0;
5272 struct sk_buff *skb, *next;
5273 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5274
5275 list_for_each_entry_safe(skb, next, head, list) {
5276 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5277 struct list_head sublist;
5278
5279 /* Handle the previous sublist */
5280 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005281 if (!list_empty(&sublist))
5282 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005283 pfmemalloc = !pfmemalloc;
5284 /* See comments in __netif_receive_skb */
5285 if (pfmemalloc)
5286 noreclaim_flag = memalloc_noreclaim_save();
5287 else
5288 memalloc_noreclaim_restore(noreclaim_flag);
5289 }
5290 }
5291 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005292 if (!list_empty(head))
5293 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005294 /* Restore pflags */
5295 if (pfmemalloc)
5296 memalloc_noreclaim_restore(noreclaim_flag);
5297}
5298
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005299static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005300{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005301 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005302 struct bpf_prog *new = xdp->prog;
5303 int ret = 0;
5304
5305 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005306 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005307 rcu_assign_pointer(dev->xdp_prog, new);
5308 if (old)
5309 bpf_prog_put(old);
5310
5311 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005312 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005313 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005314 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005315 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005316 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005317 }
5318 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005319
5320 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07005321 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04005322 break;
5323
5324 default:
5325 ret = -EINVAL;
5326 break;
5327 }
5328
5329 return ret;
5330}
5331
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005332static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005333{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005334 int ret;
5335
Eric Dumazet588f0332011-11-15 04:12:55 +00005336 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005337
Richard Cochranc1f19b52010-07-17 08:49:36 +00005338 if (skb_defer_rx_timestamp(skb))
5339 return NET_RX_SUCCESS;
5340
John Fastabendbbbe2112017-09-08 14:00:30 -07005341 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005342#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005343 if (static_branch_unlikely(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005344 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005345 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005346
Eric Dumazet3b098e22010-05-15 23:57:10 -07005347 if (cpu >= 0) {
5348 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5349 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005350 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005351 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005352 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005353#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005354 ret = __netif_receive_skb(skb);
5355 rcu_read_unlock();
5356 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005357}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005358
Edward Cree7da517a2018-07-02 16:13:24 +01005359static void netif_receive_skb_list_internal(struct list_head *head)
5360{
Edward Cree7da517a2018-07-02 16:13:24 +01005361 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005362 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005363
Edward Cree8c057ef2018-07-09 18:09:54 +01005364 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005365 list_for_each_entry_safe(skb, next, head, list) {
5366 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005367 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005368 if (!skb_defer_rx_timestamp(skb))
5369 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005370 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005371 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005372
Edward Cree7da517a2018-07-02 16:13:24 +01005373 rcu_read_lock();
5374#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07005375 if (static_branch_unlikely(&rps_needed)) {
Edward Cree7da517a2018-07-02 16:13:24 +01005376 list_for_each_entry_safe(skb, next, head, list) {
5377 struct rps_dev_flow voidflow, *rflow = &voidflow;
5378 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5379
5380 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005381 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005382 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005383 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005384 }
5385 }
5386 }
5387#endif
5388 __netif_receive_skb_list(head);
5389 rcu_read_unlock();
5390}
5391
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005392/**
5393 * netif_receive_skb - process receive buffer from network
5394 * @skb: buffer to process
5395 *
5396 * netif_receive_skb() is the main receive data processing function.
5397 * It always succeeds. The buffer may be dropped during processing
5398 * for congestion control or by the protocol layers.
5399 *
5400 * This function may only be called from softirq context and interrupts
5401 * should be enabled.
5402 *
5403 * Return values (usually ignored):
5404 * NET_RX_SUCCESS: no congestion
5405 * NET_RX_DROP: packet was dropped
5406 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005407int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005408{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005409 int ret;
5410
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005411 trace_netif_receive_skb_entry(skb);
5412
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005413 ret = netif_receive_skb_internal(skb);
5414 trace_netif_receive_skb_exit(ret);
5415
5416 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005417}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005418EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
Edward Creef6ad8c12018-07-02 16:12:45 +01005420/**
5421 * netif_receive_skb_list - process many receive buffers from network
5422 * @head: list of skbs to process.
5423 *
Edward Cree7da517a2018-07-02 16:13:24 +01005424 * Since return value of netif_receive_skb() is normally ignored, and
5425 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005426 *
5427 * This function may only be called from softirq context and interrupts
5428 * should be enabled.
5429 */
5430void netif_receive_skb_list(struct list_head *head)
5431{
Edward Cree7da517a2018-07-02 16:13:24 +01005432 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005433
Edward Creeb9f463d2018-07-02 16:14:44 +01005434 if (list_empty(head))
5435 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005436 if (trace_netif_receive_skb_list_entry_enabled()) {
5437 list_for_each_entry(skb, head, list)
5438 trace_netif_receive_skb_list_entry(skb);
5439 }
Edward Cree7da517a2018-07-02 16:13:24 +01005440 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005441 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005442}
5443EXPORT_SYMBOL(netif_receive_skb_list);
5444
Eric Dumazet41852492016-08-26 12:50:39 -07005445DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005446
5447/* Network device is going away, flush any packets still pending */
5448static void flush_backlog(struct work_struct *work)
5449{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005450 struct sk_buff *skb, *tmp;
5451 struct softnet_data *sd;
5452
5453 local_bh_disable();
5454 sd = this_cpu_ptr(&softnet_data);
5455
5456 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005457 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005458 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005459 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005460 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005461 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005462 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005463 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005464 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005465 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005466 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005467
5468 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005469 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005470 __skb_unlink(skb, &sd->process_queue);
5471 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005472 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005473 }
5474 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005475 local_bh_enable();
5476}
5477
Eric Dumazet41852492016-08-26 12:50:39 -07005478static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005479{
5480 unsigned int cpu;
5481
5482 get_online_cpus();
5483
Eric Dumazet41852492016-08-26 12:50:39 -07005484 for_each_online_cpu(cpu)
5485 queue_work_on(cpu, system_highpri_wq,
5486 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005487
5488 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07005489 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005490
5491 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005492}
5493
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005494INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5495INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Herbert Xud565b0a2008-12-15 23:38:52 -08005496static int napi_gro_complete(struct sk_buff *skb)
5497{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005498 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005499 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005500 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005501 int err = -ENOENT;
5502
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005503 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5504
Herbert Xufc59f9a2009-04-14 15:11:06 -07005505 if (NAPI_GRO_CB(skb)->count == 1) {
5506 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005507 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005508 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005509
5510 rcu_read_lock();
5511 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005512 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005513 continue;
5514
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005515 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5516 ipv6_gro_complete, inet_gro_complete,
5517 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005518 break;
5519 }
5520 rcu_read_unlock();
5521
5522 if (err) {
5523 WARN_ON(&ptype->list == head);
5524 kfree_skb(skb);
5525 return NET_RX_SUCCESS;
5526 }
5527
5528out:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005529 return netif_receive_skb_internal(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005530}
5531
Li RongQing6312fe72018-07-05 14:34:32 +08005532static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005533 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005534{
Li RongQing6312fe72018-07-05 14:34:32 +08005535 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005536 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005537
David Miller07d78362018-06-24 14:14:02 +09005538 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005539 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5540 return;
David S. Miller992cba72018-07-31 15:27:56 -07005541 skb_list_del_init(skb);
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005542 napi_gro_complete(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005543 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005544 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005545
5546 if (!napi->gro_hash[index].count)
5547 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005548}
David Miller07d78362018-06-24 14:14:02 +09005549
Li RongQing6312fe72018-07-05 14:34:32 +08005550/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005551 * youngest packets at the head of it.
5552 * Complete skbs in reverse order to reduce latencies.
5553 */
5554void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5555{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005556 unsigned long bitmask = napi->gro_bitmask;
5557 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005558
Eric Dumazet42519ed2018-11-21 11:39:28 -08005559 while ((i = ffs(bitmask)) != 0) {
5560 bitmask >>= i;
5561 base += i;
5562 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005563 }
David Miller07d78362018-06-24 14:14:02 +09005564}
Eric Dumazet86cac582010-08-31 18:25:32 +00005565EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005566
David Miller07d78362018-06-24 14:14:02 +09005567static struct list_head *gro_list_prepare(struct napi_struct *napi,
5568 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005569{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005570 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005571 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005572 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005573 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005574
Li RongQing6312fe72018-07-05 14:34:32 +08005575 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005576 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005577 unsigned long diffs;
5578
Tom Herbert0b4cec82014-01-15 08:58:06 -08005579 NAPI_GRO_CB(p)->flush = 0;
5580
5581 if (hash != skb_get_hash_raw(p)) {
5582 NAPI_GRO_CB(p)->same_flow = 0;
5583 continue;
5584 }
5585
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005586 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005587 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5588 if (skb_vlan_tag_present(p))
Tonghao Zhangfc5141c2019-11-22 20:38:01 +08005589 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
Jesse Grossce87fc62016-01-20 17:59:49 -08005590 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005591 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005592 if (maclen == ETH_HLEN)
5593 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005594 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005595 else if (!diffs)
5596 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005597 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005598 maclen);
5599 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005600 }
David Miller07d78362018-06-24 14:14:02 +09005601
5602 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005603}
5604
Jerry Chu299603e82013-12-11 20:53:45 -08005605static void skb_gro_reset_offset(struct sk_buff *skb)
5606{
5607 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5608 const skb_frag_t *frag0 = &pinfo->frags[0];
5609
5610 NAPI_GRO_CB(skb)->data_offset = 0;
5611 NAPI_GRO_CB(skb)->frag0 = NULL;
5612 NAPI_GRO_CB(skb)->frag0_len = 0;
5613
Alexander Lobakin8aef9982019-11-15 12:11:35 +03005614 if (!skb_headlen(skb) && pinfo->nr_frags &&
Jerry Chu299603e82013-12-11 20:53:45 -08005615 !PageHighMem(skb_frag_page(frag0))) {
5616 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005617 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5618 skb_frag_size(frag0),
5619 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005620 }
5621}
5622
Eric Dumazeta50e2332014-03-29 21:28:21 -07005623static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5624{
5625 struct skb_shared_info *pinfo = skb_shinfo(skb);
5626
5627 BUG_ON(skb->end - skb->tail < grow);
5628
5629 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5630
5631 skb->data_len -= grow;
5632 skb->tail += grow;
5633
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07005634 skb_frag_off_add(&pinfo->frags[0], grow);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005635 skb_frag_size_sub(&pinfo->frags[0], grow);
5636
5637 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5638 skb_frag_unref(skb, 0);
5639 memmove(pinfo->frags, pinfo->frags + 1,
5640 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5641 }
5642}
5643
Li RongQing6312fe72018-07-05 14:34:32 +08005644static void gro_flush_oldest(struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005645{
Li RongQing6312fe72018-07-05 14:34:32 +08005646 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005647
Li RongQing6312fe72018-07-05 14:34:32 +08005648 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005649
Li RongQing6312fe72018-07-05 14:34:32 +08005650 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005651 * impossible.
5652 */
5653 if (WARN_ON_ONCE(!oldest))
5654 return;
5655
Li RongQingd9f37d02018-07-13 14:41:36 +08005656 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5657 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005658 */
David S. Millerece23712018-10-28 10:35:12 -07005659 skb_list_del_init(oldest);
David Miller07d78362018-06-24 14:14:02 +09005660 napi_gro_complete(oldest);
5661}
5662
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005663INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5664 struct sk_buff *));
5665INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5666 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005667static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005668{
Li RongQing6312fe72018-07-05 14:34:32 +08005669 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005670 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005671 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005672 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005673 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005674 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005675 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005676 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005677 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005678
David S. Millerb5cdae32017-04-18 15:36:58 -04005679 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005680 goto normal;
5681
David Miller07d78362018-06-24 14:14:02 +09005682 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005683
Herbert Xud565b0a2008-12-15 23:38:52 -08005684 rcu_read_lock();
5685 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005686 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005687 continue;
5688
Herbert Xu86911732009-01-29 14:19:50 +00005689 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005690 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005691 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005692 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005693 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005694 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005695 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005696 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005697 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005698 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005699
Tom Herbert662880f2014-08-27 21:26:56 -07005700 /* Setup for GRO checksum validation */
5701 switch (skb->ip_summed) {
5702 case CHECKSUM_COMPLETE:
5703 NAPI_GRO_CB(skb)->csum = skb->csum;
5704 NAPI_GRO_CB(skb)->csum_valid = 1;
5705 NAPI_GRO_CB(skb)->csum_cnt = 0;
5706 break;
5707 case CHECKSUM_UNNECESSARY:
5708 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5709 NAPI_GRO_CB(skb)->csum_valid = 0;
5710 break;
5711 default:
5712 NAPI_GRO_CB(skb)->csum_cnt = 0;
5713 NAPI_GRO_CB(skb)->csum_valid = 0;
5714 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005715
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005716 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5717 ipv6_gro_receive, inet_gro_receive,
5718 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005719 break;
5720 }
5721 rcu_read_unlock();
5722
5723 if (&ptype->list == head)
5724 goto normal;
5725
Steffen Klassert25393d32017-02-15 09:39:44 +01005726 if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
5727 ret = GRO_CONSUMED;
5728 goto ok;
5729 }
5730
Herbert Xu0da2afd52008-12-26 14:57:42 -08005731 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005732 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005733
Herbert Xud565b0a2008-12-15 23:38:52 -08005734 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005735 skb_list_del_init(pp);
David Millerd4546c22018-06-24 14:13:49 +09005736 napi_gro_complete(pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005737 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005738 }
5739
Herbert Xu0da2afd52008-12-26 14:57:42 -08005740 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005741 goto ok;
5742
Eric Dumazet600adc12014-01-09 14:12:19 -08005743 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08005744 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08005745
Li RongQing6312fe72018-07-05 14:34:32 +08005746 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
5747 gro_flush_oldest(gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08005748 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08005749 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08005750 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005751 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005752 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07005753 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00005754 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09005755 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005756 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08005757
Herbert Xuad0f9902009-02-01 01:24:55 -08005758pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005759 grow = skb_gro_offset(skb) - skb_headlen(skb);
5760 if (grow > 0)
5761 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08005762ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08005763 if (napi->gro_hash[hash].count) {
5764 if (!test_bit(hash, &napi->gro_bitmask))
5765 __set_bit(hash, &napi->gro_bitmask);
5766 } else if (test_bit(hash, &napi->gro_bitmask)) {
5767 __clear_bit(hash, &napi->gro_bitmask);
5768 }
5769
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005770 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005771
5772normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08005773 ret = GRO_NORMAL;
5774 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08005775}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005776
Jerry Chubf5a7552014-01-07 10:23:19 -08005777struct packet_offload *gro_find_receive_by_type(__be16 type)
5778{
5779 struct list_head *offload_head = &offload_base;
5780 struct packet_offload *ptype;
5781
5782 list_for_each_entry_rcu(ptype, offload_head, list) {
5783 if (ptype->type != type || !ptype->callbacks.gro_receive)
5784 continue;
5785 return ptype;
5786 }
5787 return NULL;
5788}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005789EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08005790
5791struct packet_offload *gro_find_complete_by_type(__be16 type)
5792{
5793 struct list_head *offload_head = &offload_base;
5794 struct packet_offload *ptype;
5795
5796 list_for_each_entry_rcu(ptype, offload_head, list) {
5797 if (ptype->type != type || !ptype->callbacks.gro_complete)
5798 continue;
5799 return ptype;
5800 }
5801 return NULL;
5802}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005803EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005804
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005805/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5806static void gro_normal_list(struct napi_struct *napi)
5807{
5808 if (!napi->rx_count)
5809 return;
5810 netif_receive_skb_list_internal(&napi->rx_list);
5811 INIT_LIST_HEAD(&napi->rx_list);
5812 napi->rx_count = 0;
5813}
5814
5815/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5816 * pass the whole batch up to the stack.
5817 */
5818static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5819{
5820 list_add_tail(&skb->list, &napi->rx_list);
5821 if (++napi->rx_count >= gro_normal_batch)
5822 gro_normal_list(napi);
5823}
5824
Michal Kubečeke44699d2017-06-29 11:13:36 +02005825static void napi_skb_free_stolen_head(struct sk_buff *skb)
5826{
5827 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005828 skb_ext_put(skb);
Michal Kubečeke44699d2017-06-29 11:13:36 +02005829 kmem_cache_free(skbuff_head_cache, skb);
5830}
5831
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005832static gro_result_t napi_skb_finish(struct napi_struct *napi,
5833 struct sk_buff *skb,
5834 gro_result_t ret)
Herbert Xu5d38a072009-01-04 16:13:40 -08005835{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005836 switch (ret) {
5837 case GRO_NORMAL:
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005838 gro_normal_one(napi, skb);
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005839 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005840
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005841 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08005842 kfree_skb(skb);
5843 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005844
Eric Dumazetdaa86542012-04-19 07:07:40 +00005845 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02005846 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5847 napi_skb_free_stolen_head(skb);
5848 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00005849 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00005850 break;
5851
Ben Hutchings5b252f02009-10-29 07:17:09 +00005852 case GRO_HELD:
5853 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005854 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005855 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005856 }
5857
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005858 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005859}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005860
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005861gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005862{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005863 gro_result_t ret;
5864
Eric Dumazet93f93a42015-11-18 06:30:59 -08005865 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005866 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00005867
Eric Dumazeta50e2332014-03-29 21:28:21 -07005868 skb_gro_reset_offset(skb);
5869
Alexander Lobakin6570bc72019-10-14 11:00:33 +03005870 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005871 trace_napi_gro_receive_exit(ret);
5872
5873 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005874}
5875EXPORT_SYMBOL(napi_gro_receive);
5876
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00005877static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005878{
Eric Dumazet93a35f52014-10-23 06:30:30 -07005879 if (unlikely(skb->pfmemalloc)) {
5880 consume_skb(skb);
5881 return;
5882 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005883 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00005884 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5885 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01005886 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08005887 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08005888 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08005889
5890 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5891 skb->pkt_type = PACKET_HOST;
5892
Jerry Chuc3caf112014-07-14 15:54:46 -07005893 skb->encapsulation = 0;
5894 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07005895 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Florian Westphal174e2382019-09-26 20:37:05 +02005896 skb_ext_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005897
5898 napi->skb = skb;
5899}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005900
Herbert Xu76620aa2009-04-16 02:02:07 -07005901struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005902{
Herbert Xu5d38a072009-01-04 16:13:40 -08005903 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005904
5905 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005906 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005907 if (skb) {
5908 napi->skb = skb;
5909 skb_mark_napi_id(skb, napi);
5910 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005911 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005912 return skb;
5913}
Herbert Xu76620aa2009-04-16 02:02:07 -07005914EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005915
Eric Dumazeta50e2332014-03-29 21:28:21 -07005916static gro_result_t napi_frags_finish(struct napi_struct *napi,
5917 struct sk_buff *skb,
5918 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005919{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005920 switch (ret) {
5921 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005922 case GRO_HELD:
5923 __skb_push(skb, ETH_HLEN);
5924 skb->protocol = eth_type_trans(skb, skb->dev);
Edward Cree323ebb62019-08-06 14:53:55 +01005925 if (ret == GRO_NORMAL)
5926 gro_normal_one(napi, skb);
Herbert Xu86911732009-01-29 14:19:50 +00005927 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005928
5929 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005930 napi_reuse_skb(napi, skb);
5931 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005932
Michal Kubečeke44699d2017-06-29 11:13:36 +02005933 case GRO_MERGED_FREE:
5934 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5935 napi_skb_free_stolen_head(skb);
5936 else
5937 napi_reuse_skb(napi, skb);
5938 break;
5939
Ben Hutchings5b252f02009-10-29 07:17:09 +00005940 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005941 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005942 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005943 }
5944
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005945 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005946}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005947
Eric Dumazeta50e2332014-03-29 21:28:21 -07005948/* Upper GRO stack assumes network header starts at gro_offset=0
5949 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5950 * We copy ethernet header into skb->data to have a common layout.
5951 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005952static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005953{
Herbert Xu76620aa2009-04-16 02:02:07 -07005954 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005955 const struct ethhdr *eth;
5956 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005957
5958 napi->skb = NULL;
5959
Eric Dumazeta50e2332014-03-29 21:28:21 -07005960 skb_reset_mac_header(skb);
5961 skb_gro_reset_offset(skb);
5962
Eric Dumazeta50e2332014-03-29 21:28:21 -07005963 if (unlikely(skb_gro_header_hard(skb, hlen))) {
5964 eth = skb_gro_header_slow(skb, hlen, 0);
5965 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04005966 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
5967 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005968 napi_reuse_skb(napi, skb);
5969 return NULL;
5970 }
5971 } else {
Eric Dumazeta4270d62019-05-29 15:36:10 -07005972 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005973 gro_pull_from_frag0(skb, hlen);
5974 NAPI_GRO_CB(skb)->frag0 += hlen;
5975 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07005976 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07005977 __skb_pull(skb, hlen);
5978
5979 /*
5980 * This works because the only protocols we care about don't require
5981 * special handling.
5982 * We'll fix it up properly in napi_frags_finish()
5983 */
5984 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07005985
Herbert Xu76620aa2009-04-16 02:02:07 -07005986 return skb;
5987}
Herbert Xu76620aa2009-04-16 02:02:07 -07005988
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005989gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07005990{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005991 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07005992 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005993
5994 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005995 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08005996
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005997 trace_napi_gro_frags_entry(skb);
5998
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005999 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6000 trace_napi_gro_frags_exit(ret);
6001
6002 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08006003}
6004EXPORT_SYMBOL(napi_gro_frags);
6005
Tom Herbert573e8fc2014-08-22 13:33:47 -07006006/* Compute the checksum from gro_offset and return the folded value
6007 * after adding in any pseudo checksum.
6008 */
6009__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6010{
6011 __wsum wsum;
6012 __sum16 sum;
6013
6014 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6015
6016 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6017 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08006018 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07006019 if (likely(!sum)) {
6020 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6021 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08006022 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07006023 }
6024
6025 NAPI_GRO_CB(skb)->csum = wsum;
6026 NAPI_GRO_CB(skb)->csum_valid = 1;
6027
6028 return sum;
6029}
6030EXPORT_SYMBOL(__skb_gro_checksum_complete);
6031
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306032static void net_rps_send_ipi(struct softnet_data *remsd)
6033{
6034#ifdef CONFIG_RPS
6035 while (remsd) {
6036 struct softnet_data *next = remsd->rps_ipi_next;
6037
6038 if (cpu_online(remsd->cpu))
6039 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6040 remsd = next;
6041 }
6042#endif
6043}
6044
Eric Dumazete326bed2010-04-22 00:22:45 -07006045/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08006046 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07006047 * Note: called with local irq disabled, but exits with local irq enabled.
6048 */
6049static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6050{
6051#ifdef CONFIG_RPS
6052 struct softnet_data *remsd = sd->rps_ipi_list;
6053
6054 if (remsd) {
6055 sd->rps_ipi_list = NULL;
6056
6057 local_irq_enable();
6058
6059 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05306060 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07006061 } else
6062#endif
6063 local_irq_enable();
6064}
6065
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006066static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6067{
6068#ifdef CONFIG_RPS
6069 return sd->rps_ipi_list != NULL;
6070#else
6071 return false;
6072#endif
6073}
6074
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006075static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006077 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006078 bool again = true;
6079 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Eric Dumazete326bed2010-04-22 00:22:45 -07006081 /* Check if we have pending ipi, its better to send them now,
6082 * not waiting net_rx_action() end.
6083 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006084 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07006085 local_irq_disable();
6086 net_rps_action_and_irq_enable(sd);
6087 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006088
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01006089 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006090 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
Changli Gao6e7676c2010-04-27 15:07:33 -07006093 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03006094 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07006095 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03006096 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00006097 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006098 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00006099 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006100
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006103 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006104 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07006105 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006106 /*
6107 * Inline a custom version of __napi_complete().
6108 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07006109 * and NAPI_STATE_SCHED is the only possible flag set
6110 * on backlog.
6111 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006112 * and we dont need an smp_mb() memory barrier.
6113 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07006114 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006115 again = false;
6116 } else {
6117 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6118 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006119 }
6120 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02006121 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07006122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006124 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125}
6126
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006127/**
6128 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006129 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006130 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006131 * The entry's receive function will be scheduled to run.
6132 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006133 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08006134void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006135{
6136 unsigned long flags;
6137
6138 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05006139 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006140 local_irq_restore(flags);
6141}
6142EXPORT_SYMBOL(__napi_schedule);
6143
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006144/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08006145 * napi_schedule_prep - check if napi can be scheduled
6146 * @n: napi context
6147 *
6148 * Test if NAPI routine is already running, and if not mark
6149 * it as running. This is used as a condition variable
6150 * insure only one NAPI poll instance runs. We also make
6151 * sure there is no pending NAPI disable.
6152 */
6153bool napi_schedule_prep(struct napi_struct *n)
6154{
6155 unsigned long val, new;
6156
6157 do {
6158 val = READ_ONCE(n->state);
6159 if (unlikely(val & NAPIF_STATE_DISABLE))
6160 return false;
6161 new = val | NAPIF_STATE_SCHED;
6162
6163 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6164 * This was suggested by Alexander Duyck, as compiler
6165 * emits better code than :
6166 * if (val & NAPIF_STATE_SCHED)
6167 * new |= NAPIF_STATE_MISSED;
6168 */
6169 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6170 NAPIF_STATE_MISSED;
6171 } while (cmpxchg(&n->state, val, new) != val);
6172
6173 return !(val & NAPIF_STATE_SCHED);
6174}
6175EXPORT_SYMBOL(napi_schedule_prep);
6176
6177/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006178 * __napi_schedule_irqoff - schedule for receive
6179 * @n: entry to schedule
6180 *
6181 * Variant of __napi_schedule() assuming hard irqs are masked
6182 */
6183void __napi_schedule_irqoff(struct napi_struct *n)
6184{
6185 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6186}
6187EXPORT_SYMBOL(__napi_schedule_irqoff);
6188
Eric Dumazet364b6052016-11-15 10:15:13 -08006189bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006190{
Eric Dumazet39e6c822017-02-28 10:34:50 -08006191 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08006192
6193 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006194 * 1) Don't let napi dequeue from the cpu poll list
6195 * just in case its running on a different cpu.
6196 * 2) If we are busy polling, do nothing here, we have
6197 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006198 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006199 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6200 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006201 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006202
Edward Cree323ebb62019-08-06 14:53:55 +01006203 gro_normal_list(n);
6204
Li RongQingd9f37d02018-07-13 14:41:36 +08006205 if (n->gro_bitmask) {
Eric Dumazet3b47d302014-11-06 21:09:44 -08006206 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006207
Eric Dumazet3b47d302014-11-06 21:09:44 -08006208 if (work_done)
6209 timeout = n->dev->gro_flush_timeout;
6210
Paolo Abeni605108a2018-11-21 18:21:35 +01006211 /* When the NAPI instance uses a timeout and keeps postponing
6212 * it, we need to bound somehow the time packets are kept in
6213 * the GRO layer
6214 */
6215 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006216 if (timeout)
6217 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6218 HRTIMER_MODE_REL_PINNED);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006219 }
Eric Dumazet02c16022017-02-04 15:25:02 -08006220 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006221 /* If n->poll_list is not empty, we need to mask irqs */
6222 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006223 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006224 local_irq_restore(flags);
6225 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006226
6227 do {
6228 val = READ_ONCE(n->state);
6229
6230 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6231
6232 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6233
6234 /* If STATE_MISSED was set, leave STATE_SCHED set,
6235 * because we will call napi->poll() one more time.
6236 * This C code was suggested by Alexander Duyck to help gcc.
6237 */
6238 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6239 NAPIF_STATE_SCHED;
6240 } while (cmpxchg(&n->state, val, new) != val);
6241
6242 if (unlikely(val & NAPIF_STATE_MISSED)) {
6243 __napi_schedule(n);
6244 return false;
6245 }
6246
Eric Dumazet364b6052016-11-15 10:15:13 -08006247 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006248}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006249EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006250
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006251/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006252static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006253{
6254 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6255 struct napi_struct *napi;
6256
6257 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6258 if (napi->napi_id == napi_id)
6259 return napi;
6260
6261 return NULL;
6262}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006263
6264#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006265
Eric Dumazetce6aea92015-11-18 06:30:54 -08006266#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006267
6268static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6269{
6270 int rc;
6271
Eric Dumazet39e6c822017-02-28 10:34:50 -08006272 /* Busy polling means there is a high chance device driver hard irq
6273 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6274 * set in napi_schedule_prep().
6275 * Since we are about to call napi->poll() once more, we can safely
6276 * clear NAPI_STATE_MISSED.
6277 *
6278 * Note: x86 could use a single "lock and ..." instruction
6279 * to perform these two clear_bit()
6280 */
6281 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006282 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6283
6284 local_bh_disable();
6285
6286 /* All we really want here is to re-enable device interrupts.
6287 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6288 */
6289 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006290 /* We can't gro_normal_list() here, because napi->poll() might have
6291 * rearmed the napi (napi_complete_done()) in which case it could
6292 * already be running on another CPU.
6293 */
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006294 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006295 netpoll_poll_unlock(have_poll_lock);
Edward Cree323ebb62019-08-06 14:53:55 +01006296 if (rc == BUSY_POLL_BUDGET) {
6297 /* As the whole budget was spent, we still own the napi so can
6298 * safely handle the rx_list.
6299 */
6300 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006301 __napi_schedule(napi);
Edward Cree323ebb62019-08-06 14:53:55 +01006302 }
Eric Dumazet217f6972016-11-15 10:15:11 -08006303 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006304}
6305
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006306void napi_busy_loop(unsigned int napi_id,
6307 bool (*loop_end)(void *, unsigned long),
6308 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006309{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006310 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006311 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006312 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006313 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006314
6315restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006316 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006317
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006318 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006319
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006320 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006321 if (!napi)
6322 goto out;
6323
Eric Dumazet217f6972016-11-15 10:15:11 -08006324 preempt_disable();
6325 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006326 int work = 0;
6327
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006328 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006329 if (!napi_poll) {
6330 unsigned long val = READ_ONCE(napi->state);
6331
6332 /* If multiple threads are competing for this napi,
6333 * we avoid dirtying napi->state as much as we can.
6334 */
6335 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6336 NAPIF_STATE_IN_BUSY_POLL))
6337 goto count;
6338 if (cmpxchg(&napi->state, val,
6339 val | NAPIF_STATE_IN_BUSY_POLL |
6340 NAPIF_STATE_SCHED) != val)
6341 goto count;
6342 have_poll_lock = netpoll_poll_lock(napi);
6343 napi_poll = napi->poll;
6344 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006345 work = napi_poll(napi, BUSY_POLL_BUDGET);
6346 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Edward Cree323ebb62019-08-06 14:53:55 +01006347 gro_normal_list(napi);
Eric Dumazet217f6972016-11-15 10:15:11 -08006348count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006349 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006350 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006351 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006352 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006353
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006354 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006355 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006356
Eric Dumazet217f6972016-11-15 10:15:11 -08006357 if (unlikely(need_resched())) {
6358 if (napi_poll)
6359 busy_poll_stop(napi, have_poll_lock);
6360 preempt_enable();
6361 rcu_read_unlock();
6362 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006363 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006364 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006365 goto restart;
6366 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006367 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006368 }
6369 if (napi_poll)
6370 busy_poll_stop(napi, have_poll_lock);
6371 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006372out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006373 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006374}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006375EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006376
6377#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006378
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006379static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006380{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08006381 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6382 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006383 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006384
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006385 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006386
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006387 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006388 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006389 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6390 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006391 } while (napi_by_id(napi_gen_id));
6392 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006393
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006394 hlist_add_head_rcu(&napi->napi_hash_node,
6395 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006396
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006397 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006398}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006399
6400/* Warning : caller is responsible to make sure rcu grace period
6401 * is respected before freeing memory containing @napi
6402 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08006403bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006404{
Eric Dumazet34cbe272015-11-18 06:31:02 -08006405 bool rcu_sync_needed = false;
6406
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006407 spin_lock(&napi_hash_lock);
6408
Eric Dumazet34cbe272015-11-18 06:31:02 -08006409 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6410 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006411 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006412 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006413 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006414 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006415}
6416EXPORT_SYMBOL_GPL(napi_hash_del);
6417
Eric Dumazet3b47d302014-11-06 21:09:44 -08006418static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6419{
6420 struct napi_struct *napi;
6421
6422 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006423
6424 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6425 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6426 */
Li RongQingd9f37d02018-07-13 14:41:36 +08006427 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006428 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6429 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006430
6431 return HRTIMER_NORESTART;
6432}
6433
David S. Miller7c4ec742018-07-20 23:37:55 -07006434static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006435{
David Miller07d78362018-06-24 14:14:02 +09006436 int i;
6437
Li RongQing6312fe72018-07-05 14:34:32 +08006438 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6439 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6440 napi->gro_hash[i].count = 0;
6441 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006442 napi->gro_bitmask = 0;
6443}
6444
6445void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6446 int (*poll)(struct napi_struct *, int), int weight)
6447{
6448 INIT_LIST_HEAD(&napi->poll_list);
6449 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6450 napi->timer.function = napi_watchdog;
6451 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006452 napi->skb = NULL;
Edward Cree323ebb62019-08-06 14:53:55 +01006453 INIT_LIST_HEAD(&napi->rx_list);
6454 napi->rx_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006455 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006456 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006457 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6458 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006459 napi->weight = weight;
6460 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08006461 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006462#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006463 napi->poll_owner = -1;
6464#endif
6465 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006466 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006467}
6468EXPORT_SYMBOL(netif_napi_add);
6469
Eric Dumazet3b47d302014-11-06 21:09:44 -08006470void napi_disable(struct napi_struct *n)
6471{
6472 might_sleep();
6473 set_bit(NAPI_STATE_DISABLE, &n->state);
6474
6475 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6476 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006477 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6478 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006479
6480 hrtimer_cancel(&n->timer);
6481
6482 clear_bit(NAPI_STATE_DISABLE, &n->state);
6483}
6484EXPORT_SYMBOL(napi_disable);
6485
David Miller07d78362018-06-24 14:14:02 +09006486static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006487{
David Miller07d78362018-06-24 14:14:02 +09006488 int i;
David Millerd4546c22018-06-24 14:13:49 +09006489
David Miller07d78362018-06-24 14:14:02 +09006490 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6491 struct sk_buff *skb, *n;
6492
Li RongQing6312fe72018-07-05 14:34:32 +08006493 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006494 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006495 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006496 }
David Millerd4546c22018-06-24 14:13:49 +09006497}
6498
Eric Dumazet93d05d42015-11-18 06:31:03 -08006499/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08006500void netif_napi_del(struct napi_struct *napi)
6501{
Eric Dumazet93d05d42015-11-18 06:31:03 -08006502 might_sleep();
6503 if (napi_hash_del(napi))
6504 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08006505 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006506 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006507
David Miller07d78362018-06-24 14:14:02 +09006508 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006509 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006510}
6511EXPORT_SYMBOL(netif_napi_del);
6512
Herbert Xu726ce702014-12-21 07:16:21 +11006513static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6514{
6515 void *have;
6516 int work, weight;
6517
6518 list_del_init(&n->poll_list);
6519
6520 have = netpoll_poll_lock(n);
6521
6522 weight = n->weight;
6523
6524 /* This NAPI_STATE_SCHED test is for avoiding a race
6525 * with netpoll's poll_napi(). Only the entity which
6526 * obtains the lock and sees NAPI_STATE_SCHED set will
6527 * actually make the ->poll() call. Therefore we avoid
6528 * accidentally calling ->poll() when NAPI is not scheduled.
6529 */
6530 work = 0;
6531 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6532 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006533 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006534 }
6535
6536 WARN_ON_ONCE(work > weight);
6537
6538 if (likely(work < weight))
6539 goto out_unlock;
6540
6541 /* Drivers must not modify the NAPI state if they
6542 * consume the entire weight. In such cases this code
6543 * still "owns" the NAPI instance and therefore can
6544 * move the instance around on the list at-will.
6545 */
6546 if (unlikely(napi_disable_pending(n))) {
6547 napi_complete(n);
6548 goto out_unlock;
6549 }
6550
Edward Cree323ebb62019-08-06 14:53:55 +01006551 gro_normal_list(n);
6552
Li RongQingd9f37d02018-07-13 14:41:36 +08006553 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006554 /* flush too old packets
6555 * If HZ < 1000, flush all packets.
6556 */
6557 napi_gro_flush(n, HZ >= 1000);
6558 }
6559
Herbert Xu001ce542014-12-21 07:16:22 +11006560 /* Some drivers may have called napi_schedule
6561 * prior to exhausting their budget.
6562 */
6563 if (unlikely(!list_empty(&n->poll_list))) {
6564 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6565 n->dev ? n->dev->name : "backlog");
6566 goto out_unlock;
6567 }
6568
Herbert Xu726ce702014-12-21 07:16:21 +11006569 list_add_tail(&n->poll_list, repoll);
6570
6571out_unlock:
6572 netpoll_poll_unlock(have);
6573
6574 return work;
6575}
6576
Emese Revfy0766f782016-06-20 20:42:34 +02006577static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006579 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006580 unsigned long time_limit = jiffies +
6581 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006582 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006583 LIST_HEAD(list);
6584 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006585
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006587 list_splice_init(&sd->poll_list, &list);
6588 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006590 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006591 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006593 if (list_empty(&list)) {
6594 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006595 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006596 break;
6597 }
6598
Herbert Xu6bd373e2014-12-21 07:16:24 +11006599 n = list_first_entry(&list, struct napi_struct, poll_list);
6600 budget -= napi_poll(n, &repoll);
6601
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006602 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006603 * Allow this to run for 2 jiffies since which will allow
6604 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006605 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006606 if (unlikely(budget <= 0 ||
6607 time_after_eq(jiffies, time_limit))) {
6608 sd->time_squeeze++;
6609 break;
6610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006612
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006613 local_irq_disable();
6614
6615 list_splice_tail_init(&sd->poll_list, &list);
6616 list_splice_tail(&repoll, &list);
6617 list_splice(&list, &sd->poll_list);
6618 if (!list_empty(&sd->poll_list))
6619 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6620
Eric Dumazete326bed2010-04-22 00:22:45 -07006621 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006622out:
6623 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}
6625
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006626struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006627 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006628
6629 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006630 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006631
Taehee Yoo32b6d342019-10-21 18:47:56 +00006632 /* lookup ignore flag */
6633 bool ignore;
6634
Veaceslav Falico5d261912013-08-28 23:25:05 +02006635 /* counter for the number of times this device was added to us */
6636 u16 ref_nr;
6637
Veaceslav Falico402dae92013-09-25 09:20:09 +02006638 /* private field for the users */
6639 void *private;
6640
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006641 struct list_head list;
6642 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006643};
6644
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006645static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006646 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006647{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006648 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006649
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006650 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006651 if (adj->dev == adj_dev)
6652 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006653 }
6654 return NULL;
6655}
6656
Taehee Yoo32b6d342019-10-21 18:47:56 +00006657static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data)
David Ahernf1170fd2016-10-17 19:15:51 -07006658{
6659 struct net_device *dev = data;
6660
6661 return upper_dev == dev;
6662}
6663
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006664/**
6665 * netdev_has_upper_dev - Check if device is linked to an upper device
6666 * @dev: device
6667 * @upper_dev: upper device to check
6668 *
6669 * Find out if a device is linked to specified upper device and return true
6670 * in case it is. Note that this checks only immediate upper device,
6671 * not through a complete stack of devices. The caller must hold the RTNL lock.
6672 */
6673bool netdev_has_upper_dev(struct net_device *dev,
6674 struct net_device *upper_dev)
6675{
6676 ASSERT_RTNL();
6677
Taehee Yoo32b6d342019-10-21 18:47:56 +00006678 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
David Ahernf1170fd2016-10-17 19:15:51 -07006679 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006680}
6681EXPORT_SYMBOL(netdev_has_upper_dev);
6682
6683/**
David Ahern1a3f0602016-10-17 19:15:44 -07006684 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6685 * @dev: device
6686 * @upper_dev: upper device to check
6687 *
6688 * Find out if a device is linked to specified upper device and return true
6689 * in case it is. Note that this checks the entire upper device chain.
6690 * The caller must hold rcu lock.
6691 */
6692
David Ahern1a3f0602016-10-17 19:15:44 -07006693bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6694 struct net_device *upper_dev)
6695{
Taehee Yoo32b6d342019-10-21 18:47:56 +00006696 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
David Ahern1a3f0602016-10-17 19:15:44 -07006697 upper_dev);
6698}
6699EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6700
6701/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006702 * netdev_has_any_upper_dev - Check if device is linked to some device
6703 * @dev: device
6704 *
6705 * Find out if a device is linked to an upper device and return true in case
6706 * it is. The caller must hold the RTNL lock.
6707 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006708bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006709{
6710 ASSERT_RTNL();
6711
David Ahernf1170fd2016-10-17 19:15:51 -07006712 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006713}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006714EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006715
6716/**
6717 * netdev_master_upper_dev_get - Get master upper device
6718 * @dev: device
6719 *
6720 * Find a master upper device and return pointer to it or NULL in case
6721 * it's not there. The caller must hold the RTNL lock.
6722 */
6723struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6724{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006725 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006726
6727 ASSERT_RTNL();
6728
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006729 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006730 return NULL;
6731
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006732 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006733 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006734 if (likely(upper->master))
6735 return upper->dev;
6736 return NULL;
6737}
6738EXPORT_SYMBOL(netdev_master_upper_dev_get);
6739
Taehee Yoo32b6d342019-10-21 18:47:56 +00006740static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6741{
6742 struct netdev_adjacent *upper;
6743
6744 ASSERT_RTNL();
6745
6746 if (list_empty(&dev->adj_list.upper))
6747 return NULL;
6748
6749 upper = list_first_entry(&dev->adj_list.upper,
6750 struct netdev_adjacent, list);
6751 if (likely(upper->master) && !upper->ignore)
6752 return upper->dev;
6753 return NULL;
6754}
6755
David Ahern0f524a82016-10-17 19:15:52 -07006756/**
6757 * netdev_has_any_lower_dev - Check if device is linked to some device
6758 * @dev: device
6759 *
6760 * Find out if a device is linked to a lower device and return true in case
6761 * it is. The caller must hold the RTNL lock.
6762 */
6763static bool netdev_has_any_lower_dev(struct net_device *dev)
6764{
6765 ASSERT_RTNL();
6766
6767 return !list_empty(&dev->adj_list.lower);
6768}
6769
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02006770void *netdev_adjacent_get_private(struct list_head *adj_list)
6771{
6772 struct netdev_adjacent *adj;
6773
6774 adj = list_entry(adj_list, struct netdev_adjacent, list);
6775
6776 return adj->private;
6777}
6778EXPORT_SYMBOL(netdev_adjacent_get_private);
6779
Veaceslav Falico31088a12013-09-25 09:20:12 +02006780/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04006781 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6782 * @dev: device
6783 * @iter: list_head ** of the current position
6784 *
6785 * Gets the next device from the dev's upper list, starting from iter
6786 * position. The caller must hold RCU read lock.
6787 */
6788struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6789 struct list_head **iter)
6790{
6791 struct netdev_adjacent *upper;
6792
6793 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6794
6795 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6796
6797 if (&upper->list == &dev->adj_list.upper)
6798 return NULL;
6799
6800 *iter = &upper->list;
6801
6802 return upper->dev;
6803}
6804EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6805
Taehee Yoo32b6d342019-10-21 18:47:56 +00006806static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6807 struct list_head **iter,
6808 bool *ignore)
Taehee Yoo5343da42019-10-21 18:47:50 +00006809{
6810 struct netdev_adjacent *upper;
6811
6812 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6813
6814 if (&upper->list == &dev->adj_list.upper)
6815 return NULL;
6816
6817 *iter = &upper->list;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006818 *ignore = upper->ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006819
6820 return upper->dev;
6821}
6822
David Ahern1a3f0602016-10-17 19:15:44 -07006823static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6824 struct list_head **iter)
6825{
6826 struct netdev_adjacent *upper;
6827
6828 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6829
6830 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6831
6832 if (&upper->list == &dev->adj_list.upper)
6833 return NULL;
6834
6835 *iter = &upper->list;
6836
6837 return upper->dev;
6838}
6839
Taehee Yoo32b6d342019-10-21 18:47:56 +00006840static int __netdev_walk_all_upper_dev(struct net_device *dev,
6841 int (*fn)(struct net_device *dev,
6842 void *data),
6843 void *data)
Taehee Yoo5343da42019-10-21 18:47:50 +00006844{
6845 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6846 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6847 int ret, cur = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006848 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00006849
6850 now = dev;
6851 iter = &dev->adj_list.upper;
6852
6853 while (1) {
6854 if (now != dev) {
6855 ret = fn(now, data);
6856 if (ret)
6857 return ret;
6858 }
6859
6860 next = NULL;
6861 while (1) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00006862 udev = __netdev_next_upper_dev(now, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00006863 if (!udev)
6864 break;
Taehee Yoo32b6d342019-10-21 18:47:56 +00006865 if (ignore)
6866 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00006867
6868 next = udev;
6869 niter = &udev->adj_list.upper;
6870 dev_stack[cur] = now;
6871 iter_stack[cur++] = iter;
6872 break;
6873 }
6874
6875 if (!next) {
6876 if (!cur)
6877 return 0;
6878 next = dev_stack[--cur];
6879 niter = iter_stack[cur];
6880 }
6881
6882 now = next;
6883 iter = niter;
6884 }
6885
6886 return 0;
6887}
6888
David Ahern1a3f0602016-10-17 19:15:44 -07006889int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6890 int (*fn)(struct net_device *dev,
6891 void *data),
6892 void *data)
6893{
Taehee Yoo5343da42019-10-21 18:47:50 +00006894 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6895 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6896 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07006897
Taehee Yoo5343da42019-10-21 18:47:50 +00006898 now = dev;
6899 iter = &dev->adj_list.upper;
David Ahern1a3f0602016-10-17 19:15:44 -07006900
Taehee Yoo5343da42019-10-21 18:47:50 +00006901 while (1) {
6902 if (now != dev) {
6903 ret = fn(now, data);
6904 if (ret)
6905 return ret;
6906 }
6907
6908 next = NULL;
6909 while (1) {
6910 udev = netdev_next_upper_dev_rcu(now, &iter);
6911 if (!udev)
6912 break;
6913
6914 next = udev;
6915 niter = &udev->adj_list.upper;
6916 dev_stack[cur] = now;
6917 iter_stack[cur++] = iter;
6918 break;
6919 }
6920
6921 if (!next) {
6922 if (!cur)
6923 return 0;
6924 next = dev_stack[--cur];
6925 niter = iter_stack[cur];
6926 }
6927
6928 now = next;
6929 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07006930 }
6931
6932 return 0;
6933}
6934EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6935
Taehee Yoo32b6d342019-10-21 18:47:56 +00006936static bool __netdev_has_upper_dev(struct net_device *dev,
6937 struct net_device *upper_dev)
6938{
6939 ASSERT_RTNL();
6940
6941 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
6942 upper_dev);
6943}
6944
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006945/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02006946 * netdev_lower_get_next_private - Get the next ->private from the
6947 * lower neighbour list
6948 * @dev: device
6949 * @iter: list_head ** of the current position
6950 *
6951 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6952 * list, starting from iter position. The caller must hold either hold the
6953 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006954 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02006955 */
6956void *netdev_lower_get_next_private(struct net_device *dev,
6957 struct list_head **iter)
6958{
6959 struct netdev_adjacent *lower;
6960
6961 lower = list_entry(*iter, struct netdev_adjacent, list);
6962
6963 if (&lower->list == &dev->adj_list.lower)
6964 return NULL;
6965
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006966 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006967
6968 return lower->private;
6969}
6970EXPORT_SYMBOL(netdev_lower_get_next_private);
6971
6972/**
6973 * netdev_lower_get_next_private_rcu - Get the next ->private from the
6974 * lower neighbour list, RCU
6975 * variant
6976 * @dev: device
6977 * @iter: list_head ** of the current position
6978 *
6979 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6980 * list, starting from iter position. The caller must hold RCU read lock.
6981 */
6982void *netdev_lower_get_next_private_rcu(struct net_device *dev,
6983 struct list_head **iter)
6984{
6985 struct netdev_adjacent *lower;
6986
6987 WARN_ON_ONCE(!rcu_read_lock_held());
6988
6989 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6990
6991 if (&lower->list == &dev->adj_list.lower)
6992 return NULL;
6993
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006994 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006995
6996 return lower->private;
6997}
6998EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
6999
7000/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007001 * netdev_lower_get_next - Get the next device from the lower neighbour
7002 * list
7003 * @dev: device
7004 * @iter: list_head ** of the current position
7005 *
7006 * Gets the next netdev_adjacent from the dev's lower neighbour
7007 * list, starting from iter position. The caller must hold RTNL lock or
7008 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00007009 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007010 */
7011void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7012{
7013 struct netdev_adjacent *lower;
7014
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007015 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007016
7017 if (&lower->list == &dev->adj_list.lower)
7018 return NULL;
7019
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01007020 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007021
7022 return lower->dev;
7023}
7024EXPORT_SYMBOL(netdev_lower_get_next);
7025
David Ahern1a3f0602016-10-17 19:15:44 -07007026static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7027 struct list_head **iter)
7028{
7029 struct netdev_adjacent *lower;
7030
David Ahern46b5ab12016-10-26 13:21:33 -07007031 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07007032
7033 if (&lower->list == &dev->adj_list.lower)
7034 return NULL;
7035
David Ahern46b5ab12016-10-26 13:21:33 -07007036 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07007037
7038 return lower->dev;
7039}
7040
Taehee Yoo32b6d342019-10-21 18:47:56 +00007041static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7042 struct list_head **iter,
7043 bool *ignore)
7044{
7045 struct netdev_adjacent *lower;
7046
7047 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7048
7049 if (&lower->list == &dev->adj_list.lower)
7050 return NULL;
7051
7052 *iter = &lower->list;
7053 *ignore = lower->ignore;
7054
7055 return lower->dev;
7056}
7057
David Ahern1a3f0602016-10-17 19:15:44 -07007058int netdev_walk_all_lower_dev(struct net_device *dev,
7059 int (*fn)(struct net_device *dev,
7060 void *data),
7061 void *data)
7062{
Taehee Yoo5343da42019-10-21 18:47:50 +00007063 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7064 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7065 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007066
Taehee Yoo5343da42019-10-21 18:47:50 +00007067 now = dev;
7068 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007069
Taehee Yoo5343da42019-10-21 18:47:50 +00007070 while (1) {
7071 if (now != dev) {
7072 ret = fn(now, data);
7073 if (ret)
7074 return ret;
7075 }
7076
7077 next = NULL;
7078 while (1) {
7079 ldev = netdev_next_lower_dev(now, &iter);
7080 if (!ldev)
7081 break;
7082
7083 next = ldev;
7084 niter = &ldev->adj_list.lower;
7085 dev_stack[cur] = now;
7086 iter_stack[cur++] = iter;
7087 break;
7088 }
7089
7090 if (!next) {
7091 if (!cur)
7092 return 0;
7093 next = dev_stack[--cur];
7094 niter = iter_stack[cur];
7095 }
7096
7097 now = next;
7098 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007099 }
7100
7101 return 0;
7102}
7103EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7104
Taehee Yoo32b6d342019-10-21 18:47:56 +00007105static int __netdev_walk_all_lower_dev(struct net_device *dev,
7106 int (*fn)(struct net_device *dev,
7107 void *data),
7108 void *data)
7109{
7110 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7111 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7112 int ret, cur = 0;
7113 bool ignore;
7114
7115 now = dev;
7116 iter = &dev->adj_list.lower;
7117
7118 while (1) {
7119 if (now != dev) {
7120 ret = fn(now, data);
7121 if (ret)
7122 return ret;
7123 }
7124
7125 next = NULL;
7126 while (1) {
7127 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7128 if (!ldev)
7129 break;
7130 if (ignore)
7131 continue;
7132
7133 next = ldev;
7134 niter = &ldev->adj_list.lower;
7135 dev_stack[cur] = now;
7136 iter_stack[cur++] = iter;
7137 break;
7138 }
7139
7140 if (!next) {
7141 if (!cur)
7142 return 0;
7143 next = dev_stack[--cur];
7144 niter = iter_stack[cur];
7145 }
7146
7147 now = next;
7148 iter = niter;
7149 }
7150
7151 return 0;
7152}
7153
David Ahern1a3f0602016-10-17 19:15:44 -07007154static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7155 struct list_head **iter)
7156{
7157 struct netdev_adjacent *lower;
7158
7159 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7160 if (&lower->list == &dev->adj_list.lower)
7161 return NULL;
7162
7163 *iter = &lower->list;
7164
7165 return lower->dev;
7166}
7167
Taehee Yoo5343da42019-10-21 18:47:50 +00007168static u8 __netdev_upper_depth(struct net_device *dev)
7169{
7170 struct net_device *udev;
7171 struct list_head *iter;
7172 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007173 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007174
7175 for (iter = &dev->adj_list.upper,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007176 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007177 udev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007178 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7179 if (ignore)
7180 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007181 if (max_depth < udev->upper_level)
7182 max_depth = udev->upper_level;
7183 }
7184
7185 return max_depth;
7186}
7187
7188static u8 __netdev_lower_depth(struct net_device *dev)
7189{
7190 struct net_device *ldev;
7191 struct list_head *iter;
7192 u8 max_depth = 0;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007193 bool ignore;
Taehee Yoo5343da42019-10-21 18:47:50 +00007194
7195 for (iter = &dev->adj_list.lower,
Taehee Yoo32b6d342019-10-21 18:47:56 +00007196 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
Taehee Yoo5343da42019-10-21 18:47:50 +00007197 ldev;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007198 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7199 if (ignore)
7200 continue;
Taehee Yoo5343da42019-10-21 18:47:50 +00007201 if (max_depth < ldev->lower_level)
7202 max_depth = ldev->lower_level;
7203 }
7204
7205 return max_depth;
7206}
7207
7208static int __netdev_update_upper_level(struct net_device *dev, void *data)
7209{
7210 dev->upper_level = __netdev_upper_depth(dev) + 1;
7211 return 0;
7212}
7213
7214static int __netdev_update_lower_level(struct net_device *dev, void *data)
7215{
7216 dev->lower_level = __netdev_lower_depth(dev) + 1;
7217 return 0;
7218}
7219
David Ahern1a3f0602016-10-17 19:15:44 -07007220int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7221 int (*fn)(struct net_device *dev,
7222 void *data),
7223 void *data)
7224{
Taehee Yoo5343da42019-10-21 18:47:50 +00007225 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7226 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7227 int ret, cur = 0;
David Ahern1a3f0602016-10-17 19:15:44 -07007228
Taehee Yoo5343da42019-10-21 18:47:50 +00007229 now = dev;
7230 iter = &dev->adj_list.lower;
David Ahern1a3f0602016-10-17 19:15:44 -07007231
Taehee Yoo5343da42019-10-21 18:47:50 +00007232 while (1) {
7233 if (now != dev) {
7234 ret = fn(now, data);
7235 if (ret)
7236 return ret;
7237 }
7238
7239 next = NULL;
7240 while (1) {
7241 ldev = netdev_next_lower_dev_rcu(now, &iter);
7242 if (!ldev)
7243 break;
7244
7245 next = ldev;
7246 niter = &ldev->adj_list.lower;
7247 dev_stack[cur] = now;
7248 iter_stack[cur++] = iter;
7249 break;
7250 }
7251
7252 if (!next) {
7253 if (!cur)
7254 return 0;
7255 next = dev_stack[--cur];
7256 niter = iter_stack[cur];
7257 }
7258
7259 now = next;
7260 iter = niter;
David Ahern1a3f0602016-10-17 19:15:44 -07007261 }
7262
7263 return 0;
7264}
7265EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7266
Jiri Pirko7ce856a2016-07-04 08:23:12 +02007267/**
dingtianhonge001bfa2013-12-13 10:19:55 +08007268 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7269 * lower neighbour list, RCU
7270 * variant
7271 * @dev: device
7272 *
7273 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7274 * list. The caller must hold RCU read lock.
7275 */
7276void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7277{
7278 struct netdev_adjacent *lower;
7279
7280 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7281 struct netdev_adjacent, list);
7282 if (lower)
7283 return lower->private;
7284 return NULL;
7285}
7286EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7287
7288/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007289 * netdev_master_upper_dev_get_rcu - Get master upper device
7290 * @dev: device
7291 *
7292 * Find a master upper device and return pointer to it or NULL in case
7293 * it's not there. The caller must hold the RCU read lock.
7294 */
7295struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7296{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007297 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007298
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007299 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02007300 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007301 if (upper && likely(upper->master))
7302 return upper->dev;
7303 return NULL;
7304}
7305EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7306
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307307static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007308 struct net_device *adj_dev,
7309 struct list_head *dev_list)
7310{
7311 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007312
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007313 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7314 "upper_%s" : "lower_%s", adj_dev->name);
7315 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7316 linkname);
7317}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05307318static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007319 char *name,
7320 struct list_head *dev_list)
7321{
7322 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11007323
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007324 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7325 "upper_%s" : "lower_%s", name);
7326 sysfs_remove_link(&(dev->dev.kobj), linkname);
7327}
7328
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007329static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7330 struct net_device *adj_dev,
7331 struct list_head *dev_list)
7332{
7333 return (dev_list == &dev->adj_list.upper ||
7334 dev_list == &dev->adj_list.lower) &&
7335 net_eq(dev_net(dev), dev_net(adj_dev));
7336}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007337
Veaceslav Falico5d261912013-08-28 23:25:05 +02007338static int __netdev_adjacent_dev_insert(struct net_device *dev,
7339 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02007340 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007341 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007342{
7343 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007344 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007345
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007346 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007347
7348 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07007349 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07007350 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7351 dev->name, adj_dev->name, adj->ref_nr);
7352
Veaceslav Falico5d261912013-08-28 23:25:05 +02007353 return 0;
7354 }
7355
7356 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7357 if (!adj)
7358 return -ENOMEM;
7359
7360 adj->dev = adj_dev;
7361 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07007362 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007363 adj->private = private;
Taehee Yoo32b6d342019-10-21 18:47:56 +00007364 adj->ignore = false;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007365 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007366
David Ahern67b62f92016-10-17 19:15:53 -07007367 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7368 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007369
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007370 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007371 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007372 if (ret)
7373 goto free_adj;
7374 }
7375
Veaceslav Falico7863c052013-09-25 09:20:06 +02007376 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007377 if (master) {
7378 ret = sysfs_create_link(&(dev->dev.kobj),
7379 &(adj_dev->dev.kobj), "master");
7380 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02007381 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007382
Veaceslav Falico7863c052013-09-25 09:20:06 +02007383 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007384 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02007385 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007386 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007387
7388 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007389
Veaceslav Falico5831d662013-09-25 09:20:32 +02007390remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007391 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007392 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007393free_adj:
7394 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02007395 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007396
7397 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007398}
7399
stephen hemminger1d143d92013-12-29 14:01:29 -08007400static void __netdev_adjacent_dev_remove(struct net_device *dev,
7401 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007402 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007403 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007404{
7405 struct netdev_adjacent *adj;
7406
David Ahern67b62f92016-10-17 19:15:53 -07007407 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7408 dev->name, adj_dev->name, ref_nr);
7409
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007410 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007411
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007412 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07007413 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007414 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07007415 WARN_ON(1);
7416 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007417 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02007418
Andrew Collins93409032016-10-03 13:43:02 -06007419 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07007420 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7421 dev->name, adj_dev->name, ref_nr,
7422 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06007423 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007424 return;
7425 }
7426
Veaceslav Falico842d67a2013-09-25 09:20:31 +02007427 if (adj->master)
7428 sysfs_remove_link(&(dev->dev.kobj), "master");
7429
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04007430 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01007431 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02007432
Veaceslav Falico5d261912013-08-28 23:25:05 +02007433 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07007434 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007435 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007436 dev_put(adj_dev);
7437 kfree_rcu(adj, rcu);
7438}
7439
stephen hemminger1d143d92013-12-29 14:01:29 -08007440static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7441 struct net_device *upper_dev,
7442 struct list_head *up_list,
7443 struct list_head *down_list,
7444 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007445{
7446 int ret;
7447
David Ahern790510d2016-10-17 19:15:43 -07007448 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06007449 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007450 if (ret)
7451 return ret;
7452
David Ahern790510d2016-10-17 19:15:43 -07007453 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06007454 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007455 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07007456 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007457 return ret;
7458 }
7459
7460 return 0;
7461}
7462
stephen hemminger1d143d92013-12-29 14:01:29 -08007463static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7464 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007465 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007466 struct list_head *up_list,
7467 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007468{
Andrew Collins93409032016-10-03 13:43:02 -06007469 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7470 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007471}
7472
stephen hemminger1d143d92013-12-29 14:01:29 -08007473static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7474 struct net_device *upper_dev,
7475 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007476{
David Ahernf1170fd2016-10-17 19:15:51 -07007477 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7478 &dev->adj_list.upper,
7479 &upper_dev->adj_list.lower,
7480 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007481}
7482
stephen hemminger1d143d92013-12-29 14:01:29 -08007483static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7484 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007485{
Andrew Collins93409032016-10-03 13:43:02 -06007486 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007487 &dev->adj_list.upper,
7488 &upper_dev->adj_list.lower);
7489}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007490
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007491static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007492 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007493 void *upper_priv, void *upper_info,
7494 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007495{
David Ahern51d0c0472017-10-04 17:48:45 -07007496 struct netdev_notifier_changeupper_info changeupper_info = {
7497 .info = {
7498 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007499 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007500 },
7501 .upper_dev = upper_dev,
7502 .master = master,
7503 .linking = true,
7504 .upper_info = upper_info,
7505 };
Mike Manning50d629e2018-02-26 23:49:30 +00007506 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007507 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007508
7509 ASSERT_RTNL();
7510
7511 if (dev == upper_dev)
7512 return -EBUSY;
7513
7514 /* To prevent loops, check if dev is not upper device to upper_dev. */
Taehee Yoo32b6d342019-10-21 18:47:56 +00007515 if (__netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007516 return -EBUSY;
7517
Taehee Yoo5343da42019-10-21 18:47:50 +00007518 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7519 return -EMLINK;
7520
Mike Manning50d629e2018-02-26 23:49:30 +00007521 if (!master) {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007522 if (__netdev_has_upper_dev(dev, upper_dev))
Mike Manning50d629e2018-02-26 23:49:30 +00007523 return -EEXIST;
7524 } else {
Taehee Yoo32b6d342019-10-21 18:47:56 +00007525 master_dev = __netdev_master_upper_dev_get(dev);
Mike Manning50d629e2018-02-26 23:49:30 +00007526 if (master_dev)
7527 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7528 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007529
David Ahern51d0c0472017-10-04 17:48:45 -07007530 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007531 &changeupper_info.info);
7532 ret = notifier_to_errno(ret);
7533 if (ret)
7534 return ret;
7535
Jiri Pirko6dffb042015-12-03 12:12:10 +01007536 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007537 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007538 if (ret)
7539 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007540
David Ahern51d0c0472017-10-04 17:48:45 -07007541 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007542 &changeupper_info.info);
7543 ret = notifier_to_errno(ret);
7544 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007545 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007546
Taehee Yoo5343da42019-10-21 18:47:50 +00007547 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007548 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007549
7550 __netdev_update_lower_level(upper_dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007551 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7552 NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007553
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007554 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007555
David Ahernf1170fd2016-10-17 19:15:51 -07007556rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007557 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007558
7559 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007560}
7561
7562/**
7563 * netdev_upper_dev_link - Add a link to the upper device
7564 * @dev: device
7565 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007566 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007567 *
7568 * Adds a link to device which is upper to this one. The caller must hold
7569 * the RTNL lock. On a failure a negative errno code is returned.
7570 * On success the reference counts are adjusted and the function
7571 * returns zero.
7572 */
7573int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007574 struct net_device *upper_dev,
7575 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007576{
David Ahern42ab19e2017-10-04 17:48:47 -07007577 return __netdev_upper_dev_link(dev, upper_dev, false,
7578 NULL, NULL, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007579}
7580EXPORT_SYMBOL(netdev_upper_dev_link);
7581
7582/**
7583 * netdev_master_upper_dev_link - Add a master link to the upper device
7584 * @dev: device
7585 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007586 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007587 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007588 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007589 *
7590 * Adds a link to device which is upper to this one. In this case, only
7591 * one master upper device can be linked, although other non-master devices
7592 * might be linked as well. The caller must hold the RTNL lock.
7593 * On a failure a negative errno code is returned. On success the reference
7594 * counts are adjusted and the function returns zero.
7595 */
7596int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007597 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007598 void *upper_priv, void *upper_info,
7599 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007600{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007601 return __netdev_upper_dev_link(dev, upper_dev, true,
David Ahern42ab19e2017-10-04 17:48:47 -07007602 upper_priv, upper_info, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007603}
7604EXPORT_SYMBOL(netdev_master_upper_dev_link);
7605
7606/**
7607 * netdev_upper_dev_unlink - Removes a link to upper device
7608 * @dev: device
7609 * @upper_dev: new upper device
7610 *
7611 * Removes a link to device which is upper to this one. The caller must hold
7612 * the RTNL lock.
7613 */
7614void netdev_upper_dev_unlink(struct net_device *dev,
7615 struct net_device *upper_dev)
7616{
David Ahern51d0c0472017-10-04 17:48:45 -07007617 struct netdev_notifier_changeupper_info changeupper_info = {
7618 .info = {
7619 .dev = dev,
7620 },
7621 .upper_dev = upper_dev,
7622 .linking = false,
7623 };
tchardingf4563a72017-02-09 17:56:07 +11007624
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007625 ASSERT_RTNL();
7626
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007627 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007628
David Ahern51d0c0472017-10-04 17:48:45 -07007629 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007630 &changeupper_info.info);
7631
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007632 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007633
David Ahern51d0c0472017-10-04 17:48:45 -07007634 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007635 &changeupper_info.info);
Taehee Yoo5343da42019-10-21 18:47:50 +00007636
7637 __netdev_update_upper_level(dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007638 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
Taehee Yoo5343da42019-10-21 18:47:50 +00007639
7640 __netdev_update_lower_level(upper_dev, NULL);
Taehee Yoo32b6d342019-10-21 18:47:56 +00007641 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7642 NULL);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007643}
7644EXPORT_SYMBOL(netdev_upper_dev_unlink);
7645
Taehee Yoo32b6d342019-10-21 18:47:56 +00007646static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7647 struct net_device *lower_dev,
7648 bool val)
7649{
7650 struct netdev_adjacent *adj;
7651
7652 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7653 if (adj)
7654 adj->ignore = val;
7655
7656 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7657 if (adj)
7658 adj->ignore = val;
7659}
7660
7661static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7662 struct net_device *lower_dev)
7663{
7664 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7665}
7666
7667static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7668 struct net_device *lower_dev)
7669{
7670 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7671}
7672
7673int netdev_adjacent_change_prepare(struct net_device *old_dev,
7674 struct net_device *new_dev,
7675 struct net_device *dev,
7676 struct netlink_ext_ack *extack)
7677{
7678 int err;
7679
7680 if (!new_dev)
7681 return 0;
7682
7683 if (old_dev && new_dev != old_dev)
7684 netdev_adjacent_dev_disable(dev, old_dev);
7685
7686 err = netdev_upper_dev_link(new_dev, dev, extack);
7687 if (err) {
7688 if (old_dev && new_dev != old_dev)
7689 netdev_adjacent_dev_enable(dev, old_dev);
7690 return err;
7691 }
7692
7693 return 0;
7694}
7695EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7696
7697void netdev_adjacent_change_commit(struct net_device *old_dev,
7698 struct net_device *new_dev,
7699 struct net_device *dev)
7700{
7701 if (!new_dev || !old_dev)
7702 return;
7703
7704 if (new_dev == old_dev)
7705 return;
7706
7707 netdev_adjacent_dev_enable(dev, old_dev);
7708 netdev_upper_dev_unlink(old_dev, dev);
7709}
7710EXPORT_SYMBOL(netdev_adjacent_change_commit);
7711
7712void netdev_adjacent_change_abort(struct net_device *old_dev,
7713 struct net_device *new_dev,
7714 struct net_device *dev)
7715{
7716 if (!new_dev)
7717 return;
7718
7719 if (old_dev && new_dev != old_dev)
7720 netdev_adjacent_dev_enable(dev, old_dev);
7721
7722 netdev_upper_dev_unlink(new_dev, dev);
7723}
7724EXPORT_SYMBOL(netdev_adjacent_change_abort);
7725
Moni Shoua61bd3852015-02-03 16:48:29 +02007726/**
7727 * netdev_bonding_info_change - Dispatch event about slave change
7728 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09007729 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02007730 *
7731 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7732 * The caller must hold the RTNL lock.
7733 */
7734void netdev_bonding_info_change(struct net_device *dev,
7735 struct netdev_bonding_info *bonding_info)
7736{
David Ahern51d0c0472017-10-04 17:48:45 -07007737 struct netdev_notifier_bonding_info info = {
7738 .info.dev = dev,
7739 };
Moni Shoua61bd3852015-02-03 16:48:29 +02007740
7741 memcpy(&info.bonding_info, bonding_info,
7742 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07007743 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02007744 &info.info);
7745}
7746EXPORT_SYMBOL(netdev_bonding_info_change);
7747
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007748static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007749{
7750 struct netdev_adjacent *iter;
7751
7752 struct net *net = dev_net(dev);
7753
7754 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007755 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007756 continue;
7757 netdev_adjacent_sysfs_add(iter->dev, dev,
7758 &iter->dev->adj_list.lower);
7759 netdev_adjacent_sysfs_add(dev, iter->dev,
7760 &dev->adj_list.upper);
7761 }
7762
7763 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007764 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007765 continue;
7766 netdev_adjacent_sysfs_add(iter->dev, dev,
7767 &iter->dev->adj_list.upper);
7768 netdev_adjacent_sysfs_add(dev, iter->dev,
7769 &dev->adj_list.lower);
7770 }
7771}
7772
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007773static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007774{
7775 struct netdev_adjacent *iter;
7776
7777 struct net *net = dev_net(dev);
7778
7779 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007780 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007781 continue;
7782 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7783 &iter->dev->adj_list.lower);
7784 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7785 &dev->adj_list.upper);
7786 }
7787
7788 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007789 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007790 continue;
7791 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7792 &iter->dev->adj_list.upper);
7793 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7794 &dev->adj_list.lower);
7795 }
7796}
7797
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007798void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02007799{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007800 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007801
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007802 struct net *net = dev_net(dev);
7803
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007804 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007805 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007806 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007807 netdev_adjacent_sysfs_del(iter->dev, oldname,
7808 &iter->dev->adj_list.lower);
7809 netdev_adjacent_sysfs_add(iter->dev, dev,
7810 &iter->dev->adj_list.lower);
7811 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007812
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007813 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007814 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007815 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007816 netdev_adjacent_sysfs_del(iter->dev, oldname,
7817 &iter->dev->adj_list.upper);
7818 netdev_adjacent_sysfs_add(iter->dev, dev,
7819 &iter->dev->adj_list.upper);
7820 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007821}
Veaceslav Falico402dae92013-09-25 09:20:09 +02007822
7823void *netdev_lower_dev_get_private(struct net_device *dev,
7824 struct net_device *lower_dev)
7825{
7826 struct netdev_adjacent *lower;
7827
7828 if (!lower_dev)
7829 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007830 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02007831 if (!lower)
7832 return NULL;
7833
7834 return lower->private;
7835}
7836EXPORT_SYMBOL(netdev_lower_dev_get_private);
7837
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007838
Jiri Pirko04d48262015-12-03 12:12:15 +01007839/**
7840 * netdev_lower_change - Dispatch event about lower device state change
7841 * @lower_dev: device
7842 * @lower_state_info: state to dispatch
7843 *
7844 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7845 * The caller must hold the RTNL lock.
7846 */
7847void netdev_lower_state_changed(struct net_device *lower_dev,
7848 void *lower_state_info)
7849{
David Ahern51d0c0472017-10-04 17:48:45 -07007850 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7851 .info.dev = lower_dev,
7852 };
Jiri Pirko04d48262015-12-03 12:12:15 +01007853
7854 ASSERT_RTNL();
7855 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07007856 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01007857 &changelowerstate_info.info);
7858}
7859EXPORT_SYMBOL(netdev_lower_state_changed);
7860
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007861static void dev_change_rx_flags(struct net_device *dev, int flags)
7862{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007863 const struct net_device_ops *ops = dev->netdev_ops;
7864
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05007865 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007866 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007867}
7868
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007869static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07007870{
Eric Dumazetb536db92011-11-30 21:42:26 +00007871 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06007872 kuid_t uid;
7873 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07007874
Patrick McHardy24023452007-07-14 18:51:31 -07007875 ASSERT_RTNL();
7876
Wang Chendad9b332008-06-18 01:48:28 -07007877 dev->flags |= IFF_PROMISC;
7878 dev->promiscuity += inc;
7879 if (dev->promiscuity == 0) {
7880 /*
7881 * Avoid overflow.
7882 * If inc causes overflow, untouch promisc and return error.
7883 */
7884 if (inc < 0)
7885 dev->flags &= ~IFF_PROMISC;
7886 else {
7887 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007888 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7889 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007890 return -EOVERFLOW;
7891 }
7892 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007893 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007894 pr_info("device %s %s promiscuous mode\n",
7895 dev->name,
7896 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11007897 if (audit_enabled) {
7898 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04007899 audit_log(audit_context(), GFP_ATOMIC,
7900 AUDIT_ANOM_PROMISCUOUS,
7901 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7902 dev->name, (dev->flags & IFF_PROMISC),
7903 (old_flags & IFF_PROMISC),
7904 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7905 from_kuid(&init_user_ns, uid),
7906 from_kgid(&init_user_ns, gid),
7907 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11007908 }
Patrick McHardy24023452007-07-14 18:51:31 -07007909
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007910 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07007911 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007912 if (notify)
7913 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07007914 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007915}
7916
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917/**
7918 * dev_set_promiscuity - update promiscuity count on a device
7919 * @dev: device
7920 * @inc: modifier
7921 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07007922 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 * remains above zero the interface remains promiscuous. Once it hits zero
7924 * the device reverts back to normal filtering operation. A negative inc
7925 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07007926 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 */
Wang Chendad9b332008-06-18 01:48:28 -07007928int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929{
Eric Dumazetb536db92011-11-30 21:42:26 +00007930 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07007931 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007933 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07007934 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07007935 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07007936 if (dev->flags != old_flags)
7937 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07007938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007940EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007942static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007944 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945
Patrick McHardy24023452007-07-14 18:51:31 -07007946 ASSERT_RTNL();
7947
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07007949 dev->allmulti += inc;
7950 if (dev->allmulti == 0) {
7951 /*
7952 * Avoid overflow.
7953 * If inc causes overflow, untouch allmulti and return error.
7954 */
7955 if (inc < 0)
7956 dev->flags &= ~IFF_ALLMULTI;
7957 else {
7958 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007959 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
7960 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007961 return -EOVERFLOW;
7962 }
7963 }
Patrick McHardy24023452007-07-14 18:51:31 -07007964 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007965 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07007966 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007967 if (notify)
7968 __dev_notify_flags(dev, old_flags,
7969 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07007970 }
Wang Chendad9b332008-06-18 01:48:28 -07007971 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007972}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007973
7974/**
7975 * dev_set_allmulti - update allmulti count on a device
7976 * @dev: device
7977 * @inc: modifier
7978 *
7979 * Add or remove reception of all multicast frames to a device. While the
7980 * count in the device remains above zero the interface remains listening
7981 * to all interfaces. Once it hits zero the device reverts back to normal
7982 * filtering operation. A negative @inc value is used to drop the counter
7983 * when releasing a resource needing all multicasts.
7984 * Return 0 if successful or a negative errno code on error.
7985 */
7986
7987int dev_set_allmulti(struct net_device *dev, int inc)
7988{
7989 return __dev_set_allmulti(dev, inc, true);
7990}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007991EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07007992
7993/*
7994 * Upload unicast and multicast address lists to device and
7995 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08007996 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07007997 * are present.
7998 */
7999void __dev_set_rx_mode(struct net_device *dev)
8000{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008001 const struct net_device_ops *ops = dev->netdev_ops;
8002
Patrick McHardy4417da62007-06-27 01:28:10 -07008003 /* dev_open will call this function so the list will stay sane. */
8004 if (!(dev->flags&IFF_UP))
8005 return;
8006
8007 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09008008 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07008009
Jiri Pirko01789342011-08-16 06:29:00 +00008010 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07008011 /* Unicast addresses changes may only happen under the rtnl,
8012 * therefore calling __dev_set_promiscuity here is safe.
8013 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008014 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008015 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008016 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08008017 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008018 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07008019 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07008020 }
Patrick McHardy4417da62007-06-27 01:28:10 -07008021 }
Jiri Pirko01789342011-08-16 06:29:00 +00008022
8023 if (ops->ndo_set_rx_mode)
8024 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008025}
8026
8027void dev_set_rx_mode(struct net_device *dev)
8028{
David S. Millerb9e40852008-07-15 00:15:08 -07008029 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07008030 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07008031 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032}
8033
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008034/**
8035 * dev_get_flags - get flags reported to userspace
8036 * @dev: device
8037 *
8038 * Get the combination of flag bits exported through APIs to userspace.
8039 */
Eric Dumazet95c96172012-04-15 05:58:06 +00008040unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041{
Eric Dumazet95c96172012-04-15 05:58:06 +00008042 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043
8044 flags = (dev->flags & ~(IFF_PROMISC |
8045 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08008046 IFF_RUNNING |
8047 IFF_LOWER_UP |
8048 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 (dev->gflags & (IFF_PROMISC |
8050 IFF_ALLMULTI));
8051
Stefan Rompfb00055a2006-03-20 17:09:11 -08008052 if (netif_running(dev)) {
8053 if (netif_oper_up(dev))
8054 flags |= IFF_RUNNING;
8055 if (netif_carrier_ok(dev))
8056 flags |= IFF_LOWER_UP;
8057 if (netif_dormant(dev))
8058 flags |= IFF_DORMANT;
8059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060
8061 return flags;
8062}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008063EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064
Petr Machata6d040322018-12-06 17:05:43 +00008065int __dev_change_flags(struct net_device *dev, unsigned int flags,
8066 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067{
Eric Dumazetb536db92011-11-30 21:42:26 +00008068 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00008069 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070
Patrick McHardy24023452007-07-14 18:51:31 -07008071 ASSERT_RTNL();
8072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 /*
8074 * Set the flags on our device.
8075 */
8076
8077 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8078 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8079 IFF_AUTOMEDIA)) |
8080 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8081 IFF_ALLMULTI));
8082
8083 /*
8084 * Load in the correct multicast list now the flags have changed.
8085 */
8086
Patrick McHardyb6c40d62008-10-07 15:26:48 -07008087 if ((old_flags ^ flags) & IFF_MULTICAST)
8088 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07008089
Patrick McHardy4417da62007-06-27 01:28:10 -07008090 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091
8092 /*
8093 * Have we downed the interface. We handle IFF_UP ourselves
8094 * according to user attempts to set it, rather than blindly
8095 * setting it.
8096 */
8097
8098 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07008099 if ((old_flags ^ flags) & IFF_UP) {
8100 if (old_flags & IFF_UP)
8101 __dev_close(dev);
8102 else
Petr Machata40c900a2018-12-06 17:05:47 +00008103 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07008104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008107 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008108 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008109
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008111
8112 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8113 if (dev->flags != old_flags)
8114 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115 }
8116
8117 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11008118 * is important. Some (broken) drivers set IFF_PROMISC, when
8119 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 */
8121 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008122 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8123
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008125 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 }
8127
Patrick McHardybd380812010-02-26 06:34:53 +00008128 return ret;
8129}
8130
Nicolas Dichtela528c212013-09-25 12:02:44 +02008131void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8132 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00008133{
8134 unsigned int changes = dev->flags ^ old_flags;
8135
Nicolas Dichtela528c212013-09-25 12:02:44 +02008136 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008137 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008138
Patrick McHardybd380812010-02-26 06:34:53 +00008139 if (changes & IFF_UP) {
8140 if (dev->flags & IFF_UP)
8141 call_netdevice_notifiers(NETDEV_UP, dev);
8142 else
8143 call_netdevice_notifiers(NETDEV_DOWN, dev);
8144 }
8145
8146 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008147 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07008148 struct netdev_notifier_change_info change_info = {
8149 .info = {
8150 .dev = dev,
8151 },
8152 .flags_changed = changes,
8153 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008154
David Ahern51d0c0472017-10-04 17:48:45 -07008155 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00008156 }
Patrick McHardybd380812010-02-26 06:34:53 +00008157}
8158
8159/**
8160 * dev_change_flags - change device settings
8161 * @dev: device
8162 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00008163 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00008164 *
8165 * Change settings on device based state flags. The flags are
8166 * in the userspace exported format.
8167 */
Petr Machata567c5e12018-12-06 17:05:42 +00008168int dev_change_flags(struct net_device *dev, unsigned int flags,
8169 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00008170{
Eric Dumazetb536db92011-11-30 21:42:26 +00008171 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008172 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00008173
Petr Machata6d040322018-12-06 17:05:43 +00008174 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00008175 if (ret < 0)
8176 return ret;
8177
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02008178 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02008179 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 return ret;
8181}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008182EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183
WANG Congf51048c2017-07-06 15:01:57 -07008184int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008185{
8186 const struct net_device_ops *ops = dev->netdev_ops;
8187
8188 if (ops->ndo_change_mtu)
8189 return ops->ndo_change_mtu(dev, new_mtu);
8190
8191 dev->mtu = new_mtu;
8192 return 0;
8193}
WANG Congf51048c2017-07-06 15:01:57 -07008194EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008195
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008196/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008197 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008198 * @dev: device
8199 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008200 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008201 *
8202 * Change the maximum transfer size of the network device.
8203 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008204int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8205 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008207 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208
8209 if (new_mtu == dev->mtu)
8210 return 0;
8211
Jarod Wilson61e84622016-10-07 22:04:33 -04008212 /* MTU must be positive, and in range */
8213 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008214 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 return -EINVAL;
Jarod Wilson61e84622016-10-07 22:04:33 -04008216 }
8217
8218 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008219 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
Jarod Wilson61e84622016-10-07 22:04:33 -04008220 return -EINVAL;
8221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
8223 if (!netif_device_present(dev))
8224 return -ENODEV;
8225
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01008226 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8227 err = notifier_to_errno(err);
8228 if (err)
8229 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008230
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008231 orig_mtu = dev->mtu;
8232 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008234 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008235 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8236 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008237 err = notifier_to_errno(err);
8238 if (err) {
8239 /* setting mtu back and notifying everyone again,
8240 * so that they have a chance to revert changes.
8241 */
8242 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02008243 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8244 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01008245 }
8246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247 return err;
8248}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008249
8250int dev_set_mtu(struct net_device *dev, int new_mtu)
8251{
8252 struct netlink_ext_ack extack;
8253 int err;
8254
Li RongQinga6bcfc82018-08-03 15:45:21 +08008255 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008256 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08008257 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07008258 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8259 return err;
8260}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008261EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008263/**
Cong Wang6a643dd2018-01-25 18:26:22 -08008264 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8265 * @dev: device
8266 * @new_len: new tx queue length
8267 */
8268int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8269{
8270 unsigned int orig_len = dev->tx_queue_len;
8271 int res;
8272
8273 if (new_len != (unsigned int)new_len)
8274 return -ERANGE;
8275
8276 if (new_len != orig_len) {
8277 dev->tx_queue_len = new_len;
8278 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8279 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03008280 if (res)
8281 goto err_rollback;
8282 res = dev_qdisc_change_tx_queue_len(dev);
8283 if (res)
8284 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08008285 }
8286
8287 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03008288
8289err_rollback:
8290 netdev_err(dev, "refused to change device tx_queue_len\n");
8291 dev->tx_queue_len = orig_len;
8292 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08008293}
8294
8295/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008296 * dev_set_group - Change group this device belongs to
8297 * @dev: device
8298 * @new_group: group this device should belong to
8299 */
8300void dev_set_group(struct net_device *dev, int new_group)
8301{
8302 dev->group = new_group;
8303}
8304EXPORT_SYMBOL(dev_set_group);
8305
8306/**
Petr Machatad59cdf92018-12-13 11:54:35 +00008307 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8308 * @dev: device
8309 * @addr: new address
8310 * @extack: netlink extended ack
8311 */
8312int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8313 struct netlink_ext_ack *extack)
8314{
8315 struct netdev_notifier_pre_changeaddr_info info = {
8316 .info.dev = dev,
8317 .info.extack = extack,
8318 .dev_addr = addr,
8319 };
8320 int rc;
8321
8322 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8323 return notifier_to_errno(rc);
8324}
8325EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8326
8327/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008328 * dev_set_mac_address - Change Media Access Control Address
8329 * @dev: device
8330 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00008331 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008332 *
8333 * Change the hardware (MAC) address of the device
8334 */
Petr Machata3a37a962018-12-13 11:54:30 +00008335int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8336 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008338 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 int err;
8340
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008341 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342 return -EOPNOTSUPP;
8343 if (sa->sa_family != dev->type)
8344 return -EINVAL;
8345 if (!netif_device_present(dev))
8346 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00008347 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8348 if (err)
8349 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008350 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00008351 if (err)
8352 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00008353 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00008354 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008355 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00008356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008358EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008360/**
8361 * dev_change_carrier - Change device carrier
8362 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00008363 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00008364 *
8365 * Change device carrier
8366 */
8367int dev_change_carrier(struct net_device *dev, bool new_carrier)
8368{
8369 const struct net_device_ops *ops = dev->netdev_ops;
8370
8371 if (!ops->ndo_change_carrier)
8372 return -EOPNOTSUPP;
8373 if (!netif_device_present(dev))
8374 return -ENODEV;
8375 return ops->ndo_change_carrier(dev, new_carrier);
8376}
8377EXPORT_SYMBOL(dev_change_carrier);
8378
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02008380 * dev_get_phys_port_id - Get device physical port ID
8381 * @dev: device
8382 * @ppid: port ID
8383 *
8384 * Get device physical port ID
8385 */
8386int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008387 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02008388{
8389 const struct net_device_ops *ops = dev->netdev_ops;
8390
8391 if (!ops->ndo_get_phys_port_id)
8392 return -EOPNOTSUPP;
8393 return ops->ndo_get_phys_port_id(dev, ppid);
8394}
8395EXPORT_SYMBOL(dev_get_phys_port_id);
8396
8397/**
David Aherndb24a902015-03-17 20:23:15 -06008398 * dev_get_phys_port_name - Get device physical port name
8399 * @dev: device
8400 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00008401 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06008402 *
8403 * Get device physical port name
8404 */
8405int dev_get_phys_port_name(struct net_device *dev,
8406 char *name, size_t len)
8407{
8408 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008409 int err;
David Aherndb24a902015-03-17 20:23:15 -06008410
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008411 if (ops->ndo_get_phys_port_name) {
8412 err = ops->ndo_get_phys_port_name(dev, name, len);
8413 if (err != -EOPNOTSUPP)
8414 return err;
8415 }
8416 return devlink_compat_phys_port_name_get(dev, name, len);
David Aherndb24a902015-03-17 20:23:15 -06008417}
8418EXPORT_SYMBOL(dev_get_phys_port_name);
8419
8420/**
Florian Fainellid6abc5962019-02-06 09:45:35 -08008421 * dev_get_port_parent_id - Get the device's port parent identifier
8422 * @dev: network device
8423 * @ppid: pointer to a storage for the port's parent identifier
8424 * @recurse: allow/disallow recursion to lower devices
8425 *
8426 * Get the devices's port parent identifier
8427 */
8428int dev_get_port_parent_id(struct net_device *dev,
8429 struct netdev_phys_item_id *ppid,
8430 bool recurse)
8431{
8432 const struct net_device_ops *ops = dev->netdev_ops;
8433 struct netdev_phys_item_id first = { };
8434 struct net_device *lower_dev;
8435 struct list_head *iter;
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008436 int err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008437
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008438 if (ops->ndo_get_port_parent_id) {
8439 err = ops->ndo_get_port_parent_id(dev, ppid);
8440 if (err != -EOPNOTSUPP)
8441 return err;
8442 }
8443
8444 err = devlink_compat_switch_id_get(dev, ppid);
8445 if (!err || err != -EOPNOTSUPP)
8446 return err;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008447
8448 if (!recurse)
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008449 return -EOPNOTSUPP;
Florian Fainellid6abc5962019-02-06 09:45:35 -08008450
8451 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8452 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8453 if (err)
8454 break;
8455 if (!first.id_len)
8456 first = *ppid;
8457 else if (memcmp(&first, ppid, sizeof(*ppid)))
8458 return -ENODATA;
8459 }
8460
8461 return err;
8462}
8463EXPORT_SYMBOL(dev_get_port_parent_id);
8464
8465/**
8466 * netdev_port_same_parent_id - Indicate if two network devices have
8467 * the same port parent identifier
8468 * @a: first network device
8469 * @b: second network device
8470 */
8471bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8472{
8473 struct netdev_phys_item_id a_id = { };
8474 struct netdev_phys_item_id b_id = { };
8475
8476 if (dev_get_port_parent_id(a, &a_id, true) ||
8477 dev_get_port_parent_id(b, &b_id, true))
8478 return false;
8479
8480 return netdev_phys_item_id_same(&a_id, &b_id);
8481}
8482EXPORT_SYMBOL(netdev_port_same_parent_id);
8483
8484/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008485 * dev_change_proto_down - update protocol port state information
8486 * @dev: device
8487 * @proto_down: new value
8488 *
8489 * This info can be used by switch drivers to set the phys state of the
8490 * port.
8491 */
8492int dev_change_proto_down(struct net_device *dev, bool proto_down)
8493{
8494 const struct net_device_ops *ops = dev->netdev_ops;
8495
8496 if (!ops->ndo_change_proto_down)
8497 return -EOPNOTSUPP;
8498 if (!netif_device_present(dev))
8499 return -ENODEV;
8500 return ops->ndo_change_proto_down(dev, proto_down);
8501}
8502EXPORT_SYMBOL(dev_change_proto_down);
8503
Andy Roulinb5899672019-02-22 18:06:36 +00008504/**
8505 * dev_change_proto_down_generic - generic implementation for
8506 * ndo_change_proto_down that sets carrier according to
8507 * proto_down.
8508 *
8509 * @dev: device
8510 * @proto_down: new value
8511 */
8512int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8513{
8514 if (proto_down)
8515 netif_carrier_off(dev);
8516 else
8517 netif_carrier_on(dev);
8518 dev->proto_down = proto_down;
8519 return 0;
8520}
8521EXPORT_SYMBOL(dev_change_proto_down_generic);
8522
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008523u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
8524 enum bpf_netdev_command cmd)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008525{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008526 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008527
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008528 if (!bpf_op)
8529 return 0;
Martin KaFai Lau58038692017-06-15 17:29:09 -07008530
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008531 memset(&xdp, 0, sizeof(xdp));
8532 xdp.command = cmd;
8533
8534 /* Query must always succeed. */
8535 WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008536
Jakub Kicinski6b867582018-07-11 20:36:39 -07008537 return xdp.prog_id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008538}
8539
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008540static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07008541 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008542 struct bpf_prog *prog)
8543{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008544 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008545
8546 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07008547 if (flags & XDP_FLAGS_HW_MODE)
8548 xdp.command = XDP_SETUP_PROG_HW;
8549 else
8550 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008551 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07008552 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008553 xdp.prog = prog;
8554
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008555 return bpf_op(dev, &xdp);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008556}
8557
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008558static void dev_xdp_uninstall(struct net_device *dev)
8559{
8560 struct netdev_bpf xdp;
8561 bpf_op_t ndo_bpf;
8562
8563 /* Remove generic XDP */
8564 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
8565
8566 /* Remove from the driver */
8567 ndo_bpf = dev->netdev_ops->ndo_bpf;
8568 if (!ndo_bpf)
8569 return;
8570
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008571 memset(&xdp, 0, sizeof(xdp));
8572 xdp.command = XDP_QUERY_PROG;
8573 WARN_ON(ndo_bpf(dev, &xdp));
8574 if (xdp.prog_id)
8575 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8576 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008577
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008578 /* Remove HW offload */
8579 memset(&xdp, 0, sizeof(xdp));
8580 xdp.command = XDP_QUERY_PROG_HW;
8581 if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
8582 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8583 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008584}
8585
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07008586/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07008587 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
8588 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07008589 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07008590 * @fd: new program fd or negative value to clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01008591 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07008592 *
8593 * Set or clear a bpf program for a device
8594 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07008595int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
8596 int fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07008597{
8598 const struct net_device_ops *ops = dev->netdev_ops;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008599 enum bpf_netdev_command query;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008600 struct bpf_prog *prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008601 bpf_op_t bpf_op, bpf_chk;
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008602 bool offload;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008603 int err;
8604
Daniel Borkmann85de8572016-11-28 23:16:54 +01008605 ASSERT_RTNL();
8606
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008607 offload = flags & XDP_FLAGS_HW_MODE;
8608 query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07008609
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008610 bpf_op = bpf_chk = ops->ndo_bpf;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008611 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) {
8612 NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode");
Daniel Borkmann0489df92017-05-12 01:04:45 +02008613 return -EOPNOTSUPP;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008614 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008615 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
8616 bpf_op = generic_xdp_install;
8617 if (bpf_op == bpf_chk)
8618 bpf_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04008619
Brenden Blancoa7862b42016-07-19 12:16:48 -07008620 if (fd >= 0) {
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008621 u32 prog_id;
8622
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008623 if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) {
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008624 NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time");
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008625 return -EEXIST;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008626 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008627
8628 prog_id = __dev_xdp_query(dev, bpf_op, query);
8629 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) {
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008630 NL_SET_ERR_MSG(extack, "XDP program already attached");
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02008631 return -EBUSY;
Maciej Fijalkowski01dde202019-02-01 22:42:27 +01008632 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01008633
Jakub Kicinski288b3de2017-11-20 15:21:54 -08008634 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
8635 bpf_op == ops->ndo_bpf);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008636 if (IS_ERR(prog))
8637 return PTR_ERR(prog);
Jakub Kicinski441a3302017-11-20 15:21:55 -08008638
Jakub Kicinski9ee963d2019-02-05 20:03:21 -08008639 if (!offload && bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinski441a3302017-11-20 15:21:55 -08008640 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
8641 bpf_prog_put(prog);
8642 return -EINVAL;
8643 }
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008644
Jakub Kicinskiaefc3e72019-10-31 20:07:00 -07008645 /* prog->aux->id may be 0 for orphaned device-bound progs */
8646 if (prog->aux->id && prog->aux->id == prog_id) {
Maxim Mikityanskiyc14a9f62019-08-14 14:34:06 +00008647 bpf_prog_put(prog);
8648 return 0;
8649 }
8650 } else {
8651 if (!__dev_xdp_query(dev, bpf_op, query))
8652 return 0;
Brenden Blancoa7862b42016-07-19 12:16:48 -07008653 }
8654
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008655 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008656 if (err < 0 && prog)
8657 bpf_prog_put(prog);
8658
8659 return err;
8660}
Brenden Blancoa7862b42016-07-19 12:16:48 -07008661
8662/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07008664 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 *
8666 * Returns a suitable unique value for a new device interface
8667 * number. The caller must hold the rtnl semaphore or the
8668 * dev_base_lock to be sure it remains unique.
8669 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07008670static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008672 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11008673
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674 for (;;) {
8675 if (++ifindex <= 0)
8676 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008677 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008678 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679 }
8680}
8681
Linus Torvalds1da177e2005-04-16 15:20:36 -07008682/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08008683static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07008684DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685
Stephen Hemminger6f05f622007-03-08 20:46:03 -08008686static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008689 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690}
8691
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008692static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008693{
Krishna Kumare93737b2009-12-08 22:26:02 +00008694 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008695 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008696
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008697 BUG_ON(dev_boot_phase);
8698 ASSERT_RTNL();
8699
Krishna Kumare93737b2009-12-08 22:26:02 +00008700 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008701 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00008702 * for initialization unwind. Remove those
8703 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008704 */
8705 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008706 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
8707 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008708
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008709 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00008710 list_del(&dev->unreg_list);
8711 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008712 }
Eric Dumazet449f4542011-05-19 12:24:16 +00008713 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008714 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00008715 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008716
Octavian Purdila44345722010-12-13 12:44:07 +00008717 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008718 list_for_each_entry(dev, head, unreg_list)
8719 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04008720 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008721
Octavian Purdila44345722010-12-13 12:44:07 +00008722 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008723 /* And unlink it from device chain. */
8724 unlist_netdevice(dev);
8725
8726 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008727 }
Eric Dumazet41852492016-08-26 12:50:39 -07008728 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008729
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008730 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008731
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008732 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008733 struct sk_buff *skb = NULL;
8734
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008735 /* Shutdown queueing discipline. */
8736 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008737
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008738 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008739
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008740 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008741 * this device. They should clean all the things.
8742 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008743 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
8744
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008745 if (!dev->rtnl_link_ops ||
8746 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04008747 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01008748 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008749
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008750 /*
8751 * Flush the unicast and multicast chains
8752 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008753 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008754 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008755
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02008756 netdev_name_node_alt_flush(dev);
Jiri Pirkoff927412019-09-30 11:48:15 +02008757 netdev_name_node_free(dev->name_node);
8758
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008759 if (dev->netdev_ops->ndo_uninit)
8760 dev->netdev_ops->ndo_uninit(dev);
8761
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008762 if (skb)
8763 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07008764
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008765 /* Notifier chain MUST detach us all upper devices. */
8766 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07008767 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008768
8769 /* Remove entries from kobject tree */
8770 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00008771#ifdef CONFIG_XPS
8772 /* Remove XPS queueing entries */
8773 netif_reset_xps_queues_gt(dev, 0);
8774#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008775 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008776
Eric W. Biederman850a5452011-10-13 22:25:23 +00008777 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008778
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00008779 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008780 dev_put(dev);
8781}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008782
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008783static void rollback_registered(struct net_device *dev)
8784{
8785 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008786
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008787 list_add(&dev->unreg_list, &single);
8788 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00008789 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008790}
8791
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008792static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
8793 struct net_device *upper, netdev_features_t features)
8794{
8795 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8796 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008797 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008798
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01008799 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008800 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008801 if (!(upper->wanted_features & feature)
8802 && (features & feature)) {
8803 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
8804 &feature, upper->name);
8805 features &= ~feature;
8806 }
8807 }
8808
8809 return features;
8810}
8811
8812static void netdev_sync_lower_features(struct net_device *upper,
8813 struct net_device *lower, netdev_features_t features)
8814{
8815 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8816 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008817 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008818
Hauke Mehrtens3b89ea92019-02-15 17:58:54 +01008819 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008820 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008821 if (!(features & feature) && (lower->features & feature)) {
8822 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
8823 &feature, lower->name);
8824 lower->wanted_features &= ~feature;
8825 netdev_update_features(lower);
8826
8827 if (unlikely(lower->features & feature))
8828 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
8829 &feature, lower->name);
8830 }
8831 }
8832}
8833
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008834static netdev_features_t netdev_fix_features(struct net_device *dev,
8835 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07008836{
Michał Mirosław57422dc2011-01-22 12:14:12 +00008837 /* Fix illegal checksum combinations */
8838 if ((features & NETIF_F_HW_CSUM) &&
8839 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008840 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00008841 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
8842 }
8843
Herbert Xub63365a2008-10-23 01:11:29 -07008844 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008845 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008846 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008847 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07008848 }
8849
Pravin B Shelarec5f0612013-03-07 09:28:01 +00008850 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
8851 !(features & NETIF_F_IP_CSUM)) {
8852 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
8853 features &= ~NETIF_F_TSO;
8854 features &= ~NETIF_F_TSO_ECN;
8855 }
8856
8857 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
8858 !(features & NETIF_F_IPV6_CSUM)) {
8859 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
8860 features &= ~NETIF_F_TSO6;
8861 }
8862
Alexander Duyckb1dc4972016-05-02 09:38:24 -07008863 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8864 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
8865 features &= ~NETIF_F_TSO_MANGLEID;
8866
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00008867 /* TSO ECN requires that TSO is present as well. */
8868 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
8869 features &= ~NETIF_F_TSO_ECN;
8870
Michał Mirosław212b5732011-02-15 16:59:16 +00008871 /* Software GSO depends on SG. */
8872 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008873 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00008874 features &= ~NETIF_F_GSO;
8875 }
8876
Alexander Duyck802ab552016-04-10 21:45:03 -04008877 /* GSO partial features require GSO partial be set */
8878 if ((features & dev->gso_partial_features) &&
8879 !(features & NETIF_F_GSO_PARTIAL)) {
8880 netdev_dbg(dev,
8881 "Dropping partially supported GSO features since no GSO partial.\n");
8882 features &= ~dev->gso_partial_features;
8883 }
8884
Michael Chanfb1f5f72017-12-16 03:09:40 -05008885 if (!(features & NETIF_F_RXCSUM)) {
8886 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8887 * successfully merged by hardware must also have the
8888 * checksum verified by hardware. If the user does not
8889 * want to enable RXCSUM, logically, we should disable GRO_HW.
8890 */
8891 if (features & NETIF_F_GRO_HW) {
8892 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8893 features &= ~NETIF_F_GRO_HW;
8894 }
8895 }
8896
Gal Pressmande8d5ab2018-03-12 11:48:49 +02008897 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8898 if (features & NETIF_F_RXFCS) {
8899 if (features & NETIF_F_LRO) {
8900 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
8901 features &= ~NETIF_F_LRO;
8902 }
8903
8904 if (features & NETIF_F_GRO_HW) {
8905 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8906 features &= ~NETIF_F_GRO_HW;
8907 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02008908 }
8909
Herbert Xub63365a2008-10-23 01:11:29 -07008910 return features;
8911}
Herbert Xub63365a2008-10-23 01:11:29 -07008912
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008913int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00008914{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008915 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008916 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008917 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05008918 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00008919
Michał Mirosław87267482011-04-12 09:56:38 +00008920 ASSERT_RTNL();
8921
Michał Mirosław5455c692011-02-15 16:59:17 +00008922 features = netdev_get_wanted_features(dev);
8923
8924 if (dev->netdev_ops->ndo_fix_features)
8925 features = dev->netdev_ops->ndo_fix_features(dev, features);
8926
8927 /* driver might be less strict about feature dependencies */
8928 features = netdev_fix_features(dev, features);
8929
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008930 /* some features can't be enabled if they're off an an upper device */
8931 netdev_for_each_upper_dev_rcu(dev, upper, iter)
8932 features = netdev_sync_upper_features(dev, upper, features);
8933
Michał Mirosław5455c692011-02-15 16:59:17 +00008934 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05008935 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00008936
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008937 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
8938 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00008939
8940 if (dev->netdev_ops->ndo_set_features)
8941 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01008942 else
8943 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00008944
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008945 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00008946 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008947 "set_features() failed (%d); wanted %pNF, left %pNF\n",
8948 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01008949 /* return non-0 since some features might have changed and
8950 * it's better to fire a spurious notification than miss it
8951 */
8952 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008953 }
8954
Jarod Wilsone7868a82015-11-03 23:09:32 -05008955sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008956 /* some features must be disabled on lower devices when disabled
8957 * on an upper device (think: bonding master or bridge)
8958 */
8959 netdev_for_each_lower_dev(dev, lower, iter)
8960 netdev_sync_lower_features(dev, lower, features);
8961
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02008962 if (!err) {
8963 netdev_features_t diff = features ^ dev->features;
8964
8965 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
8966 /* udp_tunnel_{get,drop}_rx_info both need
8967 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
8968 * device, or they won't do anything.
8969 * Thus we need to update dev->features
8970 * *before* calling udp_tunnel_get_rx_info,
8971 * but *after* calling udp_tunnel_drop_rx_info.
8972 */
8973 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
8974 dev->features = features;
8975 udp_tunnel_get_rx_info(dev);
8976 } else {
8977 udp_tunnel_drop_rx_info(dev);
8978 }
8979 }
8980
Gal Pressman9daae9b2018-03-28 17:46:54 +03008981 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
8982 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
8983 dev->features = features;
8984 err |= vlan_get_rx_ctag_filter_info(dev);
8985 } else {
8986 vlan_drop_rx_ctag_filter_info(dev);
8987 }
8988 }
8989
8990 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
8991 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
8992 dev->features = features;
8993 err |= vlan_get_rx_stag_filter_info(dev);
8994 } else {
8995 vlan_drop_rx_stag_filter_info(dev);
8996 }
8997 }
8998
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008999 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02009000 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009001
Jarod Wilsone7868a82015-11-03 23:09:32 -05009002 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009003}
9004
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009005/**
9006 * netdev_update_features - recalculate device features
9007 * @dev: the device to check
9008 *
9009 * Recalculate dev->features set and send notifications if it
9010 * has changed. Should be called after driver or hardware dependent
9011 * conditions might have changed that influence the features.
9012 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009013void netdev_update_features(struct net_device *dev)
9014{
9015 if (__netdev_update_features(dev))
9016 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00009017}
9018EXPORT_SYMBOL(netdev_update_features);
9019
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00009021 * netdev_change_features - recalculate device features
9022 * @dev: the device to check
9023 *
9024 * Recalculate dev->features set and send notifications even
9025 * if they have not changed. Should be called instead of
9026 * netdev_update_features() if also dev->vlan_features might
9027 * have changed to allow the changes to be propagated to stacked
9028 * VLAN devices.
9029 */
9030void netdev_change_features(struct net_device *dev)
9031{
9032 __netdev_update_features(dev);
9033 netdev_features_change(dev);
9034}
9035EXPORT_SYMBOL(netdev_change_features);
9036
9037/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009038 * netif_stacked_transfer_operstate - transfer operstate
9039 * @rootdev: the root or lower level device to transfer state from
9040 * @dev: the device to transfer operstate to
9041 *
9042 * Transfer operational state from root to device. This is normally
9043 * called when a stacking relationship exists between the root
9044 * device and the device(a leaf device).
9045 */
9046void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9047 struct net_device *dev)
9048{
9049 if (rootdev->operstate == IF_OPER_DORMANT)
9050 netif_dormant_on(dev);
9051 else
9052 netif_dormant_off(dev);
9053
Zhang Shengju0575c862017-04-26 17:49:38 +08009054 if (netif_carrier_ok(rootdev))
9055 netif_carrier_on(dev);
9056 else
9057 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009058}
9059EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9060
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009061static int netif_alloc_rx_queues(struct net_device *dev)
9062{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009063 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00009064 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05309065 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009066 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009067
Tom Herbertbd25fa72010-10-18 18:00:16 +00009068 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009069
Michal Hockodcda9b02017-07-12 14:36:45 -07009070 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009071 if (!rx)
9072 return -ENOMEM;
9073
Tom Herbertbd25fa72010-10-18 18:00:16 +00009074 dev->_rx = rx;
9075
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009076 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00009077 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009078
9079 /* XDP RX-queue setup */
9080 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
9081 if (err < 0)
9082 goto err_rxq_info;
9083 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009084 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009085
9086err_rxq_info:
9087 /* Rollback successful reg's and free other resources */
9088 while (i--)
9089 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08009090 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009091 dev->_rx = NULL;
9092 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009093}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009094
9095static void netif_free_rx_queues(struct net_device *dev)
9096{
9097 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009098
9099 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9100 if (!dev->_rx)
9101 return;
9102
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009103 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08009104 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9105
9106 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009107}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00009108
Changli Gaoaa942102010-12-04 02:31:41 +00009109static void netdev_init_one_queue(struct net_device *dev,
9110 struct netdev_queue *queue, void *_unused)
9111{
9112 /* Initialize queue lock */
9113 spin_lock_init(&queue->_xmit_lock);
Taehee Yooab92d682019-10-21 18:47:51 +00009114 lockdep_set_class(&queue->_xmit_lock, &dev->qdisc_xmit_lock_key);
Changli Gaoaa942102010-12-04 02:31:41 +00009115 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00009116 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00009117 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00009118#ifdef CONFIG_BQL
9119 dql_init(&queue->dql, HZ);
9120#endif
Changli Gaoaa942102010-12-04 02:31:41 +00009121}
9122
Eric Dumazet60877a32013-06-20 01:15:51 -07009123static void netif_free_tx_queues(struct net_device *dev)
9124{
WANG Cong4cb28972014-06-02 15:55:22 -07009125 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07009126}
9127
Tom Herberte6484932010-10-18 18:04:39 +00009128static int netif_alloc_netdev_queues(struct net_device *dev)
9129{
9130 unsigned int count = dev->num_tx_queues;
9131 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07009132 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00009133
Eric Dumazetd3397272015-07-06 17:13:26 +02009134 if (count < 1 || count > 0xffff)
9135 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00009136
Michal Hockodcda9b02017-07-12 14:36:45 -07009137 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07009138 if (!tx)
9139 return -ENOMEM;
9140
Tom Herberte6484932010-10-18 18:04:39 +00009141 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00009142
Tom Herberte6484932010-10-18 18:04:39 +00009143 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9144 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00009145
9146 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00009147}
9148
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009149void netif_tx_stop_all_queues(struct net_device *dev)
9150{
9151 unsigned int i;
9152
9153 for (i = 0; i < dev->num_tx_queues; i++) {
9154 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11009155
Denys Vlasenkoa2029242015-05-11 21:17:53 +02009156 netif_tx_stop_queue(txq);
9157 }
9158}
9159EXPORT_SYMBOL(netif_tx_stop_all_queues);
9160
Taehee Yooab92d682019-10-21 18:47:51 +00009161static void netdev_register_lockdep_key(struct net_device *dev)
9162{
9163 lockdep_register_key(&dev->qdisc_tx_busylock_key);
9164 lockdep_register_key(&dev->qdisc_running_key);
9165 lockdep_register_key(&dev->qdisc_xmit_lock_key);
9166 lockdep_register_key(&dev->addr_list_lock_key);
9167}
9168
9169static void netdev_unregister_lockdep_key(struct net_device *dev)
9170{
9171 lockdep_unregister_key(&dev->qdisc_tx_busylock_key);
9172 lockdep_unregister_key(&dev->qdisc_running_key);
9173 lockdep_unregister_key(&dev->qdisc_xmit_lock_key);
9174 lockdep_unregister_key(&dev->addr_list_lock_key);
9175}
9176
9177void netdev_update_lockdep_key(struct net_device *dev)
9178{
9179 struct netdev_queue *queue;
9180 int i;
9181
9182 lockdep_unregister_key(&dev->qdisc_xmit_lock_key);
9183 lockdep_unregister_key(&dev->addr_list_lock_key);
9184
9185 lockdep_register_key(&dev->qdisc_xmit_lock_key);
9186 lockdep_register_key(&dev->addr_list_lock_key);
9187
9188 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
9189 for (i = 0; i < dev->num_tx_queues; i++) {
9190 queue = netdev_get_tx_queue(dev, i);
9191
9192 lockdep_set_class(&queue->_xmit_lock,
9193 &dev->qdisc_xmit_lock_key);
9194 }
9195}
9196EXPORT_SYMBOL(netdev_update_lockdep_key);
9197
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08009198/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199 * register_netdevice - register a network device
9200 * @dev: device to register
9201 *
9202 * Take a completed network device structure and add it to the kernel
9203 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9204 * chain. 0 is returned on success. A negative errno code is returned
9205 * on a failure to set up the device, or if the name is a duplicate.
9206 *
9207 * Callers must hold the rtnl semaphore. You may want
9208 * register_netdev() instead of this.
9209 *
9210 * BUGS:
9211 * The locking appears insufficient to guarantee two parallel registers
9212 * will not get the same name.
9213 */
9214
9215int register_netdevice(struct net_device *dev)
9216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009218 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219
Florian Fainellie283de32018-04-30 14:20:05 -07009220 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9221 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 BUG_ON(dev_boot_phase);
9223 ASSERT_RTNL();
9224
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009225 might_sleep();
9226
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 /* When net_device's are persistent, this will be fatal. */
9228 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009229 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230
David S. Millerf1f28aa2008-07-15 00:08:33 -07009231 spin_lock_init(&dev->addr_list_lock);
Taehee Yooab92d682019-10-21 18:47:51 +00009232 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233
Gao feng828de4f2012-09-13 20:58:27 +00009234 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00009235 if (ret < 0)
9236 goto out;
9237
Eric Dumazet9077f052019-10-03 08:59:24 -07009238 ret = -ENOMEM;
Jiri Pirkoff927412019-09-30 11:48:15 +02009239 dev->name_node = netdev_name_node_head_alloc(dev);
9240 if (!dev->name_node)
9241 goto out;
9242
Linus Torvalds1da177e2005-04-16 15:20:36 -07009243 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009244 if (dev->netdev_ops->ndo_init) {
9245 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 if (ret) {
9247 if (ret > 0)
9248 ret = -EIO;
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009249 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250 }
9251 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009252
Patrick McHardyf6469682013-04-19 02:04:27 +00009253 if (((dev->hw_features | dev->features) &
9254 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00009255 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9256 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9257 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9258 ret = -EINVAL;
9259 goto err_uninit;
9260 }
9261
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00009262 ret = -EBUSY;
9263 if (!dev->ifindex)
9264 dev->ifindex = dev_new_index(net);
9265 else if (__dev_get_by_index(net, dev->ifindex))
9266 goto err_uninit;
9267
Michał Mirosław5455c692011-02-15 16:59:17 +00009268 /* Transfer changeable features to wanted_features and enable
9269 * software offloads (GSO and GRO).
9270 */
9271 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00009272 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02009273
9274 if (dev->netdev_ops->ndo_udp_tunnel_add) {
9275 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9276 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9277 }
9278
Michał Mirosław14d12322011-02-22 16:52:28 +00009279 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009281 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00009282 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009283
Alexander Duyck7f348a62016-04-20 16:51:00 -04009284 /* If IPv4 TCP segmentation offload is supported we should also
9285 * allow the device to enable segmenting the frame with the option
9286 * of ignoring a static IP ID value. This doesn't enable the
9287 * feature itself but allows the user to enable it later.
9288 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04009289 if (dev->hw_features & NETIF_F_TSO)
9290 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04009291 if (dev->vlan_features & NETIF_F_TSO)
9292 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9293 if (dev->mpls_features & NETIF_F_TSO)
9294 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9295 if (dev->hw_enc_features & NETIF_F_TSO)
9296 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07009297
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009298 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00009299 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07009300 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00009301
Pravin B Shelaree579672013-03-07 09:28:08 +00009302 /* Make NETIF_F_SG inheritable to tunnel devices.
9303 */
Alexander Duyck802ab552016-04-10 21:45:03 -04009304 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00009305
Simon Horman0d89d202013-05-23 21:02:52 +00009306 /* Make NETIF_F_SG inheritable to MPLS.
9307 */
9308 dev->mpls_features |= NETIF_F_SG;
9309
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00009310 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9311 ret = notifier_to_errno(ret);
9312 if (ret)
9313 goto err_uninit;
9314
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009315 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009316 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009317 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009318 dev->reg_state = NETREG_REGISTERED;
9319
Michał Mirosław6cb6a272011-04-02 22:48:47 -07009320 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00009321
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 /*
9323 * Default initial state at registry is that the
9324 * device is present.
9325 */
9326
9327 set_bit(__LINK_STATE_PRESENT, &dev->state);
9328
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01009329 linkwatch_init_dev(dev);
9330
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009333 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04009334 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335
Jiri Pirko948b3372013-01-08 01:38:25 +00009336 /* If the device has permanent device address, driver should
9337 * set dev_addr and also addr_assign_type should be set to
9338 * NET_ADDR_PERM (default value).
9339 */
9340 if (dev->addr_assign_type == NET_ADDR_PERM)
9341 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
9342
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009344 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07009345 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009346 if (ret) {
9347 rollback_registered(dev);
Subash Abhinov Kasiviswanathan10cc5142019-09-10 14:02:57 -06009348 rcu_barrier();
9349
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07009350 dev->reg_state = NETREG_UNREGISTERED;
9351 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009352 /*
9353 * Prevent userspace races by waiting until the network
9354 * device is fully setup before sending notifications.
9355 */
Patrick McHardya2835762010-02-26 06:34:51 +00009356 if (!dev->rtnl_link_ops ||
9357 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07009358 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359
9360out:
9361 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009362
9363err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08009364 if (dev->netdev_ops->ndo_uninit)
9365 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04009366 if (dev->priv_destructor)
9367 dev->priv_destructor(dev);
Dan Carpenter42c17fa2019-12-03 17:12:39 +03009368err_free_name:
9369 netdev_name_node_free(dev->name_node);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07009370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009372EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373
9374/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009375 * init_dummy_netdev - init a dummy network device for NAPI
9376 * @dev: device to init
9377 *
9378 * This takes a network device structure and initialize the minimum
9379 * amount of fields so it can be used to schedule NAPI polls without
9380 * registering a full blown interface. This is to be used by drivers
9381 * that need to tie several hardware interfaces to a single NAPI
9382 * poll scheduler due to HW limitations.
9383 */
9384int init_dummy_netdev(struct net_device *dev)
9385{
9386 /* Clear everything. Note we don't initialize spinlocks
9387 * are they aren't supposed to be taken by any of the
9388 * NAPI code and this dummy netdev is supposed to be
9389 * only ever used for NAPI polls
9390 */
9391 memset(dev, 0, sizeof(struct net_device));
9392
9393 /* make sure we BUG if trying to hit standard
9394 * register/unregister code path
9395 */
9396 dev->reg_state = NETREG_DUMMY;
9397
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009398 /* NAPI wants this */
9399 INIT_LIST_HEAD(&dev->napi_list);
9400
9401 /* a dummy interface is started by default */
9402 set_bit(__LINK_STATE_PRESENT, &dev->state);
9403 set_bit(__LINK_STATE_START, &dev->state);
9404
Josh Elsasser35edfdc2019-01-26 14:38:33 -08009405 /* napi_busy_loop stats accounting wants this */
9406 dev_net_set(dev, &init_net);
9407
Eric Dumazet29b44332010-10-11 10:22:12 +00009408 /* Note : We dont allocate pcpu_refcnt for dummy devices,
9409 * because users of this 'device' dont need to change
9410 * its refcount.
9411 */
9412
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08009413 return 0;
9414}
9415EXPORT_SYMBOL_GPL(init_dummy_netdev);
9416
9417
9418/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 * register_netdev - register a network device
9420 * @dev: device to register
9421 *
9422 * Take a completed network device structure and add it to the kernel
9423 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9424 * chain. 0 is returned on success. A negative errno code is returned
9425 * on a failure to set up the device, or if the name is a duplicate.
9426 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07009427 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428 * and expands the device name if you passed a format string to
9429 * alloc_netdev.
9430 */
9431int register_netdev(struct net_device *dev)
9432{
9433 int err;
9434
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +03009435 if (rtnl_lock_killable())
9436 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438 rtnl_unlock();
9439 return err;
9440}
9441EXPORT_SYMBOL(register_netdev);
9442
Eric Dumazet29b44332010-10-11 10:22:12 +00009443int netdev_refcnt_read(const struct net_device *dev)
9444{
9445 int i, refcnt = 0;
9446
9447 for_each_possible_cpu(i)
9448 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
9449 return refcnt;
9450}
9451EXPORT_SYMBOL(netdev_refcnt_read);
9452
Ben Hutchings2c530402012-07-10 10:55:09 +00009453/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00009455 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 *
9457 * This is called when unregistering network devices.
9458 *
9459 * Any protocol or device that holds a reference should register
9460 * for netdevice notification, and cleanup and put back the
9461 * reference if they receive an UNREGISTER event.
9462 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009463 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 */
9465static void netdev_wait_allrefs(struct net_device *dev)
9466{
9467 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00009468 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469
Eric Dumazete014deb2009-11-17 05:59:21 +00009470 linkwatch_forget_dev(dev);
9471
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00009473 refcnt = netdev_refcnt_read(dev);
9474
9475 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009477 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478
9479 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07009480 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481
Eric Dumazet748e2d92012-08-22 21:50:59 +00009482 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009483 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00009484 rtnl_lock();
9485
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
9487 &dev->state)) {
9488 /* We must not have linkwatch events
9489 * pending on unregister. If this
9490 * happens, we simply run the queue
9491 * unscheduled, resulting in a noop
9492 * for this device.
9493 */
9494 linkwatch_run_queue();
9495 }
9496
Stephen Hemminger6756ae42006-03-20 22:23:58 -08009497 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498
9499 rebroadcast_time = jiffies;
9500 }
9501
9502 msleep(250);
9503
Eric Dumazet29b44332010-10-11 10:22:12 +00009504 refcnt = netdev_refcnt_read(dev);
9505
Eric Dumazetd7c04b02019-05-16 08:09:57 -07009506 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009507 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9508 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 warning_time = jiffies;
9510 }
9511 }
9512}
9513
9514/* The sequence is:
9515 *
9516 * rtnl_lock();
9517 * ...
9518 * register_netdevice(x1);
9519 * register_netdevice(x2);
9520 * ...
9521 * unregister_netdevice(y1);
9522 * unregister_netdevice(y2);
9523 * ...
9524 * rtnl_unlock();
9525 * free_netdev(y1);
9526 * free_netdev(y2);
9527 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07009528 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009530 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531 * without deadlocking with linkwatch via keventd.
9532 * 2) Since we run with the RTNL semaphore not held, we can sleep
9533 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07009534 *
9535 * We must not return until all unregister events added during
9536 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538void netdev_run_todo(void)
9539{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009540 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009541
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009543 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07009544
9545 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07009546
Eric Dumazet0115e8e2012-08-22 17:19:46 +00009547
9548 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00009549 if (!list_empty(&list))
9550 rcu_barrier();
9551
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 while (!list_empty(&list)) {
9553 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00009554 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 list_del(&dev->todo_list);
9556
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009557 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009558 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009560 dump_stack();
9561 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009563
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07009564 dev->reg_state = NETREG_UNREGISTERED;
9565
9566 netdev_wait_allrefs(dev);
9567
9568 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00009569 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08009570 BUG_ON(!list_empty(&dev->ptype_all));
9571 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00009572 WARN_ON(rcu_access_pointer(dev->ip_ptr));
9573 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -08009574#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -07009575 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -08009576#endif
David S. Millercf124db2017-05-08 12:52:56 -04009577 if (dev->priv_destructor)
9578 dev->priv_destructor(dev);
9579 if (dev->needs_free_netdev)
9580 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009581
Eric W. Biederman50624c92013-09-23 21:19:49 -07009582 /* Report a network device has been unregistered */
9583 rtnl_lock();
9584 dev_net(dev)->dev_unreg_count--;
9585 __rtnl_unlock();
9586 wake_up(&netdev_unregistering_wq);
9587
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07009588 /* Free network device */
9589 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591}
9592
Jarod Wilson92566452016-02-01 18:51:04 -05009593/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
9594 * all the same fields in the same order as net_device_stats, with only
9595 * the type differing, but rtnl_link_stats64 may have additional fields
9596 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00009597 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009598void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
9599 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00009600{
9601#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05009602 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02009603 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05009604 /* zero out counters that only exist in rtnl_link_stats64 */
9605 memset((char *)stats64 + sizeof(*netdev_stats), 0,
9606 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009607#else
Jarod Wilson92566452016-02-01 18:51:04 -05009608 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009609 const unsigned long *src = (const unsigned long *)netdev_stats;
9610 u64 *dst = (u64 *)stats64;
9611
Jarod Wilson92566452016-02-01 18:51:04 -05009612 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009613 for (i = 0; i < n; i++)
9614 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05009615 /* zero out counters that only exist in rtnl_link_stats64 */
9616 memset((char *)stats64 + n * sizeof(u64), 0,
9617 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00009618#endif
9619}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00009620EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00009621
Eric Dumazetd83345a2009-11-16 03:36:51 +00009622/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009623 * dev_get_stats - get network device statistics
9624 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07009625 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009626 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00009627 * Get network statistics from device. Return @storage.
9628 * The device driver may provide its own method by setting
9629 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
9630 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009631 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00009632struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
9633 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00009634{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009635 const struct net_device_ops *ops = dev->netdev_ops;
9636
Eric Dumazet28172732010-07-07 14:58:56 -07009637 if (ops->ndo_get_stats64) {
9638 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00009639 ops->ndo_get_stats64(dev, storage);
9640 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00009641 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00009642 } else {
9643 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07009644 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07009645 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
9646 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
9647 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07009648 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07009649}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08009650EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07009651
Eric Dumazet24824a02010-10-02 06:11:55 +00009652struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07009653{
Eric Dumazet24824a02010-10-02 06:11:55 +00009654 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07009655
Eric Dumazet24824a02010-10-02 06:11:55 +00009656#ifdef CONFIG_NET_CLS_ACT
9657 if (queue)
9658 return queue;
9659 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
9660 if (!queue)
9661 return NULL;
9662 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08009663 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00009664 queue->qdisc_sleeping = &noop_qdisc;
9665 rcu_assign_pointer(dev->ingress_queue, queue);
9666#endif
9667 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07009668}
9669
Eric Dumazet2c60db02012-09-16 09:17:26 +00009670static const struct ethtool_ops default_ethtool_ops;
9671
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00009672void netdev_set_default_ethtool_ops(struct net_device *dev,
9673 const struct ethtool_ops *ops)
9674{
9675 if (dev->ethtool_ops == &default_ethtool_ops)
9676 dev->ethtool_ops = ops;
9677}
9678EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
9679
Eric Dumazet74d332c2013-10-30 13:10:44 -07009680void netdev_freemem(struct net_device *dev)
9681{
9682 char *addr = (char *)dev - dev->padded;
9683
WANG Cong4cb28972014-06-02 15:55:22 -07009684 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009685}
9686
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687/**
tcharding722c9a02017-02-09 17:56:04 +11009688 * alloc_netdev_mqs - allocate network device
9689 * @sizeof_priv: size of private data to allocate space for
9690 * @name: device name format string
9691 * @name_assign_type: origin of device name
9692 * @setup: callback to initialize device
9693 * @txqs: the number of TX subqueues to allocate
9694 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695 *
tcharding722c9a02017-02-09 17:56:04 +11009696 * Allocates a struct net_device with private data area for driver use
9697 * and performs basic initialization. Also allocates subqueue structs
9698 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699 */
Tom Herbert36909ea2011-01-09 19:36:31 +00009700struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02009701 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00009702 void (*setup)(struct net_device *),
9703 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03009706 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009707 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07009709 BUG_ON(strlen(name) >= sizeof(dev->name));
9710
Tom Herbert36909ea2011-01-09 19:36:31 +00009711 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009712 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00009713 return NULL;
9714 }
9715
Tom Herbert36909ea2011-01-09 19:36:31 +00009716 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009717 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00009718 return NULL;
9719 }
Tom Herbert36909ea2011-01-09 19:36:31 +00009720
David S. Millerfd2ea0a2008-07-17 01:56:23 -07009721 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009722 if (sizeof_priv) {
9723 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009724 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009725 alloc_size += sizeof_priv;
9726 }
9727 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009728 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729
Michal Hockodcda9b02017-07-12 14:36:45 -07009730 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00009731 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009734 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009736
Eric Dumazet29b44332010-10-11 10:22:12 +00009737 dev->pcpu_refcnt = alloc_percpu(int);
9738 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07009739 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009740
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00009742 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743
Jiri Pirko22bedad32010-04-01 21:22:57 +00009744 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009745 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00009746
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09009747 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748
Taehee Yooab92d682019-10-21 18:47:51 +00009749 netdev_register_lockdep_key(dev);
9750
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07009751 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00009752 dev->gso_max_segs = GSO_MAX_SEGS;
Taehee Yoo5343da42019-10-21 18:47:50 +00009753 dev->upper_level = 1;
9754 dev->lower_level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755
Herbert Xud565b0a2008-12-15 23:38:52 -08009756 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009757 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009758 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00009759 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02009760 INIT_LIST_HEAD(&dev->adj_list.upper);
9761 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08009762 INIT_LIST_HEAD(&dev->ptype_all);
9763 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02009764#ifdef CONFIG_NET_SCHED
9765 hash_init(dev->qdisc_hash);
9766#endif
Eric Dumazet02875872014-10-05 18:38:35 -07009767 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009769
Phil Suttera8131042016-02-17 15:37:43 +01009770 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02009771 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01009772 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01009773 }
Phil Sutter906470c2015-08-18 10:30:48 +02009774
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009775 dev->num_tx_queues = txqs;
9776 dev->real_num_tx_queues = txqs;
9777 if (netif_alloc_netdev_queues(dev))
9778 goto free_all;
9779
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009780 dev->num_rx_queues = rxqs;
9781 dev->real_num_rx_queues = rxqs;
9782 if (netif_alloc_rx_queues(dev))
9783 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009784
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02009786 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00009787 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00009788 if (!dev->ethtool_ops)
9789 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02009790
9791 nf_hook_ingress_init(dev);
9792
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009794
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009795free_all:
9796 free_netdev(dev);
9797 return NULL;
9798
Eric Dumazet29b44332010-10-11 10:22:12 +00009799free_pcpu:
9800 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009801free_dev:
9802 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009803 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804}
Tom Herbert36909ea2011-01-09 19:36:31 +00009805EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
9807/**
tcharding722c9a02017-02-09 17:56:04 +11009808 * free_netdev - free network device
9809 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 *
tcharding722c9a02017-02-09 17:56:04 +11009811 * This function does the last stage of destroying an allocated device
9812 * interface. The reference to the device object is released. If this
9813 * is the last reference then it will be freed.Must be called in process
9814 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815 */
9816void free_netdev(struct net_device *dev)
9817{
Herbert Xud565b0a2008-12-15 23:38:52 -08009818 struct napi_struct *p, *n;
9819
Eric Dumazet93d05d42015-11-18 06:31:03 -08009820 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07009821 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009822 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -07009823
Eric Dumazet33d480c2011-08-11 19:30:52 +00009824 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00009825
Jiri Pirkof001fde2009-05-05 02:48:28 +00009826 /* Flush device addresses */
9827 dev_addr_flush(dev);
9828
Herbert Xud565b0a2008-12-15 23:38:52 -08009829 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
9830 netif_napi_del(p);
9831
Eric Dumazet29b44332010-10-11 10:22:12 +00009832 free_percpu(dev->pcpu_refcnt);
9833 dev->pcpu_refcnt = NULL;
9834
Taehee Yooab92d682019-10-21 18:47:51 +00009835 netdev_unregister_lockdep_key(dev);
9836
Stephen Hemminger3041a062006-05-26 13:25:24 -07009837 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07009839 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 return;
9841 }
9842
9843 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
9844 dev->reg_state = NETREG_RELEASED;
9845
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07009846 /* will free via device release */
9847 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009849EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009850
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009851/**
9852 * synchronize_net - Synchronize with packet receive processing
9853 *
9854 * Wait for packets currently being received to be done.
9855 * Does not block later packets from starting.
9856 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009857void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858{
9859 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00009860 if (rtnl_is_locked())
9861 synchronize_rcu_expedited();
9862 else
9863 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009865EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866
9867/**
Eric Dumazet44a08732009-10-27 07:03:04 +00009868 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00009870 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08009871 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009873 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00009874 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 *
9876 * Callers must hold the rtnl semaphore. You may want
9877 * unregister_netdev() instead of this.
9878 */
9879
Eric Dumazet44a08732009-10-27 07:03:04 +00009880void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881{
Herbert Xua6620712007-12-12 19:21:56 -08009882 ASSERT_RTNL();
9883
Eric Dumazet44a08732009-10-27 07:03:04 +00009884 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009885 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00009886 } else {
9887 rollback_registered(dev);
9888 /* Finish processing unregister after unlock */
9889 net_set_todo(dev);
9890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009891}
Eric Dumazet44a08732009-10-27 07:03:04 +00009892EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893
9894/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009895 * unregister_netdevice_many - unregister many devices
9896 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07009897 *
9898 * Note: As most callers use a stack allocated list_head,
9899 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009900 */
9901void unregister_netdevice_many(struct list_head *head)
9902{
9903 struct net_device *dev;
9904
9905 if (!list_empty(head)) {
9906 rollback_registered_many(head);
9907 list_for_each_entry(dev, head, unreg_list)
9908 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07009909 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009910 }
9911}
Eric Dumazet63c80992009-10-27 07:06:49 +00009912EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009913
9914/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915 * unregister_netdev - remove device from the kernel
9916 * @dev: device
9917 *
9918 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009919 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920 *
9921 * This is just a wrapper for unregister_netdevice that takes
9922 * the rtnl semaphore. In general you want to use this and not
9923 * unregister_netdevice.
9924 */
9925void unregister_netdev(struct net_device *dev)
9926{
9927 rtnl_lock();
9928 unregister_netdevice(dev);
9929 rtnl_unlock();
9930}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931EXPORT_SYMBOL(unregister_netdev);
9932
Eric W. Biedermance286d32007-09-12 13:53:49 +02009933/**
9934 * dev_change_net_namespace - move device to different nethost namespace
9935 * @dev: device
9936 * @net: network namespace
9937 * @pat: If not NULL name pattern to try if the current device name
9938 * is already taken in the destination network namespace.
9939 *
9940 * This function shuts down a device interface and moves it
9941 * to a new network namespace. On success 0 is returned, on
9942 * a failure a netagive errno code is returned.
9943 *
9944 * Callers must hold the rtnl semaphore.
9945 */
9946
9947int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
9948{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009949 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009950
9951 ASSERT_RTNL();
9952
9953 /* Don't allow namespace local devices to be moved. */
9954 err = -EINVAL;
9955 if (dev->features & NETIF_F_NETNS_LOCAL)
9956 goto out;
9957
9958 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009959 if (dev->reg_state != NETREG_REGISTERED)
9960 goto out;
9961
9962 /* Get out if there is nothing todo */
9963 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09009964 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02009965 goto out;
9966
9967 /* Pick the destination device name, and ensure
9968 * we can use it in the destination network namespace.
9969 */
9970 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00009971 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009972 /* We get here if we can't use the current device name */
9973 if (!pat)
9974 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +08009975 err = dev_get_valid_name(net, dev, pat);
9976 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02009977 goto out;
9978 }
9979
9980 /*
9981 * And now a mini version of register_netdevice unregister_netdevice.
9982 */
9983
9984 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07009985 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009986
9987 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009988 unlist_netdevice(dev);
9989
9990 synchronize_net();
9991
9992 /* Shutdown queueing discipline. */
9993 dev_shutdown(dev);
9994
9995 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11009996 * this device. They should clean all the things.
9997 *
9998 * Note that dev->reg_state stays at NETREG_REGISTERED.
9999 * This is wanted because this way 8021q and macvlan know
10000 * the device is just moving and can keep their slaves up.
10001 */
Eric W. Biedermance286d32007-09-12 13:53:49 +020010002 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +000010003 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010004
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +020010005 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010006 /* If there is an ifindex conflict assign a new one */
10007 if (__dev_get_by_index(net, dev->ifindex))
10008 new_ifindex = dev_new_index(net);
10009 else
10010 new_ifindex = dev->ifindex;
10011
10012 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10013 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010014
10015 /*
10016 * Flush the unicast and multicast chains
10017 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +000010018 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +000010019 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010020
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010021 /* Send a netdev-removed uevent to the old namespace */
10022 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010023 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010024
Eric W. Biedermance286d32007-09-12 13:53:49 +020010025 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +090010026 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +010010027 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010028
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010029 /* Send a netdev-add uevent to the new namespace */
10030 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +040010031 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +000010032
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010033 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -070010034 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010035 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010036
10037 /* Add the device back in the hashes */
10038 list_netdevice(dev);
10039
10040 /* Notify protocols, that a new device appeared. */
10041 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10042
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010043 /*
10044 * Prevent userspace races by waiting until the network
10045 * device is fully setup before sending notifications.
10046 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -070010047 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +000010048
Eric W. Biedermance286d32007-09-12 13:53:49 +020010049 synchronize_net();
10050 err = 0;
10051out:
10052 return err;
10053}
Johannes Berg463d0182009-07-14 00:33:35 +020010054EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010055
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010056static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057{
10058 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010060 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +053010061 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063 local_irq_disable();
10064 cpu = smp_processor_id();
10065 sd = &per_cpu(softnet_data, cpu);
10066 oldsd = &per_cpu(softnet_data, oldcpu);
10067
10068 /* Find end of our completion_queue. */
10069 list_skb = &sd->completion_queue;
10070 while (*list_skb)
10071 list_skb = &(*list_skb)->next;
10072 /* Append completion queue from offline CPU. */
10073 *list_skb = oldsd->completion_queue;
10074 oldsd->completion_queue = NULL;
10075
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +000010077 if (oldsd->output_queue) {
10078 *sd->output_queue_tailp = oldsd->output_queue;
10079 sd->output_queue_tailp = oldsd->output_queue_tailp;
10080 oldsd->output_queue = NULL;
10081 oldsd->output_queue_tailp = &oldsd->output_queue;
10082 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010083 /* Append NAPI poll list from offline CPU, with one exception :
10084 * process_backlog() must be called by cpu owning percpu backlog.
10085 * We properly handle process_queue & input_pkt_queue later.
10086 */
10087 while (!list_empty(&oldsd->poll_list)) {
10088 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10089 struct napi_struct,
10090 poll_list);
10091
10092 list_del_init(&napi->poll_list);
10093 if (napi->poll == process_backlog)
10094 napi->state = 0;
10095 else
10096 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +000010097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
10099 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10100 local_irq_enable();
10101
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +053010102#ifdef CONFIG_RPS
10103 remsd = oldsd->rps_ipi_list;
10104 oldsd->rps_ipi_list = NULL;
10105#endif
10106 /* send out pending IPI's on offline CPU */
10107 net_rps_send_ipi(remsd);
10108
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +000010110 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010111 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010112 input_queue_head_incr(oldsd);
10113 }
Eric Dumazetac64da02015-01-15 17:04:22 -080010114 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -080010115 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +000010116 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -070010117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121
Herbert Xu7f353bf2007-08-10 15:47:58 -070010122/**
Herbert Xub63365a2008-10-23 01:11:29 -070010123 * netdev_increment_features - increment feature set by one
10124 * @all: current feature set
10125 * @one: new feature set
10126 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -070010127 *
10128 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -070010129 * @one to the master device with current feature set @all. Will not
10130 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -070010131 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +000010132netdev_features_t netdev_increment_features(netdev_features_t all,
10133 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -070010134{
Tom Herbertc8cd0982015-12-14 11:19:44 -080010135 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -080010136 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +000010137 mask |= NETIF_F_VLAN_CHALLENGED;
10138
Tom Herberta1882222015-12-14 11:19:43 -080010139 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +000010140 all &= one | ~NETIF_F_ALL_FOR_ALL;
10141
Michał Mirosław1742f182011-04-22 06:31:16 +000010142 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -080010143 if (all & NETIF_F_HW_CSUM)
10144 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010145
10146 return all;
10147}
Herbert Xub63365a2008-10-23 01:11:29 -070010148EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -070010149
Baruch Siach430f03c2013-06-02 20:43:55 +000010150static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010151{
10152 int i;
10153 struct hlist_head *hash;
10154
Kees Cook6da2ec52018-06-12 13:55:00 -070010155 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010156 if (hash != NULL)
10157 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10158 INIT_HLIST_HEAD(&hash[i]);
10159
10160 return hash;
10161}
10162
Eric W. Biederman881d9662007-09-17 11:56:21 -070010163/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -070010164static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010165{
Li RongQingd9f37d02018-07-13 14:41:36 +080010166 BUILD_BUG_ON(GRO_HASH_BUCKETS >
David S. Millerccdb5172018-07-16 17:02:04 -070010167 8 * FIELD_SIZEOF(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +080010168
Rustad, Mark D734b6542012-07-18 09:06:07 +000010169 if (net != &init_net)
10170 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -070010171
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010172 net->dev_name_head = netdev_create_hash();
10173 if (net->dev_name_head == NULL)
10174 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010175
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010176 net->dev_index_head = netdev_create_hash();
10177 if (net->dev_index_head == NULL)
10178 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010179
Jiri Pirkoa30c7b42019-09-30 10:15:10 +020010180 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10181
Eric W. Biederman881d9662007-09-17 11:56:21 -070010182 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -070010183
10184err_idx:
10185 kfree(net->dev_name_head);
10186err_name:
10187 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -070010188}
10189
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010190/**
10191 * netdev_drivername - network driver for the device
10192 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -070010193 *
10194 * Determine network driver for device.
10195 */
David S. Miller3019de12011-06-06 16:41:33 -070010196const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -070010197{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -070010198 const struct device_driver *driver;
10199 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -070010200 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -070010201
10202 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010203 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -070010204 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010205
10206 driver = parent->driver;
10207 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -070010208 return driver->name;
10209 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -070010210}
10211
Joe Perches6ea754e2014-09-22 11:10:50 -070010212static void __netdev_printk(const char *level, const struct net_device *dev,
10213 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +000010214{
Joe Perchesb004ff42012-09-12 20:12:19 -070010215 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010216 dev_printk_emit(level[1] - '0',
10217 dev->dev.parent,
10218 "%s %s %s%s: %pV",
10219 dev_driver_string(dev->dev.parent),
10220 dev_name(dev->dev.parent),
10221 netdev_name(dev), netdev_reg_state(dev),
10222 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010223 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -070010224 printk("%s%s%s: %pV",
10225 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010226 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -070010227 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010228 }
Joe Perches256df2f2010-06-27 01:02:35 +000010229}
10230
Joe Perches6ea754e2014-09-22 11:10:50 -070010231void netdev_printk(const char *level, const struct net_device *dev,
10232 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +000010233{
10234 struct va_format vaf;
10235 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +000010236
10237 va_start(args, format);
10238
10239 vaf.fmt = format;
10240 vaf.va = &args;
10241
Joe Perches6ea754e2014-09-22 11:10:50 -070010242 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -070010243
Joe Perches256df2f2010-06-27 01:02:35 +000010244 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +000010245}
10246EXPORT_SYMBOL(netdev_printk);
10247
10248#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -070010249void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +000010250{ \
Joe Perches256df2f2010-06-27 01:02:35 +000010251 struct va_format vaf; \
10252 va_list args; \
10253 \
10254 va_start(args, fmt); \
10255 \
10256 vaf.fmt = fmt; \
10257 vaf.va = &args; \
10258 \
Joe Perches6ea754e2014-09-22 11:10:50 -070010259 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -070010260 \
Joe Perches256df2f2010-06-27 01:02:35 +000010261 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +000010262} \
10263EXPORT_SYMBOL(func);
10264
10265define_netdev_printk_level(netdev_emerg, KERN_EMERG);
10266define_netdev_printk_level(netdev_alert, KERN_ALERT);
10267define_netdev_printk_level(netdev_crit, KERN_CRIT);
10268define_netdev_printk_level(netdev_err, KERN_ERR);
10269define_netdev_printk_level(netdev_warn, KERN_WARNING);
10270define_netdev_printk_level(netdev_notice, KERN_NOTICE);
10271define_netdev_printk_level(netdev_info, KERN_INFO);
10272
Pavel Emelyanov46650792007-10-08 20:38:39 -070010273static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -070010274{
10275 kfree(net->dev_name_head);
10276 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +030010277 if (net != &init_net)
10278 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -070010279}
10280
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010281static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -070010282 .init = netdev_init,
10283 .exit = netdev_exit,
10284};
10285
Pavel Emelyanov46650792007-10-08 20:38:39 -070010286static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +020010287{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010288 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +020010289 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010290 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +020010291 * initial network namespace
10292 */
10293 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010294 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010295 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010296 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +020010297
10298 /* Ignore unmoveable devices (i.e. loopback) */
10299 if (dev->features & NETIF_F_NETNS_LOCAL)
10300 continue;
10301
Eric W. Biedermane008b5f2009-11-29 22:25:30 +000010302 /* Leave virtual devices for the generic cleanup */
10303 if (dev->rtnl_link_ops)
10304 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -080010305
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010306 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010307 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
Jiri Pirko55b40db2019-07-28 14:56:36 +020010308 if (__dev_get_by_name(&init_net, fb_name))
10309 snprintf(fb_name, IFNAMSIZ, "dev%%d");
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010310 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +020010311 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +000010312 pr_emerg("%s: failed to move %s to init_net: %d\n",
10313 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -070010314 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +020010315 }
10316 }
10317 rtnl_unlock();
10318}
10319
Eric W. Biederman50624c92013-09-23 21:19:49 -070010320static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
10321{
10322 /* Return with the rtnl_lock held when there are no network
10323 * devices unregistering in any network namespace in net_list.
10324 */
10325 struct net *net;
10326 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +010010327 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010328
Peter Zijlstraff960a72014-10-29 17:04:56 +010010329 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010330 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -070010331 unregistering = false;
10332 rtnl_lock();
10333 list_for_each_entry(net, net_list, exit_list) {
10334 if (net->dev_unreg_count > 0) {
10335 unregistering = true;
10336 break;
10337 }
10338 }
10339 if (!unregistering)
10340 break;
10341 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +010010342
10343 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010344 }
Peter Zijlstraff960a72014-10-29 17:04:56 +010010345 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -070010346}
10347
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010348static void __net_exit default_device_exit_batch(struct list_head *net_list)
10349{
10350 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040010351 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010352 * Do this across as many network namespaces as possible to
10353 * improve batching efficiency.
10354 */
10355 struct net_device *dev;
10356 struct net *net;
10357 LIST_HEAD(dev_kill_list);
10358
Eric W. Biederman50624c92013-09-23 21:19:49 -070010359 /* To prevent network device cleanup code from dereferencing
10360 * loopback devices or network devices that have been freed
10361 * wait here for all pending unregistrations to complete,
10362 * before unregistring the loopback device and allowing the
10363 * network namespace be freed.
10364 *
10365 * The netdev todo list containing all network devices
10366 * unregistrations that happen in default_device_exit_batch
10367 * will run in the rtnl_unlock() at the end of
10368 * default_device_exit_batch.
10369 */
10370 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010371 list_for_each_entry(net, net_list, exit_list) {
10372 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +020010373 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010374 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
10375 else
10376 unregister_netdevice_queue(dev, &dev_kill_list);
10377 }
10378 }
10379 unregister_netdevice_many(&dev_kill_list);
10380 rtnl_unlock();
10381}
10382
Denis V. Lunev022cbae2007-11-13 03:23:50 -080010383static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +020010384 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +000010385 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +020010386};
10387
Linus Torvalds1da177e2005-04-16 15:20:36 -070010388/*
10389 * Initialize the DEV module. At boot time this walks the device list and
10390 * unhooks any devices that fail to initialise (normally hardware not
10391 * present) and leaves us with a valid list of present and active devices.
10392 *
10393 */
10394
10395/*
10396 * This is called single threaded during boot, so no need
10397 * to take the rtnl semaphore.
10398 */
10399static int __init net_dev_init(void)
10400{
10401 int i, rc = -ENOMEM;
10402
10403 BUG_ON(!dev_boot_phase);
10404
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 if (dev_proc_init())
10406 goto out;
10407
Eric W. Biederman8b41d182007-09-26 22:02:53 -070010408 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409 goto out;
10410
10411 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +080010412 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413 INIT_LIST_HEAD(&ptype_base[i]);
10414
Vlad Yasevich62532da2012-11-15 08:49:10 +000010415 INIT_LIST_HEAD(&offload_base);
10416
Eric W. Biederman881d9662007-09-17 11:56:21 -070010417 if (register_pernet_subsys(&netdev_net_ops))
10418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419
10420 /*
10421 * Initialise the packet receive queues.
10422 */
10423
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070010424 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -070010425 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010426 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427
Eric Dumazet41852492016-08-26 12:50:39 -070010428 INIT_WORK(flush, flush_backlog);
10429
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010430 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -070010431 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +010010432#ifdef CONFIG_XFRM_OFFLOAD
10433 skb_queue_head_init(&sd->xfrm_backlog);
10434#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010435 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +000010436 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +000010437#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010438 sd->csd.func = rps_trigger_softirq;
10439 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010440 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -070010441#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +000010442
David S. Miller7c4ec742018-07-20 23:37:55 -070010443 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +000010444 sd->backlog.poll = process_backlog;
10445 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 }
10447
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448 dev_boot_phase = 0;
10449
Eric W. Biederman505d4f72008-11-07 22:54:20 -080010450 /* The loopback device is special if any other network devices
10451 * is present in a network namespace the loopback device must
10452 * be present. Since we now dynamically allocate and free the
10453 * loopback device ensure this invariant is maintained by
10454 * keeping the loopback device as the first device on the
10455 * list of network devices. Ensuring the loopback devices
10456 * is the first device that appears and the last network device
10457 * that disappears.
10458 */
10459 if (register_pernet_device(&loopback_net_ops))
10460 goto out;
10461
10462 if (register_pernet_device(&default_device_ops))
10463 goto out;
10464
Carlos R. Mafra962cf362008-05-15 11:15:37 -030010465 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
10466 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +010010468 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
10469 NULL, dev_cpu_dead);
10470 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471 rc = 0;
10472out:
10473 return rc;
10474}
10475
10476subsys_initcall(net_dev_init);