blob: 82f20022259da123e085e0e296e9cd1b97580999 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
tcharding722c9a02017-02-09 17:56:04 +11002 * NET3 Protocol independent device support routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
tcharding722c9a02017-02-09 17:56:04 +110010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
21 *
22 * Changes:
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
tcharding722c9a02017-02-09 17:56:04 +110024 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
34 * drivers
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
tcharding722c9a02017-02-09 17:56:04 +110039 * Alan Cox : Fix ETH_P_ALL echoback lengths.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
44 * call a packet.
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
tcharding722c9a02017-02-09 17:56:04 +110049 * Alan Cox : Fixed nasty side effect of device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
58 * 1 device.
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
66 * the backlog queue.
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
tcharding722c9a02017-02-09 17:56:04 +110070 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * - netif_rx() feedback
73 */
74
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080077#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpu.h>
79#include <linux/types.h>
80#include <linux/kernel.h>
stephen hemminger08e98972009-11-10 07:20:34 +000081#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090082#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -070084#include <linux/sched/mm.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080085#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/string.h>
87#include <linux/mm.h>
88#include <linux/socket.h>
89#include <linux/sockios.h>
90#include <linux/errno.h>
91#include <linux/interrupt.h>
92#include <linux/if_ether.h>
93#include <linux/netdevice.h>
94#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070095#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/skbuff.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070097#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040098#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020099#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -0800101#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700105#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200107#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000109#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/highmem.h>
111#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <linux/netpoll.h>
114#include <linux/rcupdate.h>
115#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500118#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700119#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700120#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700121#include <linux/ctype.h>
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700122#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700123#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700124#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700125#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700126#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700127#include <linux/ipv6.h>
128#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700129#include <linux/jhash.h>
130#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700131#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900132#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900133#include <trace/events/skb.h>
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +0000134#include <linux/pci.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700135#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000136#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100137#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300138#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700139#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100140#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400141#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800142#include <linux/hrtimer.h>
Pablo Neirae687ad62015-05-13 18:19:38 +0200143#include <linux/netfilter_ingress.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530144#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200145#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200146#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200147#include <linux/net_namespace.h>
Paolo Abeniaaa5d902018-12-14 11:51:58 +0100148#include <linux/indirect_call_wrapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700150#include "net-sysfs.h"
151
Herbert Xud565b0a2008-12-15 23:38:52 -0800152#define MAX_GRO_SKBS 8
153
Herbert Xu5d38a072009-01-04 16:13:40 -0800154/* This should be increased if a protocol with a bigger head is added. */
155#define GRO_MAX_HEAD (MAX_HEADER + 128)
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000158static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000159struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
160struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000161static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000163static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700164static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700165 struct netdev_notifier_info *info);
Petr Machata26372602018-12-06 17:05:45 +0000166static int call_netdevice_notifiers_extack(unsigned long val,
167 struct net_device *dev,
168 struct netlink_ext_ack *extack);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200169static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700172 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * semaphore.
174 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800175 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700178 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * actual updates. This allows pure readers to access the list even
180 * while a writer is preparing to update it.
181 *
182 * To put it another way, dev_base_lock is held for writing only to
183 * protect against pure readers; the rtnl semaphore provides the
184 * protection against other writers.
185 *
186 * See, for example usages, register_netdevice() and
187 * unregister_netdevice(), which must be called with the rtnl
188 * semaphore held.
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191EXPORT_SYMBOL(dev_base_lock);
192
Florian Westphal6c557002017-10-02 23:50:05 +0200193static DEFINE_MUTEX(ifalias_mutex);
194
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300195/* protects napi_hash addition/deletion and napi_gen_id */
196static DEFINE_SPINLOCK(napi_hash_lock);
197
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800198static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800199static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300200
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200201static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000202
Thomas Graf4e985ad2011-06-21 03:11:20 +0000203static inline void dev_base_seq_inc(struct net *net)
204{
tcharding643aa9c2017-02-09 17:56:05 +1100205 while (++net->dev_base_seq == 0)
206 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000207}
208
Eric W. Biederman881d9662007-09-17 11:56:21 -0700209static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700211 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000212
stephen hemminger08e98972009-11-10 07:20:34 +0000213 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Eric W. Biederman881d9662007-09-17 11:56:21 -0700216static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700218 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000221static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000222{
223#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000224 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000225#endif
226}
227
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000228static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000229{
230#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000231 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000232#endif
233}
234
Eric W. Biedermance286d32007-09-12 13:53:49 +0200235/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000236static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200237{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900238 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200239
240 ASSERT_RTNL();
241
242 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800243 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000244 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000245 hlist_add_head_rcu(&dev->index_hlist,
246 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200247 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000248
249 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200250}
251
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000252/* Device list removal
253 * caller must respect a RCU grace period before freeing/reusing dev
254 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200255static void unlist_netdevice(struct net_device *dev)
256{
257 ASSERT_RTNL();
258
259 /* Unlink dev from the device chain */
260 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800261 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000262 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000263 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200264 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000265
266 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
270 * Our notifier list
271 */
272
Alan Sternf07d5b92006-05-09 15:23:03 -0700273static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/*
276 * Device drivers call our routines to queue packets here. We empty the
277 * queue in the local softnet handler.
278 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700279
Eric Dumazet9958da02010-04-17 04:17:02 +0000280DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700281EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David S. Millercf508b12008-07-22 14:16:42 -0700283#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700284/*
David S. Millerc773e842008-07-08 23:13:53 -0700285 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700286 * according to dev->type
287 */
tcharding643aa9c2017-02-09 17:56:05 +1100288static const unsigned short netdev_lock_type[] = {
289 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700290 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
291 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
292 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
293 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
294 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
295 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
296 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
297 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
298 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
299 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
300 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
Paul Gortmaker211ed862012-05-10 17:14:35 -0400301 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
302 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
303 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700304
tcharding643aa9c2017-02-09 17:56:05 +1100305static const char *const netdev_lock_name[] = {
306 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
307 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
308 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
309 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
310 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
311 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
312 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
313 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
314 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
315 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
316 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
317 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
318 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
319 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
320 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700321
322static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700323static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700324
325static inline unsigned short netdev_lock_pos(unsigned short dev_type)
326{
327 int i;
328
329 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
330 if (netdev_lock_type[i] == dev_type)
331 return i;
332 /* the last key is used by default */
333 return ARRAY_SIZE(netdev_lock_type) - 1;
334}
335
David S. Millercf508b12008-07-22 14:16:42 -0700336static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
337 unsigned short dev_type)
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700338{
339 int i;
340
341 i = netdev_lock_pos(dev_type);
342 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
343 netdev_lock_name[i]);
344}
David S. Millercf508b12008-07-22 14:16:42 -0700345
346static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
347{
348 int i;
349
350 i = netdev_lock_pos(dev->type);
351 lockdep_set_class_and_name(&dev->addr_list_lock,
352 &netdev_addr_lock_key[i],
353 netdev_lock_name[i]);
354}
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700355#else
David S. Millercf508b12008-07-22 14:16:42 -0700356static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
357 unsigned short dev_type)
358{
359}
360static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700361{
362}
363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100366 *
367 * Protocol management and registration routines
368 *
369 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * Add a protocol ID to the list. Now that the input handler is
374 * smarter we can dispense with all the messy stuff that used to be
375 * here.
376 *
377 * BEWARE!!! Protocol handlers, mangling input packets,
378 * MUST BE last in hash buckets and checking protocol handlers
379 * MUST start from promiscuous ptype_all chain in net_bh.
380 * It is true now, do not change it.
381 * Explanation follows: if protocol handler, mangling packet, will
382 * be the first on list, it is not able to sense, that packet
383 * is cloned and should be copied-on-write, so that it will
384 * change it and subsequent readers will get broken packet.
385 * --ANK (980803)
386 */
387
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000388static inline struct list_head *ptype_head(const struct packet_type *pt)
389{
390 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800391 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000392 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800393 return pt->dev ? &pt->dev->ptype_specific :
394 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/**
398 * dev_add_pack - add packet handler
399 * @pt: packet type declaration
400 *
401 * Add a protocol handler to the networking stack. The passed &packet_type
402 * is linked into kernel lists and may not be freed until it has been
403 * removed from the kernel lists.
404 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900405 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * guarantee all CPU's that are in middle of receiving packets
407 * will see the new packet type (until the next received packet).
408 */
409
410void dev_add_pack(struct packet_type *pt)
411{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000412 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000414 spin_lock(&ptype_lock);
415 list_add_rcu(&pt->list, head);
416 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700418EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
421 * __dev_remove_pack - remove packet handler
422 * @pt: packet type declaration
423 *
424 * Remove a protocol handler that was previously added to the kernel
425 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
426 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900427 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *
429 * The packet type might still be in use by receivers
430 * and must not be freed until after all the CPU's have gone
431 * through a quiescent state.
432 */
433void __dev_remove_pack(struct packet_type *pt)
434{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000435 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct packet_type *pt1;
437
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000438 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 list_for_each_entry(pt1, head, list) {
441 if (pt == pt1) {
442 list_del_rcu(&pt->list);
443 goto out;
444 }
445 }
446
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000447 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000449 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700451EXPORT_SYMBOL(__dev_remove_pack);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/**
454 * dev_remove_pack - remove packet handler
455 * @pt: packet type declaration
456 *
457 * Remove a protocol handler that was previously added to the kernel
458 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
459 * from the kernel lists and can be freed or reused once this function
460 * returns.
461 *
462 * This call sleeps to guarantee that no CPU is looking at the packet
463 * type after return.
464 */
465void dev_remove_pack(struct packet_type *pt)
466{
467 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 synchronize_net();
470}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700471EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Vlad Yasevich62532da2012-11-15 08:49:10 +0000473
474/**
475 * dev_add_offload - register offload handlers
476 * @po: protocol offload declaration
477 *
478 * Add protocol offload handlers to the networking stack. The passed
479 * &proto_offload is linked into kernel lists and may not be freed until
480 * it has been removed from the kernel lists.
481 *
482 * This call does not sleep therefore it can not
483 * guarantee all CPU's that are in middle of receiving packets
484 * will see the new offload handlers (until the next received packet).
485 */
486void dev_add_offload(struct packet_offload *po)
487{
David S. Millerbdef7de2015-06-01 14:56:09 -0700488 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000489
490 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700491 list_for_each_entry(elem, &offload_base, list) {
492 if (po->priority < elem->priority)
493 break;
494 }
495 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000496 spin_unlock(&offload_lock);
497}
498EXPORT_SYMBOL(dev_add_offload);
499
500/**
501 * __dev_remove_offload - remove offload handler
502 * @po: packet offload declaration
503 *
504 * Remove a protocol offload handler that was previously added to the
505 * kernel offload handlers by dev_add_offload(). The passed &offload_type
506 * is removed from the kernel lists and can be freed or reused once this
507 * function returns.
508 *
509 * The packet type might still be in use by receivers
510 * and must not be freed until after all the CPU's have gone
511 * through a quiescent state.
512 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800513static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000514{
515 struct list_head *head = &offload_base;
516 struct packet_offload *po1;
517
Eric Dumazetc53aa502012-11-16 08:08:23 +0000518 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000519
520 list_for_each_entry(po1, head, list) {
521 if (po == po1) {
522 list_del_rcu(&po->list);
523 goto out;
524 }
525 }
526
527 pr_warn("dev_remove_offload: %p not found\n", po);
528out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000529 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000530}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000531
532/**
533 * dev_remove_offload - remove packet offload handler
534 * @po: packet offload declaration
535 *
536 * Remove a packet offload handler that was previously added to the kernel
537 * offload handlers by dev_add_offload(). The passed &offload_type is
538 * removed from the kernel lists and can be freed or reused once this
539 * function returns.
540 *
541 * This call sleeps to guarantee that no CPU is looking at the packet
542 * type after return.
543 */
544void dev_remove_offload(struct packet_offload *po)
545{
546 __dev_remove_offload(po);
547
548 synchronize_net();
549}
550EXPORT_SYMBOL(dev_remove_offload);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100553 *
554 * Device Boot-time Settings Routines
555 *
556 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558/* Boot time configuration table */
559static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
560
561/**
562 * netdev_boot_setup_add - add new setup entry
563 * @name: name of the device
564 * @map: configured settings for the device
565 *
566 * Adds new setup entry to the dev_boot_setup list. The function
567 * returns 0 on error and 1 on success. This is a generic routine to
568 * all netdevices.
569 */
570static int netdev_boot_setup_add(char *name, struct ifmap *map)
571{
572 struct netdev_boot_setup *s;
573 int i;
574
575 s = dev_boot_setup;
576 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
577 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
578 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff92008-07-01 19:57:19 -0700579 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 memcpy(&s[i].map, map, sizeof(s[i].map));
581 break;
582 }
583 }
584
585 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
586}
587
588/**
tcharding722c9a02017-02-09 17:56:04 +1100589 * netdev_boot_setup_check - check boot time settings
590 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
tcharding722c9a02017-02-09 17:56:04 +1100592 * Check boot time settings for the device.
593 * The found settings are set for the device to be used
594 * later in the device probing.
595 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
597int netdev_boot_setup_check(struct net_device *dev)
598{
599 struct netdev_boot_setup *s = dev_boot_setup;
600 int i;
601
602 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
603 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff92008-07-01 19:57:19 -0700604 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100605 dev->irq = s[i].map.irq;
606 dev->base_addr = s[i].map.base_addr;
607 dev->mem_start = s[i].map.mem_start;
608 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 1;
610 }
611 }
612 return 0;
613}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700614EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616
617/**
tcharding722c9a02017-02-09 17:56:04 +1100618 * netdev_boot_base - get address from boot time settings
619 * @prefix: prefix for network device
620 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *
tcharding722c9a02017-02-09 17:56:04 +1100622 * Check boot time settings for the base address of device.
623 * The found settings are set for the device to be used
624 * later in the device probing.
625 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
627unsigned long netdev_boot_base(const char *prefix, int unit)
628{
629 const struct netdev_boot_setup *s = dev_boot_setup;
630 char name[IFNAMSIZ];
631 int i;
632
633 sprintf(name, "%s%d", prefix, unit);
634
635 /*
636 * If device already registered then return base of 1
637 * to indicate not to probe for this interface
638 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700639 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 1;
641
642 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
643 if (!strcmp(name, s[i].name))
644 return s[i].map.base_addr;
645 return 0;
646}
647
648/*
649 * Saves at boot time configured settings for any netdevice.
650 */
651int __init netdev_boot_setup(char *str)
652{
653 int ints[5];
654 struct ifmap map;
655
656 str = get_options(str, ARRAY_SIZE(ints), ints);
657 if (!str || !*str)
658 return 0;
659
660 /* Save settings */
661 memset(&map, 0, sizeof(map));
662 if (ints[0] > 0)
663 map.irq = ints[1];
664 if (ints[0] > 1)
665 map.base_addr = ints[2];
666 if (ints[0] > 2)
667 map.mem_start = ints[3];
668 if (ints[0] > 3)
669 map.mem_end = ints[4];
670
671 /* Add new entry to the list */
672 return netdev_boot_setup_add(str, &map);
673}
674
675__setup("netdev=", netdev_boot_setup);
676
677/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100678 *
679 * Device Interface Subroutines
680 *
681 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200684 * dev_get_iflink - get 'iflink' value of a interface
685 * @dev: targeted interface
686 *
687 * Indicates the ifindex the interface is linked to.
688 * Physical interfaces have the same 'ifindex' and 'iflink' values.
689 */
690
691int dev_get_iflink(const struct net_device *dev)
692{
693 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
694 return dev->netdev_ops->ndo_get_iflink(dev);
695
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200696 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200697}
698EXPORT_SYMBOL(dev_get_iflink);
699
700/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700701 * dev_fill_metadata_dst - Retrieve tunnel egress information.
702 * @dev: targeted interface
703 * @skb: The packet.
704 *
705 * For better visibility of tunnel traffic OVS needs to retrieve
706 * egress tunnel information for a packet. Following API allows
707 * user to get this info.
708 */
709int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
710{
711 struct ip_tunnel_info *info;
712
713 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
714 return -EINVAL;
715
716 info = skb_tunnel_info_unclone(skb);
717 if (!info)
718 return -ENOMEM;
719 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
720 return -EINVAL;
721
722 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
723}
724EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
725
726/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700728 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * @name: name to find
730 *
731 * Find an interface by name. Must be called under RTNL semaphore
732 * or @dev_base_lock. If the name is found a pointer to the device
733 * is returned. If the name is not found then %NULL is returned. The
734 * reference counters are not incremented so the caller must be
735 * careful with locks.
736 */
737
Eric W. Biederman881d9662007-09-17 11:56:21 -0700738struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700740 struct net_device *dev;
741 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Sasha Levinb67bfe02013-02-27 17:06:00 -0800743 hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!strncmp(dev->name, name, IFNAMSIZ))
745 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return NULL;
748}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700749EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751/**
tcharding722c9a02017-02-09 17:56:04 +1100752 * dev_get_by_name_rcu - find a device by its name
753 * @net: the applicable net namespace
754 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000755 *
tcharding722c9a02017-02-09 17:56:04 +1100756 * Find an interface by name.
757 * If the name is found a pointer to the device is returned.
758 * If the name is not found then %NULL is returned.
759 * The reference counters are not incremented so the caller must be
760 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000761 */
762
763struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
764{
Eric Dumazet72c95282009-10-30 07:11:27 +0000765 struct net_device *dev;
766 struct hlist_head *head = dev_name_hash(net, name);
767
Sasha Levinb67bfe02013-02-27 17:06:00 -0800768 hlist_for_each_entry_rcu(dev, head, name_hlist)
Eric Dumazet72c95282009-10-30 07:11:27 +0000769 if (!strncmp(dev->name, name, IFNAMSIZ))
770 return dev;
771
772 return NULL;
773}
774EXPORT_SYMBOL(dev_get_by_name_rcu);
775
776/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700778 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * @name: name to find
780 *
781 * Find an interface by name. This can be called from any
782 * context and does its own locking. The returned handle has
783 * the usage count incremented and the caller must use dev_put() to
784 * release it when it is no longer needed. %NULL is returned if no
785 * matching device is found.
786 */
787
Eric W. Biederman881d9662007-09-17 11:56:21 -0700788struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct net_device *dev;
791
Eric Dumazet72c95282009-10-30 07:11:27 +0000792 rcu_read_lock();
793 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (dev)
795 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000796 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return dev;
798}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700799EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801/**
802 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700803 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * @ifindex: index of device
805 *
806 * Search for an interface by index. Returns %NULL if the device
807 * is not found or a pointer to the device. The device has not
808 * had its reference counter increased so the caller must be careful
809 * about locking. The caller must hold either the RTNL semaphore
810 * or @dev_base_lock.
811 */
812
Eric W. Biederman881d9662007-09-17 11:56:21 -0700813struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700815 struct net_device *dev;
816 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Sasha Levinb67bfe02013-02-27 17:06:00 -0800818 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (dev->ifindex == ifindex)
820 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return NULL;
823}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700824EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000826/**
827 * dev_get_by_index_rcu - find a device by its ifindex
828 * @net: the applicable net namespace
829 * @ifindex: index of device
830 *
831 * Search for an interface by index. Returns %NULL if the device
832 * is not found or a pointer to the device. The device has not
833 * had its reference counter increased so the caller must be careful
834 * about locking. The caller must hold RCU lock.
835 */
836
837struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
838{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000839 struct net_device *dev;
840 struct hlist_head *head = dev_index_hash(net, ifindex);
841
Sasha Levinb67bfe02013-02-27 17:06:00 -0800842 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000843 if (dev->ifindex == ifindex)
844 return dev;
845
846 return NULL;
847}
848EXPORT_SYMBOL(dev_get_by_index_rcu);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851/**
852 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700853 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * @ifindex: index of device
855 *
856 * Search for an interface by index. Returns NULL if the device
857 * is not found or a pointer to the device. The device returned has
858 * had a reference added and the pointer is safe until the user calls
859 * dev_put to indicate they have finished with it.
860 */
861
Eric W. Biederman881d9662007-09-17 11:56:21 -0700862struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct net_device *dev;
865
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000866 rcu_read_lock();
867 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (dev)
869 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000870 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return dev;
872}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700873EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200876 * dev_get_by_napi_id - find a device by napi_id
877 * @napi_id: ID of the NAPI struct
878 *
879 * Search for an interface by NAPI ID. Returns %NULL if the device
880 * is not found or a pointer to the device. The device has not had
881 * its reference counter increased so the caller must be careful
882 * about locking. The caller must hold RCU lock.
883 */
884
885struct net_device *dev_get_by_napi_id(unsigned int napi_id)
886{
887 struct napi_struct *napi;
888
889 WARN_ON_ONCE(!rcu_read_lock_held());
890
891 if (napi_id < MIN_NAPI_ID)
892 return NULL;
893
894 napi = napi_by_id(napi_id);
895
896 return napi ? napi->dev : NULL;
897}
898EXPORT_SYMBOL(dev_get_by_napi_id);
899
900/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200901 * netdev_get_name - get a netdevice name, knowing its ifindex.
902 * @net: network namespace
903 * @name: a pointer to the buffer where the name will be stored.
904 * @ifindex: the ifindex of the interface to get the name from.
905 *
906 * The use of raw_seqcount_begin() and cond_resched() before
907 * retrying is required as we want to give the writers a chance
908 * to complete when CONFIG_PREEMPT is not set.
909 */
910int netdev_get_name(struct net *net, char *name, int ifindex)
911{
912 struct net_device *dev;
913 unsigned int seq;
914
915retry:
916 seq = raw_seqcount_begin(&devnet_rename_seq);
917 rcu_read_lock();
918 dev = dev_get_by_index_rcu(net, ifindex);
919 if (!dev) {
920 rcu_read_unlock();
921 return -ENODEV;
922 }
923
924 strcpy(name, dev->name);
925 rcu_read_unlock();
926 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
927 cond_resched();
928 goto retry;
929 }
930
931 return 0;
932}
933
934/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000935 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700936 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * @type: media type of device
938 * @ha: hardware address
939 *
940 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800941 * is not found or a pointer to the device.
942 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000943 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * and the caller must therefore be careful about locking
945 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
947
Eric Dumazet941666c2010-12-05 01:23:53 +0000948struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
949 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 struct net_device *dev;
952
Eric Dumazet941666c2010-12-05 01:23:53 +0000953 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (dev->type == type &&
955 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700956 return dev;
957
958 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Eric Dumazet941666c2010-12-05 01:23:53 +0000960EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300961
Eric W. Biederman881d9662007-09-17 11:56:21 -0700962struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700963{
964 struct net_device *dev;
965
966 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700967 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700968 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700969 return dev;
970
971 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700972}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700973EXPORT_SYMBOL(__dev_getfirstbyhwtype);
974
Eric W. Biederman881d9662007-09-17 11:56:21 -0700975struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000977 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000979 rcu_read_lock();
980 for_each_netdev_rcu(net, dev)
981 if (dev->type == type) {
982 dev_hold(dev);
983 ret = dev;
984 break;
985 }
986 rcu_read_unlock();
987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989EXPORT_SYMBOL(dev_getfirstbyhwtype);
990
991/**
WANG Cong6c555492014-09-11 15:35:09 -0700992 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700993 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 * @if_flags: IFF_* values
995 * @mask: bitmask of bits in if_flags to check
996 *
997 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000998 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -0700999 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
1001
WANG Cong6c555492014-09-11 15:35:09 -07001002struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1003 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001005 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
WANG Cong6c555492014-09-11 15:35:09 -07001007 ASSERT_RTNL();
1008
Pavel Emelianov7562f872007-05-03 15:13:45 -07001009 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001010 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001012 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 }
1015 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
WANG Cong6c555492014-09-11 15:35:09 -07001018EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020/**
1021 * dev_valid_name - check if name is okay for network device
1022 * @name: name string
1023 *
1024 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -07001025 * to allow sysfs to work. We also disallow any kind of
1026 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
David S. Miller95f050b2012-03-06 16:12:15 -05001028bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001030 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001031 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001032 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001033 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001034 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001035 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001036
1037 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001038 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001039 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001040 name++;
1041 }
David S. Miller95f050b2012-03-06 16:12:15 -05001042 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001044EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001047 * __dev_alloc_name - allocate a name for a device
1048 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001050 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
1052 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001053 * id. It scans list of devices to build up a free map, then chooses
1054 * the first empty slot. The caller must hold the dev_base or rtnl lock
1055 * while allocating the name and adding the device in order to avoid
1056 * duplicates.
1057 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1058 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
1060
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001061static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 const char *p;
1065 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001066 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct net_device *d;
1068
Rasmus Villemoes93809102017-11-13 00:15:08 +01001069 if (!dev_valid_name(name))
1070 return -EINVAL;
1071
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001072 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (p) {
1074 /*
1075 * Verify the string as this thing may have come from
1076 * the user. There must be either one "%d" and no other "%"
1077 * characters.
1078 */
1079 if (p[1] != 'd' || strchr(p + 2, '%'))
1080 return -EINVAL;
1081
1082 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001083 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!inuse)
1085 return -ENOMEM;
1086
Eric W. Biederman881d9662007-09-17 11:56:21 -07001087 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (!sscanf(d->name, name, &i))
1089 continue;
1090 if (i < 0 || i >= max_netdevices)
1091 continue;
1092
1093 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001094 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!strncmp(buf, d->name, IFNAMSIZ))
1096 set_bit(i, inuse);
1097 }
1098
1099 i = find_first_zero_bit(inuse, max_netdevices);
1100 free_page((unsigned long) inuse);
1101 }
1102
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001103 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001104 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /* It is possible to run out of possible slots
1108 * when the name is long and there isn't enough space left
1109 * for the digits, or if all bits are used.
1110 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001111 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001114static int dev_alloc_name_ns(struct net *net,
1115 struct net_device *dev,
1116 const char *name)
1117{
1118 char buf[IFNAMSIZ];
1119 int ret;
1120
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001121 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001122 ret = __dev_alloc_name(net, name, buf);
1123 if (ret >= 0)
1124 strlcpy(dev->name, buf, IFNAMSIZ);
1125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001128/**
1129 * dev_alloc_name - allocate a name for a device
1130 * @dev: device
1131 * @name: name format string
1132 *
1133 * Passed a format string - eg "lt%d" it will try and find a suitable
1134 * id. It scans list of devices to build up a free map, then chooses
1135 * the first empty slot. The caller must hold the dev_base or rtnl lock
1136 * while allocating the name and adding the device in order to avoid
1137 * duplicates.
1138 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1139 * Returns the number of the unit assigned or a negative errno code.
1140 */
1141
1142int dev_alloc_name(struct net_device *dev, const char *name)
1143{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001144 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001145}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001146EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001147
Cong Wang0ad646c2017-10-13 11:58:53 -07001148int dev_get_valid_name(struct net *net, struct net_device *dev,
1149 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001150{
David S. Miller55a5ec92018-01-02 11:45:07 -05001151 BUG_ON(!net);
1152
1153 if (!dev_valid_name(name))
1154 return -EINVAL;
1155
1156 if (strchr(name, '%'))
1157 return dev_alloc_name_ns(net, dev, name);
1158 else if (__dev_get_by_name(net, name))
1159 return -EEXIST;
1160 else if (dev->name != name)
1161 strlcpy(dev->name, name, IFNAMSIZ);
1162
1163 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001164}
Cong Wang0ad646c2017-10-13 11:58:53 -07001165EXPORT_SYMBOL(dev_get_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167/**
1168 * dev_change_name - change name of a device
1169 * @dev: device
1170 * @newname: name (or format string) must be at least IFNAMSIZ
1171 *
1172 * Change name of a device, can pass format strings "eth%d".
1173 * for wildcarding.
1174 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001175int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Tom Gundersen238fa362014-07-14 16:37:23 +02001177 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001178 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001180 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001181 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001184 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001186 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (dev->flags & IFF_UP)
1188 return -EBUSY;
1189
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001190 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001191
1192 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001193 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001194 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001195 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001196
Herbert Xufcc5a032007-07-30 17:03:38 -07001197 memcpy(oldname, dev->name, IFNAMSIZ);
1198
Gao feng828de4f2012-09-13 20:58:27 +00001199 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001200 if (err < 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001201 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001202 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001205 if (oldname[0] && !strchr(oldname, '%'))
1206 netdev_info(dev, "renamed from %s\n", oldname);
1207
Tom Gundersen238fa362014-07-14 16:37:23 +02001208 old_assign_type = dev->name_assign_type;
1209 dev->name_assign_type = NET_NAME_RENAMED;
1210
Herbert Xufcc5a032007-07-30 17:03:38 -07001211rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001212 ret = device_rename(&dev->dev, dev->name);
1213 if (ret) {
1214 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001215 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001216 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001217 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001218 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001219
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001220 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001221
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001222 netdev_adjacent_rename_links(dev, oldname);
1223
Herbert Xu7f988ea2007-07-30 16:35:46 -07001224 write_lock_bh(&dev_base_lock);
Eric Dumazet372b2312011-05-17 13:56:59 -04001225 hlist_del_rcu(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001226 write_unlock_bh(&dev_base_lock);
1227
1228 synchronize_rcu();
1229
1230 write_lock_bh(&dev_base_lock);
1231 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001232 write_unlock_bh(&dev_base_lock);
1233
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001234 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001235 ret = notifier_to_errno(ret);
1236
1237 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001238 /* err >= 0 after dev_alloc_name() or stores the first errno */
1239 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001240 err = ret;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001241 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001242 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001243 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001244 dev->name_assign_type = old_assign_type;
1245 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001246 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001247 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001248 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001249 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001250 }
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 return err;
1254}
1255
1256/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001257 * dev_set_alias - change ifalias of a device
1258 * @dev: device
1259 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001260 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001261 *
1262 * Set ifalias for a device,
1263 */
1264int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1265{
Florian Westphal6c557002017-10-02 23:50:05 +02001266 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001267
1268 if (len >= IFALIASZ)
1269 return -EINVAL;
1270
Florian Westphal6c557002017-10-02 23:50:05 +02001271 if (len) {
1272 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1273 if (!new_alias)
1274 return -ENOMEM;
1275
1276 memcpy(new_alias->ifalias, alias, len);
1277 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001278 }
1279
Florian Westphal6c557002017-10-02 23:50:05 +02001280 mutex_lock(&ifalias_mutex);
1281 rcu_swap_protected(dev->ifalias, new_alias,
1282 mutex_is_locked(&ifalias_mutex));
1283 mutex_unlock(&ifalias_mutex);
1284
1285 if (new_alias)
1286 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001287
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001288 return len;
1289}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001290EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001291
Florian Westphal6c557002017-10-02 23:50:05 +02001292/**
1293 * dev_get_alias - get ifalias of a device
1294 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001295 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001296 * @len: size of buffer
1297 *
1298 * get ifalias for a device. Caller must make sure dev cannot go
1299 * away, e.g. rcu read lock or own a reference count to device.
1300 */
1301int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1302{
1303 const struct dev_ifalias *alias;
1304 int ret = 0;
1305
1306 rcu_read_lock();
1307 alias = rcu_dereference(dev->ifalias);
1308 if (alias)
1309 ret = snprintf(name, len, "%s", alias->ifalias);
1310 rcu_read_unlock();
1311
1312 return ret;
1313}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001314
1315/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001316 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001317 * @dev: device to cause notification
1318 *
1319 * Called to indicate a device has changed features.
1320 */
1321void netdev_features_change(struct net_device *dev)
1322{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001323 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001324}
1325EXPORT_SYMBOL(netdev_features_change);
1326
1327/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * netdev_state_change - device changes state
1329 * @dev: device to cause notification
1330 *
1331 * Called to indicate a device has changed state. This function calls
1332 * the notifier chains for netdev_chain and sends a NEWLINK message
1333 * to the routing socket.
1334 */
1335void netdev_state_change(struct net_device *dev)
1336{
1337 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001338 struct netdev_notifier_change_info change_info = {
1339 .info.dev = dev,
1340 };
Loic Prylli54951192014-07-01 21:39:43 -07001341
David Ahern51d0c0472017-10-04 17:48:45 -07001342 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001343 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001344 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001347EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Amerigo Wangee89bab2012-08-09 22:14:56 +00001349/**
tcharding722c9a02017-02-09 17:56:04 +11001350 * netdev_notify_peers - notify network peers about existence of @dev
1351 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001352 *
1353 * Generate traffic such that interested network peers are aware of
1354 * @dev, such as by generating a gratuitous ARP. This may be used when
1355 * a device wants to inform the rest of the network about some sort of
1356 * reconfiguration such as a failover event or virtual machine
1357 * migration.
1358 */
1359void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001360{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001361 rtnl_lock();
1362 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001363 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001364 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001365}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001366EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001367
Petr Machata40c900a2018-12-06 17:05:47 +00001368static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001370 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001371 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001373 ASSERT_RTNL();
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!netif_device_present(dev))
1376 return -ENODEV;
1377
Neil Hormanca99ca12013-02-05 08:05:43 +00001378 /* Block netpoll from trying to do any rx path servicing.
1379 * If we don't do this there is a chance ndo_poll_controller
1380 * or ndo_poll may be running while we open the device
1381 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001382 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001383
Petr Machata40c900a2018-12-06 17:05:47 +00001384 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001385 ret = notifier_to_errno(ret);
1386 if (ret)
1387 return ret;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001390
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001391 if (ops->ndo_validate_addr)
1392 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001393
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001394 if (!ret && ops->ndo_open)
1395 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Eric W. Biederman66b55522014-03-27 15:39:03 -07001397 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001398
Jeff Garzikbada3392007-10-23 20:19:37 -07001399 if (ret)
1400 clear_bit(__LINK_STATE_START, &dev->state);
1401 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001403 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001405 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return ret;
1409}
Patrick McHardybd380812010-02-26 06:34:53 +00001410
1411/**
1412 * dev_open - prepare an interface for use.
Petr Machata00f54e62018-12-06 17:05:36 +00001413 * @dev: device to open
1414 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00001415 *
1416 * Takes a device from down to up state. The device's private open
1417 * function is invoked and then the multicast lists are loaded. Finally
1418 * the device is moved into the up state and a %NETDEV_UP message is
1419 * sent to the netdev notifier chain.
1420 *
1421 * Calling this function on an active interface is a nop. On a failure
1422 * a negative errno code is returned.
1423 */
Petr Machata00f54e62018-12-06 17:05:36 +00001424int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00001425{
1426 int ret;
1427
Patrick McHardybd380812010-02-26 06:34:53 +00001428 if (dev->flags & IFF_UP)
1429 return 0;
1430
Petr Machata40c900a2018-12-06 17:05:47 +00001431 ret = __dev_open(dev, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00001432 if (ret < 0)
1433 return ret;
1434
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001435 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001436 call_netdevice_notifiers(NETDEV_UP, dev);
1437
1438 return ret;
1439}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001440EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
stephen hemminger7051b882017-07-18 15:59:27 -07001442static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Octavian Purdila44345722010-12-13 12:44:07 +00001444 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001445
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001446 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001447 might_sleep();
1448
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001449 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001450 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001451 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001452
Octavian Purdila44345722010-12-13 12:44:07 +00001453 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Octavian Purdila44345722010-12-13 12:44:07 +00001455 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Octavian Purdila44345722010-12-13 12:44:07 +00001457 /* Synchronize to scheduled poll. We cannot touch poll list, it
1458 * can be even on different cpu. So just clear netif_running().
1459 *
1460 * dev->stop() will invoke napi_disable() on all of it's
1461 * napi_struct instances on this device.
1462 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001463 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Octavian Purdila44345722010-12-13 12:44:07 +00001466 dev_deactivate_many(head);
1467
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001468 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001469 const struct net_device_ops *ops = dev->netdev_ops;
1470
1471 /*
1472 * Call the device specific close. This cannot fail.
1473 * Only if device is UP
1474 *
1475 * We allow it to be called even after a DETACH hot-plug
1476 * event.
1477 */
1478 if (ops->ndo_stop)
1479 ops->ndo_stop(dev);
1480
Octavian Purdila44345722010-12-13 12:44:07 +00001481 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001482 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001483 }
Octavian Purdila44345722010-12-13 12:44:07 +00001484}
1485
stephen hemminger7051b882017-07-18 15:59:27 -07001486static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001487{
1488 LIST_HEAD(single);
1489
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001490 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001491 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001492 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001493}
1494
stephen hemminger7051b882017-07-18 15:59:27 -07001495void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001496{
1497 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001498
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001499 /* Remove the devices that don't need to be closed */
1500 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001501 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001502 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001503
1504 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001505
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001506 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001507 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001508 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001509 if (unlink)
1510 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
David S. Miller99c4a262015-03-18 22:52:33 -04001513EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001514
1515/**
1516 * dev_close - shutdown an interface.
1517 * @dev: device to shutdown
1518 *
1519 * This function moves an active device into down state. A
1520 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1521 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1522 * chain.
1523 */
stephen hemminger7051b882017-07-18 15:59:27 -07001524void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001525{
Eric Dumazete14a5992011-05-10 12:26:06 -07001526 if (dev->flags & IFF_UP) {
1527 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001528
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001529 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001530 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001531 list_del(&single);
1532 }
Patrick McHardybd380812010-02-26 06:34:53 +00001533}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001534EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001537/**
1538 * dev_disable_lro - disable Large Receive Offload on a device
1539 * @dev: device
1540 *
1541 * Disable Large Receive Offload (LRO) on a net device. Must be
1542 * called under RTNL. This is needed if received packets may be
1543 * forwarded to another interface.
1544 */
1545void dev_disable_lro(struct net_device *dev)
1546{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001547 struct net_device *lower_dev;
1548 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001549
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001550 dev->wanted_features &= ~NETIF_F_LRO;
1551 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001552
Michał Mirosław22d59692011-04-21 12:42:15 +00001553 if (unlikely(dev->features & NETIF_F_LRO))
1554 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001555
1556 netdev_for_each_lower_dev(dev, lower_dev, iter)
1557 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001558}
1559EXPORT_SYMBOL(dev_disable_lro);
1560
Michael Chan56f5aa72017-12-16 03:09:41 -05001561/**
1562 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1563 * @dev: device
1564 *
1565 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1566 * called under RTNL. This is needed if Generic XDP is installed on
1567 * the device.
1568 */
1569static void dev_disable_gro_hw(struct net_device *dev)
1570{
1571 dev->wanted_features &= ~NETIF_F_GRO_HW;
1572 netdev_update_features(dev);
1573
1574 if (unlikely(dev->features & NETIF_F_GRO_HW))
1575 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1576}
1577
Kirill Tkhaiede27622018-03-23 19:47:19 +03001578const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1579{
1580#define N(val) \
1581 case NETDEV_##val: \
1582 return "NETDEV_" __stringify(val);
1583 switch (cmd) {
1584 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1585 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1586 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1587 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1588 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1589 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1590 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001591 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1592 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Petr Machata15704152018-12-13 11:54:33 +00001593 N(PRE_CHANGEADDR)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001594 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001595#undef N
1596 return "UNKNOWN_NETDEV_EVENT";
1597}
1598EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1599
Jiri Pirko351638e2013-05-28 01:30:21 +00001600static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1601 struct net_device *dev)
1602{
David Ahern51d0c0472017-10-04 17:48:45 -07001603 struct netdev_notifier_info info = {
1604 .dev = dev,
1605 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001606
Jiri Pirko351638e2013-05-28 01:30:21 +00001607 return nb->notifier_call(nb, val, &info);
1608}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001609
Eric W. Biederman881d9662007-09-17 11:56:21 -07001610static int dev_boot_phase = 1;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/**
tcharding722c9a02017-02-09 17:56:04 +11001613 * register_netdevice_notifier - register a network notifier block
1614 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *
tcharding722c9a02017-02-09 17:56:04 +11001616 * Register a notifier to be called when network device events occur.
1617 * The notifier passed is linked into the kernel structures and must
1618 * not be reused until it has been unregistered. A negative errno code
1619 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 *
tcharding722c9a02017-02-09 17:56:04 +11001621 * When registered all registration and up events are replayed
1622 * to the new notifier to allow device to have a race free
1623 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
1625
1626int register_netdevice_notifier(struct notifier_block *nb)
1627{
1628 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001629 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001630 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int err;
1632
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001633 /* Close race with setup_net() and cleanup_net() */
1634 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001636 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001637 if (err)
1638 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001639 if (dev_boot_phase)
1640 goto unlock;
1641 for_each_net(net) {
1642 for_each_netdev(net, dev) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001643 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001644 err = notifier_to_errno(err);
1645 if (err)
1646 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Eric W. Biederman881d9662007-09-17 11:56:21 -07001648 if (!(dev->flags & IFF_UP))
1649 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001650
Jiri Pirko351638e2013-05-28 01:30:21 +00001651 call_netdevice_notifier(nb, NETDEV_UP, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001654
1655unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001657 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001659
1660rollback:
1661 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001662 for_each_net(net) {
1663 for_each_netdev(net, dev) {
1664 if (dev == last)
RongQing.Li8f891482011-11-30 23:43:07 -05001665 goto outroll;
Herbert Xufcc5a032007-07-30 17:03:38 -07001666
Eric W. Biederman881d9662007-09-17 11:56:21 -07001667 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001668 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1669 dev);
1670 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001671 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001672 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001673 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001674 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001675
RongQing.Li8f891482011-11-30 23:43:07 -05001676outroll:
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001677 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001678 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001680EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/**
tcharding722c9a02017-02-09 17:56:04 +11001683 * unregister_netdevice_notifier - unregister a network notifier block
1684 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
tcharding722c9a02017-02-09 17:56:04 +11001686 * Unregister a notifier previously registered by
1687 * register_netdevice_notifier(). The notifier is unlinked into the
1688 * kernel structures and may then be reused. A negative errno code
1689 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001690 *
tcharding722c9a02017-02-09 17:56:04 +11001691 * After unregistering unregister and down device events are synthesized
1692 * for all devices on the device list to the removed notifier to remove
1693 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
1695
1696int unregister_netdevice_notifier(struct notifier_block *nb)
1697{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001698 struct net_device *dev;
1699 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001700 int err;
1701
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001702 /* Close race with setup_net() and cleanup_net() */
1703 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001704 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001705 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001706 if (err)
1707 goto unlock;
1708
1709 for_each_net(net) {
1710 for_each_netdev(net, dev) {
1711 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001712 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1713 dev);
1714 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001715 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001716 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001717 }
1718 }
1719unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001720 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001721 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001722 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001724EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001727 * call_netdevice_notifiers_info - call all network notifier blocks
1728 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00001729 * @info: notifier information data
1730 *
1731 * Call all network notifier blocks. Parameters and return value
1732 * are as for raw_notifier_call_chain().
1733 */
1734
stephen hemminger1d143d92013-12-29 14:01:29 -08001735static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001736 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001737{
1738 ASSERT_RTNL();
Jiri Pirko351638e2013-05-28 01:30:21 +00001739 return raw_notifier_call_chain(&netdev_chain, val, info);
1740}
Jiri Pirko351638e2013-05-28 01:30:21 +00001741
Petr Machata26372602018-12-06 17:05:45 +00001742static int call_netdevice_notifiers_extack(unsigned long val,
1743 struct net_device *dev,
1744 struct netlink_ext_ack *extack)
1745{
1746 struct netdev_notifier_info info = {
1747 .dev = dev,
1748 .extack = extack,
1749 };
1750
1751 return call_netdevice_notifiers_info(val, &info);
1752}
1753
Jiri Pirko351638e2013-05-28 01:30:21 +00001754/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * call_netdevice_notifiers - call all network notifier blocks
1756 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001757 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 *
1759 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001760 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
1762
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001763int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Petr Machata26372602018-12-06 17:05:45 +00001765 return call_netdevice_notifiers_extack(val, dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001767EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001769/**
1770 * call_netdevice_notifiers_mtu - call all network notifier blocks
1771 * @val: value passed unmodified to notifier function
1772 * @dev: net_device pointer passed unmodified to notifier function
1773 * @arg: additional u32 argument passed to the notifier function
1774 *
1775 * Call all network notifier blocks. Parameters and return value
1776 * are as for raw_notifier_call_chain().
1777 */
1778static int call_netdevice_notifiers_mtu(unsigned long val,
1779 struct net_device *dev, u32 arg)
1780{
1781 struct netdev_notifier_info_ext info = {
1782 .info.dev = dev,
1783 .ext.mtu = arg,
1784 };
1785
1786 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1787
1788 return call_netdevice_notifiers_info(val, &info.info);
1789}
1790
Pablo Neira1cf519002015-05-13 18:19:37 +02001791#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001792static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001793
1794void net_inc_ingress_queue(void)
1795{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001796 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001797}
1798EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1799
1800void net_dec_ingress_queue(void)
1801{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001802 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001803}
1804EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1805#endif
1806
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001807#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001808static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001809
1810void net_inc_egress_queue(void)
1811{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001812 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001813}
1814EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1815
1816void net_dec_egress_queue(void)
1817{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001818 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001819}
1820EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1821#endif
1822
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001823static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001824#ifdef CONFIG_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00001825static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08001826static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001827static void netstamp_clear(struct work_struct *work)
1828{
1829 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08001830 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001831
Eric Dumazet13baa002017-03-01 14:28:39 -08001832 wanted = atomic_add_return(deferred, &netstamp_wanted);
1833 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001834 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08001835 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001836 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001837}
1838static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841void net_enable_timestamp(void)
1842{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001843#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08001844 int wanted;
1845
1846 while (1) {
1847 wanted = atomic_read(&netstamp_wanted);
1848 if (wanted <= 0)
1849 break;
1850 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1851 return;
1852 }
1853 atomic_inc(&netstamp_needed_deferred);
1854 schedule_work(&netstamp_work);
1855#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001856 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08001857#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001859EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861void net_disable_timestamp(void)
1862{
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001863#ifdef CONFIG_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08001864 int wanted;
1865
1866 while (1) {
1867 wanted = atomic_read(&netstamp_wanted);
1868 if (wanted <= 1)
1869 break;
1870 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1871 return;
1872 }
1873 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001874 schedule_work(&netstamp_work);
1875#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001876 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001879EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Eric Dumazet3b098e22010-05-15 23:57:10 -07001881static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Thomas Gleixner2456e852016-12-25 11:38:40 +01001883 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001884 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001885 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001888#define net_timestamp_check(COND, SKB) \
1889 if (static_branch_unlikely(&netstamp_needed_key)) { \
1890 if ((COND) && !(SKB)->tstamp) \
1891 __net_timestamp(SKB); \
1892 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07001893
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02001894bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001895{
1896 unsigned int len;
1897
1898 if (!(dev->flags & IFF_UP))
1899 return false;
1900
1901 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1902 if (skb->len <= len)
1903 return true;
1904
1905 /* if TSO is enabled, we don't care about the length as the packet
1906 * could be forwarded without being segmented before
1907 */
1908 if (skb_is_gso(skb))
1909 return true;
1910
1911 return false;
1912}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04001913EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001914
Herbert Xua0265d22014-04-17 13:45:03 +08001915int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1916{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001917 int ret = ____dev_forward_skb(dev, skb);
1918
1919 if (likely(!ret)) {
1920 skb->protocol = eth_type_trans(skb, dev);
1921 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08001922 }
1923
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001924 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08001925}
1926EXPORT_SYMBOL_GPL(__dev_forward_skb);
1927
Arnd Bergmann44540962009-11-26 06:07:08 +00001928/**
1929 * dev_forward_skb - loopback an skb to another netif
1930 *
1931 * @dev: destination network device
1932 * @skb: buffer to forward
1933 *
1934 * return values:
1935 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001936 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001937 *
1938 * dev_forward_skb can be used for injecting an skb from the
1939 * start_xmit function of one device into the receive queue
1940 * of another device.
1941 *
1942 * The receiving device may be in another namespace, so
1943 * we have to clear all information in the skb that could
1944 * impact namespace isolation.
1945 */
1946int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1947{
Herbert Xua0265d22014-04-17 13:45:03 +08001948 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001949}
1950EXPORT_SYMBOL_GPL(dev_forward_skb);
1951
Changli Gao71d9dec2010-12-15 19:57:25 +00001952static inline int deliver_skb(struct sk_buff *skb,
1953 struct packet_type *pt_prev,
1954 struct net_device *orig_dev)
1955{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001956 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00001957 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03001958 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00001959 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1960}
1961
Salam Noureddine7866a622015-01-27 11:35:48 -08001962static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1963 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001964 struct net_device *orig_dev,
1965 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08001966 struct list_head *ptype_list)
1967{
1968 struct packet_type *ptype, *pt_prev = *pt;
1969
1970 list_for_each_entry_rcu(ptype, ptype_list, list) {
1971 if (ptype->type != type)
1972 continue;
1973 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001974 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08001975 pt_prev = ptype;
1976 }
1977 *pt = pt_prev;
1978}
1979
Eric Leblondc0de08d2012-08-16 22:02:58 +00001980static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1981{
Eric Leblonda3d744e2012-11-06 02:10:10 +00001982 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001983 return false;
1984
1985 if (ptype->id_match)
1986 return ptype->id_match(ptype, skb->sk);
1987 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1988 return true;
1989
1990 return false;
1991}
1992
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01001993/**
1994 * dev_nit_active - return true if any network interface taps are in use
1995 *
1996 * @dev: network device to check for the presence of taps
1997 */
1998bool dev_nit_active(struct net_device *dev)
1999{
2000 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2001}
2002EXPORT_SYMBOL_GPL(dev_nit_active);
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/*
2005 * Support routine. Sends outgoing frames to any network
2006 * taps currently in use.
2007 */
2008
David Ahern74b20582016-05-10 11:19:50 -07002009void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00002012 struct sk_buff *skb2 = NULL;
2013 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002014 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002017again:
2018 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002019 if (ptype->ignore_outgoing)
2020 continue;
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 /* Never send packets back to the socket
2023 * they originated from - MvS (miquels@drinkel.ow.org)
2024 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002025 if (skb_loop_sk(ptype, skb))
2026 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002027
Salam Noureddine7866a622015-01-27 11:35:48 -08002028 if (pt_prev) {
2029 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002030 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002031 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002033
2034 /* need to clone skb, done only once */
2035 skb2 = skb_clone(skb, GFP_ATOMIC);
2036 if (!skb2)
2037 goto out_unlock;
2038
2039 net_timestamp_set(skb2);
2040
2041 /* skb->nh should be correctly
2042 * set by sender, so that the second statement is
2043 * just protection against buggy protocols.
2044 */
2045 skb_reset_mac_header(skb2);
2046
2047 if (skb_network_header(skb2) < skb2->data ||
2048 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2049 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2050 ntohs(skb2->protocol),
2051 dev->name);
2052 skb_reset_network_header(skb2);
2053 }
2054
2055 skb2->transport_header = skb2->network_header;
2056 skb2->pkt_type = PACKET_OUTGOING;
2057 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002059
2060 if (ptype_list == &ptype_all) {
2061 ptype_list = &dev->ptype_all;
2062 goto again;
2063 }
2064out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002065 if (pt_prev) {
2066 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2067 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2068 else
2069 kfree_skb(skb2);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 rcu_read_unlock();
2072}
David Ahern74b20582016-05-10 11:19:50 -07002073EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Ben Hutchings2c530402012-07-10 10:55:09 +00002075/**
2076 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002077 * @dev: Network device
2078 * @txq: number of queues available
2079 *
2080 * If real_num_tx_queues is changed the tc mappings may no longer be
2081 * valid. To resolve this verify the tc mapping remains valid and if
2082 * not NULL the mapping. With no priorities mapping to this
2083 * offset/count pair it will no longer be used. In the worst case TC0
2084 * is invalid nothing can be done so disable priority mappings. If is
2085 * expected that drivers will fix this mapping if they can before
2086 * calling netif_set_real_num_tx_queues.
2087 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002088static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002089{
2090 int i;
2091 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2092
2093 /* If TC0 is invalidated disable TC mapping */
2094 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002095 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002096 dev->num_tc = 0;
2097 return;
2098 }
2099
2100 /* Invalidated prio to tc mappings set to TC0 */
2101 for (i = 1; i < TC_BITMASK + 1; i++) {
2102 int q = netdev_get_prio_tc_map(dev, i);
2103
2104 tc = &dev->tc_to_txq[q];
2105 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002106 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2107 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002108 netdev_set_prio_tc_map(dev, i, 0);
2109 }
2110 }
2111}
2112
Alexander Duyck8d059b02016-10-28 11:43:49 -04002113int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2114{
2115 if (dev->num_tc) {
2116 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2117 int i;
2118
Alexander Duyckffcfe252018-07-09 12:19:38 -04002119 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002120 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2121 if ((txq - tc->offset) < tc->count)
2122 return i;
2123 }
2124
Alexander Duyckffcfe252018-07-09 12:19:38 -04002125 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002126 return -1;
2127 }
2128
2129 return 0;
2130}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002131EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002132
Alexander Duyck537c00d2013-01-10 08:57:02 +00002133#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002134struct static_key xps_needed __read_mostly;
2135EXPORT_SYMBOL(xps_needed);
2136struct static_key xps_rxqs_needed __read_mostly;
2137EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002138static DEFINE_MUTEX(xps_map_mutex);
2139#define xmap_dereference(P) \
2140 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2141
Alexander Duyck6234f872016-10-28 11:46:49 -04002142static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2143 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002144{
2145 struct xps_map *map = NULL;
2146 int pos;
2147
2148 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002149 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002150 if (!map)
2151 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002152
Alexander Duyck6234f872016-10-28 11:46:49 -04002153 for (pos = map->len; pos--;) {
2154 if (map->queues[pos] != index)
2155 continue;
2156
2157 if (map->len > 1) {
2158 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002159 break;
2160 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002161
Amritha Nambiar80d19662018-06-29 21:26:41 -07002162 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002163 kfree_rcu(map, rcu);
2164 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002165 }
2166
Alexander Duyck6234f872016-10-28 11:46:49 -04002167 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002168}
2169
Alexander Duyck6234f872016-10-28 11:46:49 -04002170static bool remove_xps_queue_cpu(struct net_device *dev,
2171 struct xps_dev_maps *dev_maps,
2172 int cpu, u16 offset, u16 count)
2173{
Alexander Duyck184c4492016-10-28 11:50:13 -04002174 int num_tc = dev->num_tc ? : 1;
2175 bool active = false;
2176 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002177
Alexander Duyck184c4492016-10-28 11:50:13 -04002178 for (tci = cpu * num_tc; num_tc--; tci++) {
2179 int i, j;
2180
2181 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002182 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002183 break;
2184 }
2185
2186 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002187 }
2188
Alexander Duyck184c4492016-10-28 11:50:13 -04002189 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002190}
2191
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002192static void reset_xps_maps(struct net_device *dev,
2193 struct xps_dev_maps *dev_maps,
2194 bool is_rxqs_map)
2195{
2196 if (is_rxqs_map) {
2197 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2198 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2199 } else {
2200 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2201 }
2202 static_key_slow_dec_cpuslocked(&xps_needed);
2203 kfree_rcu(dev_maps, rcu);
2204}
2205
Amritha Nambiar80d19662018-06-29 21:26:41 -07002206static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2207 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2208 u16 offset, u16 count, bool is_rxqs_map)
2209{
2210 bool active = false;
2211 int i, j;
2212
2213 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2214 j < nr_ids;)
2215 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2216 count);
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002217 if (!active)
2218 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002219
Sabrina Dubrocaf28c0202018-11-29 14:14:48 +01002220 if (!is_rxqs_map) {
2221 for (i = offset + (count - 1); count--; i--) {
2222 netdev_queue_numa_node_write(
2223 netdev_get_tx_queue(dev, i),
2224 NUMA_NO_NODE);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002225 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002226 }
2227}
2228
Alexander Duyck6234f872016-10-28 11:46:49 -04002229static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2230 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002231{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002232 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002233 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002234 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002235
Amritha Nambiar04157462018-06-29 21:26:46 -07002236 if (!static_key_false(&xps_needed))
2237 return;
2238
Andrei Vagin4d99f662018-08-08 20:07:35 -07002239 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002240 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002241
Amritha Nambiar04157462018-06-29 21:26:46 -07002242 if (static_key_false(&xps_rxqs_needed)) {
2243 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2244 if (dev_maps) {
2245 nr_ids = dev->num_rx_queues;
2246 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2247 offset, count, true);
2248 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002249 }
2250
2251 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002252 if (!dev_maps)
2253 goto out_no_maps;
2254
Amritha Nambiar80d19662018-06-29 21:26:41 -07002255 if (num_possible_cpus() > 1)
2256 possible_mask = cpumask_bits(cpu_possible_mask);
2257 nr_ids = nr_cpu_ids;
2258 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2259 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002260
Alexander Duyck537c00d2013-01-10 08:57:02 +00002261out_no_maps:
2262 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002263 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002264}
2265
Alexander Duyck6234f872016-10-28 11:46:49 -04002266static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2267{
2268 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2269}
2270
Amritha Nambiar80d19662018-06-29 21:26:41 -07002271static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2272 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002273{
2274 struct xps_map *new_map;
2275 int alloc_len = XPS_MIN_MAP_ALLOC;
2276 int i, pos;
2277
2278 for (pos = 0; map && pos < map->len; pos++) {
2279 if (map->queues[pos] != index)
2280 continue;
2281 return map;
2282 }
2283
Amritha Nambiar80d19662018-06-29 21:26:41 -07002284 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002285 if (map) {
2286 if (pos < map->alloc_len)
2287 return map;
2288
2289 alloc_len = map->alloc_len * 2;
2290 }
2291
Amritha Nambiar80d19662018-06-29 21:26:41 -07002292 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2293 * map
2294 */
2295 if (is_rxqs_map)
2296 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2297 else
2298 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2299 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002300 if (!new_map)
2301 return NULL;
2302
2303 for (i = 0; i < pos; i++)
2304 new_map->queues[i] = map->queues[i];
2305 new_map->alloc_len = alloc_len;
2306 new_map->len = pos;
2307
2308 return new_map;
2309}
2310
Andrei Vagin4d99f662018-08-08 20:07:35 -07002311/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002312int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2313 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002314{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002315 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002316 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002317 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002318 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002319 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002320 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002321 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002322
Alexander Duyck184c4492016-10-28 11:50:13 -04002323 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002324 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002325 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002326 if (num_tc < 0)
2327 return -EINVAL;
2328
2329 /* If queue belongs to subordinate dev use its map */
2330 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2331
Alexander Duyck184c4492016-10-28 11:50:13 -04002332 tc = netdev_txq_to_tc(dev, index);
2333 if (tc < 0)
2334 return -EINVAL;
2335 }
2336
Amritha Nambiar80d19662018-06-29 21:26:41 -07002337 mutex_lock(&xps_map_mutex);
2338 if (is_rxqs_map) {
2339 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2340 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2341 nr_ids = dev->num_rx_queues;
2342 } else {
2343 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2344 if (num_possible_cpus() > 1) {
2345 online_mask = cpumask_bits(cpu_online_mask);
2346 possible_mask = cpumask_bits(cpu_possible_mask);
2347 }
2348 dev_maps = xmap_dereference(dev->xps_cpus_map);
2349 nr_ids = nr_cpu_ids;
2350 }
2351
Alexander Duyck184c4492016-10-28 11:50:13 -04002352 if (maps_sz < L1_CACHE_BYTES)
2353 maps_sz = L1_CACHE_BYTES;
2354
Alexander Duyck01c5f862013-01-10 08:57:35 +00002355 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002356 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2357 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002358 if (!new_dev_maps)
2359 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002360 if (!new_dev_maps) {
2361 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002362 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002363 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002364
Amritha Nambiar80d19662018-06-29 21:26:41 -07002365 tci = j * num_tc + tc;
2366 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002367 NULL;
2368
Amritha Nambiar80d19662018-06-29 21:26:41 -07002369 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002370 if (!map)
2371 goto error;
2372
Amritha Nambiar80d19662018-06-29 21:26:41 -07002373 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002374 }
2375
2376 if (!new_dev_maps)
2377 goto out_no_new_maps;
2378
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002379 if (!dev_maps) {
2380 /* Increment static keys at most once per type */
2381 static_key_slow_inc_cpuslocked(&xps_needed);
2382 if (is_rxqs_map)
2383 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2384 }
Amritha Nambiar04157462018-06-29 21:26:46 -07002385
Amritha Nambiar80d19662018-06-29 21:26:41 -07002386 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2387 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002388 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002389 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002390 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002391 map = xmap_dereference(dev_maps->attr_map[tci]);
2392 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002393 }
2394
2395 /* We need to explicitly update tci as prevous loop
2396 * could break out early if dev_maps is NULL.
2397 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002398 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002399
Amritha Nambiar80d19662018-06-29 21:26:41 -07002400 if (netif_attr_test_mask(j, mask, nr_ids) &&
2401 netif_attr_test_online(j, online_mask, nr_ids)) {
2402 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002403 int pos = 0;
2404
Amritha Nambiar80d19662018-06-29 21:26:41 -07002405 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002406 while ((pos < map->len) && (map->queues[pos] != index))
2407 pos++;
2408
2409 if (pos == map->len)
2410 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002411#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002412 if (!is_rxqs_map) {
2413 if (numa_node_id == -2)
2414 numa_node_id = cpu_to_node(j);
2415 else if (numa_node_id != cpu_to_node(j))
2416 numa_node_id = -1;
2417 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002418#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002419 } else if (dev_maps) {
2420 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002421 map = xmap_dereference(dev_maps->attr_map[tci]);
2422 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002423 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002424
Alexander Duyck184c4492016-10-28 11:50:13 -04002425 /* copy maps belonging to foreign traffic classes */
2426 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2427 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002428 map = xmap_dereference(dev_maps->attr_map[tci]);
2429 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002430 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002431 }
2432
Amritha Nambiar80d19662018-06-29 21:26:41 -07002433 if (is_rxqs_map)
2434 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2435 else
2436 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002437
Alexander Duyck537c00d2013-01-10 08:57:02 +00002438 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002439 if (!dev_maps)
2440 goto out_no_old_maps;
2441
Amritha Nambiar80d19662018-06-29 21:26:41 -07002442 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2443 j < nr_ids;) {
2444 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2445 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2446 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002447 if (map && map != new_map)
2448 kfree_rcu(map, rcu);
2449 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002450 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002451
Alexander Duyck184c4492016-10-28 11:50:13 -04002452 kfree_rcu(dev_maps, rcu);
2453
2454out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002455 dev_maps = new_dev_maps;
2456 active = true;
2457
2458out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002459 if (!is_rxqs_map) {
2460 /* update Tx queue numa node */
2461 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2462 (numa_node_id >= 0) ?
2463 numa_node_id : NUMA_NO_NODE);
2464 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002465
Alexander Duyck01c5f862013-01-10 08:57:35 +00002466 if (!dev_maps)
2467 goto out_no_maps;
2468
Amritha Nambiar80d19662018-06-29 21:26:41 -07002469 /* removes tx-queue from unused CPUs/rx-queues */
2470 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2471 j < nr_ids;) {
2472 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002473 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002474 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2475 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002476 active |= remove_xps_queue(dev_maps, tci, index);
2477 for (i = num_tc - tc, tci++; --i; tci++)
2478 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002479 }
2480
2481 /* free map if not active */
Sabrina Dubroca867d0ad2018-11-29 14:14:49 +01002482 if (!active)
2483 reset_xps_maps(dev, dev_maps, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002484
2485out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002486 mutex_unlock(&xps_map_mutex);
2487
2488 return 0;
2489error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002490 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002491 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2492 j < nr_ids;) {
2493 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2494 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002495 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002496 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002497 NULL;
2498 if (new_map && new_map != map)
2499 kfree(new_map);
2500 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002501 }
2502
Alexander Duyck537c00d2013-01-10 08:57:02 +00002503 mutex_unlock(&xps_map_mutex);
2504
Alexander Duyck537c00d2013-01-10 08:57:02 +00002505 kfree(new_dev_maps);
2506 return -ENOMEM;
2507}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002508EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002509
2510int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2511 u16 index)
2512{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002513 int ret;
2514
2515 cpus_read_lock();
2516 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2517 cpus_read_unlock();
2518
2519 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002520}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002521EXPORT_SYMBOL(netif_set_xps_queue);
2522
2523#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002524static void netdev_unbind_all_sb_channels(struct net_device *dev)
2525{
2526 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2527
2528 /* Unbind any subordinate channels */
2529 while (txq-- != &dev->_tx[0]) {
2530 if (txq->sb_dev)
2531 netdev_unbind_sb_channel(dev, txq->sb_dev);
2532 }
2533}
2534
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002535void netdev_reset_tc(struct net_device *dev)
2536{
Alexander Duyck6234f872016-10-28 11:46:49 -04002537#ifdef CONFIG_XPS
2538 netif_reset_xps_queues_gt(dev, 0);
2539#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002540 netdev_unbind_all_sb_channels(dev);
2541
2542 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002543 dev->num_tc = 0;
2544 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2545 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2546}
2547EXPORT_SYMBOL(netdev_reset_tc);
2548
2549int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2550{
2551 if (tc >= dev->num_tc)
2552 return -EINVAL;
2553
Alexander Duyck6234f872016-10-28 11:46:49 -04002554#ifdef CONFIG_XPS
2555 netif_reset_xps_queues(dev, offset, count);
2556#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002557 dev->tc_to_txq[tc].count = count;
2558 dev->tc_to_txq[tc].offset = offset;
2559 return 0;
2560}
2561EXPORT_SYMBOL(netdev_set_tc_queue);
2562
2563int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2564{
2565 if (num_tc > TC_MAX_QUEUE)
2566 return -EINVAL;
2567
Alexander Duyck6234f872016-10-28 11:46:49 -04002568#ifdef CONFIG_XPS
2569 netif_reset_xps_queues_gt(dev, 0);
2570#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002571 netdev_unbind_all_sb_channels(dev);
2572
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002573 dev->num_tc = num_tc;
2574 return 0;
2575}
2576EXPORT_SYMBOL(netdev_set_num_tc);
2577
Alexander Duyckffcfe252018-07-09 12:19:38 -04002578void netdev_unbind_sb_channel(struct net_device *dev,
2579 struct net_device *sb_dev)
2580{
2581 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2582
2583#ifdef CONFIG_XPS
2584 netif_reset_xps_queues_gt(sb_dev, 0);
2585#endif
2586 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2587 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2588
2589 while (txq-- != &dev->_tx[0]) {
2590 if (txq->sb_dev == sb_dev)
2591 txq->sb_dev = NULL;
2592 }
2593}
2594EXPORT_SYMBOL(netdev_unbind_sb_channel);
2595
2596int netdev_bind_sb_channel_queue(struct net_device *dev,
2597 struct net_device *sb_dev,
2598 u8 tc, u16 count, u16 offset)
2599{
2600 /* Make certain the sb_dev and dev are already configured */
2601 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2602 return -EINVAL;
2603
2604 /* We cannot hand out queues we don't have */
2605 if ((offset + count) > dev->real_num_tx_queues)
2606 return -EINVAL;
2607
2608 /* Record the mapping */
2609 sb_dev->tc_to_txq[tc].count = count;
2610 sb_dev->tc_to_txq[tc].offset = offset;
2611
2612 /* Provide a way for Tx queue to find the tc_to_txq map or
2613 * XPS map for itself.
2614 */
2615 while (count--)
2616 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2617
2618 return 0;
2619}
2620EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2621
2622int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2623{
2624 /* Do not use a multiqueue device to represent a subordinate channel */
2625 if (netif_is_multiqueue(dev))
2626 return -ENODEV;
2627
2628 /* We allow channels 1 - 32767 to be used for subordinate channels.
2629 * Channel 0 is meant to be "native" mode and used only to represent
2630 * the main root device. We allow writing 0 to reset the device back
2631 * to normal mode after being used as a subordinate channel.
2632 */
2633 if (channel > S16_MAX)
2634 return -EINVAL;
2635
2636 dev->num_tc = -channel;
2637
2638 return 0;
2639}
2640EXPORT_SYMBOL(netdev_set_sb_channel);
2641
John Fastabendf0796d52010-07-01 13:21:57 +00002642/*
2643 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002644 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002645 */
Tom Herberte6484932010-10-18 18:04:39 +00002646int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002647{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002648 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002649 int rc;
2650
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002651 disabling = txq < dev->real_num_tx_queues;
2652
Tom Herberte6484932010-10-18 18:04:39 +00002653 if (txq < 1 || txq > dev->num_tx_queues)
2654 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002655
Ben Hutchings5c565802011-02-15 19:39:21 +00002656 if (dev->reg_state == NETREG_REGISTERED ||
2657 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002658 ASSERT_RTNL();
2659
Tom Herbert1d24eb42010-11-21 13:17:27 +00002660 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2661 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002662 if (rc)
2663 return rc;
2664
John Fastabend4f57c082011-01-17 08:06:04 +00002665 if (dev->num_tc)
2666 netif_setup_tc(dev, txq);
2667
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002668 dev->real_num_tx_queues = txq;
2669
2670 if (disabling) {
2671 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002672 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002673#ifdef CONFIG_XPS
2674 netif_reset_xps_queues_gt(dev, txq);
2675#endif
2676 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002677 } else {
2678 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002679 }
Tom Herberte6484932010-10-18 18:04:39 +00002680
Tom Herberte6484932010-10-18 18:04:39 +00002681 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002682}
2683EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002684
Michael Daltona953be52014-01-16 22:23:28 -08002685#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002686/**
2687 * netif_set_real_num_rx_queues - set actual number of RX queues used
2688 * @dev: Network device
2689 * @rxq: Actual number of RX queues
2690 *
2691 * This must be called either with the rtnl_lock held or before
2692 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002693 * negative error code. If called before registration, it always
2694 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002695 */
2696int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2697{
2698 int rc;
2699
Tom Herbertbd25fa72010-10-18 18:00:16 +00002700 if (rxq < 1 || rxq > dev->num_rx_queues)
2701 return -EINVAL;
2702
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002703 if (dev->reg_state == NETREG_REGISTERED) {
2704 ASSERT_RTNL();
2705
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002706 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2707 rxq);
2708 if (rc)
2709 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002710 }
2711
2712 dev->real_num_rx_queues = rxq;
2713 return 0;
2714}
2715EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2716#endif
2717
Ben Hutchings2c530402012-07-10 10:55:09 +00002718/**
2719 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002720 *
2721 * This routine should set an upper limit on the number of RSS queues
2722 * used by default by multiqueue devices.
2723 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002724int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002725{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302726 return is_kdump_kernel() ?
2727 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002728}
2729EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2730
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002731static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002732{
2733 struct softnet_data *sd;
2734 unsigned long flags;
2735
2736 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002737 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002738 q->next_sched = NULL;
2739 *sd->output_queue_tailp = q;
2740 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002741 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2742 local_irq_restore(flags);
2743}
2744
David S. Miller37437bb2008-07-16 02:15:04 -07002745void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002746{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002747 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2748 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002749}
2750EXPORT_SYMBOL(__netif_schedule);
2751
Eric Dumazete6247022013-12-05 04:45:08 -08002752struct dev_kfree_skb_cb {
2753 enum skb_free_reason reason;
2754};
2755
2756static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002757{
Eric Dumazete6247022013-12-05 04:45:08 -08002758 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002759}
Denis Vlasenko56079432006-03-29 15:57:29 -08002760
John Fastabend46e5da40a2014-09-12 20:04:52 -07002761void netif_schedule_queue(struct netdev_queue *txq)
2762{
2763 rcu_read_lock();
2764 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2765 struct Qdisc *q = rcu_dereference(txq->qdisc);
2766
2767 __netif_schedule(q);
2768 }
2769 rcu_read_unlock();
2770}
2771EXPORT_SYMBOL(netif_schedule_queue);
2772
John Fastabend46e5da40a2014-09-12 20:04:52 -07002773void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2774{
2775 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2776 struct Qdisc *q;
2777
2778 rcu_read_lock();
2779 q = rcu_dereference(dev_queue->qdisc);
2780 __netif_schedule(q);
2781 rcu_read_unlock();
2782 }
2783}
2784EXPORT_SYMBOL(netif_tx_wake_queue);
2785
Eric Dumazete6247022013-12-05 04:45:08 -08002786void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2787{
2788 unsigned long flags;
2789
Myungho Jung98998862017-04-25 11:58:15 -07002790 if (unlikely(!skb))
2791 return;
2792
Reshetova, Elena63354792017-06-30 13:07:58 +03002793 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08002794 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03002795 refcount_set(&skb->users, 0);
2796 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08002797 return;
2798 }
2799 get_kfree_skb_cb(skb)->reason = reason;
2800 local_irq_save(flags);
2801 skb->next = __this_cpu_read(softnet_data.completion_queue);
2802 __this_cpu_write(softnet_data.completion_queue, skb);
2803 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2804 local_irq_restore(flags);
2805}
2806EXPORT_SYMBOL(__dev_kfree_skb_irq);
2807
2808void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08002809{
2810 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08002811 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08002812 else
2813 dev_kfree_skb(skb);
2814}
Eric Dumazete6247022013-12-05 04:45:08 -08002815EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08002816
2817
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002818/**
2819 * netif_device_detach - mark device as removed
2820 * @dev: network device
2821 *
2822 * Mark device as removed from system and therefore no longer available.
2823 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002824void netif_device_detach(struct net_device *dev)
2825{
2826 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2827 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002828 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002829 }
2830}
2831EXPORT_SYMBOL(netif_device_detach);
2832
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002833/**
2834 * netif_device_attach - mark device as attached
2835 * @dev: network device
2836 *
2837 * Mark device as attached from system and restart if needed.
2838 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002839void netif_device_attach(struct net_device *dev)
2840{
2841 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2842 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002843 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002844 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002845 }
2846}
2847EXPORT_SYMBOL(netif_device_attach);
2848
Jiri Pirko5605c762015-05-12 14:56:12 +02002849/*
2850 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2851 * to be used as a distribution range.
2852 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04002853static u16 skb_tx_hash(const struct net_device *dev,
2854 const struct net_device *sb_dev,
2855 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02002856{
2857 u32 hash;
2858 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04002859 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02002860
Alexander Duyckeadec8772018-07-09 12:19:48 -04002861 if (dev->num_tc) {
2862 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2863
2864 qoffset = sb_dev->tc_to_txq[tc].offset;
2865 qcount = sb_dev->tc_to_txq[tc].count;
2866 }
2867
Jiri Pirko5605c762015-05-12 14:56:12 +02002868 if (skb_rx_queue_recorded(skb)) {
2869 hash = skb_get_rx_queue(skb);
Alexander Duyck1b837d42018-04-27 14:06:53 -04002870 while (unlikely(hash >= qcount))
2871 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04002872 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02002873 }
2874
2875 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2876}
Jiri Pirko5605c762015-05-12 14:56:12 +02002877
Ben Hutchings36c92472012-01-17 07:57:56 +00002878static void skb_warn_bad_offload(const struct sk_buff *skb)
2879{
Wei Tang84d15ae2016-06-16 21:17:49 +08002880 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00002881 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01002882 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00002883
Ben Greearc846ad92013-04-19 10:45:52 +00002884 if (!net_ratelimit())
2885 return;
2886
Bjørn Mork88ad4172015-11-16 19:16:40 +01002887 if (dev) {
2888 if (dev->dev.parent)
2889 name = dev_driver_string(dev->dev.parent);
2890 else
2891 name = netdev_name(dev);
2892 }
Ben Hutchings36c92472012-01-17 07:57:56 +00002893 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2894 "gso_type=%d ip_summed=%d\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01002895 name, dev ? &dev->features : &null_features,
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002896 skb->sk ? &skb->sk->sk_route_caps : &null_features,
Ben Hutchings36c92472012-01-17 07:57:56 +00002897 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2898 skb_shinfo(skb)->gso_type, skb->ip_summed);
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901/*
2902 * Invalidate hardware checksum when packet is to be mangled, and
2903 * complete checksum manually on outgoing path.
2904 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002905int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Al Virod3bc23e2006-11-14 21:24:49 -08002907 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07002908 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Patrick McHardy84fa7932006-08-29 16:44:56 -07002910 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07002911 goto out_set_summed;
2912
2913 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00002914 skb_warn_bad_offload(skb);
2915 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917
Eric Dumazetcef401d2013-01-25 20:34:37 +00002918 /* Before computing a checksum, we should make sure no frag could
2919 * be modified by an external entity : checksum could be wrong.
2920 */
2921 if (skb_has_shared_frag(skb)) {
2922 ret = __skb_linearize(skb);
2923 if (ret)
2924 goto out;
2925 }
2926
Michał Mirosław55508d62010-12-14 15:24:08 +00002927 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07002928 BUG_ON(offset >= skb_headlen(skb));
2929 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2930
2931 offset += skb->csum_offset;
2932 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2933
2934 if (skb_cloned(skb) &&
2935 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2937 if (ret)
2938 goto out;
2939 }
2940
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07002941 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07002942out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002944out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return ret;
2946}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002947EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Davide Carattib72b5bf2017-05-18 15:44:38 +02002949int skb_crc32c_csum_help(struct sk_buff *skb)
2950{
2951 __le32 crc32c_csum;
2952 int ret = 0, offset, start;
2953
2954 if (skb->ip_summed != CHECKSUM_PARTIAL)
2955 goto out;
2956
2957 if (unlikely(skb_is_gso(skb)))
2958 goto out;
2959
2960 /* Before computing a checksum, we should make sure no frag could
2961 * be modified by an external entity : checksum could be wrong.
2962 */
2963 if (unlikely(skb_has_shared_frag(skb))) {
2964 ret = __skb_linearize(skb);
2965 if (ret)
2966 goto out;
2967 }
2968 start = skb_checksum_start_offset(skb);
2969 offset = start + offsetof(struct sctphdr, checksum);
2970 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
2971 ret = -EINVAL;
2972 goto out;
2973 }
2974 if (skb_cloned(skb) &&
2975 !skb_clone_writable(skb, offset + sizeof(__le32))) {
2976 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2977 if (ret)
2978 goto out;
2979 }
2980 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
2981 skb->len - start, ~(__u32)0,
2982 crc32c_csum_stub));
2983 *(__le32 *)(skb->data + offset) = crc32c_csum;
2984 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02002985 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02002986out:
2987 return ret;
2988}
2989
Vlad Yasevich53d64712014-03-27 17:26:18 -04002990__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002991{
2992 __be16 type = skb->protocol;
2993
Pravin B Shelar19acc322013-05-07 20:41:07 +00002994 /* Tunnel gso handlers can set protocol to ethernet. */
2995 if (type == htons(ETH_P_TEB)) {
2996 struct ethhdr *eth;
2997
2998 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2999 return 0;
3000
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07003001 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00003002 type = eth->h_proto;
3003 }
3004
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09003005 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003006}
3007
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003008/**
3009 * skb_mac_gso_segment - mac layer segmentation handler.
3010 * @skb: buffer to segment
3011 * @features: features for the output path (see dev->features)
3012 */
3013struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3014 netdev_features_t features)
3015{
3016 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3017 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003018 int vlan_depth = skb->mac_len;
3019 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003020
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003021 if (unlikely(!type))
3022 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003023
Vlad Yasevich53d64712014-03-27 17:26:18 -04003024 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003025
3026 rcu_read_lock();
3027 list_for_each_entry_rcu(ptype, &offload_base, list) {
3028 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003029 segs = ptype->callbacks.gso_segment(skb, features);
3030 break;
3031 }
3032 }
3033 rcu_read_unlock();
3034
3035 __skb_push(skb, skb->data - skb_mac_header(skb));
3036
3037 return segs;
3038}
3039EXPORT_SYMBOL(skb_mac_gso_segment);
3040
3041
Cong Wang12b00042013-02-05 16:36:38 +00003042/* openvswitch calls this on rx path, so we need a different check.
3043 */
3044static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3045{
3046 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003047 return skb->ip_summed != CHECKSUM_PARTIAL &&
3048 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003049
3050 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003051}
3052
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003053/**
Cong Wang12b00042013-02-05 16:36:38 +00003054 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003055 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003056 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003057 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003058 *
3059 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003060 *
3061 * It may return NULL if the skb requires no segmentation. This is
3062 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003063 *
3064 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003065 */
Cong Wang12b00042013-02-05 16:36:38 +00003066struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3067 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003068{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003069 struct sk_buff *segs;
3070
Cong Wang12b00042013-02-05 16:36:38 +00003071 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003072 int err;
3073
Eric Dumazetb2504a52017-01-31 10:20:32 -08003074 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003075 err = skb_cow_head(skb, 0);
3076 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003077 return ERR_PTR(err);
3078 }
3079
Alexander Duyck802ab552016-04-10 21:45:03 -04003080 /* Only report GSO partial support if it will enable us to
3081 * support segmentation on this frame without needing additional
3082 * work.
3083 */
3084 if (features & NETIF_F_GSO_PARTIAL) {
3085 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3086 struct net_device *dev = skb->dev;
3087
3088 partial_features |= dev->features & dev->gso_partial_features;
3089 if (!skb_gso_ok(skb, features | partial_features))
3090 features &= ~NETIF_F_GSO_PARTIAL;
3091 }
3092
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003093 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3094 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3095
Pravin B Shelar68c33162013-02-14 14:02:41 +00003096 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003097 SKB_GSO_CB(skb)->encap_level = 0;
3098
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003099 skb_reset_mac_header(skb);
3100 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003101
Eric Dumazetb2504a52017-01-31 10:20:32 -08003102 segs = skb_mac_gso_segment(skb, features);
3103
Willem de Bruijn8d74e9f2017-12-12 11:39:04 -05003104 if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003105 skb_warn_bad_offload(skb);
3106
3107 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003108}
Cong Wang12b00042013-02-05 16:36:38 +00003109EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003110
Herbert Xufb286bb2005-11-10 13:01:24 -08003111/* Take action when hardware reception checksum errors are detected. */
3112#ifdef CONFIG_BUG
Cong Wang7fe50ac2018-11-12 14:47:18 -08003113void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08003114{
3115 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003116 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Cong Wang7fe50ac2018-11-12 14:47:18 -08003117 if (dev)
3118 pr_err("dev features: %pNF\n", &dev->features);
3119 pr_err("skb len=%u data_len=%u pkt_type=%u gso_size=%u gso_type=%u nr_frags=%u ip_summed=%u csum=%x csum_complete_sw=%d csum_valid=%d csum_level=%u\n",
3120 skb->len, skb->data_len, skb->pkt_type,
3121 skb_shinfo(skb)->gso_size, skb_shinfo(skb)->gso_type,
3122 skb_shinfo(skb)->nr_frags, skb->ip_summed, skb->csum,
3123 skb->csum_complete_sw, skb->csum_valid, skb->csum_level);
Herbert Xufb286bb2005-11-10 13:01:24 -08003124 dump_stack();
3125 }
3126}
3127EXPORT_SYMBOL(netdev_rx_csum_fault);
3128#endif
3129
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003130/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003131static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003133#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 int i;
tchardingf4563a72017-02-09 17:56:07 +11003135
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003136 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003137 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3138 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003139
Ian Campbellea2ab692011-08-22 23:44:58 +00003140 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003141 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003142 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003143 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return 0;
3146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Simon Horman3b392dd2014-06-04 08:53:17 +09003148/* If MPLS offload request, verify we are testing hardware MPLS features
3149 * instead of standard features for the netdev.
3150 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003151#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003152static netdev_features_t net_mpls_features(struct sk_buff *skb,
3153 netdev_features_t features,
3154 __be16 type)
3155{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003156 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003157 features &= skb->dev->mpls_features;
3158
3159 return features;
3160}
3161#else
3162static netdev_features_t net_mpls_features(struct sk_buff *skb,
3163 netdev_features_t features,
3164 __be16 type)
3165{
3166 return features;
3167}
3168#endif
3169
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003170static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003171 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003172{
Vlad Yasevich53d64712014-03-27 17:26:18 -04003173 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09003174 __be16 type;
3175
3176 type = skb_network_protocol(skb, &tmp);
3177 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003178
Ed Cashinc0d680e2012-09-19 15:49:00 +00003179 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003180 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003181 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003182 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003183 if (illegal_highdma(skb->dev, skb))
3184 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003185
3186 return features;
3187}
3188
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003189netdev_features_t passthru_features_check(struct sk_buff *skb,
3190 struct net_device *dev,
3191 netdev_features_t features)
3192{
3193 return features;
3194}
3195EXPORT_SYMBOL(passthru_features_check);
3196
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003197static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003198 struct net_device *dev,
3199 netdev_features_t features)
3200{
3201 return vlan_features_check(skb, features);
3202}
3203
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003204static netdev_features_t gso_features_check(const struct sk_buff *skb,
3205 struct net_device *dev,
3206 netdev_features_t features)
3207{
3208 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3209
3210 if (gso_segs > dev->gso_max_segs)
3211 return features & ~NETIF_F_GSO_MASK;
3212
Alexander Duyck802ab552016-04-10 21:45:03 -04003213 /* Support for GSO partial features requires software
3214 * intervention before we can actually process the packets
3215 * so we need to strip support for any partial features now
3216 * and we can pull them back in after we have partially
3217 * segmented the frame.
3218 */
3219 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3220 features &= ~dev->gso_partial_features;
3221
3222 /* Make sure to clear the IPv4 ID mangling feature if the
3223 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003224 */
3225 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3226 struct iphdr *iph = skb->encapsulation ?
3227 inner_ip_hdr(skb) : ip_hdr(skb);
3228
3229 if (!(iph->frag_off & htons(IP_DF)))
3230 features &= ~NETIF_F_TSO_MANGLEID;
3231 }
3232
3233 return features;
3234}
3235
Florian Westphalc1e756b2014-05-05 15:00:44 +02003236netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003237{
Jesse Gross5f352272014-12-23 22:37:26 -08003238 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003239 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003240
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003241 if (skb_is_gso(skb))
3242 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003243
Jesse Gross5f352272014-12-23 22:37:26 -08003244 /* If encapsulation offload request, verify we are testing
3245 * hardware encapsulation features instead of standard
3246 * features for the netdev
3247 */
3248 if (skb->encapsulation)
3249 features &= dev->hw_enc_features;
3250
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003251 if (skb_vlan_tagged(skb))
3252 features = netdev_intersect_features(features,
3253 dev->vlan_features |
3254 NETIF_F_HW_VLAN_CTAG_TX |
3255 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003256
Jesse Gross5f352272014-12-23 22:37:26 -08003257 if (dev->netdev_ops->ndo_features_check)
3258 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3259 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003260 else
3261 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003262
Florian Westphalc1e756b2014-05-05 15:00:44 +02003263 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003264}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003265EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003266
David S. Miller2ea25512014-08-29 21:10:01 -07003267static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003268 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003269{
David S. Miller2ea25512014-08-29 21:10:01 -07003270 unsigned int len;
3271 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003272
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003273 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003274 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003275
David S. Miller2ea25512014-08-29 21:10:01 -07003276 len = skb->len;
3277 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003278 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003279 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003280
Patrick McHardy572a9d72009-11-10 06:14:14 +00003281 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003282}
David S. Miller2ea25512014-08-29 21:10:01 -07003283
David S. Miller8dcda222014-09-01 15:06:40 -07003284struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3285 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003286{
3287 struct sk_buff *skb = first;
3288 int rc = NETDEV_TX_OK;
3289
3290 while (skb) {
3291 struct sk_buff *next = skb->next;
3292
David S. Millera8305bf2018-07-29 20:42:53 -07003293 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003294 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003295 if (unlikely(!dev_xmit_complete(rc))) {
3296 skb->next = next;
3297 goto out;
3298 }
3299
3300 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003301 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003302 rc = NETDEV_TX_BUSY;
3303 break;
3304 }
3305 }
3306
3307out:
3308 *ret = rc;
3309 return skb;
3310}
3311
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003312static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3313 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003314{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003315 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003316 !vlan_hw_offload_capable(features, skb->vlan_proto))
3317 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003318 return skb;
3319}
3320
Davide Caratti43c26a12017-05-18 15:44:41 +02003321int skb_csum_hwoffload_help(struct sk_buff *skb,
3322 const netdev_features_t features)
3323{
3324 if (unlikely(skb->csum_not_inet))
3325 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3326 skb_crc32c_csum_help(skb);
3327
3328 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3329}
3330EXPORT_SYMBOL(skb_csum_hwoffload_help);
3331
Steffen Klassertf53c7232017-12-20 10:41:36 +01003332static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003333{
3334 netdev_features_t features;
3335
David S. Millereae3f882014-08-30 15:17:13 -07003336 features = netif_skb_features(skb);
3337 skb = validate_xmit_vlan(skb, features);
3338 if (unlikely(!skb))
3339 goto out_null;
3340
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003341 skb = sk_validate_xmit_skb(skb, dev);
3342 if (unlikely(!skb))
3343 goto out_null;
3344
Johannes Berg8b86a612015-04-17 15:45:04 +02003345 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003346 struct sk_buff *segs;
3347
3348 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003349 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003350 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003351 } else if (segs) {
3352 consume_skb(skb);
3353 skb = segs;
3354 }
David S. Millereae3f882014-08-30 15:17:13 -07003355 } else {
3356 if (skb_needs_linearize(skb, features) &&
3357 __skb_linearize(skb))
3358 goto out_kfree_skb;
3359
3360 /* If packet is not checksummed and device does not
3361 * support checksumming for this protocol, complete
3362 * checksumming here.
3363 */
3364 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3365 if (skb->encapsulation)
3366 skb_set_inner_transport_header(skb,
3367 skb_checksum_start_offset(skb));
3368 else
3369 skb_set_transport_header(skb,
3370 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003371 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003372 goto out_kfree_skb;
3373 }
3374 }
3375
Steffen Klassertf53c7232017-12-20 10:41:36 +01003376 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003377
David S. Millereae3f882014-08-30 15:17:13 -07003378 return skb;
3379
3380out_kfree_skb:
3381 kfree_skb(skb);
3382out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003383 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003384 return NULL;
3385}
3386
Steffen Klassertf53c7232017-12-20 10:41:36 +01003387struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003388{
3389 struct sk_buff *next, *head = NULL, *tail;
3390
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003391 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003392 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003393 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003394
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003395 /* in case skb wont be segmented, point to itself */
3396 skb->prev = skb;
3397
Steffen Klassertf53c7232017-12-20 10:41:36 +01003398 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003399 if (!skb)
3400 continue;
3401
3402 if (!head)
3403 head = skb;
3404 else
3405 tail->next = skb;
3406 /* If skb was segmented, skb->prev points to
3407 * the last segment. If not, it still contains skb.
3408 */
3409 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003410 }
3411 return head;
3412}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003413EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003414
Eric Dumazet1def9232013-01-10 12:36:42 +00003415static void qdisc_pkt_len_init(struct sk_buff *skb)
3416{
3417 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3418
3419 qdisc_skb_cb(skb)->pkt_len = skb->len;
3420
3421 /* To get more precise estimation of bytes sent on wire,
3422 * we add to pkt_len the headers size of all segments
3423 */
3424 if (shinfo->gso_size) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003425 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003426 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003427
Eric Dumazet757b8b12013-01-15 21:14:21 -08003428 /* mac layer + network layer */
3429 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3430
3431 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003432 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3433 const struct tcphdr *th;
3434 struct tcphdr _tcphdr;
3435
3436 th = skb_header_pointer(skb, skb_transport_offset(skb),
3437 sizeof(_tcphdr), &_tcphdr);
3438 if (likely(th))
3439 hdr_len += __tcp_hdrlen(th);
3440 } else {
3441 struct udphdr _udphdr;
3442
3443 if (skb_header_pointer(skb, skb_transport_offset(skb),
3444 sizeof(_udphdr), &_udphdr))
3445 hdr_len += sizeof(struct udphdr);
3446 }
Jason Wang15e5a032013-03-25 20:19:59 +00003447
3448 if (shinfo->gso_type & SKB_GSO_DODGY)
3449 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3450 shinfo->gso_size);
3451
3452 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003453 }
3454}
3455
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003456static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3457 struct net_device *dev,
3458 struct netdev_queue *txq)
3459{
3460 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003461 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003462 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003463 int rc;
3464
Eric Dumazeta2da5702011-01-20 03:48:19 +00003465 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003466
3467 if (q->flags & TCQ_F_NOLOCK) {
3468 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3469 __qdisc_drop(skb, &to_free);
3470 rc = NET_XMIT_DROP;
3471 } else {
3472 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Paolo Abeni32f7b442018-05-15 10:50:31 +02003473 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003474 }
3475
3476 if (unlikely(to_free))
3477 kfree_skb_list(to_free);
3478 return rc;
3479 }
3480
Eric Dumazet79640a42010-06-02 05:09:29 -07003481 /*
3482 * Heuristic to force contended enqueues to serialize on a
3483 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003484 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003485 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003486 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003487 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003488 if (unlikely(contended))
3489 spin_lock(&q->busylock);
3490
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003491 spin_lock(root_lock);
3492 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003493 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003494 rc = NET_XMIT_DROP;
3495 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003496 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003497 /*
3498 * This is a work-conserving queue; there are no old skbs
3499 * waiting to be sent out; and the qdisc is not running -
3500 * xmit the skb directly.
3501 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003502
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003503 qdisc_bstats_update(q, skb);
3504
Eric Dumazet55a93b32014-10-03 15:31:07 -07003505 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003506 if (unlikely(contended)) {
3507 spin_unlock(&q->busylock);
3508 contended = false;
3509 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003510 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003511 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003512
John Fastabend6c148182017-12-07 09:54:06 -08003513 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003514 rc = NET_XMIT_SUCCESS;
3515 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003516 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003517 if (qdisc_run_begin(q)) {
3518 if (unlikely(contended)) {
3519 spin_unlock(&q->busylock);
3520 contended = false;
3521 }
3522 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003523 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003524 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003525 }
3526 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003527 if (unlikely(to_free))
3528 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003529 if (unlikely(contended))
3530 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003531 return rc;
3532}
3533
Daniel Borkmann86f85152013-12-29 17:27:11 +01003534#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003535static void skb_update_prio(struct sk_buff *skb)
3536{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003537 const struct netprio_map *map;
3538 const struct sock *sk;
3539 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003540
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003541 if (skb->priority)
3542 return;
3543 map = rcu_dereference_bh(skb->dev->priomap);
3544 if (!map)
3545 return;
3546 sk = skb_to_full_sk(skb);
3547 if (!sk)
3548 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003549
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003550 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3551
3552 if (prioidx < map->priomap_len)
3553 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003554}
3555#else
3556#define skb_update_prio(skb)
3557#endif
3558
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +02003559DEFINE_PER_CPU(int, xmit_recursion);
3560EXPORT_SYMBOL(xmit_recursion);
3561
Dave Jonesd29f7492008-07-22 14:09:06 -07003562/**
Michel Machado95603e22012-06-12 10:16:35 +00003563 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003564 * @net: network namespace this loopback is happening in
3565 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003566 * @skb: buffer to transmit
3567 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003568int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003569{
3570 skb_reset_mac_header(skb);
3571 __skb_pull(skb, skb_network_offset(skb));
3572 skb->pkt_type = PACKET_LOOPBACK;
3573 skb->ip_summed = CHECKSUM_UNNECESSARY;
3574 WARN_ON(!skb_dst(skb));
3575 skb_dst_force(skb);
3576 netif_rx_ni(skb);
3577 return 0;
3578}
3579EXPORT_SYMBOL(dev_loopback_xmit);
3580
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003581#ifdef CONFIG_NET_EGRESS
3582static struct sk_buff *
3583sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3584{
Jiri Pirko46209402017-11-03 11:46:25 +01003585 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003586 struct tcf_result cl_res;
3587
Jiri Pirko46209402017-11-03 11:46:25 +01003588 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003589 return skb;
3590
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003591 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Jiri Pirko46209402017-11-03 11:46:25 +01003592 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003593
Jiri Pirko46209402017-11-03 11:46:25 +01003594 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003595 case TC_ACT_OK:
3596 case TC_ACT_RECLASSIFY:
3597 skb->tc_index = TC_H_MIN(cl_res.classid);
3598 break;
3599 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003600 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003601 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003602 kfree_skb(skb);
3603 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003604 case TC_ACT_STOLEN:
3605 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003606 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003607 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003608 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003609 return NULL;
3610 case TC_ACT_REDIRECT:
3611 /* No need to push/pop skb's mac_header here on egress! */
3612 skb_do_redirect(skb);
3613 *ret = NET_XMIT_SUCCESS;
3614 return NULL;
3615 default:
3616 break;
3617 }
3618
3619 return skb;
3620}
3621#endif /* CONFIG_NET_EGRESS */
3622
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003623#ifdef CONFIG_XPS
3624static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3625 struct xps_dev_maps *dev_maps, unsigned int tci)
3626{
3627 struct xps_map *map;
3628 int queue_index = -1;
3629
3630 if (dev->num_tc) {
3631 tci *= dev->num_tc;
3632 tci += netdev_get_prio_tc_map(dev, skb->priority);
3633 }
3634
3635 map = rcu_dereference(dev_maps->attr_map[tci]);
3636 if (map) {
3637 if (map->len == 1)
3638 queue_index = map->queues[0];
3639 else
3640 queue_index = map->queues[reciprocal_scale(
3641 skb_get_hash(skb), map->len)];
3642 if (unlikely(queue_index >= dev->real_num_tx_queues))
3643 queue_index = -1;
3644 }
3645 return queue_index;
3646}
3647#endif
3648
Alexander Duyckeadec8772018-07-09 12:19:48 -04003649static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3650 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003651{
3652#ifdef CONFIG_XPS
3653 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003654 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003655 int queue_index = -1;
3656
Amritha Nambiar04157462018-06-29 21:26:46 -07003657 if (!static_key_false(&xps_needed))
3658 return -1;
3659
Jiri Pirko638b2a62015-05-12 14:56:13 +02003660 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003661 if (!static_key_false(&xps_rxqs_needed))
3662 goto get_cpus_map;
3663
Alexander Duyckeadec8772018-07-09 12:19:48 -04003664 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003665 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003666 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003667
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003668 if (tci >= 0 && tci < dev->num_rx_queues)
3669 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3670 tci);
3671 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003672
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003673get_cpus_map:
3674 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003675 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003676 if (dev_maps) {
3677 unsigned int tci = skb->sender_cpu - 1;
3678
3679 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3680 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003681 }
3682 }
3683 rcu_read_unlock();
3684
3685 return queue_index;
3686#else
3687 return -1;
3688#endif
3689}
3690
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003691u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -04003692 struct net_device *sb_dev,
3693 select_queue_fallback_t fallback)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003694{
3695 return 0;
3696}
3697EXPORT_SYMBOL(dev_pick_tx_zero);
3698
3699u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -04003700 struct net_device *sb_dev,
3701 select_queue_fallback_t fallback)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003702{
3703 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3704}
3705EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3706
Alexander Duyck8ec56fc2018-07-09 12:20:04 -04003707static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3708 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003709{
3710 struct sock *sk = skb->sk;
3711 int queue_index = sk_tx_queue_get(sk);
3712
Alexander Duyckeadec8772018-07-09 12:19:48 -04003713 sb_dev = sb_dev ? : dev;
3714
Jiri Pirko638b2a62015-05-12 14:56:13 +02003715 if (queue_index < 0 || skb->ooo_okay ||
3716 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003717 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003718
Jiri Pirko638b2a62015-05-12 14:56:13 +02003719 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003720 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003721
3722 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003723 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003724 rcu_access_pointer(sk->sk_dst_cache))
3725 sk_tx_queue_set(sk, new_index);
3726
3727 queue_index = new_index;
3728 }
3729
3730 return queue_index;
3731}
3732
3733struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3734 struct sk_buff *skb,
Alexander Duyckeadec8772018-07-09 12:19:48 -04003735 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003736{
3737 int queue_index = 0;
3738
3739#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003740 u32 sender_cpu = skb->sender_cpu - 1;
3741
3742 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003743 skb->sender_cpu = raw_smp_processor_id() + 1;
3744#endif
3745
3746 if (dev->real_num_tx_queues != 1) {
3747 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003748
Jiri Pirko638b2a62015-05-12 14:56:13 +02003749 if (ops->ndo_select_queue)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003750 queue_index = ops->ndo_select_queue(dev, skb, sb_dev,
Jiri Pirko638b2a62015-05-12 14:56:13 +02003751 __netdev_pick_tx);
3752 else
Alexander Duyck8ec56fc2018-07-09 12:20:04 -04003753 queue_index = __netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003754
Alexander Duyckd5845272017-11-22 10:57:41 -08003755 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003756 }
3757
3758 skb_set_queue_mapping(skb, queue_index);
3759 return netdev_get_tx_queue(dev, queue_index);
3760}
3761
Michel Machado95603e22012-06-12 10:16:35 +00003762/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003763 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003764 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04003765 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003766 *
3767 * Queue a buffer for transmission to a network device. The caller must
3768 * have set the device and priority and built the buffer before calling
3769 * this function. The function can be called from an interrupt.
3770 *
3771 * A negative errno code is returned on a failure. A success does not
3772 * guarantee the frame will be transmitted as it may be dropped due
3773 * to congestion or traffic shaping.
3774 *
3775 * -----------------------------------------------------------------------------------
3776 * I notice this method can also return errors from the queue disciplines,
3777 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3778 * be positive.
3779 *
3780 * Regardless of the return value, the skb is consumed, so it is currently
3781 * difficult to retry a send to this method. (You can bump the ref count
3782 * before sending to hold a reference for retry if you are careful.)
3783 *
3784 * When calling this method, interrupts MUST be enabled. This is because
3785 * the BH enable code must have IRQs enabled so that it will not deadlock.
3786 * --BLG
3787 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003788static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003791 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 struct Qdisc *q;
3793 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01003794 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003796 skb_reset_mac_header(skb);
3797
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003798 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3799 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3800
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003801 /* Disable soft irqs for various locks below. Also
3802 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003804 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Neil Horman5bc14212011-11-22 05:10:51 +00003806 skb_update_prio(skb);
3807
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003808 qdisc_pkt_len_init(skb);
3809#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003810 skb->tc_at_ingress = 0;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003811# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07003812 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003813 skb = sch_handle_egress(skb, &rc, dev);
3814 if (!skb)
3815 goto out;
3816 }
3817# endif
3818#endif
Eric Dumazet02875872014-10-05 18:38:35 -07003819 /* If device/qdisc don't need skb->dst, release it right now while
3820 * its hot in this cpu cache.
3821 */
3822 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3823 skb_dst_drop(skb);
3824 else
3825 skb_dst_force(skb);
3826
Alexander Duyckeadec8772018-07-09 12:19:48 -04003827 txq = netdev_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08003828 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07003829
Koki Sanagicf66ba52010-08-23 18:45:02 +09003830 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003832 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07003833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 }
3835
3836 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11003837 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
tchardingeb13da12017-02-09 17:56:06 +11003839 * Really, it is unlikely that netif_tx_lock protection is necessary
3840 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3841 * counters.)
3842 * However, it is possible, that they rely on protection
3843 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
tchardingeb13da12017-02-09 17:56:06 +11003845 * Check this and shot the lock. It is not prone from deadlocks.
3846 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 */
3848 if (dev->flags & IFF_UP) {
3849 int cpu = smp_processor_id(); /* ok because BHs are off */
3850
David S. Millerc773e842008-07-08 23:13:53 -07003851 if (txq->xmit_lock_owner != cpu) {
Daniel Borkmanna70b5062016-06-10 21:19:06 +02003852 if (unlikely(__this_cpu_read(xmit_recursion) >
3853 XMIT_RECURSION_LIMIT))
Eric Dumazet745e20f2010-09-29 13:23:09 -07003854 goto recursion_alert;
3855
Steffen Klassertf53c7232017-12-20 10:41:36 +01003856 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003857 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07003858 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003859
David S. Millerc773e842008-07-08 23:13:53 -07003860 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Tom Herbert734664982011-11-28 16:32:44 +00003862 if (!netif_xmit_stopped(txq)) {
Eric Dumazet745e20f2010-09-29 13:23:09 -07003863 __this_cpu_inc(xmit_recursion);
David S. Millerce937182014-08-30 19:22:20 -07003864 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Eric Dumazet745e20f2010-09-29 13:23:09 -07003865 __this_cpu_dec(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00003866 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07003867 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 goto out;
3869 }
3870 }
David S. Millerc773e842008-07-08 23:13:53 -07003871 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00003872 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3873 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 } else {
3875 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07003876 * unfortunately
3877 */
3878recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00003879 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3880 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 }
3882 }
3883
3884 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07003885 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Eric Dumazet015f0682014-03-27 08:45:56 -07003887 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003888 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 return rc;
3890out:
Herbert Xud4828d82006-06-22 02:28:18 -07003891 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return rc;
3893}
Jason Wangf663dd92014-01-10 16:18:26 +08003894
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003895int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08003896{
3897 return __dev_queue_xmit(skb, NULL);
3898}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003899EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Alexander Duyckeadec8772018-07-09 12:19:48 -04003901int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08003902{
Alexander Duyckeadec8772018-07-09 12:19:48 -04003903 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08003904}
3905EXPORT_SYMBOL(dev_queue_xmit_accel);
3906
Magnus Karlsson865b03f2018-05-02 13:01:33 +02003907int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
3908{
3909 struct net_device *dev = skb->dev;
3910 struct sk_buff *orig_skb = skb;
3911 struct netdev_queue *txq;
3912 int ret = NETDEV_TX_BUSY;
3913 bool again = false;
3914
3915 if (unlikely(!netif_running(dev) ||
3916 !netif_carrier_ok(dev)))
3917 goto drop;
3918
3919 skb = validate_xmit_skb_list(skb, dev, &again);
3920 if (skb != orig_skb)
3921 goto drop;
3922
3923 skb_set_queue_mapping(skb, queue_id);
3924 txq = skb_get_tx_queue(dev, skb);
3925
3926 local_bh_disable();
3927
3928 HARD_TX_LOCK(dev, txq, smp_processor_id());
3929 if (!netif_xmit_frozen_or_drv_stopped(txq))
3930 ret = netdev_start_xmit(skb, dev, txq, false);
3931 HARD_TX_UNLOCK(dev, txq);
3932
3933 local_bh_enable();
3934
3935 if (!dev_xmit_complete(ret))
3936 kfree_skb(skb);
3937
3938 return ret;
3939drop:
3940 atomic_long_inc(&dev->tx_dropped);
3941 kfree_skb_list(skb);
3942 return NET_XMIT_DROP;
3943}
3944EXPORT_SYMBOL(dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
tchardingeb13da12017-02-09 17:56:06 +11003946/*************************************************************************
3947 * Receiver routines
3948 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003950int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00003951EXPORT_SYMBOL(netdev_max_backlog);
3952
Eric Dumazet3b098e22010-05-15 23:57:10 -07003953int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003954int netdev_budget __read_mostly = 300;
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04003955unsigned int __read_mostly netdev_budget_usecs = 2000;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01003956int weight_p __read_mostly = 64; /* old backlog weight */
3957int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
3958int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
3959int dev_rx_weight __read_mostly = 64;
3960int dev_tx_weight __read_mostly = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003962/* Called with irq disabled */
3963static inline void ____napi_schedule(struct softnet_data *sd,
3964 struct napi_struct *napi)
3965{
3966 list_add_tail(&napi->poll_list, &sd->poll_list);
3967 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3968}
3969
Eric Dumazetdf334542010-03-24 19:13:54 +00003970#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07003971
3972/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00003973struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07003974EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003975u32 rps_cpu_mask __read_mostly;
3976EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07003977
Ingo Molnarc5905af2012-02-24 08:31:31 +01003978struct static_key rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04003979EXPORT_SYMBOL(rps_needed);
Eric Dumazet13bfff22016-12-07 08:29:10 -08003980struct static_key rfs_needed __read_mostly;
3981EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00003982
Ben Hutchingsc4454772011-01-19 11:03:53 +00003983static struct rps_dev_flow *
3984set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3985 struct rps_dev_flow *rflow, u16 next_cpu)
3986{
Eric Dumazeta31196b2015-04-25 09:35:24 -07003987 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00003988#ifdef CONFIG_RFS_ACCEL
3989 struct netdev_rx_queue *rxqueue;
3990 struct rps_dev_flow_table *flow_table;
3991 struct rps_dev_flow *old_rflow;
3992 u32 flow_id;
3993 u16 rxq_index;
3994 int rc;
3995
3996 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00003997 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3998 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00003999 goto out;
4000 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4001 if (rxq_index == skb_get_rx_queue(skb))
4002 goto out;
4003
4004 rxqueue = dev->_rx + rxq_index;
4005 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4006 if (!flow_table)
4007 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07004008 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004009 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4010 rxq_index, flow_id);
4011 if (rc < 0)
4012 goto out;
4013 old_rflow = rflow;
4014 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00004015 rflow->filter = rc;
4016 if (old_rflow->filter == rflow->filter)
4017 old_rflow->filter = RPS_NO_FILTER;
4018 out:
4019#endif
4020 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00004021 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004022 }
4023
Ben Hutchings09994d12011-10-03 04:42:46 +00004024 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004025 return rflow;
4026}
4027
Tom Herbert0a9627f2010-03-16 08:03:29 +00004028/*
4029 * get_rps_cpu is called from netif_receive_skb and returns the target
4030 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004031 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004032 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004033static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4034 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004035{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004036 const struct rps_sock_flow_table *sock_flow_table;
4037 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004038 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004039 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004040 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004041 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004042 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004043
Tom Herbert0a9627f2010-03-16 08:03:29 +00004044 if (skb_rx_queue_recorded(skb)) {
4045 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004046
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004047 if (unlikely(index >= dev->real_num_rx_queues)) {
4048 WARN_ONCE(dev->real_num_rx_queues > 1,
4049 "%s received packet on queue %u, but number "
4050 "of RX queues is %u\n",
4051 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004052 goto done;
4053 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004054 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004055 }
4056
Eric Dumazet567e4b72015-02-06 12:59:01 -08004057 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4058
4059 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4060 map = rcu_dereference(rxqueue->rps_map);
4061 if (!flow_table && !map)
4062 goto done;
4063
Changli Gao2d47b452010-08-17 19:00:56 +00004064 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004065 hash = skb_get_hash(skb);
4066 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004067 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004068
Tom Herbertfec5e652010-04-16 16:01:27 -07004069 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4070 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004071 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004072 u32 next_cpu;
4073 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004074
Eric Dumazet567e4b72015-02-06 12:59:01 -08004075 /* First check into global flow table if there is a match */
4076 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4077 if ((ident ^ hash) & ~rps_cpu_mask)
4078 goto try_rps;
4079
4080 next_cpu = ident & rps_cpu_mask;
4081
4082 /* OK, now we know there is a match,
4083 * we can look at the local (per receive queue) flow table
4084 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004085 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004086 tcpu = rflow->cpu;
4087
Tom Herbertfec5e652010-04-16 16:01:27 -07004088 /*
4089 * If the desired CPU (where last recvmsg was done) is
4090 * different from current CPU (one in the rx-queue flow
4091 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004092 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004093 * - Current CPU is offline.
4094 * - The current CPU's queue tail has advanced beyond the
4095 * last packet that was enqueued using this table entry.
4096 * This guarantees that all previous packets for the flow
4097 * have been dequeued, thus preserving in order delivery.
4098 */
4099 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004100 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004101 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004102 rflow->last_qtail)) >= 0)) {
4103 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004104 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004105 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004106
Eric Dumazeta31196b2015-04-25 09:35:24 -07004107 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004108 *rflowp = rflow;
4109 cpu = tcpu;
4110 goto done;
4111 }
4112 }
4113
Eric Dumazet567e4b72015-02-06 12:59:01 -08004114try_rps:
4115
Tom Herbert0a9627f2010-03-16 08:03:29 +00004116 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004117 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004118 if (cpu_online(tcpu)) {
4119 cpu = tcpu;
4120 goto done;
4121 }
4122 }
4123
4124done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004125 return cpu;
4126}
4127
Ben Hutchingsc4454772011-01-19 11:03:53 +00004128#ifdef CONFIG_RFS_ACCEL
4129
4130/**
4131 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4132 * @dev: Device on which the filter was set
4133 * @rxq_index: RX queue index
4134 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4135 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4136 *
4137 * Drivers that implement ndo_rx_flow_steer() should periodically call
4138 * this function for each installed filter and remove the filters for
4139 * which it returns %true.
4140 */
4141bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4142 u32 flow_id, u16 filter_id)
4143{
4144 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4145 struct rps_dev_flow_table *flow_table;
4146 struct rps_dev_flow *rflow;
4147 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004148 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004149
4150 rcu_read_lock();
4151 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4152 if (flow_table && flow_id <= flow_table->mask) {
4153 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004154 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004155 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004156 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4157 rflow->last_qtail) <
4158 (int)(10 * flow_table->mask)))
4159 expire = false;
4160 }
4161 rcu_read_unlock();
4162 return expire;
4163}
4164EXPORT_SYMBOL(rps_may_expire_flow);
4165
4166#endif /* CONFIG_RFS_ACCEL */
4167
Tom Herbert0a9627f2010-03-16 08:03:29 +00004168/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004169static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004170{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004171 struct softnet_data *sd = data;
4172
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004173 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004174 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004175}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004176
Tom Herbertfec5e652010-04-16 16:01:27 -07004177#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004178
4179/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004180 * Check if this softnet_data structure is another cpu one
4181 * If yes, queue it to our IPI list and return 1
4182 * If no, return 0
4183 */
4184static int rps_ipi_queued(struct softnet_data *sd)
4185{
4186#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004187 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004188
4189 if (sd != mysd) {
4190 sd->rps_ipi_next = mysd->rps_ipi_list;
4191 mysd->rps_ipi_list = sd;
4192
4193 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4194 return 1;
4195 }
4196#endif /* CONFIG_RPS */
4197 return 0;
4198}
4199
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004200#ifdef CONFIG_NET_FLOW_LIMIT
4201int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4202#endif
4203
4204static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4205{
4206#ifdef CONFIG_NET_FLOW_LIMIT
4207 struct sd_flow_limit *fl;
4208 struct softnet_data *sd;
4209 unsigned int old_flow, new_flow;
4210
4211 if (qlen < (netdev_max_backlog >> 1))
4212 return false;
4213
Christoph Lameter903ceff2014-08-17 12:30:35 -05004214 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004215
4216 rcu_read_lock();
4217 fl = rcu_dereference(sd->flow_limit);
4218 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004219 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004220 old_flow = fl->history[fl->history_head];
4221 fl->history[fl->history_head] = new_flow;
4222
4223 fl->history_head++;
4224 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4225
4226 if (likely(fl->buckets[old_flow]))
4227 fl->buckets[old_flow]--;
4228
4229 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4230 fl->count++;
4231 rcu_read_unlock();
4232 return true;
4233 }
4234 }
4235 rcu_read_unlock();
4236#endif
4237 return false;
4238}
4239
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004240/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004241 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4242 * queue (may be a remote CPU queue).
4243 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004244static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4245 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004246{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004247 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004248 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004249 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004250
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004251 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004252
4253 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004254
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004255 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004256 if (!netif_running(skb->dev))
4257 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004258 qlen = skb_queue_len(&sd->input_pkt_queue);
4259 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004260 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004261enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004262 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004263 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004264 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004265 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004266 return NET_RX_SUCCESS;
4267 }
4268
Eric Dumazetebda37c22010-05-06 23:51:21 +00004269 /* Schedule NAPI for backlog device
4270 * We can use non atomic operation since we own the queue lock
4271 */
4272 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004273 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004274 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004275 }
4276 goto enqueue;
4277 }
4278
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004279drop:
Changli Gaodee42872010-05-02 05:42:16 +00004280 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004281 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004282
Tom Herbert0a9627f2010-03-16 08:03:29 +00004283 local_irq_restore(flags);
4284
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004285 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004286 kfree_skb(skb);
4287 return NET_RX_DROP;
4288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004290static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4291{
4292 struct net_device *dev = skb->dev;
4293 struct netdev_rx_queue *rxqueue;
4294
4295 rxqueue = dev->_rx;
4296
4297 if (skb_rx_queue_recorded(skb)) {
4298 u16 index = skb_get_rx_queue(skb);
4299
4300 if (unlikely(index >= dev->real_num_rx_queues)) {
4301 WARN_ONCE(dev->real_num_rx_queues > 1,
4302 "%s received packet on queue %u, but number "
4303 "of RX queues is %u\n",
4304 dev->name, index, dev->real_num_rx_queues);
4305
4306 return rxqueue; /* Return first rxqueue */
4307 }
4308 rxqueue += index;
4309 }
4310 return rxqueue;
4311}
4312
John Fastabendd4455162017-07-17 09:26:45 -07004313static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004314 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004315 struct bpf_prog *xdp_prog)
4316{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004317 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004318 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004319 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004320 __be16 orig_eth_type;
4321 struct ethhdr *eth;
4322 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004323 int hlen, off;
4324 u32 mac_len;
4325
4326 /* Reinjected packets coming from act_mirred or similar should
4327 * not get XDP generic processing.
4328 */
Paolo Abenicd11b1642018-07-30 14:30:44 +02004329 if (skb_cloned(skb) || skb_is_tc_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004330 return XDP_PASS;
4331
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004332 /* XDP packets must be linear and must have sufficient headroom
4333 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4334 * native XDP provides, thus we need to do it here as well.
4335 */
4336 if (skb_is_nonlinear(skb) ||
4337 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4338 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4339 int troom = skb->tail + skb->data_len - skb->end;
4340
4341 /* In case we have to go down the path and also linearize,
4342 * then lets do the pskb_expand_head() work just once here.
4343 */
4344 if (pskb_expand_head(skb,
4345 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4346 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4347 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004348 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004349 goto do_drop;
4350 }
John Fastabendd4455162017-07-17 09:26:45 -07004351
4352 /* The XDP program wants to see the packet starting at the MAC
4353 * header.
4354 */
4355 mac_len = skb->data - skb_mac_header(skb);
4356 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004357 xdp->data = skb->data - mac_len;
4358 xdp->data_meta = xdp->data;
4359 xdp->data_end = xdp->data + hlen;
4360 xdp->data_hard_start = skb->data - skb_headroom(skb);
4361 orig_data_end = xdp->data_end;
4362 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004363 eth = (struct ethhdr *)xdp->data;
4364 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4365 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004366
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004367 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004368 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004369
Björn Töpel02671e22018-05-02 13:01:30 +02004370 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004371
Björn Töpel02671e22018-05-02 13:01:30 +02004372 off = xdp->data - orig_data;
John Fastabendd4455162017-07-17 09:26:45 -07004373 if (off > 0)
4374 __skb_pull(skb, off);
4375 else if (off < 0)
4376 __skb_push(skb, -off);
Edward Cree92dd5452017-09-19 18:45:56 +01004377 skb->mac_header += off;
John Fastabendd4455162017-07-17 09:26:45 -07004378
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004379 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4380 * pckt.
4381 */
Björn Töpel02671e22018-05-02 13:01:30 +02004382 off = orig_data_end - xdp->data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004383 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004384 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004385 skb->len -= off;
Björn Töpel02671e22018-05-02 13:01:30 +02004386
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004387 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004388
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004389 /* check if XDP changed eth hdr such SKB needs update */
4390 eth = (struct ethhdr *)xdp->data;
4391 if ((orig_eth_type != eth->h_proto) ||
4392 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4393 __skb_push(skb, ETH_HLEN);
4394 skb->protocol = eth_type_trans(skb, skb->dev);
4395 }
4396
John Fastabendd4455162017-07-17 09:26:45 -07004397 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004398 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004399 case XDP_TX:
4400 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004401 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004402 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004403 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004404 if (metalen)
4405 skb_metadata_set(skb, metalen);
4406 break;
John Fastabendd4455162017-07-17 09:26:45 -07004407 default:
4408 bpf_warn_invalid_xdp_action(act);
4409 /* fall through */
4410 case XDP_ABORTED:
4411 trace_xdp_exception(skb->dev, xdp_prog, act);
4412 /* fall through */
4413 case XDP_DROP:
4414 do_drop:
4415 kfree_skb(skb);
4416 break;
4417 }
4418
4419 return act;
4420}
4421
4422/* When doing generic XDP we have to bypass the qdisc layer and the
4423 * network taps in order to match in-driver-XDP behavior.
4424 */
Jason Wang7c497472017-08-11 19:41:17 +08004425void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004426{
4427 struct net_device *dev = skb->dev;
4428 struct netdev_queue *txq;
4429 bool free_skb = true;
4430 int cpu, rc;
4431
4432 txq = netdev_pick_tx(dev, skb, NULL);
4433 cpu = smp_processor_id();
4434 HARD_TX_LOCK(dev, txq, cpu);
4435 if (!netif_xmit_stopped(txq)) {
4436 rc = netdev_start_xmit(skb, dev, txq, 0);
4437 if (dev_xmit_complete(rc))
4438 free_skb = false;
4439 }
4440 HARD_TX_UNLOCK(dev, txq);
4441 if (free_skb) {
4442 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4443 kfree_skb(skb);
4444 }
4445}
Jason Wang7c497472017-08-11 19:41:17 +08004446EXPORT_SYMBOL_GPL(generic_xdp_tx);
John Fastabendd4455162017-07-17 09:26:45 -07004447
Davidlohr Bueso02786472018-05-08 09:07:02 -07004448static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004449
Jason Wang7c497472017-08-11 19:41:17 +08004450int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004451{
John Fastabendd4455162017-07-17 09:26:45 -07004452 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004453 struct xdp_buff xdp;
4454 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004455 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004456
Björn Töpel02671e22018-05-02 13:01:30 +02004457 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004458 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004459 switch (act) {
4460 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004461 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004462 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004463 if (err)
4464 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004465 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004466 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004467 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004468 break;
4469 }
John Fastabendd4455162017-07-17 09:26:45 -07004470 return XDP_DROP;
4471 }
4472 }
4473 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004474out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004475 kfree_skb(skb);
4476 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004477}
Jason Wang7c497472017-08-11 19:41:17 +08004478EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004479
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004480static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004482 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Eric Dumazet588f0332011-11-15 04:12:55 +00004484 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Koki Sanagicf66ba52010-08-23 18:45:02 +09004486 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004487
Davidlohr Bueso02786472018-05-08 09:07:02 -07004488 if (static_branch_unlikely(&generic_xdp_needed_key)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07004489 int ret;
4490
4491 preempt_disable();
4492 rcu_read_lock();
4493 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4494 rcu_read_unlock();
4495 preempt_enable();
John Fastabendd4455162017-07-17 09:26:45 -07004496
John Fastabend6103aa92017-07-17 09:27:50 -07004497 /* Consider XDP consuming the packet a success from
4498 * the netdev point of view we do not want to count
4499 * this as an error.
4500 */
John Fastabendd4455162017-07-17 09:26:45 -07004501 if (ret != XDP_PASS)
John Fastabend6103aa92017-07-17 09:27:50 -07004502 return NET_RX_SUCCESS;
John Fastabendd4455162017-07-17 09:26:45 -07004503 }
4504
Eric Dumazetdf334542010-03-24 19:13:54 +00004505#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004506 if (static_key_false(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004507 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004508 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Changli Gaocece1942010-08-07 20:35:43 -07004510 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004511 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004512
4513 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004514 if (cpu < 0)
4515 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004516
4517 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4518
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004519 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004520 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004521 } else
4522#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004523 {
4524 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004525
Tom Herbertfec5e652010-04-16 16:01:27 -07004526 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4527 put_cpu();
4528 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004531
4532/**
4533 * netif_rx - post buffer to the network code
4534 * @skb: buffer to post
4535 *
4536 * This function receives a packet from a device driver and queues it for
4537 * the upper (protocol) levels to process. It always succeeds. The buffer
4538 * may be dropped during processing for congestion control or by the
4539 * protocol layers.
4540 *
4541 * return values:
4542 * NET_RX_SUCCESS (no congestion)
4543 * NET_RX_DROP (packet was dropped)
4544 *
4545 */
4546
4547int netif_rx(struct sk_buff *skb)
4548{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004549 int ret;
4550
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004551 trace_netif_rx_entry(skb);
4552
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004553 ret = netif_rx_internal(skb);
4554 trace_netif_rx_exit(ret);
4555
4556 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004557}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004558EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560int netif_rx_ni(struct sk_buff *skb)
4561{
4562 int err;
4563
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004564 trace_netif_rx_ni_entry(skb);
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004567 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (local_softirq_pending())
4569 do_softirq();
4570 preempt_enable();
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05004571 trace_netif_rx_ni_exit(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
4573 return err;
4574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575EXPORT_SYMBOL(netif_rx_ni);
4576
Emese Revfy0766f782016-06-20 20:42:34 +02004577static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004579 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 if (sd->completion_queue) {
4582 struct sk_buff *clist;
4583
4584 local_irq_disable();
4585 clist = sd->completion_queue;
4586 sd->completion_queue = NULL;
4587 local_irq_enable();
4588
4589 while (clist) {
4590 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 clist = clist->next;
4593
Reshetova, Elena63354792017-06-30 13:07:58 +03004594 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004595 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4596 trace_consume_skb(skb);
4597 else
4598 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004599
4600 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4601 __kfree_skb(skb);
4602 else
4603 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004605
4606 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 }
4608
4609 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004610 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 local_irq_disable();
4613 head = sd->output_queue;
4614 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004615 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 local_irq_enable();
4617
4618 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004619 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004620 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 head = head->next_sched;
4623
John Fastabend6b3ba912017-12-07 09:54:25 -08004624 if (!(q->flags & TCQ_F_NOLOCK)) {
4625 root_lock = qdisc_lock(q);
4626 spin_lock(root_lock);
4627 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004628 /* We need to make sure head->next_sched is read
4629 * before clearing __QDISC_STATE_SCHED
4630 */
4631 smp_mb__before_atomic();
4632 clear_bit(__QDISC_STATE_SCHED, &q->state);
4633 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004634 if (root_lock)
4635 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 }
4637 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004638
4639 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004642#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004643/* This hook is defined here for ATM LANE */
4644int (*br_fdb_test_addr_hook)(struct net_device *dev,
4645 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004646EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004647#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004649static inline struct sk_buff *
4650sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4651 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004652{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004653#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004654 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004655 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004656
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004657 /* If there's at least one ingress present somewhere (so
4658 * we get here via enabled static key), remaining devices
4659 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004660 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004661 */
Jiri Pirko46209402017-11-03 11:46:25 +01004662 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004663 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004664
Herbert Xuf697c3e2007-10-14 00:38:47 -07004665 if (*pt_prev) {
4666 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4667 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004668 }
4669
Florian Westphal33654952015-05-14 00:36:28 +02004670 qdisc_skb_cb(skb)->pkt_len = skb->len;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004671 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004672 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004673
Jiri Pirko46209402017-11-03 11:46:25 +01004674 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004675 case TC_ACT_OK:
4676 case TC_ACT_RECLASSIFY:
4677 skb->tc_index = TC_H_MIN(cl_res.classid);
4678 break;
4679 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004680 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004681 kfree_skb(skb);
4682 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004683 case TC_ACT_STOLEN:
4684 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004685 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004686 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004687 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004688 case TC_ACT_REDIRECT:
4689 /* skb_mac_header check was done by cls/act_bpf, so
4690 * we can safely push the L2 header back before
4691 * redirecting to another netdev
4692 */
4693 __skb_push(skb, skb->mac_len);
4694 skb_do_redirect(skb);
4695 return NULL;
Paolo Abenicd11b1642018-07-30 14:30:44 +02004696 case TC_ACT_REINSERT:
4697 /* this does not scrub the packet, and updates stats on error */
4698 skb_tc_reinsert(skb, &cl_res);
4699 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004700 default:
4701 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004702 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004703#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004704 return skb;
4705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004707/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004708 * netdev_is_rx_handler_busy - check if receive handler is registered
4709 * @dev: device to check
4710 *
4711 * Check if a receive handler is already registered for a given device.
4712 * Return true if there one.
4713 *
4714 * The caller must hold the rtnl_mutex.
4715 */
4716bool netdev_is_rx_handler_busy(struct net_device *dev)
4717{
4718 ASSERT_RTNL();
4719 return dev && rtnl_dereference(dev->rx_handler);
4720}
4721EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4722
4723/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004724 * netdev_rx_handler_register - register receive handler
4725 * @dev: device to register a handler for
4726 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004727 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004728 *
Masanari Iidae2278672014-02-18 22:54:36 +09004729 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004730 * called from __netif_receive_skb. A negative errno code is returned
4731 * on a failure.
4732 *
4733 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004734 *
4735 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004736 */
4737int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004738 rx_handler_func_t *rx_handler,
4739 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004740{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004741 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004742 return -EBUSY;
4743
Paolo Abenif54262502018-03-09 10:39:24 +01004744 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4745 return -EINVAL;
4746
Eric Dumazet00cfec32013-03-29 03:01:22 +00004747 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004748 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004749 rcu_assign_pointer(dev->rx_handler, rx_handler);
4750
4751 return 0;
4752}
4753EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4754
4755/**
4756 * netdev_rx_handler_unregister - unregister receive handler
4757 * @dev: device to unregister a handler from
4758 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004759 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004760 *
4761 * The caller must hold the rtnl_mutex.
4762 */
4763void netdev_rx_handler_unregister(struct net_device *dev)
4764{
4765
4766 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004767 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004768 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4769 * section has a guarantee to see a non NULL rx_handler_data
4770 * as well.
4771 */
4772 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004773 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004774}
4775EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4776
Mel Gormanb4b9e352012-07-31 16:44:26 -07004777/*
4778 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4779 * the special handling of PFMEMALLOC skbs.
4780 */
4781static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4782{
4783 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004784 case htons(ETH_P_ARP):
4785 case htons(ETH_P_IP):
4786 case htons(ETH_P_IPV6):
4787 case htons(ETH_P_8021Q):
4788 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004789 return true;
4790 default:
4791 return false;
4792 }
4793}
4794
Pablo Neirae687ad62015-05-13 18:19:38 +02004795static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4796 int *ret, struct net_device *orig_dev)
4797{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004798#ifdef CONFIG_NETFILTER_INGRESS
Pablo Neirae687ad62015-05-13 18:19:38 +02004799 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004800 int ingress_retval;
4801
Pablo Neirae687ad62015-05-13 18:19:38 +02004802 if (*pt_prev) {
4803 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4804 *pt_prev = NULL;
4805 }
4806
Aaron Conole2c1e2702016-09-21 11:35:03 -04004807 rcu_read_lock();
4808 ingress_retval = nf_hook_ingress(skb);
4809 rcu_read_unlock();
4810 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004811 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004812#endif /* CONFIG_NETFILTER_INGRESS */
Pablo Neirae687ad62015-05-13 18:19:38 +02004813 return 0;
4814}
Pablo Neirae687ad62015-05-13 18:19:38 +02004815
Edward Cree88eb1942018-07-02 16:13:56 +01004816static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
4817 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
4819 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004820 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004821 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004822 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004824 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Eric Dumazet588f0332011-11-15 04:12:55 +00004826 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07004827
Koki Sanagicf66ba52010-08-23 18:45:02 +09004828 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08004829
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07004830 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00004831
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07004832 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00004833 if (!skb_transport_header_was_set(skb))
4834 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00004835 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 pt_prev = NULL;
4838
David S. Miller63d8ea72011-02-28 10:48:59 -08004839another_round:
David S. Millerb6858172012-07-23 16:27:54 -07004840 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08004841
4842 __this_cpu_inc(softnet_data.processed);
4843
Patrick McHardy8ad227f2013-04-19 02:04:31 +00004844 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4845 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004846 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004847 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004848 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004849 }
4850
Willem de Bruijne7246e12017-01-07 17:06:35 -05004851 if (skb_skip_tc_classify(skb))
4852 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
David S. Miller9754e292013-02-14 15:57:38 -05004854 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07004855 goto skip_taps;
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08004858 if (pt_prev)
4859 ret = deliver_skb(skb, pt_prev, orig_dev);
4860 pt_prev = ptype;
4861 }
4862
4863 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
4864 if (pt_prev)
4865 ret = deliver_skb(skb, pt_prev, orig_dev);
4866 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868
Mel Gormanb4b9e352012-07-31 16:44:26 -07004869skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02004870#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004871 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004872 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02004873 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004874 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02004875
4876 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004877 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02004878 }
Pablo Neira1cf519002015-05-13 18:19:37 +02004879#endif
Willem de Bruijna5135bc2017-01-07 17:06:36 -05004880 skb_reset_tc(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05004881skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05004882 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07004883 goto drop;
4884
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004885 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00004886 if (pt_prev) {
4887 ret = deliver_skb(skb, pt_prev, orig_dev);
4888 pt_prev = NULL;
4889 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004890 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00004891 goto another_round;
4892 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004893 goto out;
John Fastabend24257172011-10-10 09:16:41 +00004894 }
4895
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004896 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004897 if (rx_handler) {
4898 if (pt_prev) {
4899 ret = deliver_skb(skb, pt_prev, orig_dev);
4900 pt_prev = NULL;
4901 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004902 switch (rx_handler(&skb)) {
4903 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00004904 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004905 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004906 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08004907 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004908 case RX_HANDLER_EXACT:
4909 deliver_exact = true;
4910 case RX_HANDLER_PASS:
4911 break;
4912 default:
4913 BUG();
4914 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004917 if (unlikely(skb_vlan_tag_present(skb))) {
4918 if (skb_vlan_tag_get_id(skb))
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004919 skb->pkt_type = PACKET_OTHERHOST;
4920 /* Note: we might in the future use prio bits
4921 * and set skb->priority like in vlan_do_receive()
4922 * For the time being, just ignore Priority Code Point
4923 */
Michał Mirosławb18175242018-11-09 00:18:02 +01004924 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004925 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08004928
4929 /* deliver only exact match when indicated */
4930 if (likely(!deliver_exact)) {
4931 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4932 &ptype_base[ntohs(type) &
4933 PTYPE_HASH_MASK]);
4934 }
4935
4936 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4937 &orig_dev->ptype_specific);
4938
4939 if (unlikely(skb->dev != orig_dev)) {
4940 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4941 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943
4944 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004945 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00004946 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01004947 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07004949drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05004950 if (!deliver_exact)
4951 atomic_long_inc(&skb->dev->rx_dropped);
4952 else
4953 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 kfree_skb(skb);
4955 /* Jamal, now you will not able to escape explaining
4956 * me how you were going to use this. :-)
4957 */
4958 ret = NET_RX_DROP;
4959 }
4960
Julian Anastasov2c17d272015-07-09 09:59:10 +03004961out:
David S. Miller9754e292013-02-14 15:57:38 -05004962 return ret;
4963}
4964
Edward Cree88eb1942018-07-02 16:13:56 +01004965static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
4966{
4967 struct net_device *orig_dev = skb->dev;
4968 struct packet_type *pt_prev = NULL;
4969 int ret;
4970
4971 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
4972 if (pt_prev)
4973 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
4974 return ret;
4975}
4976
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02004977/**
4978 * netif_receive_skb_core - special purpose version of netif_receive_skb
4979 * @skb: buffer to process
4980 *
4981 * More direct receive version of netif_receive_skb(). It should
4982 * only be used by callers that have a need to skip RPS and Generic XDP.
4983 * Caller must also take care of handling if (page_is_)pfmemalloc.
4984 *
4985 * This function may only be called from softirq context and interrupts
4986 * should be enabled.
4987 *
4988 * Return values (usually ignored):
4989 * NET_RX_SUCCESS: no congestion
4990 * NET_RX_DROP: packet was dropped
4991 */
4992int netif_receive_skb_core(struct sk_buff *skb)
4993{
4994 int ret;
4995
4996 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01004997 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02004998 rcu_read_unlock();
4999
5000 return ret;
5001}
5002EXPORT_SYMBOL(netif_receive_skb_core);
5003
Edward Cree88eb1942018-07-02 16:13:56 +01005004static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5005 struct packet_type *pt_prev,
5006 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01005007{
5008 struct sk_buff *skb, *next;
5009
Edward Cree88eb1942018-07-02 16:13:56 +01005010 if (!pt_prev)
5011 return;
5012 if (list_empty(head))
5013 return;
Edward Cree17266ee2018-07-02 16:14:12 +01005014 if (pt_prev->list_func != NULL)
5015 pt_prev->list_func(head, pt_prev, orig_dev);
5016 else
5017 list_for_each_entry_safe(skb, next, head, list)
5018 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01005019}
5020
5021static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5022{
5023 /* Fast-path assumptions:
5024 * - There is no RX handler.
5025 * - Only one packet_type matches.
5026 * If either of these fails, we will end up doing some per-packet
5027 * processing in-line, then handling the 'last ptype' for the whole
5028 * sublist. This can't cause out-of-order delivery to any single ptype,
5029 * because the 'last ptype' must be constant across the sublist, and all
5030 * other ptypes are handled per-packet.
5031 */
5032 /* Current (common) ptype of sublist */
5033 struct packet_type *pt_curr = NULL;
5034 /* Current (common) orig_dev of sublist */
5035 struct net_device *od_curr = NULL;
5036 struct list_head sublist;
5037 struct sk_buff *skb, *next;
5038
Edward Cree9af86f92018-07-09 18:10:19 +01005039 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005040 list_for_each_entry_safe(skb, next, head, list) {
5041 struct net_device *orig_dev = skb->dev;
5042 struct packet_type *pt_prev = NULL;
5043
Edward Cree22f6bbb2018-12-04 17:37:57 +00005044 skb_list_del_init(skb);
Edward Cree88eb1942018-07-02 16:13:56 +01005045 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005046 if (!pt_prev)
5047 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005048 if (pt_curr != pt_prev || od_curr != orig_dev) {
5049 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005050 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5051 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005052 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005053 pt_curr = pt_prev;
5054 od_curr = orig_dev;
5055 }
Edward Cree9af86f92018-07-09 18:10:19 +01005056 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005057 }
5058
5059 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005060 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005061}
5062
David S. Miller9754e292013-02-14 15:57:38 -05005063static int __netif_receive_skb(struct sk_buff *skb)
5064{
5065 int ret;
5066
5067 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005068 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005069
5070 /*
5071 * PFMEMALLOC skbs are special, they should
5072 * - be delivered to SOCK_MEMALLOC sockets only
5073 * - stay away from userspace
5074 * - have bounded memory usage
5075 *
5076 * Use PF_MEMALLOC as this saves us from propagating the allocation
5077 * context down to all allocation sites.
5078 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005079 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005080 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005081 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005082 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005083 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return ret;
5086}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005087
Edward Cree4ce00172018-07-02 16:13:40 +01005088static void __netif_receive_skb_list(struct list_head *head)
5089{
5090 unsigned long noreclaim_flag = 0;
5091 struct sk_buff *skb, *next;
5092 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5093
5094 list_for_each_entry_safe(skb, next, head, list) {
5095 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5096 struct list_head sublist;
5097
5098 /* Handle the previous sublist */
5099 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005100 if (!list_empty(&sublist))
5101 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005102 pfmemalloc = !pfmemalloc;
5103 /* See comments in __netif_receive_skb */
5104 if (pfmemalloc)
5105 noreclaim_flag = memalloc_noreclaim_save();
5106 else
5107 memalloc_noreclaim_restore(noreclaim_flag);
5108 }
5109 }
5110 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005111 if (!list_empty(head))
5112 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005113 /* Restore pflags */
5114 if (pfmemalloc)
5115 memalloc_noreclaim_restore(noreclaim_flag);
5116}
5117
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005118static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005119{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005120 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005121 struct bpf_prog *new = xdp->prog;
5122 int ret = 0;
5123
5124 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005125 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005126 rcu_assign_pointer(dev->xdp_prog, new);
5127 if (old)
5128 bpf_prog_put(old);
5129
5130 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005131 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005132 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005133 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005134 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005135 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005136 }
5137 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005138
5139 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07005140 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04005141 break;
5142
5143 default:
5144 ret = -EINVAL;
5145 break;
5146 }
5147
5148 return ret;
5149}
5150
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005151static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005152{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005153 int ret;
5154
Eric Dumazet588f0332011-11-15 04:12:55 +00005155 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005156
Richard Cochranc1f19b52010-07-17 08:49:36 +00005157 if (skb_defer_rx_timestamp(skb))
5158 return NET_RX_SUCCESS;
5159
Davidlohr Bueso02786472018-05-08 09:07:02 -07005160 if (static_branch_unlikely(&generic_xdp_needed_key)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07005161 int ret;
David S. Millerb5cdae32017-04-18 15:36:58 -04005162
John Fastabendbbbe2112017-09-08 14:00:30 -07005163 preempt_disable();
5164 rcu_read_lock();
5165 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5166 rcu_read_unlock();
5167 preempt_enable();
5168
5169 if (ret != XDP_PASS)
John Fastabendd4455162017-07-17 09:26:45 -07005170 return NET_RX_DROP;
David S. Millerb5cdae32017-04-18 15:36:58 -04005171 }
5172
John Fastabendbbbe2112017-09-08 14:00:30 -07005173 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005174#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01005175 if (static_key_false(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005176 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005177 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005178
Eric Dumazet3b098e22010-05-15 23:57:10 -07005179 if (cpu >= 0) {
5180 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5181 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005182 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005183 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005184 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005185#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005186 ret = __netif_receive_skb(skb);
5187 rcu_read_unlock();
5188 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005189}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005190
Edward Cree7da517a2018-07-02 16:13:24 +01005191static void netif_receive_skb_list_internal(struct list_head *head)
5192{
5193 struct bpf_prog *xdp_prog = NULL;
5194 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005195 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005196
Edward Cree8c057ef2018-07-09 18:09:54 +01005197 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005198 list_for_each_entry_safe(skb, next, head, list) {
5199 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005200 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005201 if (!skb_defer_rx_timestamp(skb))
5202 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005203 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005204 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005205
5206 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5207 preempt_disable();
5208 rcu_read_lock();
5209 list_for_each_entry_safe(skb, next, head, list) {
5210 xdp_prog = rcu_dereference(skb->dev->xdp_prog);
Edward Cree22f6bbb2018-12-04 17:37:57 +00005211 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005212 if (do_xdp_generic(xdp_prog, skb) == XDP_PASS)
5213 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005214 }
5215 rcu_read_unlock();
5216 preempt_enable();
Edward Cree8c057ef2018-07-09 18:09:54 +01005217 /* Put passed packets back on main list */
5218 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005219 }
5220
5221 rcu_read_lock();
5222#ifdef CONFIG_RPS
5223 if (static_key_false(&rps_needed)) {
5224 list_for_each_entry_safe(skb, next, head, list) {
5225 struct rps_dev_flow voidflow, *rflow = &voidflow;
5226 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5227
5228 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005229 /* Will be handled, remove from list */
Edward Cree22f6bbb2018-12-04 17:37:57 +00005230 skb_list_del_init(skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005231 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005232 }
5233 }
5234 }
5235#endif
5236 __netif_receive_skb_list(head);
5237 rcu_read_unlock();
5238}
5239
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005240/**
5241 * netif_receive_skb - process receive buffer from network
5242 * @skb: buffer to process
5243 *
5244 * netif_receive_skb() is the main receive data processing function.
5245 * It always succeeds. The buffer may be dropped during processing
5246 * for congestion control or by the protocol layers.
5247 *
5248 * This function may only be called from softirq context and interrupts
5249 * should be enabled.
5250 *
5251 * Return values (usually ignored):
5252 * NET_RX_SUCCESS: no congestion
5253 * NET_RX_DROP: packet was dropped
5254 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005255int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005256{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005257 int ret;
5258
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005259 trace_netif_receive_skb_entry(skb);
5260
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005261 ret = netif_receive_skb_internal(skb);
5262 trace_netif_receive_skb_exit(ret);
5263
5264 return ret;
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005265}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005266EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
Edward Creef6ad8c12018-07-02 16:12:45 +01005268/**
5269 * netif_receive_skb_list - process many receive buffers from network
5270 * @head: list of skbs to process.
5271 *
Edward Cree7da517a2018-07-02 16:13:24 +01005272 * Since return value of netif_receive_skb() is normally ignored, and
5273 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005274 *
5275 * This function may only be called from softirq context and interrupts
5276 * should be enabled.
5277 */
5278void netif_receive_skb_list(struct list_head *head)
5279{
Edward Cree7da517a2018-07-02 16:13:24 +01005280 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005281
Edward Creeb9f463d2018-07-02 16:14:44 +01005282 if (list_empty(head))
5283 return;
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005284 if (trace_netif_receive_skb_list_entry_enabled()) {
5285 list_for_each_entry(skb, head, list)
5286 trace_netif_receive_skb_list_entry(skb);
5287 }
Edward Cree7da517a2018-07-02 16:13:24 +01005288 netif_receive_skb_list_internal(head);
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005289 trace_netif_receive_skb_list_exit(0);
Edward Creef6ad8c12018-07-02 16:12:45 +01005290}
5291EXPORT_SYMBOL(netif_receive_skb_list);
5292
Eric Dumazet41852492016-08-26 12:50:39 -07005293DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005294
5295/* Network device is going away, flush any packets still pending */
5296static void flush_backlog(struct work_struct *work)
5297{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005298 struct sk_buff *skb, *tmp;
5299 struct softnet_data *sd;
5300
5301 local_bh_disable();
5302 sd = this_cpu_ptr(&softnet_data);
5303
5304 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005305 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005306 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005307 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005308 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005309 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005310 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005311 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005312 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005313 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005314 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005315
5316 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005317 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005318 __skb_unlink(skb, &sd->process_queue);
5319 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005320 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005321 }
5322 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005323 local_bh_enable();
5324}
5325
Eric Dumazet41852492016-08-26 12:50:39 -07005326static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005327{
5328 unsigned int cpu;
5329
5330 get_online_cpus();
5331
Eric Dumazet41852492016-08-26 12:50:39 -07005332 for_each_online_cpu(cpu)
5333 queue_work_on(cpu, system_highpri_wq,
5334 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005335
5336 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07005337 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005338
5339 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005340}
5341
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005342INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5343INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
Herbert Xud565b0a2008-12-15 23:38:52 -08005344static int napi_gro_complete(struct sk_buff *skb)
5345{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005346 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005347 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005348 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005349 int err = -ENOENT;
5350
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005351 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5352
Herbert Xufc59f9a2009-04-14 15:11:06 -07005353 if (NAPI_GRO_CB(skb)->count == 1) {
5354 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005355 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005356 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005357
5358 rcu_read_lock();
5359 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005360 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005361 continue;
5362
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005363 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5364 ipv6_gro_complete, inet_gro_complete,
5365 skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005366 break;
5367 }
5368 rcu_read_unlock();
5369
5370 if (err) {
5371 WARN_ON(&ptype->list == head);
5372 kfree_skb(skb);
5373 return NET_RX_SUCCESS;
5374 }
5375
5376out:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005377 return netif_receive_skb_internal(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005378}
5379
Li RongQing6312fe72018-07-05 14:34:32 +08005380static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005381 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005382{
Li RongQing6312fe72018-07-05 14:34:32 +08005383 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005384 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005385
David Miller07d78362018-06-24 14:14:02 +09005386 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005387 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5388 return;
David S. Miller992cba72018-07-31 15:27:56 -07005389 skb_list_del_init(skb);
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005390 napi_gro_complete(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005391 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005392 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005393
5394 if (!napi->gro_hash[index].count)
5395 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005396}
David Miller07d78362018-06-24 14:14:02 +09005397
Li RongQing6312fe72018-07-05 14:34:32 +08005398/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005399 * youngest packets at the head of it.
5400 * Complete skbs in reverse order to reduce latencies.
5401 */
5402void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5403{
Eric Dumazet42519ed2018-11-21 11:39:28 -08005404 unsigned long bitmask = napi->gro_bitmask;
5405 unsigned int i, base = ~0U;
David Miller07d78362018-06-24 14:14:02 +09005406
Eric Dumazet42519ed2018-11-21 11:39:28 -08005407 while ((i = ffs(bitmask)) != 0) {
5408 bitmask >>= i;
5409 base += i;
5410 __napi_gro_flush_chain(napi, base, flush_old);
Li RongQingd9f37d02018-07-13 14:41:36 +08005411 }
David Miller07d78362018-06-24 14:14:02 +09005412}
Eric Dumazet86cac582010-08-31 18:25:32 +00005413EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005414
David Miller07d78362018-06-24 14:14:02 +09005415static struct list_head *gro_list_prepare(struct napi_struct *napi,
5416 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005417{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005418 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005419 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005420 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005421 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005422
Li RongQing6312fe72018-07-05 14:34:32 +08005423 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005424 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005425 unsigned long diffs;
5426
Tom Herbert0b4cec82014-01-15 08:58:06 -08005427 NAPI_GRO_CB(p)->flush = 0;
5428
5429 if (hash != skb_get_hash_raw(p)) {
5430 NAPI_GRO_CB(p)->same_flow = 0;
5431 continue;
5432 }
5433
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005434 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005435 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5436 if (skb_vlan_tag_present(p))
5437 diffs |= p->vlan_tci ^ skb->vlan_tci;
Jesse Grossce87fc62016-01-20 17:59:49 -08005438 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005439 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005440 if (maclen == ETH_HLEN)
5441 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005442 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005443 else if (!diffs)
5444 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005445 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005446 maclen);
5447 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005448 }
David Miller07d78362018-06-24 14:14:02 +09005449
5450 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005451}
5452
Jerry Chu299603e82013-12-11 20:53:45 -08005453static void skb_gro_reset_offset(struct sk_buff *skb)
5454{
5455 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5456 const skb_frag_t *frag0 = &pinfo->frags[0];
5457
5458 NAPI_GRO_CB(skb)->data_offset = 0;
5459 NAPI_GRO_CB(skb)->frag0 = NULL;
5460 NAPI_GRO_CB(skb)->frag0_len = 0;
5461
5462 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
5463 pinfo->nr_frags &&
5464 !PageHighMem(skb_frag_page(frag0))) {
5465 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005466 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5467 skb_frag_size(frag0),
5468 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005469 }
5470}
5471
Eric Dumazeta50e2332014-03-29 21:28:21 -07005472static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5473{
5474 struct skb_shared_info *pinfo = skb_shinfo(skb);
5475
5476 BUG_ON(skb->end - skb->tail < grow);
5477
5478 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5479
5480 skb->data_len -= grow;
5481 skb->tail += grow;
5482
5483 pinfo->frags[0].page_offset += grow;
5484 skb_frag_size_sub(&pinfo->frags[0], grow);
5485
5486 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5487 skb_frag_unref(skb, 0);
5488 memmove(pinfo->frags, pinfo->frags + 1,
5489 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5490 }
5491}
5492
Li RongQing6312fe72018-07-05 14:34:32 +08005493static void gro_flush_oldest(struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005494{
Li RongQing6312fe72018-07-05 14:34:32 +08005495 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005496
Li RongQing6312fe72018-07-05 14:34:32 +08005497 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005498
Li RongQing6312fe72018-07-05 14:34:32 +08005499 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005500 * impossible.
5501 */
5502 if (WARN_ON_ONCE(!oldest))
5503 return;
5504
Li RongQingd9f37d02018-07-13 14:41:36 +08005505 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5506 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005507 */
David S. Millerece23712018-10-28 10:35:12 -07005508 skb_list_del_init(oldest);
David Miller07d78362018-06-24 14:14:02 +09005509 napi_gro_complete(oldest);
5510}
5511
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005512INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5513 struct sk_buff *));
5514INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5515 struct sk_buff *));
Rami Rosenbb728822012-11-28 21:55:25 +00005516static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005517{
Li RongQing6312fe72018-07-05 14:34:32 +08005518 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005519 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005520 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005521 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005522 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005523 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005524 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005525 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005526 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005527
David S. Millerb5cdae32017-04-18 15:36:58 -04005528 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005529 goto normal;
5530
David Miller07d78362018-06-24 14:14:02 +09005531 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005532
Herbert Xud565b0a2008-12-15 23:38:52 -08005533 rcu_read_lock();
5534 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005535 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005536 continue;
5537
Herbert Xu86911732009-01-29 14:19:50 +00005538 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005539 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005540 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005541 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005542 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005543 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005544 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005545 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005546 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005547 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005548
Tom Herbert662880f2014-08-27 21:26:56 -07005549 /* Setup for GRO checksum validation */
5550 switch (skb->ip_summed) {
5551 case CHECKSUM_COMPLETE:
5552 NAPI_GRO_CB(skb)->csum = skb->csum;
5553 NAPI_GRO_CB(skb)->csum_valid = 1;
5554 NAPI_GRO_CB(skb)->csum_cnt = 0;
5555 break;
5556 case CHECKSUM_UNNECESSARY:
5557 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5558 NAPI_GRO_CB(skb)->csum_valid = 0;
5559 break;
5560 default:
5561 NAPI_GRO_CB(skb)->csum_cnt = 0;
5562 NAPI_GRO_CB(skb)->csum_valid = 0;
5563 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005564
Paolo Abeniaaa5d902018-12-14 11:51:58 +01005565 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5566 ipv6_gro_receive, inet_gro_receive,
5567 gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005568 break;
5569 }
5570 rcu_read_unlock();
5571
5572 if (&ptype->list == head)
5573 goto normal;
5574
Steffen Klassert25393d32017-02-15 09:39:44 +01005575 if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
5576 ret = GRO_CONSUMED;
5577 goto ok;
5578 }
5579
Herbert Xu0da2afd52008-12-26 14:57:42 -08005580 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005581 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005582
Herbert Xud565b0a2008-12-15 23:38:52 -08005583 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005584 skb_list_del_init(pp);
David Millerd4546c22018-06-24 14:13:49 +09005585 napi_gro_complete(pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005586 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005587 }
5588
Herbert Xu0da2afd52008-12-26 14:57:42 -08005589 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005590 goto ok;
5591
Eric Dumazet600adc12014-01-09 14:12:19 -08005592 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08005593 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08005594
Li RongQing6312fe72018-07-05 14:34:32 +08005595 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
5596 gro_flush_oldest(gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08005597 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08005598 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08005599 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005600 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005601 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07005602 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00005603 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09005604 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005605 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08005606
Herbert Xuad0f99042009-02-01 01:24:55 -08005607pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005608 grow = skb_gro_offset(skb) - skb_headlen(skb);
5609 if (grow > 0)
5610 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08005611ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08005612 if (napi->gro_hash[hash].count) {
5613 if (!test_bit(hash, &napi->gro_bitmask))
5614 __set_bit(hash, &napi->gro_bitmask);
5615 } else if (test_bit(hash, &napi->gro_bitmask)) {
5616 __clear_bit(hash, &napi->gro_bitmask);
5617 }
5618
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005619 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005620
5621normal:
Herbert Xuad0f99042009-02-01 01:24:55 -08005622 ret = GRO_NORMAL;
5623 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08005624}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005625
Jerry Chubf5a7552014-01-07 10:23:19 -08005626struct packet_offload *gro_find_receive_by_type(__be16 type)
5627{
5628 struct list_head *offload_head = &offload_base;
5629 struct packet_offload *ptype;
5630
5631 list_for_each_entry_rcu(ptype, offload_head, list) {
5632 if (ptype->type != type || !ptype->callbacks.gro_receive)
5633 continue;
5634 return ptype;
5635 }
5636 return NULL;
5637}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005638EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08005639
5640struct packet_offload *gro_find_complete_by_type(__be16 type)
5641{
5642 struct list_head *offload_head = &offload_base;
5643 struct packet_offload *ptype;
5644
5645 list_for_each_entry_rcu(ptype, offload_head, list) {
5646 if (ptype->type != type || !ptype->callbacks.gro_complete)
5647 continue;
5648 return ptype;
5649 }
5650 return NULL;
5651}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005652EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005653
Michal Kubečeke44699d2017-06-29 11:13:36 +02005654static void napi_skb_free_stolen_head(struct sk_buff *skb)
5655{
5656 skb_dst_drop(skb);
5657 secpath_reset(skb);
5658 kmem_cache_free(skbuff_head_cache, skb);
5659}
5660
Rami Rosenbb728822012-11-28 21:55:25 +00005661static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08005662{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005663 switch (ret) {
5664 case GRO_NORMAL:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005665 if (netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005666 ret = GRO_DROP;
5667 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005668
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005669 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08005670 kfree_skb(skb);
5671 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005672
Eric Dumazetdaa86542012-04-19 07:07:40 +00005673 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02005674 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5675 napi_skb_free_stolen_head(skb);
5676 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00005677 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00005678 break;
5679
Ben Hutchings5b252f02009-10-29 07:17:09 +00005680 case GRO_HELD:
5681 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005682 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005683 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005684 }
5685
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005686 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005687}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005688
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005689gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005690{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005691 gro_result_t ret;
5692
Eric Dumazet93f93a42015-11-18 06:30:59 -08005693 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005694 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00005695
Eric Dumazeta50e2332014-03-29 21:28:21 -07005696 skb_gro_reset_offset(skb);
5697
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005698 ret = napi_skb_finish(dev_gro_receive(napi, skb), skb);
5699 trace_napi_gro_receive_exit(ret);
5700
5701 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005702}
5703EXPORT_SYMBOL(napi_gro_receive);
5704
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00005705static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005706{
Eric Dumazet93a35f52014-10-23 06:30:30 -07005707 if (unlikely(skb->pfmemalloc)) {
5708 consume_skb(skb);
5709 return;
5710 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005711 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00005712 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5713 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01005714 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08005715 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08005716 skb->skb_iif = 0;
Eric Dumazet33d9a2c2018-11-17 21:57:02 -08005717
5718 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5719 skb->pkt_type = PACKET_HOST;
5720
Jerry Chuc3caf112014-07-14 15:54:46 -07005721 skb->encapsulation = 0;
5722 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07005723 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Steffen Klassertf991bb92017-01-30 06:45:38 +01005724 secpath_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005725
5726 napi->skb = skb;
5727}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005728
Herbert Xu76620aa2009-04-16 02:02:07 -07005729struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005730{
Herbert Xu5d38a072009-01-04 16:13:40 -08005731 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005732
5733 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005734 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005735 if (skb) {
5736 napi->skb = skb;
5737 skb_mark_napi_id(skb, napi);
5738 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005739 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005740 return skb;
5741}
Herbert Xu76620aa2009-04-16 02:02:07 -07005742EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005743
Eric Dumazeta50e2332014-03-29 21:28:21 -07005744static gro_result_t napi_frags_finish(struct napi_struct *napi,
5745 struct sk_buff *skb,
5746 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005747{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005748 switch (ret) {
5749 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005750 case GRO_HELD:
5751 __skb_push(skb, ETH_HLEN);
5752 skb->protocol = eth_type_trans(skb, skb->dev);
5753 if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005754 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00005755 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005756
5757 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005758 napi_reuse_skb(napi, skb);
5759 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005760
Michal Kubečeke44699d2017-06-29 11:13:36 +02005761 case GRO_MERGED_FREE:
5762 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5763 napi_skb_free_stolen_head(skb);
5764 else
5765 napi_reuse_skb(napi, skb);
5766 break;
5767
Ben Hutchings5b252f02009-10-29 07:17:09 +00005768 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005769 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005770 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005771 }
5772
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005773 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005774}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005775
Eric Dumazeta50e2332014-03-29 21:28:21 -07005776/* Upper GRO stack assumes network header starts at gro_offset=0
5777 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5778 * We copy ethernet header into skb->data to have a common layout.
5779 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005780static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005781{
Herbert Xu76620aa2009-04-16 02:02:07 -07005782 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005783 const struct ethhdr *eth;
5784 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005785
5786 napi->skb = NULL;
5787
Eric Dumazeta50e2332014-03-29 21:28:21 -07005788 skb_reset_mac_header(skb);
5789 skb_gro_reset_offset(skb);
5790
5791 eth = skb_gro_header_fast(skb, 0);
5792 if (unlikely(skb_gro_header_hard(skb, hlen))) {
5793 eth = skb_gro_header_slow(skb, hlen, 0);
5794 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04005795 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
5796 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005797 napi_reuse_skb(napi, skb);
5798 return NULL;
5799 }
5800 } else {
5801 gro_pull_from_frag0(skb, hlen);
5802 NAPI_GRO_CB(skb)->frag0 += hlen;
5803 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07005804 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07005805 __skb_pull(skb, hlen);
5806
5807 /*
5808 * This works because the only protocols we care about don't require
5809 * special handling.
5810 * We'll fix it up properly in napi_frags_finish()
5811 */
5812 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07005813
Herbert Xu76620aa2009-04-16 02:02:07 -07005814 return skb;
5815}
Herbert Xu76620aa2009-04-16 02:02:07 -07005816
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005817gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07005818{
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005819 gro_result_t ret;
Herbert Xu76620aa2009-04-16 02:02:07 -07005820 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005821
5822 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005823 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08005824
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005825 trace_napi_gro_frags_entry(skb);
5826
Geneviève Bastienb0e3f1b2018-11-27 12:52:39 -05005827 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
5828 trace_napi_gro_frags_exit(ret);
5829
5830 return ret;
Herbert Xu5d38a072009-01-04 16:13:40 -08005831}
5832EXPORT_SYMBOL(napi_gro_frags);
5833
Tom Herbert573e8fc2014-08-22 13:33:47 -07005834/* Compute the checksum from gro_offset and return the folded value
5835 * after adding in any pseudo checksum.
5836 */
5837__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
5838{
5839 __wsum wsum;
5840 __sum16 sum;
5841
5842 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
5843
5844 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
5845 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
Cong Wang14641932018-11-26 09:31:26 -08005846 /* See comments in __skb_checksum_complete(). */
Tom Herbert573e8fc2014-08-22 13:33:47 -07005847 if (likely(!sum)) {
5848 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
5849 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08005850 netdev_rx_csum_fault(skb->dev, skb);
Tom Herbert573e8fc2014-08-22 13:33:47 -07005851 }
5852
5853 NAPI_GRO_CB(skb)->csum = wsum;
5854 NAPI_GRO_CB(skb)->csum_valid = 1;
5855
5856 return sum;
5857}
5858EXPORT_SYMBOL(__skb_gro_checksum_complete);
5859
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305860static void net_rps_send_ipi(struct softnet_data *remsd)
5861{
5862#ifdef CONFIG_RPS
5863 while (remsd) {
5864 struct softnet_data *next = remsd->rps_ipi_next;
5865
5866 if (cpu_online(remsd->cpu))
5867 smp_call_function_single_async(remsd->cpu, &remsd->csd);
5868 remsd = next;
5869 }
5870#endif
5871}
5872
Eric Dumazete326bed2010-04-22 00:22:45 -07005873/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08005874 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07005875 * Note: called with local irq disabled, but exits with local irq enabled.
5876 */
5877static void net_rps_action_and_irq_enable(struct softnet_data *sd)
5878{
5879#ifdef CONFIG_RPS
5880 struct softnet_data *remsd = sd->rps_ipi_list;
5881
5882 if (remsd) {
5883 sd->rps_ipi_list = NULL;
5884
5885 local_irq_enable();
5886
5887 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305888 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07005889 } else
5890#endif
5891 local_irq_enable();
5892}
5893
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005894static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5895{
5896#ifdef CONFIG_RPS
5897 return sd->rps_ipi_list != NULL;
5898#else
5899 return false;
5900#endif
5901}
5902
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005903static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005905 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005906 bool again = true;
5907 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Eric Dumazete326bed2010-04-22 00:22:45 -07005909 /* Check if we have pending ipi, its better to send them now,
5910 * not waiting net_rx_action() end.
5911 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005912 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07005913 local_irq_disable();
5914 net_rps_action_and_irq_enable(sd);
5915 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005916
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01005917 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005918 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
Changli Gao6e7676c2010-04-27 15:07:33 -07005921 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03005922 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07005923 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03005924 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00005925 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005926 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00005927 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005928
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005931 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005932 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07005933 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005934 /*
5935 * Inline a custom version of __napi_complete().
5936 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07005937 * and NAPI_STATE_SCHED is the only possible flag set
5938 * on backlog.
5939 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005940 * and we dont need an smp_mb() memory barrier.
5941 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005942 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005943 again = false;
5944 } else {
5945 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5946 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07005947 }
5948 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005949 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005952 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953}
5954
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005955/**
5956 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07005957 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005958 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005959 * The entry's receive function will be scheduled to run.
5960 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005961 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08005962void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005963{
5964 unsigned long flags;
5965
5966 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05005967 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005968 local_irq_restore(flags);
5969}
5970EXPORT_SYMBOL(__napi_schedule);
5971
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005972/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08005973 * napi_schedule_prep - check if napi can be scheduled
5974 * @n: napi context
5975 *
5976 * Test if NAPI routine is already running, and if not mark
5977 * it as running. This is used as a condition variable
5978 * insure only one NAPI poll instance runs. We also make
5979 * sure there is no pending NAPI disable.
5980 */
5981bool napi_schedule_prep(struct napi_struct *n)
5982{
5983 unsigned long val, new;
5984
5985 do {
5986 val = READ_ONCE(n->state);
5987 if (unlikely(val & NAPIF_STATE_DISABLE))
5988 return false;
5989 new = val | NAPIF_STATE_SCHED;
5990
5991 /* Sets STATE_MISSED bit if STATE_SCHED was already set
5992 * This was suggested by Alexander Duyck, as compiler
5993 * emits better code than :
5994 * if (val & NAPIF_STATE_SCHED)
5995 * new |= NAPIF_STATE_MISSED;
5996 */
5997 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5998 NAPIF_STATE_MISSED;
5999 } while (cmpxchg(&n->state, val, new) != val);
6000
6001 return !(val & NAPIF_STATE_SCHED);
6002}
6003EXPORT_SYMBOL(napi_schedule_prep);
6004
6005/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07006006 * __napi_schedule_irqoff - schedule for receive
6007 * @n: entry to schedule
6008 *
6009 * Variant of __napi_schedule() assuming hard irqs are masked
6010 */
6011void __napi_schedule_irqoff(struct napi_struct *n)
6012{
6013 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6014}
6015EXPORT_SYMBOL(__napi_schedule_irqoff);
6016
Eric Dumazet364b6052016-11-15 10:15:13 -08006017bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08006018{
Eric Dumazet39e6c822017-02-28 10:34:50 -08006019 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08006020
6021 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08006022 * 1) Don't let napi dequeue from the cpu poll list
6023 * just in case its running on a different cpu.
6024 * 2) If we are busy polling, do nothing here, we have
6025 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08006026 */
Eric Dumazet217f6972016-11-15 10:15:11 -08006027 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6028 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08006029 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08006030
Li RongQingd9f37d02018-07-13 14:41:36 +08006031 if (n->gro_bitmask) {
Eric Dumazet3b47d302014-11-06 21:09:44 -08006032 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006033
Eric Dumazet3b47d302014-11-06 21:09:44 -08006034 if (work_done)
6035 timeout = n->dev->gro_flush_timeout;
6036
Paolo Abeni605108a2018-11-21 18:21:35 +01006037 /* When the NAPI instance uses a timeout and keeps postponing
6038 * it, we need to bound somehow the time packets are kept in
6039 * the GRO layer
6040 */
6041 napi_gro_flush(n, !!timeout);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006042 if (timeout)
6043 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6044 HRTIMER_MODE_REL_PINNED);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006045 }
Eric Dumazet02c16022017-02-04 15:25:02 -08006046 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006047 /* If n->poll_list is not empty, we need to mask irqs */
6048 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08006049 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006050 local_irq_restore(flags);
6051 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08006052
6053 do {
6054 val = READ_ONCE(n->state);
6055
6056 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6057
6058 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6059
6060 /* If STATE_MISSED was set, leave STATE_SCHED set,
6061 * because we will call napi->poll() one more time.
6062 * This C code was suggested by Alexander Duyck to help gcc.
6063 */
6064 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6065 NAPIF_STATE_SCHED;
6066 } while (cmpxchg(&n->state, val, new) != val);
6067
6068 if (unlikely(val & NAPIF_STATE_MISSED)) {
6069 __napi_schedule(n);
6070 return false;
6071 }
6072
Eric Dumazet364b6052016-11-15 10:15:13 -08006073 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006074}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006075EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006076
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006077/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006078static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006079{
6080 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6081 struct napi_struct *napi;
6082
6083 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6084 if (napi->napi_id == napi_id)
6085 return napi;
6086
6087 return NULL;
6088}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006089
6090#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006091
Eric Dumazetce6aea92015-11-18 06:30:54 -08006092#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006093
6094static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6095{
6096 int rc;
6097
Eric Dumazet39e6c822017-02-28 10:34:50 -08006098 /* Busy polling means there is a high chance device driver hard irq
6099 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6100 * set in napi_schedule_prep().
6101 * Since we are about to call napi->poll() once more, we can safely
6102 * clear NAPI_STATE_MISSED.
6103 *
6104 * Note: x86 could use a single "lock and ..." instruction
6105 * to perform these two clear_bit()
6106 */
6107 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006108 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6109
6110 local_bh_disable();
6111
6112 /* All we really want here is to re-enable device interrupts.
6113 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6114 */
6115 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006116 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006117 netpoll_poll_unlock(have_poll_lock);
6118 if (rc == BUSY_POLL_BUDGET)
6119 __napi_schedule(napi);
6120 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006121}
6122
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006123void napi_busy_loop(unsigned int napi_id,
6124 bool (*loop_end)(void *, unsigned long),
6125 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006126{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006127 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006128 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006129 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006130 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006131
6132restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006133 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006134
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006135 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006136
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006137 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006138 if (!napi)
6139 goto out;
6140
Eric Dumazet217f6972016-11-15 10:15:11 -08006141 preempt_disable();
6142 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006143 int work = 0;
6144
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006145 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006146 if (!napi_poll) {
6147 unsigned long val = READ_ONCE(napi->state);
6148
6149 /* If multiple threads are competing for this napi,
6150 * we avoid dirtying napi->state as much as we can.
6151 */
6152 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6153 NAPIF_STATE_IN_BUSY_POLL))
6154 goto count;
6155 if (cmpxchg(&napi->state, val,
6156 val | NAPIF_STATE_IN_BUSY_POLL |
6157 NAPIF_STATE_SCHED) != val)
6158 goto count;
6159 have_poll_lock = netpoll_poll_lock(napi);
6160 napi_poll = napi->poll;
6161 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006162 work = napi_poll(napi, BUSY_POLL_BUDGET);
6163 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006164count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006165 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006166 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006167 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006168 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006169
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006170 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006171 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006172
Eric Dumazet217f6972016-11-15 10:15:11 -08006173 if (unlikely(need_resched())) {
6174 if (napi_poll)
6175 busy_poll_stop(napi, have_poll_lock);
6176 preempt_enable();
6177 rcu_read_unlock();
6178 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006179 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006180 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006181 goto restart;
6182 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006183 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006184 }
6185 if (napi_poll)
6186 busy_poll_stop(napi, have_poll_lock);
6187 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006188out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006189 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006190}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006191EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006192
6193#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006194
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006195static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006196{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08006197 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6198 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006199 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006200
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006201 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006202
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006203 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006204 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006205 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6206 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006207 } while (napi_by_id(napi_gen_id));
6208 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006209
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006210 hlist_add_head_rcu(&napi->napi_hash_node,
6211 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006212
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006213 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006214}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006215
6216/* Warning : caller is responsible to make sure rcu grace period
6217 * is respected before freeing memory containing @napi
6218 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08006219bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006220{
Eric Dumazet34cbe272015-11-18 06:31:02 -08006221 bool rcu_sync_needed = false;
6222
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006223 spin_lock(&napi_hash_lock);
6224
Eric Dumazet34cbe272015-11-18 06:31:02 -08006225 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6226 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006227 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006228 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006229 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006230 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006231}
6232EXPORT_SYMBOL_GPL(napi_hash_del);
6233
Eric Dumazet3b47d302014-11-06 21:09:44 -08006234static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6235{
6236 struct napi_struct *napi;
6237
6238 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006239
6240 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6241 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6242 */
Li RongQingd9f37d02018-07-13 14:41:36 +08006243 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006244 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6245 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006246
6247 return HRTIMER_NORESTART;
6248}
6249
David S. Miller7c4ec742018-07-20 23:37:55 -07006250static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006251{
David Miller07d78362018-06-24 14:14:02 +09006252 int i;
6253
Li RongQing6312fe72018-07-05 14:34:32 +08006254 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6255 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6256 napi->gro_hash[i].count = 0;
6257 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006258 napi->gro_bitmask = 0;
6259}
6260
6261void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6262 int (*poll)(struct napi_struct *, int), int weight)
6263{
6264 INIT_LIST_HEAD(&napi->poll_list);
6265 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6266 napi->timer.function = napi_watchdog;
6267 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006268 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08006269 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006270 if (weight > NAPI_POLL_WEIGHT)
Qian Caibf29e9e2018-12-01 21:11:19 -05006271 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6272 weight);
Herbert Xud565b0a2008-12-15 23:38:52 -08006273 napi->weight = weight;
6274 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08006275 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006276#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006277 napi->poll_owner = -1;
6278#endif
6279 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006280 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006281}
6282EXPORT_SYMBOL(netif_napi_add);
6283
Eric Dumazet3b47d302014-11-06 21:09:44 -08006284void napi_disable(struct napi_struct *n)
6285{
6286 might_sleep();
6287 set_bit(NAPI_STATE_DISABLE, &n->state);
6288
6289 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6290 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006291 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6292 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006293
6294 hrtimer_cancel(&n->timer);
6295
6296 clear_bit(NAPI_STATE_DISABLE, &n->state);
6297}
6298EXPORT_SYMBOL(napi_disable);
6299
David Miller07d78362018-06-24 14:14:02 +09006300static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006301{
David Miller07d78362018-06-24 14:14:02 +09006302 int i;
David Millerd4546c22018-06-24 14:13:49 +09006303
David Miller07d78362018-06-24 14:14:02 +09006304 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6305 struct sk_buff *skb, *n;
6306
Li RongQing6312fe72018-07-05 14:34:32 +08006307 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006308 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006309 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006310 }
David Millerd4546c22018-06-24 14:13:49 +09006311}
6312
Eric Dumazet93d05d42015-11-18 06:31:03 -08006313/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08006314void netif_napi_del(struct napi_struct *napi)
6315{
Eric Dumazet93d05d42015-11-18 06:31:03 -08006316 might_sleep();
6317 if (napi_hash_del(napi))
6318 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08006319 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006320 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006321
David Miller07d78362018-06-24 14:14:02 +09006322 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006323 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006324}
6325EXPORT_SYMBOL(netif_napi_del);
6326
Herbert Xu726ce702014-12-21 07:16:21 +11006327static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6328{
6329 void *have;
6330 int work, weight;
6331
6332 list_del_init(&n->poll_list);
6333
6334 have = netpoll_poll_lock(n);
6335
6336 weight = n->weight;
6337
6338 /* This NAPI_STATE_SCHED test is for avoiding a race
6339 * with netpoll's poll_napi(). Only the entity which
6340 * obtains the lock and sees NAPI_STATE_SCHED set will
6341 * actually make the ->poll() call. Therefore we avoid
6342 * accidentally calling ->poll() when NAPI is not scheduled.
6343 */
6344 work = 0;
6345 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6346 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006347 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006348 }
6349
6350 WARN_ON_ONCE(work > weight);
6351
6352 if (likely(work < weight))
6353 goto out_unlock;
6354
6355 /* Drivers must not modify the NAPI state if they
6356 * consume the entire weight. In such cases this code
6357 * still "owns" the NAPI instance and therefore can
6358 * move the instance around on the list at-will.
6359 */
6360 if (unlikely(napi_disable_pending(n))) {
6361 napi_complete(n);
6362 goto out_unlock;
6363 }
6364
Li RongQingd9f37d02018-07-13 14:41:36 +08006365 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006366 /* flush too old packets
6367 * If HZ < 1000, flush all packets.
6368 */
6369 napi_gro_flush(n, HZ >= 1000);
6370 }
6371
Herbert Xu001ce542014-12-21 07:16:22 +11006372 /* Some drivers may have called napi_schedule
6373 * prior to exhausting their budget.
6374 */
6375 if (unlikely(!list_empty(&n->poll_list))) {
6376 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6377 n->dev ? n->dev->name : "backlog");
6378 goto out_unlock;
6379 }
6380
Herbert Xu726ce702014-12-21 07:16:21 +11006381 list_add_tail(&n->poll_list, repoll);
6382
6383out_unlock:
6384 netpoll_poll_unlock(have);
6385
6386 return work;
6387}
6388
Emese Revfy0766f782016-06-20 20:42:34 +02006389static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006391 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006392 unsigned long time_limit = jiffies +
6393 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006394 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006395 LIST_HEAD(list);
6396 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006399 list_splice_init(&sd->poll_list, &list);
6400 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006402 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006403 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006405 if (list_empty(&list)) {
6406 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006407 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006408 break;
6409 }
6410
Herbert Xu6bd373e2014-12-21 07:16:24 +11006411 n = list_first_entry(&list, struct napi_struct, poll_list);
6412 budget -= napi_poll(n, &repoll);
6413
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006414 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006415 * Allow this to run for 2 jiffies since which will allow
6416 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006417 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006418 if (unlikely(budget <= 0 ||
6419 time_after_eq(jiffies, time_limit))) {
6420 sd->time_squeeze++;
6421 break;
6422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006424
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006425 local_irq_disable();
6426
6427 list_splice_tail_init(&sd->poll_list, &list);
6428 list_splice_tail(&repoll, &list);
6429 list_splice(&list, &sd->poll_list);
6430 if (!list_empty(&sd->poll_list))
6431 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6432
Eric Dumazete326bed2010-04-22 00:22:45 -07006433 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006434out:
6435 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436}
6437
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006438struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006439 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006440
6441 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006442 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006443
Veaceslav Falico5d261912013-08-28 23:25:05 +02006444 /* counter for the number of times this device was added to us */
6445 u16 ref_nr;
6446
Veaceslav Falico402dae92013-09-25 09:20:09 +02006447 /* private field for the users */
6448 void *private;
6449
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006450 struct list_head list;
6451 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006452};
6453
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006454static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006455 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006456{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006457 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006458
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006459 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006460 if (adj->dev == adj_dev)
6461 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006462 }
6463 return NULL;
6464}
6465
David Ahernf1170fd2016-10-17 19:15:51 -07006466static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
6467{
6468 struct net_device *dev = data;
6469
6470 return upper_dev == dev;
6471}
6472
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006473/**
6474 * netdev_has_upper_dev - Check if device is linked to an upper device
6475 * @dev: device
6476 * @upper_dev: upper device to check
6477 *
6478 * Find out if a device is linked to specified upper device and return true
6479 * in case it is. Note that this checks only immediate upper device,
6480 * not through a complete stack of devices. The caller must hold the RTNL lock.
6481 */
6482bool netdev_has_upper_dev(struct net_device *dev,
6483 struct net_device *upper_dev)
6484{
6485 ASSERT_RTNL();
6486
David Ahernf1170fd2016-10-17 19:15:51 -07006487 return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
6488 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006489}
6490EXPORT_SYMBOL(netdev_has_upper_dev);
6491
6492/**
David Ahern1a3f0602016-10-17 19:15:44 -07006493 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6494 * @dev: device
6495 * @upper_dev: upper device to check
6496 *
6497 * Find out if a device is linked to specified upper device and return true
6498 * in case it is. Note that this checks the entire upper device chain.
6499 * The caller must hold rcu lock.
6500 */
6501
David Ahern1a3f0602016-10-17 19:15:44 -07006502bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6503 struct net_device *upper_dev)
6504{
6505 return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
6506 upper_dev);
6507}
6508EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6509
6510/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006511 * netdev_has_any_upper_dev - Check if device is linked to some device
6512 * @dev: device
6513 *
6514 * Find out if a device is linked to an upper device and return true in case
6515 * it is. The caller must hold the RTNL lock.
6516 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006517bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006518{
6519 ASSERT_RTNL();
6520
David Ahernf1170fd2016-10-17 19:15:51 -07006521 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006522}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006523EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006524
6525/**
6526 * netdev_master_upper_dev_get - Get master upper device
6527 * @dev: device
6528 *
6529 * Find a master upper device and return pointer to it or NULL in case
6530 * it's not there. The caller must hold the RTNL lock.
6531 */
6532struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6533{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006534 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006535
6536 ASSERT_RTNL();
6537
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006538 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006539 return NULL;
6540
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006541 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006542 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006543 if (likely(upper->master))
6544 return upper->dev;
6545 return NULL;
6546}
6547EXPORT_SYMBOL(netdev_master_upper_dev_get);
6548
David Ahern0f524a82016-10-17 19:15:52 -07006549/**
6550 * netdev_has_any_lower_dev - Check if device is linked to some device
6551 * @dev: device
6552 *
6553 * Find out if a device is linked to a lower device and return true in case
6554 * it is. The caller must hold the RTNL lock.
6555 */
6556static bool netdev_has_any_lower_dev(struct net_device *dev)
6557{
6558 ASSERT_RTNL();
6559
6560 return !list_empty(&dev->adj_list.lower);
6561}
6562
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02006563void *netdev_adjacent_get_private(struct list_head *adj_list)
6564{
6565 struct netdev_adjacent *adj;
6566
6567 adj = list_entry(adj_list, struct netdev_adjacent, list);
6568
6569 return adj->private;
6570}
6571EXPORT_SYMBOL(netdev_adjacent_get_private);
6572
Veaceslav Falico31088a12013-09-25 09:20:12 +02006573/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04006574 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6575 * @dev: device
6576 * @iter: list_head ** of the current position
6577 *
6578 * Gets the next device from the dev's upper list, starting from iter
6579 * position. The caller must hold RCU read lock.
6580 */
6581struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6582 struct list_head **iter)
6583{
6584 struct netdev_adjacent *upper;
6585
6586 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6587
6588 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6589
6590 if (&upper->list == &dev->adj_list.upper)
6591 return NULL;
6592
6593 *iter = &upper->list;
6594
6595 return upper->dev;
6596}
6597EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6598
David Ahern1a3f0602016-10-17 19:15:44 -07006599static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6600 struct list_head **iter)
6601{
6602 struct netdev_adjacent *upper;
6603
6604 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6605
6606 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6607
6608 if (&upper->list == &dev->adj_list.upper)
6609 return NULL;
6610
6611 *iter = &upper->list;
6612
6613 return upper->dev;
6614}
6615
6616int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6617 int (*fn)(struct net_device *dev,
6618 void *data),
6619 void *data)
6620{
6621 struct net_device *udev;
6622 struct list_head *iter;
6623 int ret;
6624
6625 for (iter = &dev->adj_list.upper,
6626 udev = netdev_next_upper_dev_rcu(dev, &iter);
6627 udev;
6628 udev = netdev_next_upper_dev_rcu(dev, &iter)) {
6629 /* first is the upper device itself */
6630 ret = fn(udev, data);
6631 if (ret)
6632 return ret;
6633
6634 /* then look at all of its upper devices */
6635 ret = netdev_walk_all_upper_dev_rcu(udev, fn, data);
6636 if (ret)
6637 return ret;
6638 }
6639
6640 return 0;
6641}
6642EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6643
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006644/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02006645 * netdev_lower_get_next_private - Get the next ->private from the
6646 * lower neighbour list
6647 * @dev: device
6648 * @iter: list_head ** of the current position
6649 *
6650 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6651 * list, starting from iter position. The caller must hold either hold the
6652 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006653 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02006654 */
6655void *netdev_lower_get_next_private(struct net_device *dev,
6656 struct list_head **iter)
6657{
6658 struct netdev_adjacent *lower;
6659
6660 lower = list_entry(*iter, struct netdev_adjacent, list);
6661
6662 if (&lower->list == &dev->adj_list.lower)
6663 return NULL;
6664
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006665 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006666
6667 return lower->private;
6668}
6669EXPORT_SYMBOL(netdev_lower_get_next_private);
6670
6671/**
6672 * netdev_lower_get_next_private_rcu - Get the next ->private from the
6673 * lower neighbour list, RCU
6674 * variant
6675 * @dev: device
6676 * @iter: list_head ** of the current position
6677 *
6678 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6679 * list, starting from iter position. The caller must hold RCU read lock.
6680 */
6681void *netdev_lower_get_next_private_rcu(struct net_device *dev,
6682 struct list_head **iter)
6683{
6684 struct netdev_adjacent *lower;
6685
6686 WARN_ON_ONCE(!rcu_read_lock_held());
6687
6688 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6689
6690 if (&lower->list == &dev->adj_list.lower)
6691 return NULL;
6692
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006693 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006694
6695 return lower->private;
6696}
6697EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
6698
6699/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006700 * netdev_lower_get_next - Get the next device from the lower neighbour
6701 * list
6702 * @dev: device
6703 * @iter: list_head ** of the current position
6704 *
6705 * Gets the next netdev_adjacent from the dev's lower neighbour
6706 * list, starting from iter position. The caller must hold RTNL lock or
6707 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006708 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006709 */
6710void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
6711{
6712 struct netdev_adjacent *lower;
6713
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006714 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006715
6716 if (&lower->list == &dev->adj_list.lower)
6717 return NULL;
6718
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006719 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006720
6721 return lower->dev;
6722}
6723EXPORT_SYMBOL(netdev_lower_get_next);
6724
David Ahern1a3f0602016-10-17 19:15:44 -07006725static struct net_device *netdev_next_lower_dev(struct net_device *dev,
6726 struct list_head **iter)
6727{
6728 struct netdev_adjacent *lower;
6729
David Ahern46b5ab12016-10-26 13:21:33 -07006730 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07006731
6732 if (&lower->list == &dev->adj_list.lower)
6733 return NULL;
6734
David Ahern46b5ab12016-10-26 13:21:33 -07006735 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07006736
6737 return lower->dev;
6738}
6739
6740int netdev_walk_all_lower_dev(struct net_device *dev,
6741 int (*fn)(struct net_device *dev,
6742 void *data),
6743 void *data)
6744{
6745 struct net_device *ldev;
6746 struct list_head *iter;
6747 int ret;
6748
6749 for (iter = &dev->adj_list.lower,
6750 ldev = netdev_next_lower_dev(dev, &iter);
6751 ldev;
6752 ldev = netdev_next_lower_dev(dev, &iter)) {
6753 /* first is the lower device itself */
6754 ret = fn(ldev, data);
6755 if (ret)
6756 return ret;
6757
6758 /* then look at all of its lower devices */
6759 ret = netdev_walk_all_lower_dev(ldev, fn, data);
6760 if (ret)
6761 return ret;
6762 }
6763
6764 return 0;
6765}
6766EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
6767
David Ahern1a3f0602016-10-17 19:15:44 -07006768static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
6769 struct list_head **iter)
6770{
6771 struct netdev_adjacent *lower;
6772
6773 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6774 if (&lower->list == &dev->adj_list.lower)
6775 return NULL;
6776
6777 *iter = &lower->list;
6778
6779 return lower->dev;
6780}
6781
6782int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
6783 int (*fn)(struct net_device *dev,
6784 void *data),
6785 void *data)
6786{
6787 struct net_device *ldev;
6788 struct list_head *iter;
6789 int ret;
6790
6791 for (iter = &dev->adj_list.lower,
6792 ldev = netdev_next_lower_dev_rcu(dev, &iter);
6793 ldev;
6794 ldev = netdev_next_lower_dev_rcu(dev, &iter)) {
6795 /* first is the lower device itself */
6796 ret = fn(ldev, data);
6797 if (ret)
6798 return ret;
6799
6800 /* then look at all of its lower devices */
6801 ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data);
6802 if (ret)
6803 return ret;
6804 }
6805
6806 return 0;
6807}
6808EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
6809
Jiri Pirko7ce856a2016-07-04 08:23:12 +02006810/**
dingtianhonge001bfa2013-12-13 10:19:55 +08006811 * netdev_lower_get_first_private_rcu - Get the first ->private from the
6812 * lower neighbour list, RCU
6813 * variant
6814 * @dev: device
6815 *
6816 * Gets the first netdev_adjacent->private from the dev's lower neighbour
6817 * list. The caller must hold RCU read lock.
6818 */
6819void *netdev_lower_get_first_private_rcu(struct net_device *dev)
6820{
6821 struct netdev_adjacent *lower;
6822
6823 lower = list_first_or_null_rcu(&dev->adj_list.lower,
6824 struct netdev_adjacent, list);
6825 if (lower)
6826 return lower->private;
6827 return NULL;
6828}
6829EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
6830
6831/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006832 * netdev_master_upper_dev_get_rcu - Get master upper device
6833 * @dev: device
6834 *
6835 * Find a master upper device and return pointer to it or NULL in case
6836 * it's not there. The caller must hold the RCU read lock.
6837 */
6838struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
6839{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006840 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006841
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006842 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006843 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006844 if (upper && likely(upper->master))
6845 return upper->dev;
6846 return NULL;
6847}
6848EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
6849
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306850static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006851 struct net_device *adj_dev,
6852 struct list_head *dev_list)
6853{
6854 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006855
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006856 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6857 "upper_%s" : "lower_%s", adj_dev->name);
6858 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
6859 linkname);
6860}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306861static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006862 char *name,
6863 struct list_head *dev_list)
6864{
6865 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006866
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006867 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6868 "upper_%s" : "lower_%s", name);
6869 sysfs_remove_link(&(dev->dev.kobj), linkname);
6870}
6871
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006872static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
6873 struct net_device *adj_dev,
6874 struct list_head *dev_list)
6875{
6876 return (dev_list == &dev->adj_list.upper ||
6877 dev_list == &dev->adj_list.lower) &&
6878 net_eq(dev_net(dev), dev_net(adj_dev));
6879}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006880
Veaceslav Falico5d261912013-08-28 23:25:05 +02006881static int __netdev_adjacent_dev_insert(struct net_device *dev,
6882 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02006883 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006884 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006885{
6886 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006887 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006888
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006889 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006890
6891 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07006892 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07006893 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
6894 dev->name, adj_dev->name, adj->ref_nr);
6895
Veaceslav Falico5d261912013-08-28 23:25:05 +02006896 return 0;
6897 }
6898
6899 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
6900 if (!adj)
6901 return -ENOMEM;
6902
6903 adj->dev = adj_dev;
6904 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07006905 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006906 adj->private = private;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006907 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006908
David Ahern67b62f92016-10-17 19:15:53 -07006909 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
6910 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006911
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006912 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006913 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006914 if (ret)
6915 goto free_adj;
6916 }
6917
Veaceslav Falico7863c052013-09-25 09:20:06 +02006918 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006919 if (master) {
6920 ret = sysfs_create_link(&(dev->dev.kobj),
6921 &(adj_dev->dev.kobj), "master");
6922 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02006923 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006924
Veaceslav Falico7863c052013-09-25 09:20:06 +02006925 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006926 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02006927 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006928 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006929
6930 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006931
Veaceslav Falico5831d662013-09-25 09:20:32 +02006932remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006933 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006934 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006935free_adj:
6936 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02006937 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006938
6939 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006940}
6941
stephen hemminger1d143d92013-12-29 14:01:29 -08006942static void __netdev_adjacent_dev_remove(struct net_device *dev,
6943 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006944 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006945 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006946{
6947 struct netdev_adjacent *adj;
6948
David Ahern67b62f92016-10-17 19:15:53 -07006949 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
6950 dev->name, adj_dev->name, ref_nr);
6951
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006952 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006953
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006954 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07006955 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006956 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07006957 WARN_ON(1);
6958 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006959 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006960
Andrew Collins93409032016-10-03 13:43:02 -06006961 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07006962 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
6963 dev->name, adj_dev->name, ref_nr,
6964 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06006965 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006966 return;
6967 }
6968
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006969 if (adj->master)
6970 sysfs_remove_link(&(dev->dev.kobj), "master");
6971
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006972 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006973 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006974
Veaceslav Falico5d261912013-08-28 23:25:05 +02006975 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07006976 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006977 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006978 dev_put(adj_dev);
6979 kfree_rcu(adj, rcu);
6980}
6981
stephen hemminger1d143d92013-12-29 14:01:29 -08006982static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
6983 struct net_device *upper_dev,
6984 struct list_head *up_list,
6985 struct list_head *down_list,
6986 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006987{
6988 int ret;
6989
David Ahern790510d2016-10-17 19:15:43 -07006990 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06006991 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006992 if (ret)
6993 return ret;
6994
David Ahern790510d2016-10-17 19:15:43 -07006995 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06006996 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006997 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07006998 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006999 return ret;
7000 }
7001
7002 return 0;
7003}
7004
stephen hemminger1d143d92013-12-29 14:01:29 -08007005static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7006 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06007007 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08007008 struct list_head *up_list,
7009 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02007010{
Andrew Collins93409032016-10-03 13:43:02 -06007011 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7012 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007013}
7014
stephen hemminger1d143d92013-12-29 14:01:29 -08007015static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7016 struct net_device *upper_dev,
7017 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007018{
David Ahernf1170fd2016-10-17 19:15:51 -07007019 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7020 &dev->adj_list.upper,
7021 &upper_dev->adj_list.lower,
7022 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007023}
7024
stephen hemminger1d143d92013-12-29 14:01:29 -08007025static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7026 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007027{
Andrew Collins93409032016-10-03 13:43:02 -06007028 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007029 &dev->adj_list.upper,
7030 &upper_dev->adj_list.lower);
7031}
Veaceslav Falico5d261912013-08-28 23:25:05 +02007032
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007033static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007034 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07007035 void *upper_priv, void *upper_info,
7036 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007037{
David Ahern51d0c0472017-10-04 17:48:45 -07007038 struct netdev_notifier_changeupper_info changeupper_info = {
7039 .info = {
7040 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007041 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07007042 },
7043 .upper_dev = upper_dev,
7044 .master = master,
7045 .linking = true,
7046 .upper_info = upper_info,
7047 };
Mike Manning50d629e2018-02-26 23:49:30 +00007048 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007049 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007050
7051 ASSERT_RTNL();
7052
7053 if (dev == upper_dev)
7054 return -EBUSY;
7055
7056 /* To prevent loops, check if dev is not upper device to upper_dev. */
David Ahernf1170fd2016-10-17 19:15:51 -07007057 if (netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007058 return -EBUSY;
7059
Mike Manning50d629e2018-02-26 23:49:30 +00007060 if (!master) {
7061 if (netdev_has_upper_dev(dev, upper_dev))
7062 return -EEXIST;
7063 } else {
7064 master_dev = netdev_master_upper_dev_get(dev);
7065 if (master_dev)
7066 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7067 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007068
David Ahern51d0c0472017-10-04 17:48:45 -07007069 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007070 &changeupper_info.info);
7071 ret = notifier_to_errno(ret);
7072 if (ret)
7073 return ret;
7074
Jiri Pirko6dffb042015-12-03 12:12:10 +01007075 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007076 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007077 if (ret)
7078 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007079
David Ahern51d0c0472017-10-04 17:48:45 -07007080 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007081 &changeupper_info.info);
7082 ret = notifier_to_errno(ret);
7083 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007084 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007085
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007086 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007087
David Ahernf1170fd2016-10-17 19:15:51 -07007088rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007089 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007090
7091 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007092}
7093
7094/**
7095 * netdev_upper_dev_link - Add a link to the upper device
7096 * @dev: device
7097 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007098 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007099 *
7100 * Adds a link to device which is upper to this one. The caller must hold
7101 * the RTNL lock. On a failure a negative errno code is returned.
7102 * On success the reference counts are adjusted and the function
7103 * returns zero.
7104 */
7105int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007106 struct net_device *upper_dev,
7107 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007108{
David Ahern42ab19e2017-10-04 17:48:47 -07007109 return __netdev_upper_dev_link(dev, upper_dev, false,
7110 NULL, NULL, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007111}
7112EXPORT_SYMBOL(netdev_upper_dev_link);
7113
7114/**
7115 * netdev_master_upper_dev_link - Add a master link to the upper device
7116 * @dev: device
7117 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007118 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007119 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007120 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007121 *
7122 * Adds a link to device which is upper to this one. In this case, only
7123 * one master upper device can be linked, although other non-master devices
7124 * might be linked as well. The caller must hold the RTNL lock.
7125 * On a failure a negative errno code is returned. On success the reference
7126 * counts are adjusted and the function returns zero.
7127 */
7128int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007129 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007130 void *upper_priv, void *upper_info,
7131 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007132{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007133 return __netdev_upper_dev_link(dev, upper_dev, true,
David Ahern42ab19e2017-10-04 17:48:47 -07007134 upper_priv, upper_info, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007135}
7136EXPORT_SYMBOL(netdev_master_upper_dev_link);
7137
7138/**
7139 * netdev_upper_dev_unlink - Removes a link to upper device
7140 * @dev: device
7141 * @upper_dev: new upper device
7142 *
7143 * Removes a link to device which is upper to this one. The caller must hold
7144 * the RTNL lock.
7145 */
7146void netdev_upper_dev_unlink(struct net_device *dev,
7147 struct net_device *upper_dev)
7148{
David Ahern51d0c0472017-10-04 17:48:45 -07007149 struct netdev_notifier_changeupper_info changeupper_info = {
7150 .info = {
7151 .dev = dev,
7152 },
7153 .upper_dev = upper_dev,
7154 .linking = false,
7155 };
tchardingf4563a72017-02-09 17:56:07 +11007156
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007157 ASSERT_RTNL();
7158
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007159 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007160
David Ahern51d0c0472017-10-04 17:48:45 -07007161 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007162 &changeupper_info.info);
7163
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007164 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007165
David Ahern51d0c0472017-10-04 17:48:45 -07007166 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007167 &changeupper_info.info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007168}
7169EXPORT_SYMBOL(netdev_upper_dev_unlink);
7170
Moni Shoua61bd3852015-02-03 16:48:29 +02007171/**
7172 * netdev_bonding_info_change - Dispatch event about slave change
7173 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09007174 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02007175 *
7176 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7177 * The caller must hold the RTNL lock.
7178 */
7179void netdev_bonding_info_change(struct net_device *dev,
7180 struct netdev_bonding_info *bonding_info)
7181{
David Ahern51d0c0472017-10-04 17:48:45 -07007182 struct netdev_notifier_bonding_info info = {
7183 .info.dev = dev,
7184 };
Moni Shoua61bd3852015-02-03 16:48:29 +02007185
7186 memcpy(&info.bonding_info, bonding_info,
7187 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07007188 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02007189 &info.info);
7190}
7191EXPORT_SYMBOL(netdev_bonding_info_change);
7192
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007193static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007194{
7195 struct netdev_adjacent *iter;
7196
7197 struct net *net = dev_net(dev);
7198
7199 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007200 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007201 continue;
7202 netdev_adjacent_sysfs_add(iter->dev, dev,
7203 &iter->dev->adj_list.lower);
7204 netdev_adjacent_sysfs_add(dev, iter->dev,
7205 &dev->adj_list.upper);
7206 }
7207
7208 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007209 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007210 continue;
7211 netdev_adjacent_sysfs_add(iter->dev, dev,
7212 &iter->dev->adj_list.upper);
7213 netdev_adjacent_sysfs_add(dev, iter->dev,
7214 &dev->adj_list.lower);
7215 }
7216}
7217
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007218static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007219{
7220 struct netdev_adjacent *iter;
7221
7222 struct net *net = dev_net(dev);
7223
7224 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007225 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007226 continue;
7227 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7228 &iter->dev->adj_list.lower);
7229 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7230 &dev->adj_list.upper);
7231 }
7232
7233 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007234 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007235 continue;
7236 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7237 &iter->dev->adj_list.upper);
7238 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7239 &dev->adj_list.lower);
7240 }
7241}
7242
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007243void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02007244{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007245 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007246
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007247 struct net *net = dev_net(dev);
7248
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007249 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007250 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007251 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007252 netdev_adjacent_sysfs_del(iter->dev, oldname,
7253 &iter->dev->adj_list.lower);
7254 netdev_adjacent_sysfs_add(iter->dev, dev,
7255 &iter->dev->adj_list.lower);
7256 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007257
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007258 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007259 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007260 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007261 netdev_adjacent_sysfs_del(iter->dev, oldname,
7262 &iter->dev->adj_list.upper);
7263 netdev_adjacent_sysfs_add(iter->dev, dev,
7264 &iter->dev->adj_list.upper);
7265 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007266}
Veaceslav Falico402dae92013-09-25 09:20:09 +02007267
7268void *netdev_lower_dev_get_private(struct net_device *dev,
7269 struct net_device *lower_dev)
7270{
7271 struct netdev_adjacent *lower;
7272
7273 if (!lower_dev)
7274 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007275 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02007276 if (!lower)
7277 return NULL;
7278
7279 return lower->private;
7280}
7281EXPORT_SYMBOL(netdev_lower_dev_get_private);
7282
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007283
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02007284int dev_get_nest_level(struct net_device *dev)
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007285{
7286 struct net_device *lower = NULL;
7287 struct list_head *iter;
7288 int max_nest = -1;
7289 int nest;
7290
7291 ASSERT_RTNL();
7292
7293 netdev_for_each_lower_dev(dev, lower, iter) {
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02007294 nest = dev_get_nest_level(lower);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007295 if (max_nest < nest)
7296 max_nest = nest;
7297 }
7298
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02007299 return max_nest + 1;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007300}
7301EXPORT_SYMBOL(dev_get_nest_level);
7302
Jiri Pirko04d48262015-12-03 12:12:15 +01007303/**
7304 * netdev_lower_change - Dispatch event about lower device state change
7305 * @lower_dev: device
7306 * @lower_state_info: state to dispatch
7307 *
7308 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7309 * The caller must hold the RTNL lock.
7310 */
7311void netdev_lower_state_changed(struct net_device *lower_dev,
7312 void *lower_state_info)
7313{
David Ahern51d0c0472017-10-04 17:48:45 -07007314 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7315 .info.dev = lower_dev,
7316 };
Jiri Pirko04d48262015-12-03 12:12:15 +01007317
7318 ASSERT_RTNL();
7319 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07007320 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01007321 &changelowerstate_info.info);
7322}
7323EXPORT_SYMBOL(netdev_lower_state_changed);
7324
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007325static void dev_change_rx_flags(struct net_device *dev, int flags)
7326{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007327 const struct net_device_ops *ops = dev->netdev_ops;
7328
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05007329 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007330 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007331}
7332
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007333static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07007334{
Eric Dumazetb536db92011-11-30 21:42:26 +00007335 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06007336 kuid_t uid;
7337 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07007338
Patrick McHardy24023452007-07-14 18:51:31 -07007339 ASSERT_RTNL();
7340
Wang Chendad9b332008-06-18 01:48:28 -07007341 dev->flags |= IFF_PROMISC;
7342 dev->promiscuity += inc;
7343 if (dev->promiscuity == 0) {
7344 /*
7345 * Avoid overflow.
7346 * If inc causes overflow, untouch promisc and return error.
7347 */
7348 if (inc < 0)
7349 dev->flags &= ~IFF_PROMISC;
7350 else {
7351 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007352 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7353 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007354 return -EOVERFLOW;
7355 }
7356 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007357 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007358 pr_info("device %s %s promiscuous mode\n",
7359 dev->name,
7360 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11007361 if (audit_enabled) {
7362 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04007363 audit_log(audit_context(), GFP_ATOMIC,
7364 AUDIT_ANOM_PROMISCUOUS,
7365 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7366 dev->name, (dev->flags & IFF_PROMISC),
7367 (old_flags & IFF_PROMISC),
7368 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7369 from_kuid(&init_user_ns, uid),
7370 from_kgid(&init_user_ns, gid),
7371 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11007372 }
Patrick McHardy24023452007-07-14 18:51:31 -07007373
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007374 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07007375 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007376 if (notify)
7377 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07007378 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007379}
7380
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381/**
7382 * dev_set_promiscuity - update promiscuity count on a device
7383 * @dev: device
7384 * @inc: modifier
7385 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07007386 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 * remains above zero the interface remains promiscuous. Once it hits zero
7388 * the device reverts back to normal filtering operation. A negative inc
7389 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07007390 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 */
Wang Chendad9b332008-06-18 01:48:28 -07007392int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393{
Eric Dumazetb536db92011-11-30 21:42:26 +00007394 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07007395 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007397 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07007398 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07007399 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07007400 if (dev->flags != old_flags)
7401 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07007402 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007404EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007406static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007408 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
Patrick McHardy24023452007-07-14 18:51:31 -07007410 ASSERT_RTNL();
7411
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07007413 dev->allmulti += inc;
7414 if (dev->allmulti == 0) {
7415 /*
7416 * Avoid overflow.
7417 * If inc causes overflow, untouch allmulti and return error.
7418 */
7419 if (inc < 0)
7420 dev->flags &= ~IFF_ALLMULTI;
7421 else {
7422 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007423 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
7424 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007425 return -EOVERFLOW;
7426 }
7427 }
Patrick McHardy24023452007-07-14 18:51:31 -07007428 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007429 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07007430 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007431 if (notify)
7432 __dev_notify_flags(dev, old_flags,
7433 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07007434 }
Wang Chendad9b332008-06-18 01:48:28 -07007435 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007436}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007437
7438/**
7439 * dev_set_allmulti - update allmulti count on a device
7440 * @dev: device
7441 * @inc: modifier
7442 *
7443 * Add or remove reception of all multicast frames to a device. While the
7444 * count in the device remains above zero the interface remains listening
7445 * to all interfaces. Once it hits zero the device reverts back to normal
7446 * filtering operation. A negative @inc value is used to drop the counter
7447 * when releasing a resource needing all multicasts.
7448 * Return 0 if successful or a negative errno code on error.
7449 */
7450
7451int dev_set_allmulti(struct net_device *dev, int inc)
7452{
7453 return __dev_set_allmulti(dev, inc, true);
7454}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007455EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07007456
7457/*
7458 * Upload unicast and multicast address lists to device and
7459 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08007460 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07007461 * are present.
7462 */
7463void __dev_set_rx_mode(struct net_device *dev)
7464{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007465 const struct net_device_ops *ops = dev->netdev_ops;
7466
Patrick McHardy4417da62007-06-27 01:28:10 -07007467 /* dev_open will call this function so the list will stay sane. */
7468 if (!(dev->flags&IFF_UP))
7469 return;
7470
7471 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09007472 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07007473
Jiri Pirko01789342011-08-16 06:29:00 +00007474 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07007475 /* Unicast addresses changes may only happen under the rtnl,
7476 * therefore calling __dev_set_promiscuity here is safe.
7477 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08007478 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007479 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07007480 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08007481 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007482 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07007483 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07007484 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007485 }
Jiri Pirko01789342011-08-16 06:29:00 +00007486
7487 if (ops->ndo_set_rx_mode)
7488 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07007489}
7490
7491void dev_set_rx_mode(struct net_device *dev)
7492{
David S. Millerb9e40852008-07-15 00:15:08 -07007493 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07007494 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07007495 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496}
7497
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007498/**
7499 * dev_get_flags - get flags reported to userspace
7500 * @dev: device
7501 *
7502 * Get the combination of flag bits exported through APIs to userspace.
7503 */
Eric Dumazet95c96172012-04-15 05:58:06 +00007504unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505{
Eric Dumazet95c96172012-04-15 05:58:06 +00007506 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507
7508 flags = (dev->flags & ~(IFF_PROMISC |
7509 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08007510 IFF_RUNNING |
7511 IFF_LOWER_UP |
7512 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 (dev->gflags & (IFF_PROMISC |
7514 IFF_ALLMULTI));
7515
Stefan Rompfb00055a2006-03-20 17:09:11 -08007516 if (netif_running(dev)) {
7517 if (netif_oper_up(dev))
7518 flags |= IFF_RUNNING;
7519 if (netif_carrier_ok(dev))
7520 flags |= IFF_LOWER_UP;
7521 if (netif_dormant(dev))
7522 flags |= IFF_DORMANT;
7523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
7525 return flags;
7526}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007527EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528
Petr Machata6d040322018-12-06 17:05:43 +00007529int __dev_change_flags(struct net_device *dev, unsigned int flags,
7530 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531{
Eric Dumazetb536db92011-11-30 21:42:26 +00007532 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00007533 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
Patrick McHardy24023452007-07-14 18:51:31 -07007535 ASSERT_RTNL();
7536
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 /*
7538 * Set the flags on our device.
7539 */
7540
7541 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
7542 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
7543 IFF_AUTOMEDIA)) |
7544 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
7545 IFF_ALLMULTI));
7546
7547 /*
7548 * Load in the correct multicast list now the flags have changed.
7549 */
7550
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007551 if ((old_flags ^ flags) & IFF_MULTICAST)
7552 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07007553
Patrick McHardy4417da62007-06-27 01:28:10 -07007554 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555
7556 /*
7557 * Have we downed the interface. We handle IFF_UP ourselves
7558 * according to user attempts to set it, rather than blindly
7559 * setting it.
7560 */
7561
7562 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07007563 if ((old_flags ^ flags) & IFF_UP) {
7564 if (old_flags & IFF_UP)
7565 __dev_close(dev);
7566 else
Petr Machata40c900a2018-12-06 17:05:47 +00007567 ret = __dev_open(dev, extack);
stephen hemminger7051b882017-07-18 15:59:27 -07007568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007571 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007572 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007573
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007575
7576 if (__dev_set_promiscuity(dev, inc, false) >= 0)
7577 if (dev->flags != old_flags)
7578 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 }
7580
7581 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11007582 * is important. Some (broken) drivers set IFF_PROMISC, when
7583 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 */
7585 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007586 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
7587
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007589 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 }
7591
Patrick McHardybd380812010-02-26 06:34:53 +00007592 return ret;
7593}
7594
Nicolas Dichtela528c212013-09-25 12:02:44 +02007595void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
7596 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00007597{
7598 unsigned int changes = dev->flags ^ old_flags;
7599
Nicolas Dichtela528c212013-09-25 12:02:44 +02007600 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07007601 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02007602
Patrick McHardybd380812010-02-26 06:34:53 +00007603 if (changes & IFF_UP) {
7604 if (dev->flags & IFF_UP)
7605 call_netdevice_notifiers(NETDEV_UP, dev);
7606 else
7607 call_netdevice_notifiers(NETDEV_DOWN, dev);
7608 }
7609
7610 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00007611 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07007612 struct netdev_notifier_change_info change_info = {
7613 .info = {
7614 .dev = dev,
7615 },
7616 .flags_changed = changes,
7617 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00007618
David Ahern51d0c0472017-10-04 17:48:45 -07007619 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00007620 }
Patrick McHardybd380812010-02-26 06:34:53 +00007621}
7622
7623/**
7624 * dev_change_flags - change device settings
7625 * @dev: device
7626 * @flags: device state flags
Petr Machata567c5e12018-12-06 17:05:42 +00007627 * @extack: netlink extended ack
Patrick McHardybd380812010-02-26 06:34:53 +00007628 *
7629 * Change settings on device based state flags. The flags are
7630 * in the userspace exported format.
7631 */
Petr Machata567c5e12018-12-06 17:05:42 +00007632int dev_change_flags(struct net_device *dev, unsigned int flags,
7633 struct netlink_ext_ack *extack)
Patrick McHardybd380812010-02-26 06:34:53 +00007634{
Eric Dumazetb536db92011-11-30 21:42:26 +00007635 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007636 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00007637
Petr Machata6d040322018-12-06 17:05:43 +00007638 ret = __dev_change_flags(dev, flags, extack);
Patrick McHardybd380812010-02-26 06:34:53 +00007639 if (ret < 0)
7640 return ret;
7641
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007642 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02007643 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 return ret;
7645}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007646EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
WANG Congf51048c2017-07-06 15:01:57 -07007648int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007649{
7650 const struct net_device_ops *ops = dev->netdev_ops;
7651
7652 if (ops->ndo_change_mtu)
7653 return ops->ndo_change_mtu(dev, new_mtu);
7654
7655 dev->mtu = new_mtu;
7656 return 0;
7657}
WANG Congf51048c2017-07-06 15:01:57 -07007658EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007659
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007660/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007661 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007662 * @dev: device
7663 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007664 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007665 *
7666 * Change the maximum transfer size of the network device.
7667 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007668int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
7669 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007671 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
7673 if (new_mtu == dev->mtu)
7674 return 0;
7675
Jarod Wilson61e84622016-10-07 22:04:33 -04007676 /* MTU must be positive, and in range */
7677 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007678 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 return -EINVAL;
Jarod Wilson61e84622016-10-07 22:04:33 -04007680 }
7681
7682 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007683 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
Jarod Wilson61e84622016-10-07 22:04:33 -04007684 return -EINVAL;
7685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686
7687 if (!netif_device_present(dev))
7688 return -ENODEV;
7689
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01007690 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
7691 err = notifier_to_errno(err);
7692 if (err)
7693 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007694
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007695 orig_mtu = dev->mtu;
7696 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007698 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02007699 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
7700 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007701 err = notifier_to_errno(err);
7702 if (err) {
7703 /* setting mtu back and notifying everyone again,
7704 * so that they have a chance to revert changes.
7705 */
7706 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02007707 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
7708 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007709 }
7710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 return err;
7712}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007713
7714int dev_set_mtu(struct net_device *dev, int new_mtu)
7715{
7716 struct netlink_ext_ack extack;
7717 int err;
7718
Li RongQinga6bcfc82018-08-03 15:45:21 +08007719 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007720 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08007721 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007722 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
7723 return err;
7724}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007725EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007727/**
Cong Wang6a643dd2018-01-25 18:26:22 -08007728 * dev_change_tx_queue_len - Change TX queue length of a netdevice
7729 * @dev: device
7730 * @new_len: new tx queue length
7731 */
7732int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
7733{
7734 unsigned int orig_len = dev->tx_queue_len;
7735 int res;
7736
7737 if (new_len != (unsigned int)new_len)
7738 return -ERANGE;
7739
7740 if (new_len != orig_len) {
7741 dev->tx_queue_len = new_len;
7742 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
7743 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03007744 if (res)
7745 goto err_rollback;
7746 res = dev_qdisc_change_tx_queue_len(dev);
7747 if (res)
7748 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08007749 }
7750
7751 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03007752
7753err_rollback:
7754 netdev_err(dev, "refused to change device tx_queue_len\n");
7755 dev->tx_queue_len = orig_len;
7756 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08007757}
7758
7759/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00007760 * dev_set_group - Change group this device belongs to
7761 * @dev: device
7762 * @new_group: group this device should belong to
7763 */
7764void dev_set_group(struct net_device *dev, int new_group)
7765{
7766 dev->group = new_group;
7767}
7768EXPORT_SYMBOL(dev_set_group);
7769
7770/**
Petr Machatad59cdf92018-12-13 11:54:35 +00007771 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
7772 * @dev: device
7773 * @addr: new address
7774 * @extack: netlink extended ack
7775 */
7776int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
7777 struct netlink_ext_ack *extack)
7778{
7779 struct netdev_notifier_pre_changeaddr_info info = {
7780 .info.dev = dev,
7781 .info.extack = extack,
7782 .dev_addr = addr,
7783 };
7784 int rc;
7785
7786 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
7787 return notifier_to_errno(rc);
7788}
7789EXPORT_SYMBOL(dev_pre_changeaddr_notify);
7790
7791/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007792 * dev_set_mac_address - Change Media Access Control Address
7793 * @dev: device
7794 * @sa: new address
Petr Machata3a37a962018-12-13 11:54:30 +00007795 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007796 *
7797 * Change the hardware (MAC) address of the device
7798 */
Petr Machata3a37a962018-12-13 11:54:30 +00007799int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
7800 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007802 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 int err;
7804
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007805 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 return -EOPNOTSUPP;
7807 if (sa->sa_family != dev->type)
7808 return -EINVAL;
7809 if (!netif_device_present(dev))
7810 return -ENODEV;
Petr Machatad59cdf92018-12-13 11:54:35 +00007811 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
7812 if (err)
7813 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007814 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00007815 if (err)
7816 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00007817 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00007818 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04007819 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00007820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007822EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
Jiri Pirko4bf84c32012-12-27 23:49:37 +00007824/**
7825 * dev_change_carrier - Change device carrier
7826 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00007827 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00007828 *
7829 * Change device carrier
7830 */
7831int dev_change_carrier(struct net_device *dev, bool new_carrier)
7832{
7833 const struct net_device_ops *ops = dev->netdev_ops;
7834
7835 if (!ops->ndo_change_carrier)
7836 return -EOPNOTSUPP;
7837 if (!netif_device_present(dev))
7838 return -ENODEV;
7839 return ops->ndo_change_carrier(dev, new_carrier);
7840}
7841EXPORT_SYMBOL(dev_change_carrier);
7842
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02007844 * dev_get_phys_port_id - Get device physical port ID
7845 * @dev: device
7846 * @ppid: port ID
7847 *
7848 * Get device physical port ID
7849 */
7850int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01007851 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02007852{
7853 const struct net_device_ops *ops = dev->netdev_ops;
7854
7855 if (!ops->ndo_get_phys_port_id)
7856 return -EOPNOTSUPP;
7857 return ops->ndo_get_phys_port_id(dev, ppid);
7858}
7859EXPORT_SYMBOL(dev_get_phys_port_id);
7860
7861/**
David Aherndb24a902015-03-17 20:23:15 -06007862 * dev_get_phys_port_name - Get device physical port name
7863 * @dev: device
7864 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00007865 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06007866 *
7867 * Get device physical port name
7868 */
7869int dev_get_phys_port_name(struct net_device *dev,
7870 char *name, size_t len)
7871{
7872 const struct net_device_ops *ops = dev->netdev_ops;
7873
7874 if (!ops->ndo_get_phys_port_name)
7875 return -EOPNOTSUPP;
7876 return ops->ndo_get_phys_port_name(dev, name, len);
7877}
7878EXPORT_SYMBOL(dev_get_phys_port_name);
7879
7880/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007881 * dev_change_proto_down - update protocol port state information
7882 * @dev: device
7883 * @proto_down: new value
7884 *
7885 * This info can be used by switch drivers to set the phys state of the
7886 * port.
7887 */
7888int dev_change_proto_down(struct net_device *dev, bool proto_down)
7889{
7890 const struct net_device_ops *ops = dev->netdev_ops;
7891
7892 if (!ops->ndo_change_proto_down)
7893 return -EOPNOTSUPP;
7894 if (!netif_device_present(dev))
7895 return -ENODEV;
7896 return ops->ndo_change_proto_down(dev, proto_down);
7897}
7898EXPORT_SYMBOL(dev_change_proto_down);
7899
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007900u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
7901 enum bpf_netdev_command cmd)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007902{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007903 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007904
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007905 if (!bpf_op)
7906 return 0;
Martin KaFai Lau58038692017-06-15 17:29:09 -07007907
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007908 memset(&xdp, 0, sizeof(xdp));
7909 xdp.command = cmd;
7910
7911 /* Query must always succeed. */
7912 WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007913
Jakub Kicinski6b867582018-07-11 20:36:39 -07007914 return xdp.prog_id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007915}
7916
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007917static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07007918 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007919 struct bpf_prog *prog)
7920{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007921 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007922
7923 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07007924 if (flags & XDP_FLAGS_HW_MODE)
7925 xdp.command = XDP_SETUP_PROG_HW;
7926 else
7927 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007928 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07007929 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007930 xdp.prog = prog;
7931
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007932 return bpf_op(dev, &xdp);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007933}
7934
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007935static void dev_xdp_uninstall(struct net_device *dev)
7936{
7937 struct netdev_bpf xdp;
7938 bpf_op_t ndo_bpf;
7939
7940 /* Remove generic XDP */
7941 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
7942
7943 /* Remove from the driver */
7944 ndo_bpf = dev->netdev_ops->ndo_bpf;
7945 if (!ndo_bpf)
7946 return;
7947
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007948 memset(&xdp, 0, sizeof(xdp));
7949 xdp.command = XDP_QUERY_PROG;
7950 WARN_ON(ndo_bpf(dev, &xdp));
7951 if (xdp.prog_id)
7952 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
7953 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007954
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007955 /* Remove HW offload */
7956 memset(&xdp, 0, sizeof(xdp));
7957 xdp.command = XDP_QUERY_PROG_HW;
7958 if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
7959 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
7960 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007961}
7962
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007963/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07007964 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
7965 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07007966 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07007967 * @fd: new program fd or negative value to clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01007968 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07007969 *
7970 * Set or clear a bpf program for a device
7971 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07007972int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
7973 int fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07007974{
7975 const struct net_device_ops *ops = dev->netdev_ops;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007976 enum bpf_netdev_command query;
Brenden Blancoa7862b42016-07-19 12:16:48 -07007977 struct bpf_prog *prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007978 bpf_op_t bpf_op, bpf_chk;
Brenden Blancoa7862b42016-07-19 12:16:48 -07007979 int err;
7980
Daniel Borkmann85de8572016-11-28 23:16:54 +01007981 ASSERT_RTNL();
7982
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007983 query = flags & XDP_FLAGS_HW_MODE ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
7984
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007985 bpf_op = bpf_chk = ops->ndo_bpf;
7986 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE)))
Daniel Borkmann0489df92017-05-12 01:04:45 +02007987 return -EOPNOTSUPP;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007988 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
7989 bpf_op = generic_xdp_install;
7990 if (bpf_op == bpf_chk)
7991 bpf_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04007992
Brenden Blancoa7862b42016-07-19 12:16:48 -07007993 if (fd >= 0) {
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007994 if (__dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG) ||
7995 __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG_HW))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007996 return -EEXIST;
7997 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007998 __dev_xdp_query(dev, bpf_op, query))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007999 return -EBUSY;
Daniel Borkmann85de8572016-11-28 23:16:54 +01008000
Jakub Kicinski288b3de2017-11-20 15:21:54 -08008001 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
8002 bpf_op == ops->ndo_bpf);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008003 if (IS_ERR(prog))
8004 return PTR_ERR(prog);
Jakub Kicinski441a3302017-11-20 15:21:55 -08008005
8006 if (!(flags & XDP_FLAGS_HW_MODE) &&
8007 bpf_prog_is_dev_bound(prog->aux)) {
8008 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
8009 bpf_prog_put(prog);
8010 return -EINVAL;
8011 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07008012 }
8013
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008014 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07008015 if (err < 0 && prog)
8016 bpf_prog_put(prog);
8017
8018 return err;
8019}
Brenden Blancoa7862b42016-07-19 12:16:48 -07008020
8021/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07008023 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 *
8025 * Returns a suitable unique value for a new device interface
8026 * number. The caller must hold the rtnl semaphore or the
8027 * dev_base_lock to be sure it remains unique.
8028 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07008029static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008031 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11008032
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033 for (;;) {
8034 if (++ifindex <= 0)
8035 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008036 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00008037 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038 }
8039}
8040
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08008042static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07008043DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044
Stephen Hemminger6f05f622007-03-08 20:46:03 -08008045static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008048 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049}
8050
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008051static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008052{
Krishna Kumare93737b2009-12-08 22:26:02 +00008053 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008054 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008055
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008056 BUG_ON(dev_boot_phase);
8057 ASSERT_RTNL();
8058
Krishna Kumare93737b2009-12-08 22:26:02 +00008059 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008060 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00008061 * for initialization unwind. Remove those
8062 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008063 */
8064 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008065 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
8066 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008067
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008068 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00008069 list_del(&dev->unreg_list);
8070 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008071 }
Eric Dumazet449f4542011-05-19 12:24:16 +00008072 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008073 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00008074 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008075
Octavian Purdila44345722010-12-13 12:44:07 +00008076 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008077 list_for_each_entry(dev, head, unreg_list)
8078 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04008079 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008080
Octavian Purdila44345722010-12-13 12:44:07 +00008081 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008082 /* And unlink it from device chain. */
8083 unlist_netdevice(dev);
8084
8085 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008086 }
Eric Dumazet41852492016-08-26 12:50:39 -07008087 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008088
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008089 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008090
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008091 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008092 struct sk_buff *skb = NULL;
8093
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008094 /* Shutdown queueing discipline. */
8095 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008096
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08008097 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008098
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008099 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008100 * this device. They should clean all the things.
8101 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008102 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
8103
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008104 if (!dev->rtnl_link_ops ||
8105 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04008106 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01008107 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008108
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008109 /*
8110 * Flush the unicast and multicast chains
8111 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008112 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008113 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008114
8115 if (dev->netdev_ops->ndo_uninit)
8116 dev->netdev_ops->ndo_uninit(dev);
8117
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008118 if (skb)
8119 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07008120
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008121 /* Notifier chain MUST detach us all upper devices. */
8122 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07008123 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008124
8125 /* Remove entries from kobject tree */
8126 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00008127#ifdef CONFIG_XPS
8128 /* Remove XPS queueing entries */
8129 netif_reset_xps_queues_gt(dev, 0);
8130#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008131 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008132
Eric W. Biederman850a5452011-10-13 22:25:23 +00008133 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008134
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00008135 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008136 dev_put(dev);
8137}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008138
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008139static void rollback_registered(struct net_device *dev)
8140{
8141 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008142
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008143 list_add(&dev->unreg_list, &single);
8144 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00008145 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008146}
8147
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008148static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
8149 struct net_device *upper, netdev_features_t features)
8150{
8151 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8152 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008153 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008154
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008155 for_each_netdev_feature(&upper_disables, feature_bit) {
8156 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008157 if (!(upper->wanted_features & feature)
8158 && (features & feature)) {
8159 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
8160 &feature, upper->name);
8161 features &= ~feature;
8162 }
8163 }
8164
8165 return features;
8166}
8167
8168static void netdev_sync_lower_features(struct net_device *upper,
8169 struct net_device *lower, netdev_features_t features)
8170{
8171 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8172 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008173 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008174
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008175 for_each_netdev_feature(&upper_disables, feature_bit) {
8176 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008177 if (!(features & feature) && (lower->features & feature)) {
8178 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
8179 &feature, lower->name);
8180 lower->wanted_features &= ~feature;
8181 netdev_update_features(lower);
8182
8183 if (unlikely(lower->features & feature))
8184 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
8185 &feature, lower->name);
8186 }
8187 }
8188}
8189
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008190static netdev_features_t netdev_fix_features(struct net_device *dev,
8191 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07008192{
Michał Mirosław57422dc2011-01-22 12:14:12 +00008193 /* Fix illegal checksum combinations */
8194 if ((features & NETIF_F_HW_CSUM) &&
8195 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008196 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00008197 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
8198 }
8199
Herbert Xub63365a2008-10-23 01:11:29 -07008200 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008201 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008202 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008203 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07008204 }
8205
Pravin B Shelarec5f0612013-03-07 09:28:01 +00008206 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
8207 !(features & NETIF_F_IP_CSUM)) {
8208 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
8209 features &= ~NETIF_F_TSO;
8210 features &= ~NETIF_F_TSO_ECN;
8211 }
8212
8213 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
8214 !(features & NETIF_F_IPV6_CSUM)) {
8215 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
8216 features &= ~NETIF_F_TSO6;
8217 }
8218
Alexander Duyckb1dc4972016-05-02 09:38:24 -07008219 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8220 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
8221 features &= ~NETIF_F_TSO_MANGLEID;
8222
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00008223 /* TSO ECN requires that TSO is present as well. */
8224 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
8225 features &= ~NETIF_F_TSO_ECN;
8226
Michał Mirosław212b5732011-02-15 16:59:16 +00008227 /* Software GSO depends on SG. */
8228 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008229 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00008230 features &= ~NETIF_F_GSO;
8231 }
8232
Alexander Duyck802ab552016-04-10 21:45:03 -04008233 /* GSO partial features require GSO partial be set */
8234 if ((features & dev->gso_partial_features) &&
8235 !(features & NETIF_F_GSO_PARTIAL)) {
8236 netdev_dbg(dev,
8237 "Dropping partially supported GSO features since no GSO partial.\n");
8238 features &= ~dev->gso_partial_features;
8239 }
8240
Michael Chanfb1f5f72017-12-16 03:09:40 -05008241 if (!(features & NETIF_F_RXCSUM)) {
8242 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8243 * successfully merged by hardware must also have the
8244 * checksum verified by hardware. If the user does not
8245 * want to enable RXCSUM, logically, we should disable GRO_HW.
8246 */
8247 if (features & NETIF_F_GRO_HW) {
8248 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8249 features &= ~NETIF_F_GRO_HW;
8250 }
8251 }
8252
Gal Pressmande8d5ab2018-03-12 11:48:49 +02008253 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8254 if (features & NETIF_F_RXFCS) {
8255 if (features & NETIF_F_LRO) {
8256 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
8257 features &= ~NETIF_F_LRO;
8258 }
8259
8260 if (features & NETIF_F_GRO_HW) {
8261 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8262 features &= ~NETIF_F_GRO_HW;
8263 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02008264 }
8265
Herbert Xub63365a2008-10-23 01:11:29 -07008266 return features;
8267}
Herbert Xub63365a2008-10-23 01:11:29 -07008268
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008269int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00008270{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008271 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008272 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008273 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05008274 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00008275
Michał Mirosław87267482011-04-12 09:56:38 +00008276 ASSERT_RTNL();
8277
Michał Mirosław5455c692011-02-15 16:59:17 +00008278 features = netdev_get_wanted_features(dev);
8279
8280 if (dev->netdev_ops->ndo_fix_features)
8281 features = dev->netdev_ops->ndo_fix_features(dev, features);
8282
8283 /* driver might be less strict about feature dependencies */
8284 features = netdev_fix_features(dev, features);
8285
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008286 /* some features can't be enabled if they're off an an upper device */
8287 netdev_for_each_upper_dev_rcu(dev, upper, iter)
8288 features = netdev_sync_upper_features(dev, upper, features);
8289
Michał Mirosław5455c692011-02-15 16:59:17 +00008290 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05008291 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00008292
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008293 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
8294 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00008295
8296 if (dev->netdev_ops->ndo_set_features)
8297 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01008298 else
8299 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00008300
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008301 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00008302 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008303 "set_features() failed (%d); wanted %pNF, left %pNF\n",
8304 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01008305 /* return non-0 since some features might have changed and
8306 * it's better to fire a spurious notification than miss it
8307 */
8308 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008309 }
8310
Jarod Wilsone7868a82015-11-03 23:09:32 -05008311sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008312 /* some features must be disabled on lower devices when disabled
8313 * on an upper device (think: bonding master or bridge)
8314 */
8315 netdev_for_each_lower_dev(dev, lower, iter)
8316 netdev_sync_lower_features(dev, lower, features);
8317
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02008318 if (!err) {
8319 netdev_features_t diff = features ^ dev->features;
8320
8321 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
8322 /* udp_tunnel_{get,drop}_rx_info both need
8323 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
8324 * device, or they won't do anything.
8325 * Thus we need to update dev->features
8326 * *before* calling udp_tunnel_get_rx_info,
8327 * but *after* calling udp_tunnel_drop_rx_info.
8328 */
8329 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
8330 dev->features = features;
8331 udp_tunnel_get_rx_info(dev);
8332 } else {
8333 udp_tunnel_drop_rx_info(dev);
8334 }
8335 }
8336
Gal Pressman9daae9b2018-03-28 17:46:54 +03008337 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
8338 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
8339 dev->features = features;
8340 err |= vlan_get_rx_ctag_filter_info(dev);
8341 } else {
8342 vlan_drop_rx_ctag_filter_info(dev);
8343 }
8344 }
8345
8346 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
8347 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
8348 dev->features = features;
8349 err |= vlan_get_rx_stag_filter_info(dev);
8350 } else {
8351 vlan_drop_rx_stag_filter_info(dev);
8352 }
8353 }
8354
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008355 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02008356 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008357
Jarod Wilsone7868a82015-11-03 23:09:32 -05008358 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008359}
8360
Michał Mirosławafe12cc2011-05-07 03:22:17 +00008361/**
8362 * netdev_update_features - recalculate device features
8363 * @dev: the device to check
8364 *
8365 * Recalculate dev->features set and send notifications if it
8366 * has changed. Should be called after driver or hardware dependent
8367 * conditions might have changed that influence the features.
8368 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008369void netdev_update_features(struct net_device *dev)
8370{
8371 if (__netdev_update_features(dev))
8372 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00008373}
8374EXPORT_SYMBOL(netdev_update_features);
8375
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00008377 * netdev_change_features - recalculate device features
8378 * @dev: the device to check
8379 *
8380 * Recalculate dev->features set and send notifications even
8381 * if they have not changed. Should be called instead of
8382 * netdev_update_features() if also dev->vlan_features might
8383 * have changed to allow the changes to be propagated to stacked
8384 * VLAN devices.
8385 */
8386void netdev_change_features(struct net_device *dev)
8387{
8388 __netdev_update_features(dev);
8389 netdev_features_change(dev);
8390}
8391EXPORT_SYMBOL(netdev_change_features);
8392
8393/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08008394 * netif_stacked_transfer_operstate - transfer operstate
8395 * @rootdev: the root or lower level device to transfer state from
8396 * @dev: the device to transfer operstate to
8397 *
8398 * Transfer operational state from root to device. This is normally
8399 * called when a stacking relationship exists between the root
8400 * device and the device(a leaf device).
8401 */
8402void netif_stacked_transfer_operstate(const struct net_device *rootdev,
8403 struct net_device *dev)
8404{
8405 if (rootdev->operstate == IF_OPER_DORMANT)
8406 netif_dormant_on(dev);
8407 else
8408 netif_dormant_off(dev);
8409
Zhang Shengju0575c862017-04-26 17:49:38 +08008410 if (netif_carrier_ok(rootdev))
8411 netif_carrier_on(dev);
8412 else
8413 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08008414}
8415EXPORT_SYMBOL(netif_stacked_transfer_operstate);
8416
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008417static int netif_alloc_rx_queues(struct net_device *dev)
8418{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008419 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00008420 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05308421 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008422 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008423
Tom Herbertbd25fa72010-10-18 18:00:16 +00008424 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008425
Michal Hockodcda9b02017-07-12 14:36:45 -07008426 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07008427 if (!rx)
8428 return -ENOMEM;
8429
Tom Herbertbd25fa72010-10-18 18:00:16 +00008430 dev->_rx = rx;
8431
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008432 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00008433 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008434
8435 /* XDP RX-queue setup */
8436 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
8437 if (err < 0)
8438 goto err_rxq_info;
8439 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008440 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008441
8442err_rxq_info:
8443 /* Rollback successful reg's and free other resources */
8444 while (i--)
8445 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08008446 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008447 dev->_rx = NULL;
8448 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008449}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008450
8451static void netif_free_rx_queues(struct net_device *dev)
8452{
8453 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008454
8455 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
8456 if (!dev->_rx)
8457 return;
8458
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008459 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08008460 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
8461
8462 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008463}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008464
Changli Gaoaa942102010-12-04 02:31:41 +00008465static void netdev_init_one_queue(struct net_device *dev,
8466 struct netdev_queue *queue, void *_unused)
8467{
8468 /* Initialize queue lock */
8469 spin_lock_init(&queue->_xmit_lock);
8470 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
8471 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00008472 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00008473 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00008474#ifdef CONFIG_BQL
8475 dql_init(&queue->dql, HZ);
8476#endif
Changli Gaoaa942102010-12-04 02:31:41 +00008477}
8478
Eric Dumazet60877a32013-06-20 01:15:51 -07008479static void netif_free_tx_queues(struct net_device *dev)
8480{
WANG Cong4cb28972014-06-02 15:55:22 -07008481 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07008482}
8483
Tom Herberte6484932010-10-18 18:04:39 +00008484static int netif_alloc_netdev_queues(struct net_device *dev)
8485{
8486 unsigned int count = dev->num_tx_queues;
8487 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07008488 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00008489
Eric Dumazetd3397272015-07-06 17:13:26 +02008490 if (count < 1 || count > 0xffff)
8491 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00008492
Michal Hockodcda9b02017-07-12 14:36:45 -07008493 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07008494 if (!tx)
8495 return -ENOMEM;
8496
Tom Herberte6484932010-10-18 18:04:39 +00008497 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00008498
Tom Herberte6484932010-10-18 18:04:39 +00008499 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
8500 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00008501
8502 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00008503}
8504
Denys Vlasenkoa2029242015-05-11 21:17:53 +02008505void netif_tx_stop_all_queues(struct net_device *dev)
8506{
8507 unsigned int i;
8508
8509 for (i = 0; i < dev->num_tx_queues; i++) {
8510 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11008511
Denys Vlasenkoa2029242015-05-11 21:17:53 +02008512 netif_tx_stop_queue(txq);
8513 }
8514}
8515EXPORT_SYMBOL(netif_tx_stop_all_queues);
8516
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08008517/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 * register_netdevice - register a network device
8519 * @dev: device to register
8520 *
8521 * Take a completed network device structure and add it to the kernel
8522 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
8523 * chain. 0 is returned on success. A negative errno code is returned
8524 * on a failure to set up the device, or if the name is a duplicate.
8525 *
8526 * Callers must hold the rtnl semaphore. You may want
8527 * register_netdev() instead of this.
8528 *
8529 * BUGS:
8530 * The locking appears insufficient to guarantee two parallel registers
8531 * will not get the same name.
8532 */
8533
8534int register_netdevice(struct net_device *dev)
8535{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008537 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538
Florian Fainellie283de32018-04-30 14:20:05 -07008539 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
8540 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 BUG_ON(dev_boot_phase);
8542 ASSERT_RTNL();
8543
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008544 might_sleep();
8545
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 /* When net_device's are persistent, this will be fatal. */
8547 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008548 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549
David S. Millerf1f28aa2008-07-15 00:08:33 -07008550 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07008551 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552
Gao feng828de4f2012-09-13 20:58:27 +00008553 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00008554 if (ret < 0)
8555 goto out;
8556
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008558 if (dev->netdev_ops->ndo_init) {
8559 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 if (ret) {
8561 if (ret > 0)
8562 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08008563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564 }
8565 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008566
Patrick McHardyf6469682013-04-19 02:04:27 +00008567 if (((dev->hw_features | dev->features) &
8568 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00008569 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
8570 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
8571 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
8572 ret = -EINVAL;
8573 goto err_uninit;
8574 }
8575
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00008576 ret = -EBUSY;
8577 if (!dev->ifindex)
8578 dev->ifindex = dev_new_index(net);
8579 else if (__dev_get_by_index(net, dev->ifindex))
8580 goto err_uninit;
8581
Michał Mirosław5455c692011-02-15 16:59:17 +00008582 /* Transfer changeable features to wanted_features and enable
8583 * software offloads (GSO and GRO).
8584 */
8585 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00008586 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02008587
8588 if (dev->netdev_ops->ndo_udp_tunnel_add) {
8589 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
8590 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
8591 }
8592
Michał Mirosław14d12322011-02-22 16:52:28 +00008593 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594
Alexander Duyckcbc53e02016-04-10 21:44:51 -04008595 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00008596 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04008597
Alexander Duyck7f348a62016-04-20 16:51:00 -04008598 /* If IPv4 TCP segmentation offload is supported we should also
8599 * allow the device to enable segmenting the frame with the option
8600 * of ignoring a static IP ID value. This doesn't enable the
8601 * feature itself but allows the user to enable it later.
8602 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04008603 if (dev->hw_features & NETIF_F_TSO)
8604 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04008605 if (dev->vlan_features & NETIF_F_TSO)
8606 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
8607 if (dev->mpls_features & NETIF_F_TSO)
8608 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
8609 if (dev->hw_enc_features & NETIF_F_TSO)
8610 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07008611
Michał Mirosław1180e7d2011-07-14 14:41:11 -07008612 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00008613 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07008614 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00008615
Pravin B Shelaree579672013-03-07 09:28:08 +00008616 /* Make NETIF_F_SG inheritable to tunnel devices.
8617 */
Alexander Duyck802ab552016-04-10 21:45:03 -04008618 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00008619
Simon Horman0d89d202013-05-23 21:02:52 +00008620 /* Make NETIF_F_SG inheritable to MPLS.
8621 */
8622 dev->mpls_features |= NETIF_F_SG;
8623
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00008624 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
8625 ret = notifier_to_errno(ret);
8626 if (ret)
8627 goto err_uninit;
8628
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008629 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008630 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07008631 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008632 dev->reg_state = NETREG_REGISTERED;
8633
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008634 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00008635
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 /*
8637 * Default initial state at registry is that the
8638 * device is present.
8639 */
8640
8641 set_bit(__LINK_STATE_PRESENT, &dev->state);
8642
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01008643 linkwatch_init_dev(dev);
8644
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008647 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008648 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649
Jiri Pirko948b3372013-01-08 01:38:25 +00008650 /* If the device has permanent device address, driver should
8651 * set dev_addr and also addr_assign_type should be set to
8652 * NET_ADDR_PERM (default value).
8653 */
8654 if (dev->addr_assign_type == NET_ADDR_PERM)
8655 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
8656
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07008658 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07008659 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008660 if (ret) {
8661 rollback_registered(dev);
8662 dev->reg_state = NETREG_UNREGISTERED;
8663 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008664 /*
8665 * Prevent userspace races by waiting until the network
8666 * device is fully setup before sending notifications.
8667 */
Patrick McHardya2835762010-02-26 06:34:51 +00008668 if (!dev->rtnl_link_ops ||
8669 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008670 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671
8672out:
8673 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07008674
8675err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008676 if (dev->netdev_ops->ndo_uninit)
8677 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04008678 if (dev->priv_destructor)
8679 dev->priv_destructor(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07008680 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008682EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683
8684/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08008685 * init_dummy_netdev - init a dummy network device for NAPI
8686 * @dev: device to init
8687 *
8688 * This takes a network device structure and initialize the minimum
8689 * amount of fields so it can be used to schedule NAPI polls without
8690 * registering a full blown interface. This is to be used by drivers
8691 * that need to tie several hardware interfaces to a single NAPI
8692 * poll scheduler due to HW limitations.
8693 */
8694int init_dummy_netdev(struct net_device *dev)
8695{
8696 /* Clear everything. Note we don't initialize spinlocks
8697 * are they aren't supposed to be taken by any of the
8698 * NAPI code and this dummy netdev is supposed to be
8699 * only ever used for NAPI polls
8700 */
8701 memset(dev, 0, sizeof(struct net_device));
8702
8703 /* make sure we BUG if trying to hit standard
8704 * register/unregister code path
8705 */
8706 dev->reg_state = NETREG_DUMMY;
8707
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08008708 /* NAPI wants this */
8709 INIT_LIST_HEAD(&dev->napi_list);
8710
8711 /* a dummy interface is started by default */
8712 set_bit(__LINK_STATE_PRESENT, &dev->state);
8713 set_bit(__LINK_STATE_START, &dev->state);
8714
Eric Dumazet29b44332010-10-11 10:22:12 +00008715 /* Note : We dont allocate pcpu_refcnt for dummy devices,
8716 * because users of this 'device' dont need to change
8717 * its refcount.
8718 */
8719
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08008720 return 0;
8721}
8722EXPORT_SYMBOL_GPL(init_dummy_netdev);
8723
8724
8725/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 * register_netdev - register a network device
8727 * @dev: device to register
8728 *
8729 * Take a completed network device structure and add it to the kernel
8730 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
8731 * chain. 0 is returned on success. A negative errno code is returned
8732 * on a failure to set up the device, or if the name is a duplicate.
8733 *
Borislav Petkov38b4da382007-04-20 22:14:10 -07008734 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735 * and expands the device name if you passed a format string to
8736 * alloc_netdev.
8737 */
8738int register_netdev(struct net_device *dev)
8739{
8740 int err;
8741
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +03008742 if (rtnl_lock_killable())
8743 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 rtnl_unlock();
8746 return err;
8747}
8748EXPORT_SYMBOL(register_netdev);
8749
Eric Dumazet29b44332010-10-11 10:22:12 +00008750int netdev_refcnt_read(const struct net_device *dev)
8751{
8752 int i, refcnt = 0;
8753
8754 for_each_possible_cpu(i)
8755 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
8756 return refcnt;
8757}
8758EXPORT_SYMBOL(netdev_refcnt_read);
8759
Ben Hutchings2c530402012-07-10 10:55:09 +00008760/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00008762 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763 *
8764 * This is called when unregistering network devices.
8765 *
8766 * Any protocol or device that holds a reference should register
8767 * for netdevice notification, and cleanup and put back the
8768 * reference if they receive an UNREGISTER event.
8769 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008770 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 */
8772static void netdev_wait_allrefs(struct net_device *dev)
8773{
8774 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00008775 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776
Eric Dumazete014deb2009-11-17 05:59:21 +00008777 linkwatch_forget_dev(dev);
8778
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00008780 refcnt = netdev_refcnt_read(dev);
8781
8782 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08008784 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785
8786 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07008787 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788
Eric Dumazet748e2d92012-08-22 21:50:59 +00008789 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008790 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00008791 rtnl_lock();
8792
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
8794 &dev->state)) {
8795 /* We must not have linkwatch events
8796 * pending on unregister. If this
8797 * happens, we simply run the queue
8798 * unscheduled, resulting in a noop
8799 * for this device.
8800 */
8801 linkwatch_run_queue();
8802 }
8803
Stephen Hemminger6756ae42006-03-20 22:23:58 -08008804 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805
8806 rebroadcast_time = jiffies;
8807 }
8808
8809 msleep(250);
8810
Eric Dumazet29b44332010-10-11 10:22:12 +00008811 refcnt = netdev_refcnt_read(dev);
8812
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813 if (time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008814 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
8815 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816 warning_time = jiffies;
8817 }
8818 }
8819}
8820
8821/* The sequence is:
8822 *
8823 * rtnl_lock();
8824 * ...
8825 * register_netdevice(x1);
8826 * register_netdevice(x2);
8827 * ...
8828 * unregister_netdevice(y1);
8829 * unregister_netdevice(y2);
8830 * ...
8831 * rtnl_unlock();
8832 * free_netdev(y1);
8833 * free_netdev(y2);
8834 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07008835 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008837 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838 * without deadlocking with linkwatch via keventd.
8839 * 2) Since we run with the RTNL semaphore not held, we can sleep
8840 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07008841 *
8842 * We must not return until all unregister events added during
8843 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845void netdev_run_todo(void)
8846{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008847 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008850 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07008851
8852 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008853
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008854
8855 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00008856 if (!list_empty(&list))
8857 rcu_barrier();
8858
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 while (!list_empty(&list)) {
8860 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00008861 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 list_del(&dev->todo_list);
8863
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008864 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008865 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008867 dump_stack();
8868 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008870
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008871 dev->reg_state = NETREG_UNREGISTERED;
8872
8873 netdev_wait_allrefs(dev);
8874
8875 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00008876 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08008877 BUG_ON(!list_empty(&dev->ptype_all));
8878 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00008879 WARN_ON(rcu_access_pointer(dev->ip_ptr));
8880 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -08008881#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -07008882 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -08008883#endif
David S. Millercf124db2017-05-08 12:52:56 -04008884 if (dev->priv_destructor)
8885 dev->priv_destructor(dev);
8886 if (dev->needs_free_netdev)
8887 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07008888
Eric W. Biederman50624c92013-09-23 21:19:49 -07008889 /* Report a network device has been unregistered */
8890 rtnl_lock();
8891 dev_net(dev)->dev_unreg_count--;
8892 __rtnl_unlock();
8893 wake_up(&netdev_unregistering_wq);
8894
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07008895 /* Free network device */
8896 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898}
8899
Jarod Wilson92566452016-02-01 18:51:04 -05008900/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
8901 * all the same fields in the same order as net_device_stats, with only
8902 * the type differing, but rtnl_link_stats64 may have additional fields
8903 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00008904 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00008905void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
8906 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00008907{
8908#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05008909 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02008910 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05008911 /* zero out counters that only exist in rtnl_link_stats64 */
8912 memset((char *)stats64 + sizeof(*netdev_stats), 0,
8913 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008914#else
Jarod Wilson92566452016-02-01 18:51:04 -05008915 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00008916 const unsigned long *src = (const unsigned long *)netdev_stats;
8917 u64 *dst = (u64 *)stats64;
8918
Jarod Wilson92566452016-02-01 18:51:04 -05008919 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008920 for (i = 0; i < n; i++)
8921 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05008922 /* zero out counters that only exist in rtnl_link_stats64 */
8923 memset((char *)stats64 + n * sizeof(u64), 0,
8924 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008925#endif
8926}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00008927EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00008928
Eric Dumazetd83345a2009-11-16 03:36:51 +00008929/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008930 * dev_get_stats - get network device statistics
8931 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07008932 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008933 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00008934 * Get network statistics from device. Return @storage.
8935 * The device driver may provide its own method by setting
8936 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
8937 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008938 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00008939struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
8940 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00008941{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008942 const struct net_device_ops *ops = dev->netdev_ops;
8943
Eric Dumazet28172732010-07-07 14:58:56 -07008944 if (ops->ndo_get_stats64) {
8945 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00008946 ops->ndo_get_stats64(dev, storage);
8947 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00008948 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00008949 } else {
8950 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07008951 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07008952 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
8953 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
8954 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07008955 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07008956}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008957EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07008958
Eric Dumazet24824a02010-10-02 06:11:55 +00008959struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07008960{
Eric Dumazet24824a02010-10-02 06:11:55 +00008961 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07008962
Eric Dumazet24824a02010-10-02 06:11:55 +00008963#ifdef CONFIG_NET_CLS_ACT
8964 if (queue)
8965 return queue;
8966 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
8967 if (!queue)
8968 return NULL;
8969 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008970 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00008971 queue->qdisc_sleeping = &noop_qdisc;
8972 rcu_assign_pointer(dev->ingress_queue, queue);
8973#endif
8974 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07008975}
8976
Eric Dumazet2c60db02012-09-16 09:17:26 +00008977static const struct ethtool_ops default_ethtool_ops;
8978
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00008979void netdev_set_default_ethtool_ops(struct net_device *dev,
8980 const struct ethtool_ops *ops)
8981{
8982 if (dev->ethtool_ops == &default_ethtool_ops)
8983 dev->ethtool_ops = ops;
8984}
8985EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
8986
Eric Dumazet74d332c2013-10-30 13:10:44 -07008987void netdev_freemem(struct net_device *dev)
8988{
8989 char *addr = (char *)dev - dev->padded;
8990
WANG Cong4cb28972014-06-02 15:55:22 -07008991 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07008992}
8993
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994/**
tcharding722c9a02017-02-09 17:56:04 +11008995 * alloc_netdev_mqs - allocate network device
8996 * @sizeof_priv: size of private data to allocate space for
8997 * @name: device name format string
8998 * @name_assign_type: origin of device name
8999 * @setup: callback to initialize device
9000 * @txqs: the number of TX subqueues to allocate
9001 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002 *
tcharding722c9a02017-02-09 17:56:04 +11009003 * Allocates a struct net_device with private data area for driver use
9004 * and performs basic initialization. Also allocates subqueue structs
9005 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006 */
Tom Herbert36909ea2011-01-09 19:36:31 +00009007struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02009008 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00009009 void (*setup)(struct net_device *),
9010 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03009013 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009014 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07009016 BUG_ON(strlen(name) >= sizeof(dev->name));
9017
Tom Herbert36909ea2011-01-09 19:36:31 +00009018 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009019 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00009020 return NULL;
9021 }
9022
Tom Herbert36909ea2011-01-09 19:36:31 +00009023 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009024 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00009025 return NULL;
9026 }
Tom Herbert36909ea2011-01-09 19:36:31 +00009027
David S. Millerfd2ea0a2008-07-17 01:56:23 -07009028 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009029 if (sizeof_priv) {
9030 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009031 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07009032 alloc_size += sizeof_priv;
9033 }
9034 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009035 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036
Michal Hockodcda9b02017-07-12 14:36:45 -07009037 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00009038 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009039 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00009041 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009043
Eric Dumazet29b44332010-10-11 10:22:12 +00009044 dev->pcpu_refcnt = alloc_percpu(int);
9045 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07009046 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009047
Linus Torvalds1da177e2005-04-16 15:20:36 -07009048 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00009049 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
Jiri Pirko22bedad32010-04-01 21:22:57 +00009051 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009052 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00009053
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09009054 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07009056 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00009057 dev->gso_max_segs = GSO_MAX_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058
Herbert Xud565b0a2008-12-15 23:38:52 -08009059 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009060 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07009061 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00009062 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02009063 INIT_LIST_HEAD(&dev->adj_list.upper);
9064 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08009065 INIT_LIST_HEAD(&dev->ptype_all);
9066 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02009067#ifdef CONFIG_NET_SCHED
9068 hash_init(dev->qdisc_hash);
9069#endif
Eric Dumazet02875872014-10-05 18:38:35 -07009070 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009072
Phil Suttera8131042016-02-17 15:37:43 +01009073 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02009074 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01009075 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01009076 }
Phil Sutter906470c2015-08-18 10:30:48 +02009077
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009078 dev->num_tx_queues = txqs;
9079 dev->real_num_tx_queues = txqs;
9080 if (netif_alloc_netdev_queues(dev))
9081 goto free_all;
9082
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009083 dev->num_rx_queues = rxqs;
9084 dev->real_num_rx_queues = rxqs;
9085 if (netif_alloc_rx_queues(dev))
9086 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009087
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02009089 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00009090 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00009091 if (!dev->ethtool_ops)
9092 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02009093
9094 nf_hook_ingress_init(dev);
9095
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009097
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009098free_all:
9099 free_netdev(dev);
9100 return NULL;
9101
Eric Dumazet29b44332010-10-11 10:22:12 +00009102free_pcpu:
9103 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009104free_dev:
9105 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107}
Tom Herbert36909ea2011-01-09 19:36:31 +00009108EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109
9110/**
tcharding722c9a02017-02-09 17:56:04 +11009111 * free_netdev - free network device
9112 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113 *
tcharding722c9a02017-02-09 17:56:04 +11009114 * This function does the last stage of destroying an allocated device
9115 * interface. The reference to the device object is released. If this
9116 * is the last reference then it will be freed.Must be called in process
9117 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 */
9119void free_netdev(struct net_device *dev)
9120{
Herbert Xud565b0a2008-12-15 23:38:52 -08009121 struct napi_struct *p, *n;
9122
Eric Dumazet93d05d42015-11-18 06:31:03 -08009123 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07009124 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009125 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -07009126
Eric Dumazet33d480c2011-08-11 19:30:52 +00009127 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00009128
Jiri Pirkof001fde2009-05-05 02:48:28 +00009129 /* Flush device addresses */
9130 dev_addr_flush(dev);
9131
Herbert Xud565b0a2008-12-15 23:38:52 -08009132 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
9133 netif_napi_del(p);
9134
Eric Dumazet29b44332010-10-11 10:22:12 +00009135 free_percpu(dev->pcpu_refcnt);
9136 dev->pcpu_refcnt = NULL;
9137
Stephen Hemminger3041a062006-05-26 13:25:24 -07009138 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07009140 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141 return;
9142 }
9143
9144 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
9145 dev->reg_state = NETREG_RELEASED;
9146
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07009147 /* will free via device release */
9148 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009150EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009151
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009152/**
9153 * synchronize_net - Synchronize with packet receive processing
9154 *
9155 * Wait for packets currently being received to be done.
9156 * Does not block later packets from starting.
9157 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009158void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159{
9160 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00009161 if (rtnl_is_locked())
9162 synchronize_rcu_expedited();
9163 else
9164 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009166EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
9168/**
Eric Dumazet44a08732009-10-27 07:03:04 +00009169 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00009171 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08009172 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009174 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00009175 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176 *
9177 * Callers must hold the rtnl semaphore. You may want
9178 * unregister_netdev() instead of this.
9179 */
9180
Eric Dumazet44a08732009-10-27 07:03:04 +00009181void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182{
Herbert Xua6620712007-12-12 19:21:56 -08009183 ASSERT_RTNL();
9184
Eric Dumazet44a08732009-10-27 07:03:04 +00009185 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009186 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00009187 } else {
9188 rollback_registered(dev);
9189 /* Finish processing unregister after unlock */
9190 net_set_todo(dev);
9191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192}
Eric Dumazet44a08732009-10-27 07:03:04 +00009193EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009194
9195/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009196 * unregister_netdevice_many - unregister many devices
9197 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07009198 *
9199 * Note: As most callers use a stack allocated list_head,
9200 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009201 */
9202void unregister_netdevice_many(struct list_head *head)
9203{
9204 struct net_device *dev;
9205
9206 if (!list_empty(head)) {
9207 rollback_registered_many(head);
9208 list_for_each_entry(dev, head, unreg_list)
9209 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07009210 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009211 }
9212}
Eric Dumazet63c80992009-10-27 07:06:49 +00009213EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009214
9215/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 * unregister_netdev - remove device from the kernel
9217 * @dev: device
9218 *
9219 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009220 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221 *
9222 * This is just a wrapper for unregister_netdevice that takes
9223 * the rtnl semaphore. In general you want to use this and not
9224 * unregister_netdevice.
9225 */
9226void unregister_netdev(struct net_device *dev)
9227{
9228 rtnl_lock();
9229 unregister_netdevice(dev);
9230 rtnl_unlock();
9231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232EXPORT_SYMBOL(unregister_netdev);
9233
Eric W. Biedermance286d32007-09-12 13:53:49 +02009234/**
9235 * dev_change_net_namespace - move device to different nethost namespace
9236 * @dev: device
9237 * @net: network namespace
9238 * @pat: If not NULL name pattern to try if the current device name
9239 * is already taken in the destination network namespace.
9240 *
9241 * This function shuts down a device interface and moves it
9242 * to a new network namespace. On success 0 is returned, on
9243 * a failure a netagive errno code is returned.
9244 *
9245 * Callers must hold the rtnl semaphore.
9246 */
9247
9248int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
9249{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009250 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009251
9252 ASSERT_RTNL();
9253
9254 /* Don't allow namespace local devices to be moved. */
9255 err = -EINVAL;
9256 if (dev->features & NETIF_F_NETNS_LOCAL)
9257 goto out;
9258
9259 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009260 if (dev->reg_state != NETREG_REGISTERED)
9261 goto out;
9262
9263 /* Get out if there is nothing todo */
9264 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09009265 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02009266 goto out;
9267
9268 /* Pick the destination device name, and ensure
9269 * we can use it in the destination network namespace.
9270 */
9271 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00009272 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009273 /* We get here if we can't use the current device name */
9274 if (!pat)
9275 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +08009276 err = dev_get_valid_name(net, dev, pat);
9277 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02009278 goto out;
9279 }
9280
9281 /*
9282 * And now a mini version of register_netdevice unregister_netdevice.
9283 */
9284
9285 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07009286 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009287
9288 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009289 unlist_netdevice(dev);
9290
9291 synchronize_net();
9292
9293 /* Shutdown queueing discipline. */
9294 dev_shutdown(dev);
9295
9296 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11009297 * this device. They should clean all the things.
9298 *
9299 * Note that dev->reg_state stays at NETREG_REGISTERED.
9300 * This is wanted because this way 8021q and macvlan know
9301 * the device is just moving and can keep their slaves up.
9302 */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009303 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +00009304 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009305
Nicolas Dichtelc36ac8e2018-01-25 15:01:38 +01009306 new_nsid = peernet2id_alloc(dev_net(dev), net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009307 /* If there is an ifindex conflict assign a new one */
9308 if (__dev_get_by_index(net, dev->ifindex))
9309 new_ifindex = dev_new_index(net);
9310 else
9311 new_ifindex = dev->ifindex;
9312
9313 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
9314 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009315
9316 /*
9317 * Flush the unicast and multicast chains
9318 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009319 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00009320 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009321
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009322 /* Send a netdev-removed uevent to the old namespace */
9323 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04009324 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009325
Eric W. Biedermance286d32007-09-12 13:53:49 +02009326 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09009327 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009328 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009329
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009330 /* Send a netdev-add uevent to the new namespace */
9331 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04009332 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009333
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009334 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07009335 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009336 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009337
9338 /* Add the device back in the hashes */
9339 list_netdevice(dev);
9340
9341 /* Notify protocols, that a new device appeared. */
9342 call_netdevice_notifiers(NETDEV_REGISTER, dev);
9343
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009344 /*
9345 * Prevent userspace races by waiting until the network
9346 * device is fully setup before sending notifications.
9347 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -07009348 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009349
Eric W. Biedermance286d32007-09-12 13:53:49 +02009350 synchronize_net();
9351 err = 0;
9352out:
9353 return err;
9354}
Johannes Berg463d0182009-07-14 00:33:35 +02009355EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009356
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009357static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358{
9359 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009361 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +05309362 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 local_irq_disable();
9365 cpu = smp_processor_id();
9366 sd = &per_cpu(softnet_data, cpu);
9367 oldsd = &per_cpu(softnet_data, oldcpu);
9368
9369 /* Find end of our completion_queue. */
9370 list_skb = &sd->completion_queue;
9371 while (*list_skb)
9372 list_skb = &(*list_skb)->next;
9373 /* Append completion queue from offline CPU. */
9374 *list_skb = oldsd->completion_queue;
9375 oldsd->completion_queue = NULL;
9376
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00009378 if (oldsd->output_queue) {
9379 *sd->output_queue_tailp = oldsd->output_queue;
9380 sd->output_queue_tailp = oldsd->output_queue_tailp;
9381 oldsd->output_queue = NULL;
9382 oldsd->output_queue_tailp = &oldsd->output_queue;
9383 }
Eric Dumazetac64da02015-01-15 17:04:22 -08009384 /* Append NAPI poll list from offline CPU, with one exception :
9385 * process_backlog() must be called by cpu owning percpu backlog.
9386 * We properly handle process_queue & input_pkt_queue later.
9387 */
9388 while (!list_empty(&oldsd->poll_list)) {
9389 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
9390 struct napi_struct,
9391 poll_list);
9392
9393 list_del_init(&napi->poll_list);
9394 if (napi->poll == process_backlog)
9395 napi->state = 0;
9396 else
9397 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +00009398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399
9400 raise_softirq_irqoff(NET_TX_SOFTIRQ);
9401 local_irq_enable();
9402
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05309403#ifdef CONFIG_RPS
9404 remsd = oldsd->rps_ipi_list;
9405 oldsd->rps_ipi_list = NULL;
9406#endif
9407 /* send out pending IPI's on offline CPU */
9408 net_rps_send_ipi(remsd);
9409
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00009411 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08009412 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00009413 input_queue_head_incr(oldsd);
9414 }
Eric Dumazetac64da02015-01-15 17:04:22 -08009415 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08009416 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00009417 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07009418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422
Herbert Xu7f353bf2007-08-10 15:47:58 -07009423/**
Herbert Xub63365a2008-10-23 01:11:29 -07009424 * netdev_increment_features - increment feature set by one
9425 * @all: current feature set
9426 * @one: new feature set
9427 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07009428 *
9429 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07009430 * @one to the master device with current feature set @all. Will not
9431 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07009432 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009433netdev_features_t netdev_increment_features(netdev_features_t all,
9434 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07009435{
Tom Herbertc8cd0982015-12-14 11:19:44 -08009436 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -08009437 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +00009438 mask |= NETIF_F_VLAN_CHALLENGED;
9439
Tom Herberta1882222015-12-14 11:19:43 -08009440 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +00009441 all &= one | ~NETIF_F_ALL_FOR_ALL;
9442
Michał Mirosław1742f182011-04-22 06:31:16 +00009443 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -08009444 if (all & NETIF_F_HW_CSUM)
9445 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -07009446
9447 return all;
9448}
Herbert Xub63365a2008-10-23 01:11:29 -07009449EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07009450
Baruch Siach430f03c2013-06-02 20:43:55 +00009451static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009452{
9453 int i;
9454 struct hlist_head *hash;
9455
Kees Cook6da2ec52018-06-12 13:55:00 -07009456 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009457 if (hash != NULL)
9458 for (i = 0; i < NETDEV_HASHENTRIES; i++)
9459 INIT_HLIST_HEAD(&hash[i]);
9460
9461 return hash;
9462}
9463
Eric W. Biederman881d9662007-09-17 11:56:21 -07009464/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07009465static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07009466{
Li RongQingd9f37d02018-07-13 14:41:36 +08009467 BUILD_BUG_ON(GRO_HASH_BUCKETS >
David S. Millerccdb5172018-07-16 17:02:04 -07009468 8 * FIELD_SIZEOF(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +08009469
Rustad, Mark D734b6542012-07-18 09:06:07 +00009470 if (net != &init_net)
9471 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07009472
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009473 net->dev_name_head = netdev_create_hash();
9474 if (net->dev_name_head == NULL)
9475 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009476
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009477 net->dev_index_head = netdev_create_hash();
9478 if (net->dev_index_head == NULL)
9479 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009480
9481 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009482
9483err_idx:
9484 kfree(net->dev_name_head);
9485err_name:
9486 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009487}
9488
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009489/**
9490 * netdev_drivername - network driver for the device
9491 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009492 *
9493 * Determine network driver for device.
9494 */
David S. Miller3019de12011-06-06 16:41:33 -07009495const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -07009496{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07009497 const struct device_driver *driver;
9498 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -07009499 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -07009500
9501 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07009502 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -07009503 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07009504
9505 driver = parent->driver;
9506 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -07009507 return driver->name;
9508 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07009509}
9510
Joe Perches6ea754e2014-09-22 11:10:50 -07009511static void __netdev_printk(const char *level, const struct net_device *dev,
9512 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +00009513{
Joe Perchesb004ff42012-09-12 20:12:19 -07009514 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -07009515 dev_printk_emit(level[1] - '0',
9516 dev->dev.parent,
9517 "%s %s %s%s: %pV",
9518 dev_driver_string(dev->dev.parent),
9519 dev_name(dev->dev.parent),
9520 netdev_name(dev), netdev_reg_state(dev),
9521 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009522 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -07009523 printk("%s%s%s: %pV",
9524 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009525 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -07009526 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009527 }
Joe Perches256df2f2010-06-27 01:02:35 +00009528}
9529
Joe Perches6ea754e2014-09-22 11:10:50 -07009530void netdev_printk(const char *level, const struct net_device *dev,
9531 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +00009532{
9533 struct va_format vaf;
9534 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +00009535
9536 va_start(args, format);
9537
9538 vaf.fmt = format;
9539 vaf.va = &args;
9540
Joe Perches6ea754e2014-09-22 11:10:50 -07009541 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009542
Joe Perches256df2f2010-06-27 01:02:35 +00009543 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +00009544}
9545EXPORT_SYMBOL(netdev_printk);
9546
9547#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -07009548void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +00009549{ \
Joe Perches256df2f2010-06-27 01:02:35 +00009550 struct va_format vaf; \
9551 va_list args; \
9552 \
9553 va_start(args, fmt); \
9554 \
9555 vaf.fmt = fmt; \
9556 vaf.va = &args; \
9557 \
Joe Perches6ea754e2014-09-22 11:10:50 -07009558 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -07009559 \
Joe Perches256df2f2010-06-27 01:02:35 +00009560 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +00009561} \
9562EXPORT_SYMBOL(func);
9563
9564define_netdev_printk_level(netdev_emerg, KERN_EMERG);
9565define_netdev_printk_level(netdev_alert, KERN_ALERT);
9566define_netdev_printk_level(netdev_crit, KERN_CRIT);
9567define_netdev_printk_level(netdev_err, KERN_ERR);
9568define_netdev_printk_level(netdev_warn, KERN_WARNING);
9569define_netdev_printk_level(netdev_notice, KERN_NOTICE);
9570define_netdev_printk_level(netdev_info, KERN_INFO);
9571
Pavel Emelyanov46650792007-10-08 20:38:39 -07009572static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07009573{
9574 kfree(net->dev_name_head);
9575 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +03009576 if (net != &init_net)
9577 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -07009578}
9579
Denis V. Lunev022cbae2007-11-13 03:23:50 -08009580static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07009581 .init = netdev_init,
9582 .exit = netdev_exit,
9583};
9584
Pavel Emelyanov46650792007-10-08 20:38:39 -07009585static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02009586{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009587 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009588 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009589 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02009590 * initial network namespace
9591 */
9592 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009593 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009594 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07009595 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02009596
9597 /* Ignore unmoveable devices (i.e. loopback) */
9598 if (dev->features & NETIF_F_NETNS_LOCAL)
9599 continue;
9600
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009601 /* Leave virtual devices for the generic cleanup */
9602 if (dev->rtnl_link_ops)
9603 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08009604
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009605 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07009606 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
9607 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009608 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009609 pr_emerg("%s: failed to move %s to init_net: %d\n",
9610 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07009611 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02009612 }
9613 }
9614 rtnl_unlock();
9615}
9616
Eric W. Biederman50624c92013-09-23 21:19:49 -07009617static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
9618{
9619 /* Return with the rtnl_lock held when there are no network
9620 * devices unregistering in any network namespace in net_list.
9621 */
9622 struct net *net;
9623 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +01009624 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009625
Peter Zijlstraff960a72014-10-29 17:04:56 +01009626 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009627 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -07009628 unregistering = false;
9629 rtnl_lock();
9630 list_for_each_entry(net, net_list, exit_list) {
9631 if (net->dev_unreg_count > 0) {
9632 unregistering = true;
9633 break;
9634 }
9635 }
9636 if (!unregistering)
9637 break;
9638 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +01009639
9640 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009641 }
Peter Zijlstraff960a72014-10-29 17:04:56 +01009642 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009643}
9644
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009645static void __net_exit default_device_exit_batch(struct list_head *net_list)
9646{
9647 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04009648 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009649 * Do this across as many network namespaces as possible to
9650 * improve batching efficiency.
9651 */
9652 struct net_device *dev;
9653 struct net *net;
9654 LIST_HEAD(dev_kill_list);
9655
Eric W. Biederman50624c92013-09-23 21:19:49 -07009656 /* To prevent network device cleanup code from dereferencing
9657 * loopback devices or network devices that have been freed
9658 * wait here for all pending unregistrations to complete,
9659 * before unregistring the loopback device and allowing the
9660 * network namespace be freed.
9661 *
9662 * The netdev todo list containing all network devices
9663 * unregistrations that happen in default_device_exit_batch
9664 * will run in the rtnl_unlock() at the end of
9665 * default_device_exit_batch.
9666 */
9667 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009668 list_for_each_entry(net, net_list, exit_list) {
9669 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02009670 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009671 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
9672 else
9673 unregister_netdevice_queue(dev, &dev_kill_list);
9674 }
9675 }
9676 unregister_netdevice_many(&dev_kill_list);
9677 rtnl_unlock();
9678}
9679
Denis V. Lunev022cbae2007-11-13 03:23:50 -08009680static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009681 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009682 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02009683};
9684
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685/*
9686 * Initialize the DEV module. At boot time this walks the device list and
9687 * unhooks any devices that fail to initialise (normally hardware not
9688 * present) and leaves us with a valid list of present and active devices.
9689 *
9690 */
9691
9692/*
9693 * This is called single threaded during boot, so no need
9694 * to take the rtnl semaphore.
9695 */
9696static int __init net_dev_init(void)
9697{
9698 int i, rc = -ENOMEM;
9699
9700 BUG_ON(!dev_boot_phase);
9701
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 if (dev_proc_init())
9703 goto out;
9704
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009705 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07009706 goto out;
9707
9708 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08009709 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710 INIT_LIST_HEAD(&ptype_base[i]);
9711
Vlad Yasevich62532da2012-11-15 08:49:10 +00009712 INIT_LIST_HEAD(&offload_base);
9713
Eric W. Biederman881d9662007-09-17 11:56:21 -07009714 if (register_pernet_subsys(&netdev_net_ops))
9715 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716
9717 /*
9718 * Initialise the packet receive queues.
9719 */
9720
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07009721 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -07009722 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009723 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724
Eric Dumazet41852492016-08-26 12:50:39 -07009725 INIT_WORK(flush, flush_backlog);
9726
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009727 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07009728 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +01009729#ifdef CONFIG_XFRM_OFFLOAD
9730 skb_queue_head_init(&sd->xfrm_backlog);
9731#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009732 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00009733 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00009734#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009735 sd->csd.func = rps_trigger_softirq;
9736 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009737 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07009738#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00009739
David S. Miller7c4ec742018-07-20 23:37:55 -07009740 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009741 sd->backlog.poll = process_backlog;
9742 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743 }
9744
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 dev_boot_phase = 0;
9746
Eric W. Biederman505d4f72008-11-07 22:54:20 -08009747 /* The loopback device is special if any other network devices
9748 * is present in a network namespace the loopback device must
9749 * be present. Since we now dynamically allocate and free the
9750 * loopback device ensure this invariant is maintained by
9751 * keeping the loopback device as the first device on the
9752 * list of network devices. Ensuring the loopback devices
9753 * is the first device that appears and the last network device
9754 * that disappears.
9755 */
9756 if (register_pernet_device(&loopback_net_ops))
9757 goto out;
9758
9759 if (register_pernet_device(&default_device_ops))
9760 goto out;
9761
Carlos R. Mafra962cf362008-05-15 11:15:37 -03009762 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
9763 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009765 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
9766 NULL, dev_cpu_dead);
9767 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 rc = 0;
9769out:
9770 return rc;
9771}
9772
9773subsys_initcall(net_dev_init);