blob: bf7e0a471186bad15db01b7418bce1c19941bcfc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700149#include "net-sysfs.h"
150
Herbert Xud565b0a2008-12-15 23:38:52 -0800151#define MAX_GRO_SKBS 8
152
Herbert Xu5d38a072009-01-04 16:13:40 -0800153/* This should be increased if a protocol with a bigger head is added. */
154#define GRO_MAX_HEAD (MAX_HEADER + 128)
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000157static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000158struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
159struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000160static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000162static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700163static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700164 struct netdev_notifier_info *info);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200165static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700168 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * semaphore.
170 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800171 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700174 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * actual updates. This allows pure readers to access the list even
176 * while a writer is preparing to update it.
177 *
178 * To put it another way, dev_base_lock is held for writing only to
179 * protect against pure readers; the rtnl semaphore provides the
180 * protection against other writers.
181 *
182 * See, for example usages, register_netdevice() and
183 * unregister_netdevice(), which must be called with the rtnl
184 * semaphore held.
185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187EXPORT_SYMBOL(dev_base_lock);
188
Florian Westphal6c557002017-10-02 23:50:05 +0200189static DEFINE_MUTEX(ifalias_mutex);
190
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300191/* protects napi_hash addition/deletion and napi_gen_id */
192static DEFINE_SPINLOCK(napi_hash_lock);
193
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800194static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800195static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300196
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200197static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000198
Thomas Graf4e985ad2011-06-21 03:11:20 +0000199static inline void dev_base_seq_inc(struct net *net)
200{
tcharding643aa9c2017-02-09 17:56:05 +1100201 while (++net->dev_base_seq == 0)
202 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000203}
204
Eric W. Biederman881d9662007-09-17 11:56:21 -0700205static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700207 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000208
stephen hemminger08e98972009-11-10 07:20:34 +0000209 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Eric W. Biederman881d9662007-09-17 11:56:21 -0700212static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700214 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000217static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000218{
219#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000220 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000221#endif
222}
223
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000224static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000225{
226#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000227 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000228#endif
229}
230
Eric W. Biedermance286d32007-09-12 13:53:49 +0200231/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000232static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200233{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900234 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200235
236 ASSERT_RTNL();
237
238 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800239 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000240 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000241 hlist_add_head_rcu(&dev->index_hlist,
242 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200243 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000244
245 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200246}
247
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000248/* Device list removal
249 * caller must respect a RCU grace period before freeing/reusing dev
250 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200251static void unlist_netdevice(struct net_device *dev)
252{
253 ASSERT_RTNL();
254
255 /* Unlink dev from the device chain */
256 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800257 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000258 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000259 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200260 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000261
262 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
266 * Our notifier list
267 */
268
Alan Sternf07d5b92006-05-09 15:23:03 -0700269static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/*
272 * Device drivers call our routines to queue packets here. We empty the
273 * queue in the local softnet handler.
274 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700275
Eric Dumazet9958da02010-04-17 04:17:02 +0000276DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700277EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David S. Millercf508b12008-07-22 14:16:42 -0700279#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700280/*
David S. Millerc773e842008-07-08 23:13:53 -0700281 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700282 * according to dev->type
283 */
tcharding643aa9c2017-02-09 17:56:05 +1100284static const unsigned short netdev_lock_type[] = {
285 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700286 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
287 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
288 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
289 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
290 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
291 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
292 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
293 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
294 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
295 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
296 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
Paul Gortmaker211ed862012-05-10 17:14:35 -0400297 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
298 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
299 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700300
tcharding643aa9c2017-02-09 17:56:05 +1100301static const char *const netdev_lock_name[] = {
302 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
303 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
304 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
305 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
306 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
307 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
308 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
309 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
310 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
311 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
312 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
313 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
314 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
315 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
316 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700317
318static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700319static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700320
321static inline unsigned short netdev_lock_pos(unsigned short dev_type)
322{
323 int i;
324
325 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
326 if (netdev_lock_type[i] == dev_type)
327 return i;
328 /* the last key is used by default */
329 return ARRAY_SIZE(netdev_lock_type) - 1;
330}
331
David S. Millercf508b12008-07-22 14:16:42 -0700332static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
333 unsigned short dev_type)
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700334{
335 int i;
336
337 i = netdev_lock_pos(dev_type);
338 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
339 netdev_lock_name[i]);
340}
David S. Millercf508b12008-07-22 14:16:42 -0700341
342static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
343{
344 int i;
345
346 i = netdev_lock_pos(dev->type);
347 lockdep_set_class_and_name(&dev->addr_list_lock,
348 &netdev_addr_lock_key[i],
349 netdev_lock_name[i]);
350}
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700351#else
David S. Millercf508b12008-07-22 14:16:42 -0700352static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
353 unsigned short dev_type)
354{
355}
356static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b72007-05-15 22:46:18 -0700357{
358}
359#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100362 *
363 * Protocol management and registration routines
364 *
365 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * Add a protocol ID to the list. Now that the input handler is
370 * smarter we can dispense with all the messy stuff that used to be
371 * here.
372 *
373 * BEWARE!!! Protocol handlers, mangling input packets,
374 * MUST BE last in hash buckets and checking protocol handlers
375 * MUST start from promiscuous ptype_all chain in net_bh.
376 * It is true now, do not change it.
377 * Explanation follows: if protocol handler, mangling packet, will
378 * be the first on list, it is not able to sense, that packet
379 * is cloned and should be copied-on-write, so that it will
380 * change it and subsequent readers will get broken packet.
381 * --ANK (980803)
382 */
383
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000384static inline struct list_head *ptype_head(const struct packet_type *pt)
385{
386 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800387 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000388 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800389 return pt->dev ? &pt->dev->ptype_specific :
390 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
394 * dev_add_pack - add packet handler
395 * @pt: packet type declaration
396 *
397 * Add a protocol handler to the networking stack. The passed &packet_type
398 * is linked into kernel lists and may not be freed until it has been
399 * removed from the kernel lists.
400 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900401 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 * guarantee all CPU's that are in middle of receiving packets
403 * will see the new packet type (until the next received packet).
404 */
405
406void dev_add_pack(struct packet_type *pt)
407{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000408 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000410 spin_lock(&ptype_lock);
411 list_add_rcu(&pt->list, head);
412 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700414EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
417 * __dev_remove_pack - remove packet handler
418 * @pt: packet type declaration
419 *
420 * Remove a protocol handler that was previously added to the kernel
421 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
422 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900423 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *
425 * The packet type might still be in use by receivers
426 * and must not be freed until after all the CPU's have gone
427 * through a quiescent state.
428 */
429void __dev_remove_pack(struct packet_type *pt)
430{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000431 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct packet_type *pt1;
433
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000434 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 list_for_each_entry(pt1, head, list) {
437 if (pt == pt1) {
438 list_del_rcu(&pt->list);
439 goto out;
440 }
441 }
442
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000443 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000445 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700447EXPORT_SYMBOL(__dev_remove_pack);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/**
450 * dev_remove_pack - remove packet handler
451 * @pt: packet type declaration
452 *
453 * Remove a protocol handler that was previously added to the kernel
454 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
455 * from the kernel lists and can be freed or reused once this function
456 * returns.
457 *
458 * This call sleeps to guarantee that no CPU is looking at the packet
459 * type after return.
460 */
461void dev_remove_pack(struct packet_type *pt)
462{
463 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 synchronize_net();
466}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700467EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Vlad Yasevich62532da2012-11-15 08:49:10 +0000469
470/**
471 * dev_add_offload - register offload handlers
472 * @po: protocol offload declaration
473 *
474 * Add protocol offload handlers to the networking stack. The passed
475 * &proto_offload is linked into kernel lists and may not be freed until
476 * it has been removed from the kernel lists.
477 *
478 * This call does not sleep therefore it can not
479 * guarantee all CPU's that are in middle of receiving packets
480 * will see the new offload handlers (until the next received packet).
481 */
482void dev_add_offload(struct packet_offload *po)
483{
David S. Millerbdef7de2015-06-01 14:56:09 -0700484 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000485
486 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700487 list_for_each_entry(elem, &offload_base, list) {
488 if (po->priority < elem->priority)
489 break;
490 }
491 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000492 spin_unlock(&offload_lock);
493}
494EXPORT_SYMBOL(dev_add_offload);
495
496/**
497 * __dev_remove_offload - remove offload handler
498 * @po: packet offload declaration
499 *
500 * Remove a protocol offload handler that was previously added to the
501 * kernel offload handlers by dev_add_offload(). The passed &offload_type
502 * is removed from the kernel lists and can be freed or reused once this
503 * function returns.
504 *
505 * The packet type might still be in use by receivers
506 * and must not be freed until after all the CPU's have gone
507 * through a quiescent state.
508 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800509static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000510{
511 struct list_head *head = &offload_base;
512 struct packet_offload *po1;
513
Eric Dumazetc53aa502012-11-16 08:08:23 +0000514 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000515
516 list_for_each_entry(po1, head, list) {
517 if (po == po1) {
518 list_del_rcu(&po->list);
519 goto out;
520 }
521 }
522
523 pr_warn("dev_remove_offload: %p not found\n", po);
524out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000525 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000526}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000527
528/**
529 * dev_remove_offload - remove packet offload handler
530 * @po: packet offload declaration
531 *
532 * Remove a packet offload handler that was previously added to the kernel
533 * offload handlers by dev_add_offload(). The passed &offload_type is
534 * removed from the kernel lists and can be freed or reused once this
535 * function returns.
536 *
537 * This call sleeps to guarantee that no CPU is looking at the packet
538 * type after return.
539 */
540void dev_remove_offload(struct packet_offload *po)
541{
542 __dev_remove_offload(po);
543
544 synchronize_net();
545}
546EXPORT_SYMBOL(dev_remove_offload);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100549 *
550 * Device Boot-time Settings Routines
551 *
552 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554/* Boot time configuration table */
555static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
556
557/**
558 * netdev_boot_setup_add - add new setup entry
559 * @name: name of the device
560 * @map: configured settings for the device
561 *
562 * Adds new setup entry to the dev_boot_setup list. The function
563 * returns 0 on error and 1 on success. This is a generic routine to
564 * all netdevices.
565 */
566static int netdev_boot_setup_add(char *name, struct ifmap *map)
567{
568 struct netdev_boot_setup *s;
569 int i;
570
571 s = dev_boot_setup;
572 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
573 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
574 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff92008-07-01 19:57:19 -0700575 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 memcpy(&s[i].map, map, sizeof(s[i].map));
577 break;
578 }
579 }
580
581 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
582}
583
584/**
tcharding722c9a02017-02-09 17:56:04 +1100585 * netdev_boot_setup_check - check boot time settings
586 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 *
tcharding722c9a02017-02-09 17:56:04 +1100588 * Check boot time settings for the device.
589 * The found settings are set for the device to be used
590 * later in the device probing.
591 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
593int netdev_boot_setup_check(struct net_device *dev)
594{
595 struct netdev_boot_setup *s = dev_boot_setup;
596 int i;
597
598 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
599 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff92008-07-01 19:57:19 -0700600 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100601 dev->irq = s[i].map.irq;
602 dev->base_addr = s[i].map.base_addr;
603 dev->mem_start = s[i].map.mem_start;
604 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 1;
606 }
607 }
608 return 0;
609}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700610EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612
613/**
tcharding722c9a02017-02-09 17:56:04 +1100614 * netdev_boot_base - get address from boot time settings
615 * @prefix: prefix for network device
616 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
tcharding722c9a02017-02-09 17:56:04 +1100618 * Check boot time settings for the base address of device.
619 * The found settings are set for the device to be used
620 * later in the device probing.
621 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623unsigned long netdev_boot_base(const char *prefix, int unit)
624{
625 const struct netdev_boot_setup *s = dev_boot_setup;
626 char name[IFNAMSIZ];
627 int i;
628
629 sprintf(name, "%s%d", prefix, unit);
630
631 /*
632 * If device already registered then return base of 1
633 * to indicate not to probe for this interface
634 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700635 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 1;
637
638 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
639 if (!strcmp(name, s[i].name))
640 return s[i].map.base_addr;
641 return 0;
642}
643
644/*
645 * Saves at boot time configured settings for any netdevice.
646 */
647int __init netdev_boot_setup(char *str)
648{
649 int ints[5];
650 struct ifmap map;
651
652 str = get_options(str, ARRAY_SIZE(ints), ints);
653 if (!str || !*str)
654 return 0;
655
656 /* Save settings */
657 memset(&map, 0, sizeof(map));
658 if (ints[0] > 0)
659 map.irq = ints[1];
660 if (ints[0] > 1)
661 map.base_addr = ints[2];
662 if (ints[0] > 2)
663 map.mem_start = ints[3];
664 if (ints[0] > 3)
665 map.mem_end = ints[4];
666
667 /* Add new entry to the list */
668 return netdev_boot_setup_add(str, &map);
669}
670
671__setup("netdev=", netdev_boot_setup);
672
673/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100674 *
675 * Device Interface Subroutines
676 *
677 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200680 * dev_get_iflink - get 'iflink' value of a interface
681 * @dev: targeted interface
682 *
683 * Indicates the ifindex the interface is linked to.
684 * Physical interfaces have the same 'ifindex' and 'iflink' values.
685 */
686
687int dev_get_iflink(const struct net_device *dev)
688{
689 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
690 return dev->netdev_ops->ndo_get_iflink(dev);
691
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200692 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200693}
694EXPORT_SYMBOL(dev_get_iflink);
695
696/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700697 * dev_fill_metadata_dst - Retrieve tunnel egress information.
698 * @dev: targeted interface
699 * @skb: The packet.
700 *
701 * For better visibility of tunnel traffic OVS needs to retrieve
702 * egress tunnel information for a packet. Following API allows
703 * user to get this info.
704 */
705int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
706{
707 struct ip_tunnel_info *info;
708
709 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
710 return -EINVAL;
711
712 info = skb_tunnel_info_unclone(skb);
713 if (!info)
714 return -ENOMEM;
715 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
716 return -EINVAL;
717
718 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
719}
720EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
721
722/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700724 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * @name: name to find
726 *
727 * Find an interface by name. Must be called under RTNL semaphore
728 * or @dev_base_lock. If the name is found a pointer to the device
729 * is returned. If the name is not found then %NULL is returned. The
730 * reference counters are not incremented so the caller must be
731 * careful with locks.
732 */
733
Eric W. Biederman881d9662007-09-17 11:56:21 -0700734struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700736 struct net_device *dev;
737 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Sasha Levinb67bfe02013-02-27 17:06:00 -0800739 hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!strncmp(dev->name, name, IFNAMSIZ))
741 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return NULL;
744}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700745EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747/**
tcharding722c9a02017-02-09 17:56:04 +1100748 * dev_get_by_name_rcu - find a device by its name
749 * @net: the applicable net namespace
750 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000751 *
tcharding722c9a02017-02-09 17:56:04 +1100752 * Find an interface by name.
753 * If the name is found a pointer to the device is returned.
754 * If the name is not found then %NULL is returned.
755 * The reference counters are not incremented so the caller must be
756 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000757 */
758
759struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
760{
Eric Dumazet72c95282009-10-30 07:11:27 +0000761 struct net_device *dev;
762 struct hlist_head *head = dev_name_hash(net, name);
763
Sasha Levinb67bfe02013-02-27 17:06:00 -0800764 hlist_for_each_entry_rcu(dev, head, name_hlist)
Eric Dumazet72c95282009-10-30 07:11:27 +0000765 if (!strncmp(dev->name, name, IFNAMSIZ))
766 return dev;
767
768 return NULL;
769}
770EXPORT_SYMBOL(dev_get_by_name_rcu);
771
772/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700774 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * @name: name to find
776 *
777 * Find an interface by name. This can be called from any
778 * context and does its own locking. The returned handle has
779 * the usage count incremented and the caller must use dev_put() to
780 * release it when it is no longer needed. %NULL is returned if no
781 * matching device is found.
782 */
783
Eric W. Biederman881d9662007-09-17 11:56:21 -0700784struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct net_device *dev;
787
Eric Dumazet72c95282009-10-30 07:11:27 +0000788 rcu_read_lock();
789 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (dev)
791 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000792 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return dev;
794}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700795EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797/**
798 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700799 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * @ifindex: index of device
801 *
802 * Search for an interface by index. Returns %NULL if the device
803 * is not found or a pointer to the device. The device has not
804 * had its reference counter increased so the caller must be careful
805 * about locking. The caller must hold either the RTNL semaphore
806 * or @dev_base_lock.
807 */
808
Eric W. Biederman881d9662007-09-17 11:56:21 -0700809struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700811 struct net_device *dev;
812 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Sasha Levinb67bfe02013-02-27 17:06:00 -0800814 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (dev->ifindex == ifindex)
816 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return NULL;
819}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700820EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000822/**
823 * dev_get_by_index_rcu - find a device by its ifindex
824 * @net: the applicable net namespace
825 * @ifindex: index of device
826 *
827 * Search for an interface by index. Returns %NULL if the device
828 * is not found or a pointer to the device. The device has not
829 * had its reference counter increased so the caller must be careful
830 * about locking. The caller must hold RCU lock.
831 */
832
833struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
834{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000835 struct net_device *dev;
836 struct hlist_head *head = dev_index_hash(net, ifindex);
837
Sasha Levinb67bfe02013-02-27 17:06:00 -0800838 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000839 if (dev->ifindex == ifindex)
840 return dev;
841
842 return NULL;
843}
844EXPORT_SYMBOL(dev_get_by_index_rcu);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/**
848 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700849 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * @ifindex: index of device
851 *
852 * Search for an interface by index. Returns NULL if the device
853 * is not found or a pointer to the device. The device returned has
854 * had a reference added and the pointer is safe until the user calls
855 * dev_put to indicate they have finished with it.
856 */
857
Eric W. Biederman881d9662007-09-17 11:56:21 -0700858struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct net_device *dev;
861
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000862 rcu_read_lock();
863 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (dev)
865 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000866 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return dev;
868}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700869EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200872 * dev_get_by_napi_id - find a device by napi_id
873 * @napi_id: ID of the NAPI struct
874 *
875 * Search for an interface by NAPI ID. Returns %NULL if the device
876 * is not found or a pointer to the device. The device has not had
877 * its reference counter increased so the caller must be careful
878 * about locking. The caller must hold RCU lock.
879 */
880
881struct net_device *dev_get_by_napi_id(unsigned int napi_id)
882{
883 struct napi_struct *napi;
884
885 WARN_ON_ONCE(!rcu_read_lock_held());
886
887 if (napi_id < MIN_NAPI_ID)
888 return NULL;
889
890 napi = napi_by_id(napi_id);
891
892 return napi ? napi->dev : NULL;
893}
894EXPORT_SYMBOL(dev_get_by_napi_id);
895
896/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200897 * netdev_get_name - get a netdevice name, knowing its ifindex.
898 * @net: network namespace
899 * @name: a pointer to the buffer where the name will be stored.
900 * @ifindex: the ifindex of the interface to get the name from.
901 *
902 * The use of raw_seqcount_begin() and cond_resched() before
903 * retrying is required as we want to give the writers a chance
904 * to complete when CONFIG_PREEMPT is not set.
905 */
906int netdev_get_name(struct net *net, char *name, int ifindex)
907{
908 struct net_device *dev;
909 unsigned int seq;
910
911retry:
912 seq = raw_seqcount_begin(&devnet_rename_seq);
913 rcu_read_lock();
914 dev = dev_get_by_index_rcu(net, ifindex);
915 if (!dev) {
916 rcu_read_unlock();
917 return -ENODEV;
918 }
919
920 strcpy(name, dev->name);
921 rcu_read_unlock();
922 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
923 cond_resched();
924 goto retry;
925 }
926
927 return 0;
928}
929
930/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000931 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700932 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * @type: media type of device
934 * @ha: hardware address
935 *
936 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800937 * is not found or a pointer to the device.
938 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000939 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * and the caller must therefore be careful about locking
941 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
943
Eric Dumazet941666c2010-12-05 01:23:53 +0000944struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
945 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct net_device *dev;
948
Eric Dumazet941666c2010-12-05 01:23:53 +0000949 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (dev->type == type &&
951 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700952 return dev;
953
954 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
Eric Dumazet941666c2010-12-05 01:23:53 +0000956EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300957
Eric W. Biederman881d9662007-09-17 11:56:21 -0700958struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700959{
960 struct net_device *dev;
961
962 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700963 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700964 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700965 return dev;
966
967 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700968}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700969EXPORT_SYMBOL(__dev_getfirstbyhwtype);
970
Eric W. Biederman881d9662007-09-17 11:56:21 -0700971struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000973 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000975 rcu_read_lock();
976 for_each_netdev_rcu(net, dev)
977 if (dev->type == type) {
978 dev_hold(dev);
979 ret = dev;
980 break;
981 }
982 rcu_read_unlock();
983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985EXPORT_SYMBOL(dev_getfirstbyhwtype);
986
987/**
WANG Cong6c555492014-09-11 15:35:09 -0700988 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700989 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * @if_flags: IFF_* values
991 * @mask: bitmask of bits in if_flags to check
992 *
993 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000994 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -0700995 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
997
WANG Cong6c555492014-09-11 15:35:09 -0700998struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
999 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001001 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
WANG Cong6c555492014-09-11 15:35:09 -07001003 ASSERT_RTNL();
1004
Pavel Emelianov7562f872007-05-03 15:13:45 -07001005 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001006 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001008 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 break;
1010 }
1011 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001012 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
WANG Cong6c555492014-09-11 15:35:09 -07001014EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016/**
1017 * dev_valid_name - check if name is okay for network device
1018 * @name: name string
1019 *
1020 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -07001021 * to allow sysfs to work. We also disallow any kind of
1022 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
David S. Miller95f050b2012-03-06 16:12:15 -05001024bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001026 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001027 return false;
Eric Dumazeta9d48202018-04-05 06:39:26 -07001028 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001029 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001030 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001031 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001032
1033 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001034 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001035 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001036 name++;
1037 }
David S. Miller95f050b2012-03-06 16:12:15 -05001038 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001040EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001043 * __dev_alloc_name - allocate a name for a device
1044 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001046 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
1048 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001049 * id. It scans list of devices to build up a free map, then chooses
1050 * the first empty slot. The caller must hold the dev_base or rtnl lock
1051 * while allocating the name and adding the device in order to avoid
1052 * duplicates.
1053 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1054 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
1056
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001057static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 const char *p;
1061 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001062 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct net_device *d;
1064
Rasmus Villemoes93809102017-11-13 00:15:08 +01001065 if (!dev_valid_name(name))
1066 return -EINVAL;
1067
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001068 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (p) {
1070 /*
1071 * Verify the string as this thing may have come from
1072 * the user. There must be either one "%d" and no other "%"
1073 * characters.
1074 */
1075 if (p[1] != 'd' || strchr(p + 2, '%'))
1076 return -EINVAL;
1077
1078 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001079 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!inuse)
1081 return -ENOMEM;
1082
Eric W. Biederman881d9662007-09-17 11:56:21 -07001083 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!sscanf(d->name, name, &i))
1085 continue;
1086 if (i < 0 || i >= max_netdevices)
1087 continue;
1088
1089 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001090 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!strncmp(buf, d->name, IFNAMSIZ))
1092 set_bit(i, inuse);
1093 }
1094
1095 i = find_first_zero_bit(inuse, max_netdevices);
1096 free_page((unsigned long) inuse);
1097 }
1098
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001099 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001100 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* It is possible to run out of possible slots
1104 * when the name is long and there isn't enough space left
1105 * for the digits, or if all bits are used.
1106 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001107 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001110static int dev_alloc_name_ns(struct net *net,
1111 struct net_device *dev,
1112 const char *name)
1113{
1114 char buf[IFNAMSIZ];
1115 int ret;
1116
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001117 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001118 ret = __dev_alloc_name(net, name, buf);
1119 if (ret >= 0)
1120 strlcpy(dev->name, buf, IFNAMSIZ);
1121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001124/**
1125 * dev_alloc_name - allocate a name for a device
1126 * @dev: device
1127 * @name: name format string
1128 *
1129 * Passed a format string - eg "lt%d" it will try and find a suitable
1130 * id. It scans list of devices to build up a free map, then chooses
1131 * the first empty slot. The caller must hold the dev_base or rtnl lock
1132 * while allocating the name and adding the device in order to avoid
1133 * duplicates.
1134 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1135 * Returns the number of the unit assigned or a negative errno code.
1136 */
1137
1138int dev_alloc_name(struct net_device *dev, const char *name)
1139{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001140 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001141}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001142EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001143
Cong Wang0ad646c2017-10-13 11:58:53 -07001144int dev_get_valid_name(struct net *net, struct net_device *dev,
1145 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001146{
David S. Miller55a5ec92018-01-02 11:45:07 -05001147 BUG_ON(!net);
1148
1149 if (!dev_valid_name(name))
1150 return -EINVAL;
1151
1152 if (strchr(name, '%'))
1153 return dev_alloc_name_ns(net, dev, name);
1154 else if (__dev_get_by_name(net, name))
1155 return -EEXIST;
1156 else if (dev->name != name)
1157 strlcpy(dev->name, name, IFNAMSIZ);
1158
1159 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001160}
Cong Wang0ad646c2017-10-13 11:58:53 -07001161EXPORT_SYMBOL(dev_get_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163/**
1164 * dev_change_name - change name of a device
1165 * @dev: device
1166 * @newname: name (or format string) must be at least IFNAMSIZ
1167 *
1168 * Change name of a device, can pass format strings "eth%d".
1169 * for wildcarding.
1170 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001171int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Tom Gundersen238fa362014-07-14 16:37:23 +02001173 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001174 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001176 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001177 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001180 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001182 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (dev->flags & IFF_UP)
1184 return -EBUSY;
1185
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001186 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001187
1188 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001189 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001190 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001191 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001192
Herbert Xufcc5a032007-07-30 17:03:38 -07001193 memcpy(oldname, dev->name, IFNAMSIZ);
1194
Gao feng828de4f2012-09-13 20:58:27 +00001195 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001196 if (err < 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001197 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001198 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001201 if (oldname[0] && !strchr(oldname, '%'))
1202 netdev_info(dev, "renamed from %s\n", oldname);
1203
Tom Gundersen238fa362014-07-14 16:37:23 +02001204 old_assign_type = dev->name_assign_type;
1205 dev->name_assign_type = NET_NAME_RENAMED;
1206
Herbert Xufcc5a032007-07-30 17:03:38 -07001207rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001208 ret = device_rename(&dev->dev, dev->name);
1209 if (ret) {
1210 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001211 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001212 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001213 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001214 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001215
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001216 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001217
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001218 netdev_adjacent_rename_links(dev, oldname);
1219
Herbert Xu7f988ea2007-07-30 16:35:46 -07001220 write_lock_bh(&dev_base_lock);
Eric Dumazet372b2312011-05-17 13:56:59 -04001221 hlist_del_rcu(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001222 write_unlock_bh(&dev_base_lock);
1223
1224 synchronize_rcu();
1225
1226 write_lock_bh(&dev_base_lock);
1227 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001228 write_unlock_bh(&dev_base_lock);
1229
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001230 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001231 ret = notifier_to_errno(ret);
1232
1233 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001234 /* err >= 0 after dev_alloc_name() or stores the first errno */
1235 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001236 err = ret;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001237 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001238 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001239 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001240 dev->name_assign_type = old_assign_type;
1241 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001242 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001243 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001244 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001245 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001246 }
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 return err;
1250}
1251
1252/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001253 * dev_set_alias - change ifalias of a device
1254 * @dev: device
1255 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001256 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001257 *
1258 * Set ifalias for a device,
1259 */
1260int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1261{
Florian Westphal6c557002017-10-02 23:50:05 +02001262 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001263
1264 if (len >= IFALIASZ)
1265 return -EINVAL;
1266
Florian Westphal6c557002017-10-02 23:50:05 +02001267 if (len) {
1268 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1269 if (!new_alias)
1270 return -ENOMEM;
1271
1272 memcpy(new_alias->ifalias, alias, len);
1273 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001274 }
1275
Florian Westphal6c557002017-10-02 23:50:05 +02001276 mutex_lock(&ifalias_mutex);
1277 rcu_swap_protected(dev->ifalias, new_alias,
1278 mutex_is_locked(&ifalias_mutex));
1279 mutex_unlock(&ifalias_mutex);
1280
1281 if (new_alias)
1282 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001283
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001284 return len;
1285}
Stephen Hemminger0fe554a2018-04-17 14:25:30 -07001286EXPORT_SYMBOL(dev_set_alias);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001287
Florian Westphal6c557002017-10-02 23:50:05 +02001288/**
1289 * dev_get_alias - get ifalias of a device
1290 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001291 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001292 * @len: size of buffer
1293 *
1294 * get ifalias for a device. Caller must make sure dev cannot go
1295 * away, e.g. rcu read lock or own a reference count to device.
1296 */
1297int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1298{
1299 const struct dev_ifalias *alias;
1300 int ret = 0;
1301
1302 rcu_read_lock();
1303 alias = rcu_dereference(dev->ifalias);
1304 if (alias)
1305 ret = snprintf(name, len, "%s", alias->ifalias);
1306 rcu_read_unlock();
1307
1308 return ret;
1309}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001310
1311/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001312 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001313 * @dev: device to cause notification
1314 *
1315 * Called to indicate a device has changed features.
1316 */
1317void netdev_features_change(struct net_device *dev)
1318{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001319 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001320}
1321EXPORT_SYMBOL(netdev_features_change);
1322
1323/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * netdev_state_change - device changes state
1325 * @dev: device to cause notification
1326 *
1327 * Called to indicate a device has changed state. This function calls
1328 * the notifier chains for netdev_chain and sends a NEWLINK message
1329 * to the routing socket.
1330 */
1331void netdev_state_change(struct net_device *dev)
1332{
1333 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001334 struct netdev_notifier_change_info change_info = {
1335 .info.dev = dev,
1336 };
Loic Prylli54951192014-07-01 21:39:43 -07001337
David Ahern51d0c0472017-10-04 17:48:45 -07001338 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001339 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001340 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001343EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Amerigo Wangee89bab2012-08-09 22:14:56 +00001345/**
tcharding722c9a02017-02-09 17:56:04 +11001346 * netdev_notify_peers - notify network peers about existence of @dev
1347 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001348 *
1349 * Generate traffic such that interested network peers are aware of
1350 * @dev, such as by generating a gratuitous ARP. This may be used when
1351 * a device wants to inform the rest of the network about some sort of
1352 * reconfiguration such as a failover event or virtual machine
1353 * migration.
1354 */
1355void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001356{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001357 rtnl_lock();
1358 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001359 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001360 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001361}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001362EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001363
Patrick McHardybd380812010-02-26 06:34:53 +00001364static int __dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001366 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001367 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001369 ASSERT_RTNL();
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!netif_device_present(dev))
1372 return -ENODEV;
1373
Neil Hormanca99ca12013-02-05 08:05:43 +00001374 /* Block netpoll from trying to do any rx path servicing.
1375 * If we don't do this there is a chance ndo_poll_controller
1376 * or ndo_poll may be running while we open the device
1377 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001378 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001379
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001380 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1381 ret = notifier_to_errno(ret);
1382 if (ret)
1383 return ret;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001386
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001387 if (ops->ndo_validate_addr)
1388 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001389
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001390 if (!ret && ops->ndo_open)
1391 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Eric W. Biederman66b55522014-03-27 15:39:03 -07001393 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001394
Jeff Garzikbada3392007-10-23 20:19:37 -07001395 if (ret)
1396 clear_bit(__LINK_STATE_START, &dev->state);
1397 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001399 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001401 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return ret;
1405}
Patrick McHardybd380812010-02-26 06:34:53 +00001406
1407/**
1408 * dev_open - prepare an interface for use.
1409 * @dev: device to open
1410 *
1411 * Takes a device from down to up state. The device's private open
1412 * function is invoked and then the multicast lists are loaded. Finally
1413 * the device is moved into the up state and a %NETDEV_UP message is
1414 * sent to the netdev notifier chain.
1415 *
1416 * Calling this function on an active interface is a nop. On a failure
1417 * a negative errno code is returned.
1418 */
1419int dev_open(struct net_device *dev)
1420{
1421 int ret;
1422
Patrick McHardybd380812010-02-26 06:34:53 +00001423 if (dev->flags & IFF_UP)
1424 return 0;
1425
Patrick McHardybd380812010-02-26 06:34:53 +00001426 ret = __dev_open(dev);
1427 if (ret < 0)
1428 return ret;
1429
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001430 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001431 call_netdevice_notifiers(NETDEV_UP, dev);
1432
1433 return ret;
1434}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001435EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
stephen hemminger7051b882017-07-18 15:59:27 -07001437static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Octavian Purdila44345722010-12-13 12:44:07 +00001439 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001440
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001441 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001442 might_sleep();
1443
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001444 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001445 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001446 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001447
Octavian Purdila44345722010-12-13 12:44:07 +00001448 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Octavian Purdila44345722010-12-13 12:44:07 +00001450 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Octavian Purdila44345722010-12-13 12:44:07 +00001452 /* Synchronize to scheduled poll. We cannot touch poll list, it
1453 * can be even on different cpu. So just clear netif_running().
1454 *
1455 * dev->stop() will invoke napi_disable() on all of it's
1456 * napi_struct instances on this device.
1457 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001458 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Octavian Purdila44345722010-12-13 12:44:07 +00001461 dev_deactivate_many(head);
1462
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001463 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001464 const struct net_device_ops *ops = dev->netdev_ops;
1465
1466 /*
1467 * Call the device specific close. This cannot fail.
1468 * Only if device is UP
1469 *
1470 * We allow it to be called even after a DETACH hot-plug
1471 * event.
1472 */
1473 if (ops->ndo_stop)
1474 ops->ndo_stop(dev);
1475
Octavian Purdila44345722010-12-13 12:44:07 +00001476 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001477 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001478 }
Octavian Purdila44345722010-12-13 12:44:07 +00001479}
1480
stephen hemminger7051b882017-07-18 15:59:27 -07001481static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001482{
1483 LIST_HEAD(single);
1484
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001485 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001486 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001487 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001488}
1489
stephen hemminger7051b882017-07-18 15:59:27 -07001490void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001491{
1492 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001493
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001494 /* Remove the devices that don't need to be closed */
1495 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001496 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001497 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001498
1499 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001500
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001501 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001502 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001503 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001504 if (unlink)
1505 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
David S. Miller99c4a262015-03-18 22:52:33 -04001508EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001509
1510/**
1511 * dev_close - shutdown an interface.
1512 * @dev: device to shutdown
1513 *
1514 * This function moves an active device into down state. A
1515 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1516 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1517 * chain.
1518 */
stephen hemminger7051b882017-07-18 15:59:27 -07001519void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001520{
Eric Dumazete14a5992011-05-10 12:26:06 -07001521 if (dev->flags & IFF_UP) {
1522 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001523
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001524 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001525 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001526 list_del(&single);
1527 }
Patrick McHardybd380812010-02-26 06:34:53 +00001528}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001529EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001532/**
1533 * dev_disable_lro - disable Large Receive Offload on a device
1534 * @dev: device
1535 *
1536 * Disable Large Receive Offload (LRO) on a net device. Must be
1537 * called under RTNL. This is needed if received packets may be
1538 * forwarded to another interface.
1539 */
1540void dev_disable_lro(struct net_device *dev)
1541{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001542 struct net_device *lower_dev;
1543 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001544
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001545 dev->wanted_features &= ~NETIF_F_LRO;
1546 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001547
Michał Mirosław22d59692011-04-21 12:42:15 +00001548 if (unlikely(dev->features & NETIF_F_LRO))
1549 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001550
1551 netdev_for_each_lower_dev(dev, lower_dev, iter)
1552 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001553}
1554EXPORT_SYMBOL(dev_disable_lro);
1555
Michael Chan56f5aa72017-12-16 03:09:41 -05001556/**
1557 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1558 * @dev: device
1559 *
1560 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1561 * called under RTNL. This is needed if Generic XDP is installed on
1562 * the device.
1563 */
1564static void dev_disable_gro_hw(struct net_device *dev)
1565{
1566 dev->wanted_features &= ~NETIF_F_GRO_HW;
1567 netdev_update_features(dev);
1568
1569 if (unlikely(dev->features & NETIF_F_GRO_HW))
1570 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1571}
1572
Kirill Tkhaiede27622018-03-23 19:47:19 +03001573const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1574{
1575#define N(val) \
1576 case NETDEV_##val: \
1577 return "NETDEV_" __stringify(val);
1578 switch (cmd) {
1579 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1580 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1581 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1582 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1583 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1584 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1585 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
Gal Pressman9daae9b2018-03-28 17:46:54 +03001586 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1587 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
Kirill Tkhai3f5ecd82018-04-26 15:18:38 +03001588 }
Kirill Tkhaiede27622018-03-23 19:47:19 +03001589#undef N
1590 return "UNKNOWN_NETDEV_EVENT";
1591}
1592EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1593
Jiri Pirko351638e2013-05-28 01:30:21 +00001594static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1595 struct net_device *dev)
1596{
David Ahern51d0c0472017-10-04 17:48:45 -07001597 struct netdev_notifier_info info = {
1598 .dev = dev,
1599 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001600
Jiri Pirko351638e2013-05-28 01:30:21 +00001601 return nb->notifier_call(nb, val, &info);
1602}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001603
Eric W. Biederman881d9662007-09-17 11:56:21 -07001604static int dev_boot_phase = 1;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/**
tcharding722c9a02017-02-09 17:56:04 +11001607 * register_netdevice_notifier - register a network notifier block
1608 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 *
tcharding722c9a02017-02-09 17:56:04 +11001610 * Register a notifier to be called when network device events occur.
1611 * The notifier passed is linked into the kernel structures and must
1612 * not be reused until it has been unregistered. A negative errno code
1613 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 *
tcharding722c9a02017-02-09 17:56:04 +11001615 * When registered all registration and up events are replayed
1616 * to the new notifier to allow device to have a race free
1617 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
1619
1620int register_netdevice_notifier(struct notifier_block *nb)
1621{
1622 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001623 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001624 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int err;
1626
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001627 /* Close race with setup_net() and cleanup_net() */
1628 down_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001630 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001631 if (err)
1632 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001633 if (dev_boot_phase)
1634 goto unlock;
1635 for_each_net(net) {
1636 for_each_netdev(net, dev) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001637 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001638 err = notifier_to_errno(err);
1639 if (err)
1640 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Eric W. Biederman881d9662007-09-17 11:56:21 -07001642 if (!(dev->flags & IFF_UP))
1643 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001644
Jiri Pirko351638e2013-05-28 01:30:21 +00001645 call_netdevice_notifier(nb, NETDEV_UP, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001648
1649unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001651 up_write(&pernet_ops_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001653
1654rollback:
1655 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001656 for_each_net(net) {
1657 for_each_netdev(net, dev) {
1658 if (dev == last)
RongQing.Li8f891482011-11-30 23:43:07 -05001659 goto outroll;
Herbert Xufcc5a032007-07-30 17:03:38 -07001660
Eric W. Biederman881d9662007-09-17 11:56:21 -07001661 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001662 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1663 dev);
1664 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001665 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001666 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001667 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001668 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001669
RongQing.Li8f891482011-11-30 23:43:07 -05001670outroll:
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001671 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001672 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001674EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676/**
tcharding722c9a02017-02-09 17:56:04 +11001677 * unregister_netdevice_notifier - unregister a network notifier block
1678 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 *
tcharding722c9a02017-02-09 17:56:04 +11001680 * Unregister a notifier previously registered by
1681 * register_netdevice_notifier(). The notifier is unlinked into the
1682 * kernel structures and may then be reused. A negative errno code
1683 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001684 *
tcharding722c9a02017-02-09 17:56:04 +11001685 * After unregistering unregister and down device events are synthesized
1686 * for all devices on the device list to the removed notifier to remove
1687 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
1689
1690int unregister_netdevice_notifier(struct notifier_block *nb)
1691{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001692 struct net_device *dev;
1693 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001694 int err;
1695
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001696 /* Close race with setup_net() and cleanup_net() */
1697 down_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001698 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001699 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001700 if (err)
1701 goto unlock;
1702
1703 for_each_net(net) {
1704 for_each_netdev(net, dev) {
1705 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001706 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1707 dev);
1708 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001709 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001710 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001711 }
1712 }
1713unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001714 rtnl_unlock();
Kirill Tkhai328fbe72018-03-29 17:03:45 +03001715 up_write(&pernet_ops_rwsem);
Herbert Xu9f514952006-03-25 01:24:25 -08001716 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001718EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001721 * call_netdevice_notifiers_info - call all network notifier blocks
1722 * @val: value passed unmodified to notifier function
Jiri Pirko351638e2013-05-28 01:30:21 +00001723 * @info: notifier information data
1724 *
1725 * Call all network notifier blocks. Parameters and return value
1726 * are as for raw_notifier_call_chain().
1727 */
1728
stephen hemminger1d143d92013-12-29 14:01:29 -08001729static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001730 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001731{
1732 ASSERT_RTNL();
Jiri Pirko351638e2013-05-28 01:30:21 +00001733 return raw_notifier_call_chain(&netdev_chain, val, info);
1734}
Jiri Pirko351638e2013-05-28 01:30:21 +00001735
1736/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 * call_netdevice_notifiers - call all network notifier blocks
1738 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001739 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 *
1741 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001742 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
1744
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001745int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
David Ahern51d0c0472017-10-04 17:48:45 -07001747 struct netdev_notifier_info info = {
1748 .dev = dev,
1749 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001750
David Ahern51d0c0472017-10-04 17:48:45 -07001751 return call_netdevice_notifiers_info(val, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001753EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001755/**
1756 * call_netdevice_notifiers_mtu - call all network notifier blocks
1757 * @val: value passed unmodified to notifier function
1758 * @dev: net_device pointer passed unmodified to notifier function
1759 * @arg: additional u32 argument passed to the notifier function
1760 *
1761 * Call all network notifier blocks. Parameters and return value
1762 * are as for raw_notifier_call_chain().
1763 */
1764static int call_netdevice_notifiers_mtu(unsigned long val,
1765 struct net_device *dev, u32 arg)
1766{
1767 struct netdev_notifier_info_ext info = {
1768 .info.dev = dev,
1769 .ext.mtu = arg,
1770 };
1771
1772 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1773
1774 return call_netdevice_notifiers_info(val, &info.info);
1775}
1776
Pablo Neira1cf519002015-05-13 18:19:37 +02001777#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001778static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001779
1780void net_inc_ingress_queue(void)
1781{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001782 static_branch_inc(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001783}
1784EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1785
1786void net_dec_ingress_queue(void)
1787{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001788 static_branch_dec(&ingress_needed_key);
Daniel Borkmann45771392015-04-10 23:07:54 +02001789}
1790EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1791#endif
1792
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001793#ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001794static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001795
1796void net_inc_egress_queue(void)
1797{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001798 static_branch_inc(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001799}
1800EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1801
1802void net_dec_egress_queue(void)
1803{
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07001804 static_branch_dec(&egress_needed_key);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001805}
1806EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1807#endif
1808
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001809static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001810#ifdef HAVE_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00001811static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08001812static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001813static void netstamp_clear(struct work_struct *work)
1814{
1815 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08001816 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001817
Eric Dumazet13baa002017-03-01 14:28:39 -08001818 wanted = atomic_add_return(deferred, &netstamp_wanted);
1819 if (wanted > 0)
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001820 static_branch_enable(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08001821 else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001822 static_branch_disable(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001823}
1824static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827void net_enable_timestamp(void)
1828{
Eric Dumazet13baa002017-03-01 14:28:39 -08001829#ifdef HAVE_JUMP_LABEL
1830 int wanted;
1831
1832 while (1) {
1833 wanted = atomic_read(&netstamp_wanted);
1834 if (wanted <= 0)
1835 break;
1836 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1837 return;
1838 }
1839 atomic_inc(&netstamp_needed_deferred);
1840 schedule_work(&netstamp_work);
1841#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001842 static_branch_inc(&netstamp_needed_key);
Eric Dumazet13baa002017-03-01 14:28:39 -08001843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001845EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847void net_disable_timestamp(void)
1848{
Eric Dumazetb90e5792011-11-28 11:16:50 +00001849#ifdef HAVE_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08001850 int wanted;
1851
1852 while (1) {
1853 wanted = atomic_read(&netstamp_wanted);
1854 if (wanted <= 1)
1855 break;
1856 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1857 return;
1858 }
1859 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001860 schedule_work(&netstamp_work);
1861#else
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001862 static_branch_dec(&netstamp_needed_key);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001865EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Eric Dumazet3b098e22010-05-15 23:57:10 -07001867static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Thomas Gleixner2456e852016-12-25 11:38:40 +01001869 skb->tstamp = 0;
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001870 if (static_branch_unlikely(&netstamp_needed_key))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001871 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Davidlohr Bueso39e83922018-05-08 09:07:01 -07001874#define net_timestamp_check(COND, SKB) \
1875 if (static_branch_unlikely(&netstamp_needed_key)) { \
1876 if ((COND) && !(SKB)->tstamp) \
1877 __net_timestamp(SKB); \
1878 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07001879
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02001880bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001881{
1882 unsigned int len;
1883
1884 if (!(dev->flags & IFF_UP))
1885 return false;
1886
1887 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1888 if (skb->len <= len)
1889 return true;
1890
1891 /* if TSO is enabled, we don't care about the length as the packet
1892 * could be forwarded without being segmented before
1893 */
1894 if (skb_is_gso(skb))
1895 return true;
1896
1897 return false;
1898}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04001899EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001900
Herbert Xua0265d22014-04-17 13:45:03 +08001901int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1902{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001903 int ret = ____dev_forward_skb(dev, skb);
1904
1905 if (likely(!ret)) {
1906 skb->protocol = eth_type_trans(skb, dev);
1907 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08001908 }
1909
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001910 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08001911}
1912EXPORT_SYMBOL_GPL(__dev_forward_skb);
1913
Arnd Bergmann44540962009-11-26 06:07:08 +00001914/**
1915 * dev_forward_skb - loopback an skb to another netif
1916 *
1917 * @dev: destination network device
1918 * @skb: buffer to forward
1919 *
1920 * return values:
1921 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001922 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001923 *
1924 * dev_forward_skb can be used for injecting an skb from the
1925 * start_xmit function of one device into the receive queue
1926 * of another device.
1927 *
1928 * The receiving device may be in another namespace, so
1929 * we have to clear all information in the skb that could
1930 * impact namespace isolation.
1931 */
1932int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1933{
Herbert Xua0265d22014-04-17 13:45:03 +08001934 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001935}
1936EXPORT_SYMBOL_GPL(dev_forward_skb);
1937
Changli Gao71d9dec2010-12-15 19:57:25 +00001938static inline int deliver_skb(struct sk_buff *skb,
1939 struct packet_type *pt_prev,
1940 struct net_device *orig_dev)
1941{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001942 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00001943 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03001944 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00001945 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1946}
1947
Salam Noureddine7866a622015-01-27 11:35:48 -08001948static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1949 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001950 struct net_device *orig_dev,
1951 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08001952 struct list_head *ptype_list)
1953{
1954 struct packet_type *ptype, *pt_prev = *pt;
1955
1956 list_for_each_entry_rcu(ptype, ptype_list, list) {
1957 if (ptype->type != type)
1958 continue;
1959 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001960 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08001961 pt_prev = ptype;
1962 }
1963 *pt = pt_prev;
1964}
1965
Eric Leblondc0de08d2012-08-16 22:02:58 +00001966static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1967{
Eric Leblonda3d744e2012-11-06 02:10:10 +00001968 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001969 return false;
1970
1971 if (ptype->id_match)
1972 return ptype->id_match(ptype, skb->sk);
1973 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1974 return true;
1975
1976 return false;
1977}
1978
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01001979/**
1980 * dev_nit_active - return true if any network interface taps are in use
1981 *
1982 * @dev: network device to check for the presence of taps
1983 */
1984bool dev_nit_active(struct net_device *dev)
1985{
1986 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
1987}
1988EXPORT_SYMBOL_GPL(dev_nit_active);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/*
1991 * Support routine. Sends outgoing frames to any network
1992 * taps currently in use.
1993 */
1994
David Ahern74b20582016-05-10 11:19:50 -07001995void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00001998 struct sk_buff *skb2 = NULL;
1999 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08002000 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08002003again:
2004 list_for_each_entry_rcu(ptype, ptype_list, list) {
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02002005 if (ptype->ignore_outgoing)
2006 continue;
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* Never send packets back to the socket
2009 * they originated from - MvS (miquels@drinkel.ow.org)
2010 */
Salam Noureddine7866a622015-01-27 11:35:48 -08002011 if (skb_loop_sk(ptype, skb))
2012 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00002013
Salam Noureddine7866a622015-01-27 11:35:48 -08002014 if (pt_prev) {
2015 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00002016 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08002017 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002019
2020 /* need to clone skb, done only once */
2021 skb2 = skb_clone(skb, GFP_ATOMIC);
2022 if (!skb2)
2023 goto out_unlock;
2024
2025 net_timestamp_set(skb2);
2026
2027 /* skb->nh should be correctly
2028 * set by sender, so that the second statement is
2029 * just protection against buggy protocols.
2030 */
2031 skb_reset_mac_header(skb2);
2032
2033 if (skb_network_header(skb2) < skb2->data ||
2034 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2035 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2036 ntohs(skb2->protocol),
2037 dev->name);
2038 skb_reset_network_header(skb2);
2039 }
2040
2041 skb2->transport_header = skb2->network_header;
2042 skb2->pkt_type = PACKET_OUTGOING;
2043 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Salam Noureddine7866a622015-01-27 11:35:48 -08002045
2046 if (ptype_list == &ptype_all) {
2047 ptype_list = &dev->ptype_all;
2048 goto again;
2049 }
2050out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04002051 if (pt_prev) {
2052 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2053 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2054 else
2055 kfree_skb(skb2);
2056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 rcu_read_unlock();
2058}
David Ahern74b20582016-05-10 11:19:50 -07002059EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Ben Hutchings2c530402012-07-10 10:55:09 +00002061/**
2062 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002063 * @dev: Network device
2064 * @txq: number of queues available
2065 *
2066 * If real_num_tx_queues is changed the tc mappings may no longer be
2067 * valid. To resolve this verify the tc mapping remains valid and if
2068 * not NULL the mapping. With no priorities mapping to this
2069 * offset/count pair it will no longer be used. In the worst case TC0
2070 * is invalid nothing can be done so disable priority mappings. If is
2071 * expected that drivers will fix this mapping if they can before
2072 * calling netif_set_real_num_tx_queues.
2073 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002074static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002075{
2076 int i;
2077 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2078
2079 /* If TC0 is invalidated disable TC mapping */
2080 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002081 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002082 dev->num_tc = 0;
2083 return;
2084 }
2085
2086 /* Invalidated prio to tc mappings set to TC0 */
2087 for (i = 1; i < TC_BITMASK + 1; i++) {
2088 int q = netdev_get_prio_tc_map(dev, i);
2089
2090 tc = &dev->tc_to_txq[q];
2091 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002092 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2093 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002094 netdev_set_prio_tc_map(dev, i, 0);
2095 }
2096 }
2097}
2098
Alexander Duyck8d059b02016-10-28 11:43:49 -04002099int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2100{
2101 if (dev->num_tc) {
2102 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2103 int i;
2104
Alexander Duyckffcfe252018-07-09 12:19:38 -04002105 /* walk through the TCs and see if it falls into any of them */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002106 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2107 if ((txq - tc->offset) < tc->count)
2108 return i;
2109 }
2110
Alexander Duyckffcfe252018-07-09 12:19:38 -04002111 /* didn't find it, just return -1 to indicate no match */
Alexander Duyck8d059b02016-10-28 11:43:49 -04002112 return -1;
2113 }
2114
2115 return 0;
2116}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002117EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002118
Alexander Duyck537c00d2013-01-10 08:57:02 +00002119#ifdef CONFIG_XPS
Amritha Nambiar04157462018-06-29 21:26:46 -07002120struct static_key xps_needed __read_mostly;
2121EXPORT_SYMBOL(xps_needed);
2122struct static_key xps_rxqs_needed __read_mostly;
2123EXPORT_SYMBOL(xps_rxqs_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002124static DEFINE_MUTEX(xps_map_mutex);
2125#define xmap_dereference(P) \
2126 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2127
Alexander Duyck6234f872016-10-28 11:46:49 -04002128static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2129 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002130{
2131 struct xps_map *map = NULL;
2132 int pos;
2133
2134 if (dev_maps)
Amritha Nambiar80d19662018-06-29 21:26:41 -07002135 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck6234f872016-10-28 11:46:49 -04002136 if (!map)
2137 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002138
Alexander Duyck6234f872016-10-28 11:46:49 -04002139 for (pos = map->len; pos--;) {
2140 if (map->queues[pos] != index)
2141 continue;
2142
2143 if (map->len > 1) {
2144 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002145 break;
2146 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002147
Amritha Nambiar80d19662018-06-29 21:26:41 -07002148 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
Alexander Duyck6234f872016-10-28 11:46:49 -04002149 kfree_rcu(map, rcu);
2150 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002151 }
2152
Alexander Duyck6234f872016-10-28 11:46:49 -04002153 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002154}
2155
Alexander Duyck6234f872016-10-28 11:46:49 -04002156static bool remove_xps_queue_cpu(struct net_device *dev,
2157 struct xps_dev_maps *dev_maps,
2158 int cpu, u16 offset, u16 count)
2159{
Alexander Duyck184c4492016-10-28 11:50:13 -04002160 int num_tc = dev->num_tc ? : 1;
2161 bool active = false;
2162 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002163
Alexander Duyck184c4492016-10-28 11:50:13 -04002164 for (tci = cpu * num_tc; num_tc--; tci++) {
2165 int i, j;
2166
2167 for (i = count, j = offset; i--; j++) {
Amritha Nambiar6358d492018-05-17 14:50:44 -07002168 if (!remove_xps_queue(dev_maps, tci, j))
Alexander Duyck184c4492016-10-28 11:50:13 -04002169 break;
2170 }
2171
2172 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002173 }
2174
Alexander Duyck184c4492016-10-28 11:50:13 -04002175 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002176}
2177
Amritha Nambiar80d19662018-06-29 21:26:41 -07002178static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2179 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2180 u16 offset, u16 count, bool is_rxqs_map)
2181{
2182 bool active = false;
2183 int i, j;
2184
2185 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2186 j < nr_ids;)
2187 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2188 count);
2189 if (!active) {
2190 if (is_rxqs_map) {
2191 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2192 } else {
2193 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2194
2195 for (i = offset + (count - 1); count--; i--)
2196 netdev_queue_numa_node_write(
2197 netdev_get_tx_queue(dev, i),
2198 NUMA_NO_NODE);
2199 }
2200 kfree_rcu(dev_maps, rcu);
2201 }
2202}
2203
Alexander Duyck6234f872016-10-28 11:46:49 -04002204static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2205 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002206{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002207 const unsigned long *possible_mask = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002208 struct xps_dev_maps *dev_maps;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002209 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002210
Amritha Nambiar04157462018-06-29 21:26:46 -07002211 if (!static_key_false(&xps_needed))
2212 return;
2213
Andrei Vagin4d99f662018-08-08 20:07:35 -07002214 cpus_read_lock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002215 mutex_lock(&xps_map_mutex);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002216
Amritha Nambiar04157462018-06-29 21:26:46 -07002217 if (static_key_false(&xps_rxqs_needed)) {
2218 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2219 if (dev_maps) {
2220 nr_ids = dev->num_rx_queues;
2221 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2222 offset, count, true);
2223 }
Amritha Nambiar80d19662018-06-29 21:26:41 -07002224 }
2225
2226 dev_maps = xmap_dereference(dev->xps_cpus_map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002227 if (!dev_maps)
2228 goto out_no_maps;
2229
Amritha Nambiar80d19662018-06-29 21:26:41 -07002230 if (num_possible_cpus() > 1)
2231 possible_mask = cpumask_bits(cpu_possible_mask);
2232 nr_ids = nr_cpu_ids;
2233 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2234 false);
Alexander Duyck024e9672013-01-10 08:57:46 +00002235
Alexander Duyck537c00d2013-01-10 08:57:02 +00002236out_no_maps:
Amritha Nambiar04157462018-06-29 21:26:46 -07002237 if (static_key_enabled(&xps_rxqs_needed))
Andrei Vagin4d99f662018-08-08 20:07:35 -07002238 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
Amritha Nambiar04157462018-06-29 21:26:46 -07002239
Andrei Vagin4d99f662018-08-08 20:07:35 -07002240 static_key_slow_dec_cpuslocked(&xps_needed);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002241 mutex_unlock(&xps_map_mutex);
Andrei Vagin4d99f662018-08-08 20:07:35 -07002242 cpus_read_unlock();
Alexander Duyck537c00d2013-01-10 08:57:02 +00002243}
2244
Alexander Duyck6234f872016-10-28 11:46:49 -04002245static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2246{
2247 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2248}
2249
Amritha Nambiar80d19662018-06-29 21:26:41 -07002250static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2251 u16 index, bool is_rxqs_map)
Alexander Duyck01c5f862013-01-10 08:57:35 +00002252{
2253 struct xps_map *new_map;
2254 int alloc_len = XPS_MIN_MAP_ALLOC;
2255 int i, pos;
2256
2257 for (pos = 0; map && pos < map->len; pos++) {
2258 if (map->queues[pos] != index)
2259 continue;
2260 return map;
2261 }
2262
Amritha Nambiar80d19662018-06-29 21:26:41 -07002263 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002264 if (map) {
2265 if (pos < map->alloc_len)
2266 return map;
2267
2268 alloc_len = map->alloc_len * 2;
2269 }
2270
Amritha Nambiar80d19662018-06-29 21:26:41 -07002271 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2272 * map
2273 */
2274 if (is_rxqs_map)
2275 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2276 else
2277 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2278 cpu_to_node(attr_index));
Alexander Duyck01c5f862013-01-10 08:57:35 +00002279 if (!new_map)
2280 return NULL;
2281
2282 for (i = 0; i < pos; i++)
2283 new_map->queues[i] = map->queues[i];
2284 new_map->alloc_len = alloc_len;
2285 new_map->len = pos;
2286
2287 return new_map;
2288}
2289
Andrei Vagin4d99f662018-08-08 20:07:35 -07002290/* Must be called under cpus_read_lock */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002291int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2292 u16 index, bool is_rxqs_map)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002293{
Amritha Nambiar80d19662018-06-29 21:26:41 -07002294 const unsigned long *online_mask = NULL, *possible_mask = NULL;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002295 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002296 int i, j, tci, numa_node_id = -2;
Alexander Duyck184c4492016-10-28 11:50:13 -04002297 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002298 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002299 bool active = false;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002300 unsigned int nr_ids;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002301
Alexander Duyck184c4492016-10-28 11:50:13 -04002302 if (dev->num_tc) {
Alexander Duyckffcfe252018-07-09 12:19:38 -04002303 /* Do not allow XPS on subordinate device directly */
Alexander Duyck184c4492016-10-28 11:50:13 -04002304 num_tc = dev->num_tc;
Alexander Duyckffcfe252018-07-09 12:19:38 -04002305 if (num_tc < 0)
2306 return -EINVAL;
2307
2308 /* If queue belongs to subordinate dev use its map */
2309 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2310
Alexander Duyck184c4492016-10-28 11:50:13 -04002311 tc = netdev_txq_to_tc(dev, index);
2312 if (tc < 0)
2313 return -EINVAL;
2314 }
2315
Amritha Nambiar80d19662018-06-29 21:26:41 -07002316 mutex_lock(&xps_map_mutex);
2317 if (is_rxqs_map) {
2318 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2319 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2320 nr_ids = dev->num_rx_queues;
2321 } else {
2322 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2323 if (num_possible_cpus() > 1) {
2324 online_mask = cpumask_bits(cpu_online_mask);
2325 possible_mask = cpumask_bits(cpu_possible_mask);
2326 }
2327 dev_maps = xmap_dereference(dev->xps_cpus_map);
2328 nr_ids = nr_cpu_ids;
2329 }
2330
Alexander Duyck184c4492016-10-28 11:50:13 -04002331 if (maps_sz < L1_CACHE_BYTES)
2332 maps_sz = L1_CACHE_BYTES;
2333
Alexander Duyck01c5f862013-01-10 08:57:35 +00002334 /* allocate memory for queue storage */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002335 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2336 j < nr_ids;) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002337 if (!new_dev_maps)
2338 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002339 if (!new_dev_maps) {
2340 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002341 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002342 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002343
Amritha Nambiar80d19662018-06-29 21:26:41 -07002344 tci = j * num_tc + tc;
2345 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002346 NULL;
2347
Amritha Nambiar80d19662018-06-29 21:26:41 -07002348 map = expand_xps_map(map, j, index, is_rxqs_map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002349 if (!map)
2350 goto error;
2351
Amritha Nambiar80d19662018-06-29 21:26:41 -07002352 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002353 }
2354
2355 if (!new_dev_maps)
2356 goto out_no_new_maps;
2357
Andrei Vagin4d99f662018-08-08 20:07:35 -07002358 static_key_slow_inc_cpuslocked(&xps_needed);
Amritha Nambiar04157462018-06-29 21:26:46 -07002359 if (is_rxqs_map)
Andrei Vagin4d99f662018-08-08 20:07:35 -07002360 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
Amritha Nambiar04157462018-06-29 21:26:46 -07002361
Amritha Nambiar80d19662018-06-29 21:26:41 -07002362 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2363 j < nr_ids;) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002364 /* copy maps belonging to foreign traffic classes */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002365 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002366 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002367 map = xmap_dereference(dev_maps->attr_map[tci]);
2368 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002369 }
2370
2371 /* We need to explicitly update tci as prevous loop
2372 * could break out early if dev_maps is NULL.
2373 */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002374 tci = j * num_tc + tc;
Alexander Duyck184c4492016-10-28 11:50:13 -04002375
Amritha Nambiar80d19662018-06-29 21:26:41 -07002376 if (netif_attr_test_mask(j, mask, nr_ids) &&
2377 netif_attr_test_online(j, online_mask, nr_ids)) {
2378 /* add tx-queue to CPU/rx-queue maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002379 int pos = 0;
2380
Amritha Nambiar80d19662018-06-29 21:26:41 -07002381 map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002382 while ((pos < map->len) && (map->queues[pos] != index))
2383 pos++;
2384
2385 if (pos == map->len)
2386 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002387#ifdef CONFIG_NUMA
Amritha Nambiar80d19662018-06-29 21:26:41 -07002388 if (!is_rxqs_map) {
2389 if (numa_node_id == -2)
2390 numa_node_id = cpu_to_node(j);
2391 else if (numa_node_id != cpu_to_node(j))
2392 numa_node_id = -1;
2393 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002394#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002395 } else if (dev_maps) {
2396 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002397 map = xmap_dereference(dev_maps->attr_map[tci]);
2398 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002399 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002400
Alexander Duyck184c4492016-10-28 11:50:13 -04002401 /* copy maps belonging to foreign traffic classes */
2402 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2403 /* fill in the new device map from the old device map */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002404 map = xmap_dereference(dev_maps->attr_map[tci]);
2405 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
Alexander Duyck184c4492016-10-28 11:50:13 -04002406 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002407 }
2408
Amritha Nambiar80d19662018-06-29 21:26:41 -07002409 if (is_rxqs_map)
2410 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2411 else
2412 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002413
Alexander Duyck537c00d2013-01-10 08:57:02 +00002414 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002415 if (!dev_maps)
2416 goto out_no_old_maps;
2417
Amritha Nambiar80d19662018-06-29 21:26:41 -07002418 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2419 j < nr_ids;) {
2420 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2421 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2422 map = xmap_dereference(dev_maps->attr_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002423 if (map && map != new_map)
2424 kfree_rcu(map, rcu);
2425 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002426 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002427
Alexander Duyck184c4492016-10-28 11:50:13 -04002428 kfree_rcu(dev_maps, rcu);
2429
2430out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002431 dev_maps = new_dev_maps;
2432 active = true;
2433
2434out_no_new_maps:
Amritha Nambiar80d19662018-06-29 21:26:41 -07002435 if (!is_rxqs_map) {
2436 /* update Tx queue numa node */
2437 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2438 (numa_node_id >= 0) ?
2439 numa_node_id : NUMA_NO_NODE);
2440 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002441
Alexander Duyck01c5f862013-01-10 08:57:35 +00002442 if (!dev_maps)
2443 goto out_no_maps;
2444
Amritha Nambiar80d19662018-06-29 21:26:41 -07002445 /* removes tx-queue from unused CPUs/rx-queues */
2446 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2447 j < nr_ids;) {
2448 for (i = tc, tci = j * num_tc; i--; tci++)
Alexander Duyck184c4492016-10-28 11:50:13 -04002449 active |= remove_xps_queue(dev_maps, tci, index);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002450 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2451 !netif_attr_test_online(j, online_mask, nr_ids))
Alexander Duyck184c4492016-10-28 11:50:13 -04002452 active |= remove_xps_queue(dev_maps, tci, index);
2453 for (i = num_tc - tc, tci++; --i; tci++)
2454 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002455 }
2456
2457 /* free map if not active */
2458 if (!active) {
Amritha Nambiar80d19662018-06-29 21:26:41 -07002459 if (is_rxqs_map)
2460 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2461 else
2462 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002463 kfree_rcu(dev_maps, rcu);
2464 }
2465
2466out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002467 mutex_unlock(&xps_map_mutex);
2468
2469 return 0;
2470error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002471 /* remove any maps that we added */
Amritha Nambiar80d19662018-06-29 21:26:41 -07002472 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2473 j < nr_ids;) {
2474 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2475 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
Alexander Duyck184c4492016-10-28 11:50:13 -04002476 map = dev_maps ?
Amritha Nambiar80d19662018-06-29 21:26:41 -07002477 xmap_dereference(dev_maps->attr_map[tci]) :
Alexander Duyck184c4492016-10-28 11:50:13 -04002478 NULL;
2479 if (new_map && new_map != map)
2480 kfree(new_map);
2481 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002482 }
2483
Alexander Duyck537c00d2013-01-10 08:57:02 +00002484 mutex_unlock(&xps_map_mutex);
2485
Alexander Duyck537c00d2013-01-10 08:57:02 +00002486 kfree(new_dev_maps);
2487 return -ENOMEM;
2488}
Andrei Vagin4d99f662018-08-08 20:07:35 -07002489EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
Amritha Nambiar80d19662018-06-29 21:26:41 -07002490
2491int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2492 u16 index)
2493{
Andrei Vagin4d99f662018-08-08 20:07:35 -07002494 int ret;
2495
2496 cpus_read_lock();
2497 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2498 cpus_read_unlock();
2499
2500 return ret;
Amritha Nambiar80d19662018-06-29 21:26:41 -07002501}
Alexander Duyck537c00d2013-01-10 08:57:02 +00002502EXPORT_SYMBOL(netif_set_xps_queue);
2503
2504#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002505static void netdev_unbind_all_sb_channels(struct net_device *dev)
2506{
2507 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2508
2509 /* Unbind any subordinate channels */
2510 while (txq-- != &dev->_tx[0]) {
2511 if (txq->sb_dev)
2512 netdev_unbind_sb_channel(dev, txq->sb_dev);
2513 }
2514}
2515
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002516void netdev_reset_tc(struct net_device *dev)
2517{
Alexander Duyck6234f872016-10-28 11:46:49 -04002518#ifdef CONFIG_XPS
2519 netif_reset_xps_queues_gt(dev, 0);
2520#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002521 netdev_unbind_all_sb_channels(dev);
2522
2523 /* Reset TC configuration of device */
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002524 dev->num_tc = 0;
2525 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2526 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2527}
2528EXPORT_SYMBOL(netdev_reset_tc);
2529
2530int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2531{
2532 if (tc >= dev->num_tc)
2533 return -EINVAL;
2534
Alexander Duyck6234f872016-10-28 11:46:49 -04002535#ifdef CONFIG_XPS
2536 netif_reset_xps_queues(dev, offset, count);
2537#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002538 dev->tc_to_txq[tc].count = count;
2539 dev->tc_to_txq[tc].offset = offset;
2540 return 0;
2541}
2542EXPORT_SYMBOL(netdev_set_tc_queue);
2543
2544int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2545{
2546 if (num_tc > TC_MAX_QUEUE)
2547 return -EINVAL;
2548
Alexander Duyck6234f872016-10-28 11:46:49 -04002549#ifdef CONFIG_XPS
2550 netif_reset_xps_queues_gt(dev, 0);
2551#endif
Alexander Duyckffcfe252018-07-09 12:19:38 -04002552 netdev_unbind_all_sb_channels(dev);
2553
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002554 dev->num_tc = num_tc;
2555 return 0;
2556}
2557EXPORT_SYMBOL(netdev_set_num_tc);
2558
Alexander Duyckffcfe252018-07-09 12:19:38 -04002559void netdev_unbind_sb_channel(struct net_device *dev,
2560 struct net_device *sb_dev)
2561{
2562 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2563
2564#ifdef CONFIG_XPS
2565 netif_reset_xps_queues_gt(sb_dev, 0);
2566#endif
2567 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2568 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2569
2570 while (txq-- != &dev->_tx[0]) {
2571 if (txq->sb_dev == sb_dev)
2572 txq->sb_dev = NULL;
2573 }
2574}
2575EXPORT_SYMBOL(netdev_unbind_sb_channel);
2576
2577int netdev_bind_sb_channel_queue(struct net_device *dev,
2578 struct net_device *sb_dev,
2579 u8 tc, u16 count, u16 offset)
2580{
2581 /* Make certain the sb_dev and dev are already configured */
2582 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2583 return -EINVAL;
2584
2585 /* We cannot hand out queues we don't have */
2586 if ((offset + count) > dev->real_num_tx_queues)
2587 return -EINVAL;
2588
2589 /* Record the mapping */
2590 sb_dev->tc_to_txq[tc].count = count;
2591 sb_dev->tc_to_txq[tc].offset = offset;
2592
2593 /* Provide a way for Tx queue to find the tc_to_txq map or
2594 * XPS map for itself.
2595 */
2596 while (count--)
2597 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2598
2599 return 0;
2600}
2601EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2602
2603int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2604{
2605 /* Do not use a multiqueue device to represent a subordinate channel */
2606 if (netif_is_multiqueue(dev))
2607 return -ENODEV;
2608
2609 /* We allow channels 1 - 32767 to be used for subordinate channels.
2610 * Channel 0 is meant to be "native" mode and used only to represent
2611 * the main root device. We allow writing 0 to reset the device back
2612 * to normal mode after being used as a subordinate channel.
2613 */
2614 if (channel > S16_MAX)
2615 return -EINVAL;
2616
2617 dev->num_tc = -channel;
2618
2619 return 0;
2620}
2621EXPORT_SYMBOL(netdev_set_sb_channel);
2622
John Fastabendf0796d52010-07-01 13:21:57 +00002623/*
2624 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
Gal Pressman3a053b12018-02-28 15:59:15 +02002625 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
John Fastabendf0796d52010-07-01 13:21:57 +00002626 */
Tom Herberte6484932010-10-18 18:04:39 +00002627int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002628{
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002629 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002630 int rc;
2631
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002632 disabling = txq < dev->real_num_tx_queues;
2633
Tom Herberte6484932010-10-18 18:04:39 +00002634 if (txq < 1 || txq > dev->num_tx_queues)
2635 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002636
Ben Hutchings5c565802011-02-15 19:39:21 +00002637 if (dev->reg_state == NETREG_REGISTERED ||
2638 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002639 ASSERT_RTNL();
2640
Tom Herbert1d24eb42010-11-21 13:17:27 +00002641 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2642 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002643 if (rc)
2644 return rc;
2645
John Fastabend4f57c082011-01-17 08:06:04 +00002646 if (dev->num_tc)
2647 netif_setup_tc(dev, txq);
2648
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002649 dev->real_num_tx_queues = txq;
2650
2651 if (disabling) {
2652 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002653 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002654#ifdef CONFIG_XPS
2655 netif_reset_xps_queues_gt(dev, txq);
2656#endif
2657 }
Jakub Kicinskiac5b7012018-02-12 21:35:31 -08002658 } else {
2659 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002660 }
Tom Herberte6484932010-10-18 18:04:39 +00002661
Tom Herberte6484932010-10-18 18:04:39 +00002662 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002663}
2664EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002665
Michael Daltona953be52014-01-16 22:23:28 -08002666#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002667/**
2668 * netif_set_real_num_rx_queues - set actual number of RX queues used
2669 * @dev: Network device
2670 * @rxq: Actual number of RX queues
2671 *
2672 * This must be called either with the rtnl_lock held or before
2673 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002674 * negative error code. If called before registration, it always
2675 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002676 */
2677int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2678{
2679 int rc;
2680
Tom Herbertbd25fa72010-10-18 18:00:16 +00002681 if (rxq < 1 || rxq > dev->num_rx_queues)
2682 return -EINVAL;
2683
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002684 if (dev->reg_state == NETREG_REGISTERED) {
2685 ASSERT_RTNL();
2686
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002687 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2688 rxq);
2689 if (rc)
2690 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002691 }
2692
2693 dev->real_num_rx_queues = rxq;
2694 return 0;
2695}
2696EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2697#endif
2698
Ben Hutchings2c530402012-07-10 10:55:09 +00002699/**
2700 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002701 *
2702 * This routine should set an upper limit on the number of RSS queues
2703 * used by default by multiqueue devices.
2704 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002705int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002706{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302707 return is_kdump_kernel() ?
2708 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002709}
2710EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2711
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002712static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002713{
2714 struct softnet_data *sd;
2715 unsigned long flags;
2716
2717 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002718 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002719 q->next_sched = NULL;
2720 *sd->output_queue_tailp = q;
2721 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002722 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2723 local_irq_restore(flags);
2724}
2725
David S. Miller37437bb2008-07-16 02:15:04 -07002726void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002727{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002728 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2729 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002730}
2731EXPORT_SYMBOL(__netif_schedule);
2732
Eric Dumazete6247022013-12-05 04:45:08 -08002733struct dev_kfree_skb_cb {
2734 enum skb_free_reason reason;
2735};
2736
2737static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002738{
Eric Dumazete6247022013-12-05 04:45:08 -08002739 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002740}
Denis Vlasenko56079432006-03-29 15:57:29 -08002741
John Fastabend46e5da40a2014-09-12 20:04:52 -07002742void netif_schedule_queue(struct netdev_queue *txq)
2743{
2744 rcu_read_lock();
2745 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2746 struct Qdisc *q = rcu_dereference(txq->qdisc);
2747
2748 __netif_schedule(q);
2749 }
2750 rcu_read_unlock();
2751}
2752EXPORT_SYMBOL(netif_schedule_queue);
2753
John Fastabend46e5da40a2014-09-12 20:04:52 -07002754void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2755{
2756 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2757 struct Qdisc *q;
2758
2759 rcu_read_lock();
2760 q = rcu_dereference(dev_queue->qdisc);
2761 __netif_schedule(q);
2762 rcu_read_unlock();
2763 }
2764}
2765EXPORT_SYMBOL(netif_tx_wake_queue);
2766
Eric Dumazete6247022013-12-05 04:45:08 -08002767void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2768{
2769 unsigned long flags;
2770
Myungho Jung98998862017-04-25 11:58:15 -07002771 if (unlikely(!skb))
2772 return;
2773
Reshetova, Elena63354792017-06-30 13:07:58 +03002774 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08002775 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03002776 refcount_set(&skb->users, 0);
2777 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08002778 return;
2779 }
2780 get_kfree_skb_cb(skb)->reason = reason;
2781 local_irq_save(flags);
2782 skb->next = __this_cpu_read(softnet_data.completion_queue);
2783 __this_cpu_write(softnet_data.completion_queue, skb);
2784 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2785 local_irq_restore(flags);
2786}
2787EXPORT_SYMBOL(__dev_kfree_skb_irq);
2788
2789void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08002790{
2791 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08002792 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08002793 else
2794 dev_kfree_skb(skb);
2795}
Eric Dumazete6247022013-12-05 04:45:08 -08002796EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08002797
2798
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002799/**
2800 * netif_device_detach - mark device as removed
2801 * @dev: network device
2802 *
2803 * Mark device as removed from system and therefore no longer available.
2804 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002805void netif_device_detach(struct net_device *dev)
2806{
2807 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2808 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002809 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002810 }
2811}
2812EXPORT_SYMBOL(netif_device_detach);
2813
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002814/**
2815 * netif_device_attach - mark device as attached
2816 * @dev: network device
2817 *
2818 * Mark device as attached from system and restart if needed.
2819 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002820void netif_device_attach(struct net_device *dev)
2821{
2822 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2823 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002824 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002825 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002826 }
2827}
2828EXPORT_SYMBOL(netif_device_attach);
2829
Jiri Pirko5605c762015-05-12 14:56:12 +02002830/*
2831 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2832 * to be used as a distribution range.
2833 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04002834static u16 skb_tx_hash(const struct net_device *dev,
2835 const struct net_device *sb_dev,
2836 struct sk_buff *skb)
Jiri Pirko5605c762015-05-12 14:56:12 +02002837{
2838 u32 hash;
2839 u16 qoffset = 0;
Alexander Duyck1b837d42018-04-27 14:06:53 -04002840 u16 qcount = dev->real_num_tx_queues;
Jiri Pirko5605c762015-05-12 14:56:12 +02002841
Alexander Duyckeadec8772018-07-09 12:19:48 -04002842 if (dev->num_tc) {
2843 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2844
2845 qoffset = sb_dev->tc_to_txq[tc].offset;
2846 qcount = sb_dev->tc_to_txq[tc].count;
2847 }
2848
Jiri Pirko5605c762015-05-12 14:56:12 +02002849 if (skb_rx_queue_recorded(skb)) {
2850 hash = skb_get_rx_queue(skb);
Alexander Duyck1b837d42018-04-27 14:06:53 -04002851 while (unlikely(hash >= qcount))
2852 hash -= qcount;
Alexander Duyckeadec8772018-07-09 12:19:48 -04002853 return hash + qoffset;
Jiri Pirko5605c762015-05-12 14:56:12 +02002854 }
2855
2856 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2857}
Jiri Pirko5605c762015-05-12 14:56:12 +02002858
Ben Hutchings36c92472012-01-17 07:57:56 +00002859static void skb_warn_bad_offload(const struct sk_buff *skb)
2860{
Wei Tang84d15ae2016-06-16 21:17:49 +08002861 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00002862 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01002863 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00002864
Ben Greearc846ad92013-04-19 10:45:52 +00002865 if (!net_ratelimit())
2866 return;
2867
Bjørn Mork88ad4172015-11-16 19:16:40 +01002868 if (dev) {
2869 if (dev->dev.parent)
2870 name = dev_driver_string(dev->dev.parent);
2871 else
2872 name = netdev_name(dev);
2873 }
Ben Hutchings36c92472012-01-17 07:57:56 +00002874 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2875 "gso_type=%d ip_summed=%d\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01002876 name, dev ? &dev->features : &null_features,
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002877 skb->sk ? &skb->sk->sk_route_caps : &null_features,
Ben Hutchings36c92472012-01-17 07:57:56 +00002878 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2879 skb_shinfo(skb)->gso_type, skb->ip_summed);
2880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/*
2883 * Invalidate hardware checksum when packet is to be mangled, and
2884 * complete checksum manually on outgoing path.
2885 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002886int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Al Virod3bc23e2006-11-14 21:24:49 -08002888 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07002889 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Patrick McHardy84fa7932006-08-29 16:44:56 -07002891 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07002892 goto out_set_summed;
2893
2894 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00002895 skb_warn_bad_offload(skb);
2896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898
Eric Dumazetcef401d2013-01-25 20:34:37 +00002899 /* Before computing a checksum, we should make sure no frag could
2900 * be modified by an external entity : checksum could be wrong.
2901 */
2902 if (skb_has_shared_frag(skb)) {
2903 ret = __skb_linearize(skb);
2904 if (ret)
2905 goto out;
2906 }
2907
Michał Mirosław55508d62010-12-14 15:24:08 +00002908 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07002909 BUG_ON(offset >= skb_headlen(skb));
2910 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2911
2912 offset += skb->csum_offset;
2913 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2914
2915 if (skb_cloned(skb) &&
2916 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2918 if (ret)
2919 goto out;
2920 }
2921
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07002922 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07002923out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002925out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return ret;
2927}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002928EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Davide Carattib72b5bf2017-05-18 15:44:38 +02002930int skb_crc32c_csum_help(struct sk_buff *skb)
2931{
2932 __le32 crc32c_csum;
2933 int ret = 0, offset, start;
2934
2935 if (skb->ip_summed != CHECKSUM_PARTIAL)
2936 goto out;
2937
2938 if (unlikely(skb_is_gso(skb)))
2939 goto out;
2940
2941 /* Before computing a checksum, we should make sure no frag could
2942 * be modified by an external entity : checksum could be wrong.
2943 */
2944 if (unlikely(skb_has_shared_frag(skb))) {
2945 ret = __skb_linearize(skb);
2946 if (ret)
2947 goto out;
2948 }
2949 start = skb_checksum_start_offset(skb);
2950 offset = start + offsetof(struct sctphdr, checksum);
2951 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
2952 ret = -EINVAL;
2953 goto out;
2954 }
2955 if (skb_cloned(skb) &&
2956 !skb_clone_writable(skb, offset + sizeof(__le32))) {
2957 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2958 if (ret)
2959 goto out;
2960 }
2961 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
2962 skb->len - start, ~(__u32)0,
2963 crc32c_csum_stub));
2964 *(__le32 *)(skb->data + offset) = crc32c_csum;
2965 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02002966 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02002967out:
2968 return ret;
2969}
2970
Vlad Yasevich53d64712014-03-27 17:26:18 -04002971__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002972{
2973 __be16 type = skb->protocol;
2974
Pravin B Shelar19acc322013-05-07 20:41:07 +00002975 /* Tunnel gso handlers can set protocol to ethernet. */
2976 if (type == htons(ETH_P_TEB)) {
2977 struct ethhdr *eth;
2978
2979 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2980 return 0;
2981
Eric Dumazet1dfe82e2018-03-26 08:08:07 -07002982 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00002983 type = eth->h_proto;
2984 }
2985
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09002986 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002987}
2988
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002989/**
2990 * skb_mac_gso_segment - mac layer segmentation handler.
2991 * @skb: buffer to segment
2992 * @features: features for the output path (see dev->features)
2993 */
2994struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2995 netdev_features_t features)
2996{
2997 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2998 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002999 int vlan_depth = skb->mac_len;
3000 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003001
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003002 if (unlikely(!type))
3003 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003004
Vlad Yasevich53d64712014-03-27 17:26:18 -04003005 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003006
3007 rcu_read_lock();
3008 list_for_each_entry_rcu(ptype, &offload_base, list) {
3009 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003010 segs = ptype->callbacks.gso_segment(skb, features);
3011 break;
3012 }
3013 }
3014 rcu_read_unlock();
3015
3016 __skb_push(skb, skb->data - skb_mac_header(skb));
3017
3018 return segs;
3019}
3020EXPORT_SYMBOL(skb_mac_gso_segment);
3021
3022
Cong Wang12b00042013-02-05 16:36:38 +00003023/* openvswitch calls this on rx path, so we need a different check.
3024 */
3025static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3026{
3027 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05003028 return skb->ip_summed != CHECKSUM_PARTIAL &&
3029 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08003030
3031 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00003032}
3033
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003034/**
Cong Wang12b00042013-02-05 16:36:38 +00003035 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003036 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003037 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00003038 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003039 *
3040 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07003041 *
3042 * It may return NULL if the skb requires no segmentation. This is
3043 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003044 *
3045 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003046 */
Cong Wang12b00042013-02-05 16:36:38 +00003047struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3048 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003049{
Eric Dumazetb2504a52017-01-31 10:20:32 -08003050 struct sk_buff *segs;
3051
Cong Wang12b00042013-02-05 16:36:38 +00003052 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003053 int err;
3054
Eric Dumazetb2504a52017-01-31 10:20:32 -08003055 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02003056 err = skb_cow_head(skb, 0);
3057 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07003058 return ERR_PTR(err);
3059 }
3060
Alexander Duyck802ab552016-04-10 21:45:03 -04003061 /* Only report GSO partial support if it will enable us to
3062 * support segmentation on this frame without needing additional
3063 * work.
3064 */
3065 if (features & NETIF_F_GSO_PARTIAL) {
3066 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3067 struct net_device *dev = skb->dev;
3068
3069 partial_features |= dev->features & dev->gso_partial_features;
3070 if (!skb_gso_ok(skb, features | partial_features))
3071 features &= ~NETIF_F_GSO_PARTIAL;
3072 }
3073
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03003074 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3075 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3076
Pravin B Shelar68c33162013-02-14 14:02:41 +00003077 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07003078 SKB_GSO_CB(skb)->encap_level = 0;
3079
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00003080 skb_reset_mac_header(skb);
3081 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003082
Eric Dumazetb2504a52017-01-31 10:20:32 -08003083 segs = skb_mac_gso_segment(skb, features);
3084
Willem de Bruijn8d74e9f2017-12-12 11:39:04 -05003085 if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08003086 skb_warn_bad_offload(skb);
3087
3088 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003089}
Cong Wang12b00042013-02-05 16:36:38 +00003090EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003091
Herbert Xufb286bb2005-11-10 13:01:24 -08003092/* Take action when hardware reception checksum errors are detected. */
3093#ifdef CONFIG_BUG
3094void netdev_rx_csum_fault(struct net_device *dev)
3095{
3096 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00003097 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08003098 dump_stack();
3099 }
3100}
3101EXPORT_SYMBOL(netdev_rx_csum_fault);
3102#endif
3103
Christoph Hellwigab74cfe2018-04-03 20:31:35 +02003104/* XXX: check that highmem exists at all on the given machine. */
Florian Westphalc1e756b2014-05-05 15:00:44 +02003105static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Herbert Xu3d3a8532006-06-27 13:33:10 -07003107#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 int i;
tchardingf4563a72017-02-09 17:56:07 +11003109
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003110 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003111 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3112 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11003113
Ian Campbellea2ab692011-08-22 23:44:58 +00003114 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003115 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00003116 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00003117 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07003118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return 0;
3120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Simon Horman3b392dd2014-06-04 08:53:17 +09003122/* If MPLS offload request, verify we are testing hardware MPLS features
3123 * instead of standard features for the netdev.
3124 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08003125#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09003126static netdev_features_t net_mpls_features(struct sk_buff *skb,
3127 netdev_features_t features,
3128 __be16 type)
3129{
Simon Horman25cd9ba2014-10-06 05:05:13 -07003130 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09003131 features &= skb->dev->mpls_features;
3132
3133 return features;
3134}
3135#else
3136static netdev_features_t net_mpls_features(struct sk_buff *skb,
3137 netdev_features_t features,
3138 __be16 type)
3139{
3140 return features;
3141}
3142#endif
3143
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003144static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02003145 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00003146{
Vlad Yasevich53d64712014-03-27 17:26:18 -04003147 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09003148 __be16 type;
3149
3150 type = skb_network_protocol(skb, &tmp);
3151 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003152
Ed Cashinc0d680e2012-09-19 15:49:00 +00003153 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09003154 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07003155 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00003156 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08003157 if (illegal_highdma(skb->dev, skb))
3158 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00003159
3160 return features;
3161}
3162
Toshiaki Makitae38f3022015-03-27 14:31:13 +09003163netdev_features_t passthru_features_check(struct sk_buff *skb,
3164 struct net_device *dev,
3165 netdev_features_t features)
3166{
3167 return features;
3168}
3169EXPORT_SYMBOL(passthru_features_check);
3170
Toshiaki Makita7ce23672018-04-17 18:46:14 +09003171static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003172 struct net_device *dev,
3173 netdev_features_t features)
3174{
3175 return vlan_features_check(skb, features);
3176}
3177
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003178static netdev_features_t gso_features_check(const struct sk_buff *skb,
3179 struct net_device *dev,
3180 netdev_features_t features)
3181{
3182 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3183
3184 if (gso_segs > dev->gso_max_segs)
3185 return features & ~NETIF_F_GSO_MASK;
3186
Alexander Duyck802ab552016-04-10 21:45:03 -04003187 /* Support for GSO partial features requires software
3188 * intervention before we can actually process the packets
3189 * so we need to strip support for any partial features now
3190 * and we can pull them back in after we have partially
3191 * segmented the frame.
3192 */
3193 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3194 features &= ~dev->gso_partial_features;
3195
3196 /* Make sure to clear the IPv4 ID mangling feature if the
3197 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003198 */
3199 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3200 struct iphdr *iph = skb->encapsulation ?
3201 inner_ip_hdr(skb) : ip_hdr(skb);
3202
3203 if (!(iph->frag_off & htons(IP_DF)))
3204 features &= ~NETIF_F_TSO_MANGLEID;
3205 }
3206
3207 return features;
3208}
3209
Florian Westphalc1e756b2014-05-05 15:00:44 +02003210netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00003211{
Jesse Gross5f352272014-12-23 22:37:26 -08003212 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07003213 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00003214
Alexander Duyckcbc53e02016-04-10 21:44:51 -04003215 if (skb_is_gso(skb))
3216 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00003217
Jesse Gross5f352272014-12-23 22:37:26 -08003218 /* If encapsulation offload request, verify we are testing
3219 * hardware encapsulation features instead of standard
3220 * features for the netdev
3221 */
3222 if (skb->encapsulation)
3223 features &= dev->hw_enc_features;
3224
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09003225 if (skb_vlan_tagged(skb))
3226 features = netdev_intersect_features(features,
3227 dev->vlan_features |
3228 NETIF_F_HW_VLAN_CTAG_TX |
3229 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00003230
Jesse Gross5f352272014-12-23 22:37:26 -08003231 if (dev->netdev_ops->ndo_features_check)
3232 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3233 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003234 else
3235 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003236
Florian Westphalc1e756b2014-05-05 15:00:44 +02003237 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003238}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003239EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003240
David S. Miller2ea25512014-08-29 21:10:01 -07003241static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003242 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003243{
David S. Miller2ea25512014-08-29 21:10:01 -07003244 unsigned int len;
3245 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003246
Maciej W. Rozycki9f9a7422018-10-09 23:57:49 +01003247 if (dev_nit_active(dev))
David S. Miller2ea25512014-08-29 21:10:01 -07003248 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003249
David S. Miller2ea25512014-08-29 21:10:01 -07003250 len = skb->len;
3251 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003252 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003253 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003254
Patrick McHardy572a9d72009-11-10 06:14:14 +00003255 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003256}
David S. Miller2ea25512014-08-29 21:10:01 -07003257
David S. Miller8dcda222014-09-01 15:06:40 -07003258struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3259 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003260{
3261 struct sk_buff *skb = first;
3262 int rc = NETDEV_TX_OK;
3263
3264 while (skb) {
3265 struct sk_buff *next = skb->next;
3266
David S. Millera8305bf2018-07-29 20:42:53 -07003267 skb_mark_not_on_list(skb);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003268 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003269 if (unlikely(!dev_xmit_complete(rc))) {
3270 skb->next = next;
3271 goto out;
3272 }
3273
3274 skb = next;
Eric Dumazetfe60faa2018-10-31 08:39:13 -07003275 if (netif_tx_queue_stopped(txq) && skb) {
David S. Miller7f2e8702014-08-29 21:19:14 -07003276 rc = NETDEV_TX_BUSY;
3277 break;
3278 }
3279 }
3280
3281out:
3282 *ret = rc;
3283 return skb;
3284}
3285
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003286static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3287 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003288{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003289 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003290 !vlan_hw_offload_capable(features, skb->vlan_proto))
3291 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003292 return skb;
3293}
3294
Davide Caratti43c26a12017-05-18 15:44:41 +02003295int skb_csum_hwoffload_help(struct sk_buff *skb,
3296 const netdev_features_t features)
3297{
3298 if (unlikely(skb->csum_not_inet))
3299 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3300 skb_crc32c_csum_help(skb);
3301
3302 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3303}
3304EXPORT_SYMBOL(skb_csum_hwoffload_help);
3305
Steffen Klassertf53c7232017-12-20 10:41:36 +01003306static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003307{
3308 netdev_features_t features;
3309
David S. Millereae3f882014-08-30 15:17:13 -07003310 features = netif_skb_features(skb);
3311 skb = validate_xmit_vlan(skb, features);
3312 if (unlikely(!skb))
3313 goto out_null;
3314
Ilya Lesokhinebf4e802018-04-30 10:16:12 +03003315 skb = sk_validate_xmit_skb(skb, dev);
3316 if (unlikely(!skb))
3317 goto out_null;
3318
Johannes Berg8b86a612015-04-17 15:45:04 +02003319 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003320 struct sk_buff *segs;
3321
3322 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003323 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003324 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003325 } else if (segs) {
3326 consume_skb(skb);
3327 skb = segs;
3328 }
David S. Millereae3f882014-08-30 15:17:13 -07003329 } else {
3330 if (skb_needs_linearize(skb, features) &&
3331 __skb_linearize(skb))
3332 goto out_kfree_skb;
3333
3334 /* If packet is not checksummed and device does not
3335 * support checksumming for this protocol, complete
3336 * checksumming here.
3337 */
3338 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3339 if (skb->encapsulation)
3340 skb_set_inner_transport_header(skb,
3341 skb_checksum_start_offset(skb));
3342 else
3343 skb_set_transport_header(skb,
3344 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003345 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003346 goto out_kfree_skb;
3347 }
3348 }
3349
Steffen Klassertf53c7232017-12-20 10:41:36 +01003350 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003351
David S. Millereae3f882014-08-30 15:17:13 -07003352 return skb;
3353
3354out_kfree_skb:
3355 kfree_skb(skb);
3356out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003357 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003358 return NULL;
3359}
3360
Steffen Klassertf53c7232017-12-20 10:41:36 +01003361struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003362{
3363 struct sk_buff *next, *head = NULL, *tail;
3364
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003365 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003366 next = skb->next;
David S. Millera8305bf2018-07-29 20:42:53 -07003367 skb_mark_not_on_list(skb);
Eric Dumazet55a93b32014-10-03 15:31:07 -07003368
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003369 /* in case skb wont be segmented, point to itself */
3370 skb->prev = skb;
3371
Steffen Klassertf53c7232017-12-20 10:41:36 +01003372 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003373 if (!skb)
3374 continue;
3375
3376 if (!head)
3377 head = skb;
3378 else
3379 tail->next = skb;
3380 /* If skb was segmented, skb->prev points to
3381 * the last segment. If not, it still contains skb.
3382 */
3383 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003384 }
3385 return head;
3386}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003387EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003388
Eric Dumazet1def9232013-01-10 12:36:42 +00003389static void qdisc_pkt_len_init(struct sk_buff *skb)
3390{
3391 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3392
3393 qdisc_skb_cb(skb)->pkt_len = skb->len;
3394
3395 /* To get more precise estimation of bytes sent on wire,
3396 * we add to pkt_len the headers size of all segments
3397 */
3398 if (shinfo->gso_size) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003399 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003400 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003401
Eric Dumazet757b8b12013-01-15 21:14:21 -08003402 /* mac layer + network layer */
3403 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3404
3405 /* + transport layer */
Eric Dumazet7c68d1a2018-01-18 19:59:19 -08003406 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3407 const struct tcphdr *th;
3408 struct tcphdr _tcphdr;
3409
3410 th = skb_header_pointer(skb, skb_transport_offset(skb),
3411 sizeof(_tcphdr), &_tcphdr);
3412 if (likely(th))
3413 hdr_len += __tcp_hdrlen(th);
3414 } else {
3415 struct udphdr _udphdr;
3416
3417 if (skb_header_pointer(skb, skb_transport_offset(skb),
3418 sizeof(_udphdr), &_udphdr))
3419 hdr_len += sizeof(struct udphdr);
3420 }
Jason Wang15e5a032013-03-25 20:19:59 +00003421
3422 if (shinfo->gso_type & SKB_GSO_DODGY)
3423 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3424 shinfo->gso_size);
3425
3426 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003427 }
3428}
3429
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003430static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3431 struct net_device *dev,
3432 struct netdev_queue *txq)
3433{
3434 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003435 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003436 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003437 int rc;
3438
Eric Dumazeta2da5702011-01-20 03:48:19 +00003439 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003440
3441 if (q->flags & TCQ_F_NOLOCK) {
3442 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3443 __qdisc_drop(skb, &to_free);
3444 rc = NET_XMIT_DROP;
3445 } else {
3446 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Paolo Abeni32f7b442018-05-15 10:50:31 +02003447 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003448 }
3449
3450 if (unlikely(to_free))
3451 kfree_skb_list(to_free);
3452 return rc;
3453 }
3454
Eric Dumazet79640a42010-06-02 05:09:29 -07003455 /*
3456 * Heuristic to force contended enqueues to serialize on a
3457 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003458 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003459 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003460 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003461 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003462 if (unlikely(contended))
3463 spin_lock(&q->busylock);
3464
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003465 spin_lock(root_lock);
3466 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003467 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003468 rc = NET_XMIT_DROP;
3469 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003470 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003471 /*
3472 * This is a work-conserving queue; there are no old skbs
3473 * waiting to be sent out; and the qdisc is not running -
3474 * xmit the skb directly.
3475 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003476
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003477 qdisc_bstats_update(q, skb);
3478
Eric Dumazet55a93b32014-10-03 15:31:07 -07003479 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003480 if (unlikely(contended)) {
3481 spin_unlock(&q->busylock);
3482 contended = false;
3483 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003484 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003485 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003486
John Fastabend6c148182017-12-07 09:54:06 -08003487 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003488 rc = NET_XMIT_SUCCESS;
3489 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003490 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003491 if (qdisc_run_begin(q)) {
3492 if (unlikely(contended)) {
3493 spin_unlock(&q->busylock);
3494 contended = false;
3495 }
3496 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003497 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003498 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003499 }
3500 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003501 if (unlikely(to_free))
3502 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003503 if (unlikely(contended))
3504 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003505 return rc;
3506}
3507
Daniel Borkmann86f85152013-12-29 17:27:11 +01003508#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003509static void skb_update_prio(struct sk_buff *skb)
3510{
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003511 const struct netprio_map *map;
3512 const struct sock *sk;
3513 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003514
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003515 if (skb->priority)
3516 return;
3517 map = rcu_dereference_bh(skb->dev->priomap);
3518 if (!map)
3519 return;
3520 sk = skb_to_full_sk(skb);
3521 if (!sk)
3522 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003523
Eric Dumazet4dcb31d2018-03-14 09:04:16 -07003524 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3525
3526 if (prioidx < map->priomap_len)
3527 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003528}
3529#else
3530#define skb_update_prio(skb)
3531#endif
3532
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +02003533DEFINE_PER_CPU(int, xmit_recursion);
3534EXPORT_SYMBOL(xmit_recursion);
3535
Dave Jonesd29f7492008-07-22 14:09:06 -07003536/**
Michel Machado95603e22012-06-12 10:16:35 +00003537 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003538 * @net: network namespace this loopback is happening in
3539 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003540 * @skb: buffer to transmit
3541 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003542int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003543{
3544 skb_reset_mac_header(skb);
3545 __skb_pull(skb, skb_network_offset(skb));
3546 skb->pkt_type = PACKET_LOOPBACK;
3547 skb->ip_summed = CHECKSUM_UNNECESSARY;
3548 WARN_ON(!skb_dst(skb));
3549 skb_dst_force(skb);
3550 netif_rx_ni(skb);
3551 return 0;
3552}
3553EXPORT_SYMBOL(dev_loopback_xmit);
3554
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003555#ifdef CONFIG_NET_EGRESS
3556static struct sk_buff *
3557sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3558{
Jiri Pirko46209402017-11-03 11:46:25 +01003559 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003560 struct tcf_result cl_res;
3561
Jiri Pirko46209402017-11-03 11:46:25 +01003562 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003563 return skb;
3564
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003565 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Jiri Pirko46209402017-11-03 11:46:25 +01003566 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003567
Jiri Pirko46209402017-11-03 11:46:25 +01003568 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003569 case TC_ACT_OK:
3570 case TC_ACT_RECLASSIFY:
3571 skb->tc_index = TC_H_MIN(cl_res.classid);
3572 break;
3573 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003574 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003575 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003576 kfree_skb(skb);
3577 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003578 case TC_ACT_STOLEN:
3579 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003580 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003581 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003582 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003583 return NULL;
3584 case TC_ACT_REDIRECT:
3585 /* No need to push/pop skb's mac_header here on egress! */
3586 skb_do_redirect(skb);
3587 *ret = NET_XMIT_SUCCESS;
3588 return NULL;
3589 default:
3590 break;
3591 }
3592
3593 return skb;
3594}
3595#endif /* CONFIG_NET_EGRESS */
3596
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003597#ifdef CONFIG_XPS
3598static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3599 struct xps_dev_maps *dev_maps, unsigned int tci)
3600{
3601 struct xps_map *map;
3602 int queue_index = -1;
3603
3604 if (dev->num_tc) {
3605 tci *= dev->num_tc;
3606 tci += netdev_get_prio_tc_map(dev, skb->priority);
3607 }
3608
3609 map = rcu_dereference(dev_maps->attr_map[tci]);
3610 if (map) {
3611 if (map->len == 1)
3612 queue_index = map->queues[0];
3613 else
3614 queue_index = map->queues[reciprocal_scale(
3615 skb_get_hash(skb), map->len)];
3616 if (unlikely(queue_index >= dev->real_num_tx_queues))
3617 queue_index = -1;
3618 }
3619 return queue_index;
3620}
3621#endif
3622
Alexander Duyckeadec8772018-07-09 12:19:48 -04003623static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3624 struct sk_buff *skb)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003625{
3626#ifdef CONFIG_XPS
3627 struct xps_dev_maps *dev_maps;
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003628 struct sock *sk = skb->sk;
Jiri Pirko638b2a62015-05-12 14:56:13 +02003629 int queue_index = -1;
3630
Amritha Nambiar04157462018-06-29 21:26:46 -07003631 if (!static_key_false(&xps_needed))
3632 return -1;
3633
Jiri Pirko638b2a62015-05-12 14:56:13 +02003634 rcu_read_lock();
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003635 if (!static_key_false(&xps_rxqs_needed))
3636 goto get_cpus_map;
3637
Alexander Duyckeadec8772018-07-09 12:19:48 -04003638 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003639 if (dev_maps) {
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003640 int tci = sk_rx_queue_get(sk);
Alexander Duyck184c4492016-10-28 11:50:13 -04003641
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003642 if (tci >= 0 && tci < dev->num_rx_queues)
3643 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3644 tci);
3645 }
Alexander Duyck184c4492016-10-28 11:50:13 -04003646
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003647get_cpus_map:
3648 if (queue_index < 0) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003649 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
Amritha Nambiarfc9bab22018-06-29 21:27:02 -07003650 if (dev_maps) {
3651 unsigned int tci = skb->sender_cpu - 1;
3652
3653 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3654 tci);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003655 }
3656 }
3657 rcu_read_unlock();
3658
3659 return queue_index;
3660#else
3661 return -1;
3662#endif
3663}
3664
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003665u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -04003666 struct net_device *sb_dev,
3667 select_queue_fallback_t fallback)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003668{
3669 return 0;
3670}
3671EXPORT_SYMBOL(dev_pick_tx_zero);
3672
3673u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -04003674 struct net_device *sb_dev,
3675 select_queue_fallback_t fallback)
Alexander Duycka4ea8a32018-07-09 12:19:54 -04003676{
3677 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3678}
3679EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3680
Alexander Duyck8ec56fc2018-07-09 12:20:04 -04003681static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3682 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003683{
3684 struct sock *sk = skb->sk;
3685 int queue_index = sk_tx_queue_get(sk);
3686
Alexander Duyckeadec8772018-07-09 12:19:48 -04003687 sb_dev = sb_dev ? : dev;
3688
Jiri Pirko638b2a62015-05-12 14:56:13 +02003689 if (queue_index < 0 || skb->ooo_okay ||
3690 queue_index >= dev->real_num_tx_queues) {
Alexander Duyckeadec8772018-07-09 12:19:48 -04003691 int new_index = get_xps_queue(dev, sb_dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003692
Jiri Pirko638b2a62015-05-12 14:56:13 +02003693 if (new_index < 0)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003694 new_index = skb_tx_hash(dev, sb_dev, skb);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003695
3696 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003697 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003698 rcu_access_pointer(sk->sk_dst_cache))
3699 sk_tx_queue_set(sk, new_index);
3700
3701 queue_index = new_index;
3702 }
3703
3704 return queue_index;
3705}
3706
3707struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3708 struct sk_buff *skb,
Alexander Duyckeadec8772018-07-09 12:19:48 -04003709 struct net_device *sb_dev)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003710{
3711 int queue_index = 0;
3712
3713#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003714 u32 sender_cpu = skb->sender_cpu - 1;
3715
3716 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003717 skb->sender_cpu = raw_smp_processor_id() + 1;
3718#endif
3719
3720 if (dev->real_num_tx_queues != 1) {
3721 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003722
Jiri Pirko638b2a62015-05-12 14:56:13 +02003723 if (ops->ndo_select_queue)
Alexander Duyckeadec8772018-07-09 12:19:48 -04003724 queue_index = ops->ndo_select_queue(dev, skb, sb_dev,
Jiri Pirko638b2a62015-05-12 14:56:13 +02003725 __netdev_pick_tx);
3726 else
Alexander Duyck8ec56fc2018-07-09 12:20:04 -04003727 queue_index = __netdev_pick_tx(dev, skb, sb_dev);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003728
Alexander Duyckd5845272017-11-22 10:57:41 -08003729 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003730 }
3731
3732 skb_set_queue_mapping(skb, queue_index);
3733 return netdev_get_tx_queue(dev, queue_index);
3734}
3735
Michel Machado95603e22012-06-12 10:16:35 +00003736/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003737 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003738 * @skb: buffer to transmit
Alexander Duyckeadec8772018-07-09 12:19:48 -04003739 * @sb_dev: suboordinate device used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003740 *
3741 * Queue a buffer for transmission to a network device. The caller must
3742 * have set the device and priority and built the buffer before calling
3743 * this function. The function can be called from an interrupt.
3744 *
3745 * A negative errno code is returned on a failure. A success does not
3746 * guarantee the frame will be transmitted as it may be dropped due
3747 * to congestion or traffic shaping.
3748 *
3749 * -----------------------------------------------------------------------------------
3750 * I notice this method can also return errors from the queue disciplines,
3751 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3752 * be positive.
3753 *
3754 * Regardless of the return value, the skb is consumed, so it is currently
3755 * difficult to retry a send to this method. (You can bump the ref count
3756 * before sending to hold a reference for retry if you are careful.)
3757 *
3758 * When calling this method, interrupts MUST be enabled. This is because
3759 * the BH enable code must have IRQs enabled so that it will not deadlock.
3760 * --BLG
3761 */
Alexander Duyckeadec8772018-07-09 12:19:48 -04003762static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
3764 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003765 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 struct Qdisc *q;
3767 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01003768 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003770 skb_reset_mac_header(skb);
3771
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003772 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3773 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3774
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003775 /* Disable soft irqs for various locks below. Also
3776 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003778 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Neil Horman5bc14212011-11-22 05:10:51 +00003780 skb_update_prio(skb);
3781
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003782 qdisc_pkt_len_init(skb);
3783#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003784 skb->tc_at_ingress = 0;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003785# ifdef CONFIG_NET_EGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07003786 if (static_branch_unlikely(&egress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003787 skb = sch_handle_egress(skb, &rc, dev);
3788 if (!skb)
3789 goto out;
3790 }
3791# endif
3792#endif
Eric Dumazet02875872014-10-05 18:38:35 -07003793 /* If device/qdisc don't need skb->dst, release it right now while
3794 * its hot in this cpu cache.
3795 */
3796 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3797 skb_dst_drop(skb);
3798 else
3799 skb_dst_force(skb);
3800
Alexander Duyckeadec8772018-07-09 12:19:48 -04003801 txq = netdev_pick_tx(dev, skb, sb_dev);
Paul E. McKenneya898def2010-02-22 17:04:49 -08003802 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07003803
Koki Sanagicf66ba52010-08-23 18:45:02 +09003804 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003806 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07003807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809
3810 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11003811 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
tchardingeb13da12017-02-09 17:56:06 +11003813 * Really, it is unlikely that netif_tx_lock protection is necessary
3814 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3815 * counters.)
3816 * However, it is possible, that they rely on protection
3817 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
tchardingeb13da12017-02-09 17:56:06 +11003819 * Check this and shot the lock. It is not prone from deadlocks.
3820 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 */
3822 if (dev->flags & IFF_UP) {
3823 int cpu = smp_processor_id(); /* ok because BHs are off */
3824
David S. Millerc773e842008-07-08 23:13:53 -07003825 if (txq->xmit_lock_owner != cpu) {
Daniel Borkmanna70b5062016-06-10 21:19:06 +02003826 if (unlikely(__this_cpu_read(xmit_recursion) >
3827 XMIT_RECURSION_LIMIT))
Eric Dumazet745e20f2010-09-29 13:23:09 -07003828 goto recursion_alert;
3829
Steffen Klassertf53c7232017-12-20 10:41:36 +01003830 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003831 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07003832 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003833
David S. Millerc773e842008-07-08 23:13:53 -07003834 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Tom Herbert734664982011-11-28 16:32:44 +00003836 if (!netif_xmit_stopped(txq)) {
Eric Dumazet745e20f2010-09-29 13:23:09 -07003837 __this_cpu_inc(xmit_recursion);
David S. Millerce937182014-08-30 19:22:20 -07003838 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Eric Dumazet745e20f2010-09-29 13:23:09 -07003839 __this_cpu_dec(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00003840 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07003841 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 goto out;
3843 }
3844 }
David S. Millerc773e842008-07-08 23:13:53 -07003845 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00003846 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3847 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 } else {
3849 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07003850 * unfortunately
3851 */
3852recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00003853 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3854 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856 }
3857
3858 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07003859 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Eric Dumazet015f0682014-03-27 08:45:56 -07003861 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003862 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return rc;
3864out:
Herbert Xud4828d82006-06-22 02:28:18 -07003865 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return rc;
3867}
Jason Wangf663dd92014-01-10 16:18:26 +08003868
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003869int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08003870{
3871 return __dev_queue_xmit(skb, NULL);
3872}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003873EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Alexander Duyckeadec8772018-07-09 12:19:48 -04003875int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
Jason Wangf663dd92014-01-10 16:18:26 +08003876{
Alexander Duyckeadec8772018-07-09 12:19:48 -04003877 return __dev_queue_xmit(skb, sb_dev);
Jason Wangf663dd92014-01-10 16:18:26 +08003878}
3879EXPORT_SYMBOL(dev_queue_xmit_accel);
3880
Magnus Karlsson865b03f2018-05-02 13:01:33 +02003881int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
3882{
3883 struct net_device *dev = skb->dev;
3884 struct sk_buff *orig_skb = skb;
3885 struct netdev_queue *txq;
3886 int ret = NETDEV_TX_BUSY;
3887 bool again = false;
3888
3889 if (unlikely(!netif_running(dev) ||
3890 !netif_carrier_ok(dev)))
3891 goto drop;
3892
3893 skb = validate_xmit_skb_list(skb, dev, &again);
3894 if (skb != orig_skb)
3895 goto drop;
3896
3897 skb_set_queue_mapping(skb, queue_id);
3898 txq = skb_get_tx_queue(dev, skb);
3899
3900 local_bh_disable();
3901
3902 HARD_TX_LOCK(dev, txq, smp_processor_id());
3903 if (!netif_xmit_frozen_or_drv_stopped(txq))
3904 ret = netdev_start_xmit(skb, dev, txq, false);
3905 HARD_TX_UNLOCK(dev, txq);
3906
3907 local_bh_enable();
3908
3909 if (!dev_xmit_complete(ret))
3910 kfree_skb(skb);
3911
3912 return ret;
3913drop:
3914 atomic_long_inc(&dev->tx_dropped);
3915 kfree_skb_list(skb);
3916 return NET_XMIT_DROP;
3917}
3918EXPORT_SYMBOL(dev_direct_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
tchardingeb13da12017-02-09 17:56:06 +11003920/*************************************************************************
3921 * Receiver routines
3922 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003924int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00003925EXPORT_SYMBOL(netdev_max_backlog);
3926
Eric Dumazet3b098e22010-05-15 23:57:10 -07003927int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003928int netdev_budget __read_mostly = 300;
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04003929unsigned int __read_mostly netdev_budget_usecs = 2000;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01003930int weight_p __read_mostly = 64; /* old backlog weight */
3931int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
3932int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
3933int dev_rx_weight __read_mostly = 64;
3934int dev_tx_weight __read_mostly = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003936/* Called with irq disabled */
3937static inline void ____napi_schedule(struct softnet_data *sd,
3938 struct napi_struct *napi)
3939{
3940 list_add_tail(&napi->poll_list, &sd->poll_list);
3941 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3942}
3943
Eric Dumazetdf334542010-03-24 19:13:54 +00003944#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07003945
3946/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00003947struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07003948EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003949u32 rps_cpu_mask __read_mostly;
3950EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07003951
Ingo Molnarc5905af2012-02-24 08:31:31 +01003952struct static_key rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04003953EXPORT_SYMBOL(rps_needed);
Eric Dumazet13bfff22016-12-07 08:29:10 -08003954struct static_key rfs_needed __read_mostly;
3955EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00003956
Ben Hutchingsc4454772011-01-19 11:03:53 +00003957static struct rps_dev_flow *
3958set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3959 struct rps_dev_flow *rflow, u16 next_cpu)
3960{
Eric Dumazeta31196b2015-04-25 09:35:24 -07003961 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00003962#ifdef CONFIG_RFS_ACCEL
3963 struct netdev_rx_queue *rxqueue;
3964 struct rps_dev_flow_table *flow_table;
3965 struct rps_dev_flow *old_rflow;
3966 u32 flow_id;
3967 u16 rxq_index;
3968 int rc;
3969
3970 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00003971 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3972 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00003973 goto out;
3974 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3975 if (rxq_index == skb_get_rx_queue(skb))
3976 goto out;
3977
3978 rxqueue = dev->_rx + rxq_index;
3979 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3980 if (!flow_table)
3981 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07003982 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003983 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3984 rxq_index, flow_id);
3985 if (rc < 0)
3986 goto out;
3987 old_rflow = rflow;
3988 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00003989 rflow->filter = rc;
3990 if (old_rflow->filter == rflow->filter)
3991 old_rflow->filter = RPS_NO_FILTER;
3992 out:
3993#endif
3994 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00003995 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003996 }
3997
Ben Hutchings09994d12011-10-03 04:42:46 +00003998 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003999 return rflow;
4000}
4001
Tom Herbert0a9627f2010-03-16 08:03:29 +00004002/*
4003 * get_rps_cpu is called from netif_receive_skb and returns the target
4004 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004005 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00004006 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004007static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4008 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004009{
Eric Dumazet567e4b72015-02-06 12:59:01 -08004010 const struct rps_sock_flow_table *sock_flow_table;
4011 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07004012 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004013 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004014 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004015 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07004016 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004017
Tom Herbert0a9627f2010-03-16 08:03:29 +00004018 if (skb_rx_queue_recorded(skb)) {
4019 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08004020
Ben Hutchings62fe0b42010-09-27 08:24:33 +00004021 if (unlikely(index >= dev->real_num_rx_queues)) {
4022 WARN_ONCE(dev->real_num_rx_queues > 1,
4023 "%s received packet on queue %u, but number "
4024 "of RX queues is %u\n",
4025 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004026 goto done;
4027 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08004028 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004029 }
4030
Eric Dumazet567e4b72015-02-06 12:59:01 -08004031 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4032
4033 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4034 map = rcu_dereference(rxqueue->rps_map);
4035 if (!flow_table && !map)
4036 goto done;
4037
Changli Gao2d47b452010-08-17 19:00:56 +00004038 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07004039 hash = skb_get_hash(skb);
4040 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004041 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004042
Tom Herbertfec5e652010-04-16 16:01:27 -07004043 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4044 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004045 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08004046 u32 next_cpu;
4047 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07004048
Eric Dumazet567e4b72015-02-06 12:59:01 -08004049 /* First check into global flow table if there is a match */
4050 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4051 if ((ident ^ hash) & ~rps_cpu_mask)
4052 goto try_rps;
4053
4054 next_cpu = ident & rps_cpu_mask;
4055
4056 /* OK, now we know there is a match,
4057 * we can look at the local (per receive queue) flow table
4058 */
Tom Herbert61b905d2014-03-24 15:34:47 -07004059 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07004060 tcpu = rflow->cpu;
4061
Tom Herbertfec5e652010-04-16 16:01:27 -07004062 /*
4063 * If the desired CPU (where last recvmsg was done) is
4064 * different from current CPU (one in the rx-queue flow
4065 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07004066 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07004067 * - Current CPU is offline.
4068 * - The current CPU's queue tail has advanced beyond the
4069 * last packet that was enqueued using this table entry.
4070 * This guarantees that all previous packets for the flow
4071 * have been dequeued, thus preserving in order delivery.
4072 */
4073 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07004074 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07004075 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00004076 rflow->last_qtail)) >= 0)) {
4077 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004078 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00004079 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00004080
Eric Dumazeta31196b2015-04-25 09:35:24 -07004081 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004082 *rflowp = rflow;
4083 cpu = tcpu;
4084 goto done;
4085 }
4086 }
4087
Eric Dumazet567e4b72015-02-06 12:59:01 -08004088try_rps:
4089
Tom Herbert0a9627f2010-03-16 08:03:29 +00004090 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02004091 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00004092 if (cpu_online(tcpu)) {
4093 cpu = tcpu;
4094 goto done;
4095 }
4096 }
4097
4098done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00004099 return cpu;
4100}
4101
Ben Hutchingsc4454772011-01-19 11:03:53 +00004102#ifdef CONFIG_RFS_ACCEL
4103
4104/**
4105 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4106 * @dev: Device on which the filter was set
4107 * @rxq_index: RX queue index
4108 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4109 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4110 *
4111 * Drivers that implement ndo_rx_flow_steer() should periodically call
4112 * this function for each installed filter and remove the filters for
4113 * which it returns %true.
4114 */
4115bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4116 u32 flow_id, u16 filter_id)
4117{
4118 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4119 struct rps_dev_flow_table *flow_table;
4120 struct rps_dev_flow *rflow;
4121 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07004122 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00004123
4124 rcu_read_lock();
4125 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4126 if (flow_table && flow_id <= flow_table->mask) {
4127 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07004128 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07004129 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00004130 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4131 rflow->last_qtail) <
4132 (int)(10 * flow_table->mask)))
4133 expire = false;
4134 }
4135 rcu_read_unlock();
4136 return expire;
4137}
4138EXPORT_SYMBOL(rps_may_expire_flow);
4139
4140#endif /* CONFIG_RFS_ACCEL */
4141
Tom Herbert0a9627f2010-03-16 08:03:29 +00004142/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004143static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004144{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004145 struct softnet_data *sd = data;
4146
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004147 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00004148 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004149}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004150
Tom Herbertfec5e652010-04-16 16:01:27 -07004151#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00004152
4153/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004154 * Check if this softnet_data structure is another cpu one
4155 * If yes, queue it to our IPI list and return 1
4156 * If no, return 0
4157 */
4158static int rps_ipi_queued(struct softnet_data *sd)
4159{
4160#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05004161 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004162
4163 if (sd != mysd) {
4164 sd->rps_ipi_next = mysd->rps_ipi_list;
4165 mysd->rps_ipi_list = sd;
4166
4167 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4168 return 1;
4169 }
4170#endif /* CONFIG_RPS */
4171 return 0;
4172}
4173
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004174#ifdef CONFIG_NET_FLOW_LIMIT
4175int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4176#endif
4177
4178static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4179{
4180#ifdef CONFIG_NET_FLOW_LIMIT
4181 struct sd_flow_limit *fl;
4182 struct softnet_data *sd;
4183 unsigned int old_flow, new_flow;
4184
4185 if (qlen < (netdev_max_backlog >> 1))
4186 return false;
4187
Christoph Lameter903ceff2014-08-17 12:30:35 -05004188 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004189
4190 rcu_read_lock();
4191 fl = rcu_dereference(sd->flow_limit);
4192 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08004193 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004194 old_flow = fl->history[fl->history_head];
4195 fl->history[fl->history_head] = new_flow;
4196
4197 fl->history_head++;
4198 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4199
4200 if (likely(fl->buckets[old_flow]))
4201 fl->buckets[old_flow]--;
4202
4203 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4204 fl->count++;
4205 rcu_read_unlock();
4206 return true;
4207 }
4208 }
4209 rcu_read_unlock();
4210#endif
4211 return false;
4212}
4213
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004214/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00004215 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4216 * queue (may be a remote CPU queue).
4217 */
Tom Herbertfec5e652010-04-16 16:01:27 -07004218static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4219 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004220{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004221 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004222 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004223 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004224
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004225 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004226
4227 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004228
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004229 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004230 if (!netif_running(skb->dev))
4231 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00004232 qlen = skb_queue_len(&sd->input_pkt_queue);
4233 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08004234 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00004235enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004236 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004237 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004238 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00004239 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004240 return NET_RX_SUCCESS;
4241 }
4242
Eric Dumazetebda37c22010-05-06 23:51:21 +00004243 /* Schedule NAPI for backlog device
4244 * We can use non atomic operation since we own the queue lock
4245 */
4246 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004247 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004248 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004249 }
4250 goto enqueue;
4251 }
4252
Julian Anastasove9e4dd32015-07-09 09:59:09 +03004253drop:
Changli Gaodee42872010-05-02 05:42:16 +00004254 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004255 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004256
Tom Herbert0a9627f2010-03-16 08:03:29 +00004257 local_irq_restore(flags);
4258
Eric Dumazetcaf586e2010-09-30 21:06:55 +00004259 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004260 kfree_skb(skb);
4261 return NET_RX_DROP;
4262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004264static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4265{
4266 struct net_device *dev = skb->dev;
4267 struct netdev_rx_queue *rxqueue;
4268
4269 rxqueue = dev->_rx;
4270
4271 if (skb_rx_queue_recorded(skb)) {
4272 u16 index = skb_get_rx_queue(skb);
4273
4274 if (unlikely(index >= dev->real_num_rx_queues)) {
4275 WARN_ONCE(dev->real_num_rx_queues > 1,
4276 "%s received packet on queue %u, but number "
4277 "of RX queues is %u\n",
4278 dev->name, index, dev->real_num_rx_queues);
4279
4280 return rxqueue; /* Return first rxqueue */
4281 }
4282 rxqueue += index;
4283 }
4284 return rxqueue;
4285}
4286
John Fastabendd4455162017-07-17 09:26:45 -07004287static u32 netif_receive_generic_xdp(struct sk_buff *skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004288 struct xdp_buff *xdp,
John Fastabendd4455162017-07-17 09:26:45 -07004289 struct bpf_prog *xdp_prog)
4290{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004291 struct netdev_rx_queue *rxqueue;
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004292 void *orig_data, *orig_data_end;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004293 u32 metalen, act = XDP_DROP;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004294 __be16 orig_eth_type;
4295 struct ethhdr *eth;
4296 bool orig_bcast;
John Fastabendd4455162017-07-17 09:26:45 -07004297 int hlen, off;
4298 u32 mac_len;
4299
4300 /* Reinjected packets coming from act_mirred or similar should
4301 * not get XDP generic processing.
4302 */
Paolo Abenicd11b1642018-07-30 14:30:44 +02004303 if (skb_cloned(skb) || skb_is_tc_redirected(skb))
John Fastabendd4455162017-07-17 09:26:45 -07004304 return XDP_PASS;
4305
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004306 /* XDP packets must be linear and must have sufficient headroom
4307 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4308 * native XDP provides, thus we need to do it here as well.
4309 */
4310 if (skb_is_nonlinear(skb) ||
4311 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4312 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4313 int troom = skb->tail + skb->data_len - skb->end;
4314
4315 /* In case we have to go down the path and also linearize,
4316 * then lets do the pskb_expand_head() work just once here.
4317 */
4318 if (pskb_expand_head(skb,
4319 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4320 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4321 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08004322 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004323 goto do_drop;
4324 }
John Fastabendd4455162017-07-17 09:26:45 -07004325
4326 /* The XDP program wants to see the packet starting at the MAC
4327 * header.
4328 */
4329 mac_len = skb->data - skb_mac_header(skb);
4330 hlen = skb_headlen(skb) + mac_len;
Björn Töpel02671e22018-05-02 13:01:30 +02004331 xdp->data = skb->data - mac_len;
4332 xdp->data_meta = xdp->data;
4333 xdp->data_end = xdp->data + hlen;
4334 xdp->data_hard_start = skb->data - skb_headroom(skb);
4335 orig_data_end = xdp->data_end;
4336 orig_data = xdp->data;
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004337 eth = (struct ethhdr *)xdp->data;
4338 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4339 orig_eth_type = eth->h_proto;
John Fastabendd4455162017-07-17 09:26:45 -07004340
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004341 rxqueue = netif_get_rxqueue(skb);
Björn Töpel02671e22018-05-02 13:01:30 +02004342 xdp->rxq = &rxqueue->xdp_rxq;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01004343
Björn Töpel02671e22018-05-02 13:01:30 +02004344 act = bpf_prog_run_xdp(xdp_prog, xdp);
John Fastabendd4455162017-07-17 09:26:45 -07004345
Björn Töpel02671e22018-05-02 13:01:30 +02004346 off = xdp->data - orig_data;
John Fastabendd4455162017-07-17 09:26:45 -07004347 if (off > 0)
4348 __skb_pull(skb, off);
4349 else if (off < 0)
4350 __skb_push(skb, -off);
Edward Cree92dd5452017-09-19 18:45:56 +01004351 skb->mac_header += off;
John Fastabendd4455162017-07-17 09:26:45 -07004352
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004353 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4354 * pckt.
4355 */
Björn Töpel02671e22018-05-02 13:01:30 +02004356 off = orig_data_end - xdp->data_end;
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004357 if (off != 0) {
Björn Töpel02671e22018-05-02 13:01:30 +02004358 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004359 skb->len -= off;
Björn Töpel02671e22018-05-02 13:01:30 +02004360
Nikita V. Shirokovf7613122018-04-25 07:15:03 -07004361 }
Nikita V. Shirokov198d83b2018-04-17 21:42:14 -07004362
Jesper Dangaard Brouer29724952018-10-09 12:04:43 +02004363 /* check if XDP changed eth hdr such SKB needs update */
4364 eth = (struct ethhdr *)xdp->data;
4365 if ((orig_eth_type != eth->h_proto) ||
4366 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4367 __skb_push(skb, ETH_HLEN);
4368 skb->protocol = eth_type_trans(skb, skb->dev);
4369 }
4370
John Fastabendd4455162017-07-17 09:26:45 -07004371 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004372 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004373 case XDP_TX:
4374 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004375 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004376 case XDP_PASS:
Björn Töpel02671e22018-05-02 13:01:30 +02004377 metalen = xdp->data - xdp->data_meta;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004378 if (metalen)
4379 skb_metadata_set(skb, metalen);
4380 break;
John Fastabendd4455162017-07-17 09:26:45 -07004381 default:
4382 bpf_warn_invalid_xdp_action(act);
4383 /* fall through */
4384 case XDP_ABORTED:
4385 trace_xdp_exception(skb->dev, xdp_prog, act);
4386 /* fall through */
4387 case XDP_DROP:
4388 do_drop:
4389 kfree_skb(skb);
4390 break;
4391 }
4392
4393 return act;
4394}
4395
4396/* When doing generic XDP we have to bypass the qdisc layer and the
4397 * network taps in order to match in-driver-XDP behavior.
4398 */
Jason Wang7c497472017-08-11 19:41:17 +08004399void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004400{
4401 struct net_device *dev = skb->dev;
4402 struct netdev_queue *txq;
4403 bool free_skb = true;
4404 int cpu, rc;
4405
4406 txq = netdev_pick_tx(dev, skb, NULL);
4407 cpu = smp_processor_id();
4408 HARD_TX_LOCK(dev, txq, cpu);
4409 if (!netif_xmit_stopped(txq)) {
4410 rc = netdev_start_xmit(skb, dev, txq, 0);
4411 if (dev_xmit_complete(rc))
4412 free_skb = false;
4413 }
4414 HARD_TX_UNLOCK(dev, txq);
4415 if (free_skb) {
4416 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4417 kfree_skb(skb);
4418 }
4419}
Jason Wang7c497472017-08-11 19:41:17 +08004420EXPORT_SYMBOL_GPL(generic_xdp_tx);
John Fastabendd4455162017-07-17 09:26:45 -07004421
Davidlohr Bueso02786472018-05-08 09:07:02 -07004422static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
John Fastabendd4455162017-07-17 09:26:45 -07004423
Jason Wang7c497472017-08-11 19:41:17 +08004424int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004425{
John Fastabendd4455162017-07-17 09:26:45 -07004426 if (xdp_prog) {
Björn Töpel02671e22018-05-02 13:01:30 +02004427 struct xdp_buff xdp;
4428 u32 act;
John Fastabend6103aa92017-07-17 09:27:50 -07004429 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004430
Björn Töpel02671e22018-05-02 13:01:30 +02004431 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
John Fastabendd4455162017-07-17 09:26:45 -07004432 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004433 switch (act) {
4434 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004435 err = xdp_do_generic_redirect(skb->dev, skb,
Björn Töpel02671e22018-05-02 13:01:30 +02004436 &xdp, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004437 if (err)
4438 goto out_redir;
Björn Töpel02671e22018-05-02 13:01:30 +02004439 break;
John Fastabend6103aa92017-07-17 09:27:50 -07004440 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004441 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004442 break;
4443 }
John Fastabendd4455162017-07-17 09:26:45 -07004444 return XDP_DROP;
4445 }
4446 }
4447 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004448out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004449 kfree_skb(skb);
4450 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004451}
Jason Wang7c497472017-08-11 19:41:17 +08004452EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004453
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004454static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004456 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Eric Dumazet588f0332011-11-15 04:12:55 +00004458 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Koki Sanagicf66ba52010-08-23 18:45:02 +09004460 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004461
Davidlohr Bueso02786472018-05-08 09:07:02 -07004462 if (static_branch_unlikely(&generic_xdp_needed_key)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07004463 int ret;
4464
4465 preempt_disable();
4466 rcu_read_lock();
4467 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4468 rcu_read_unlock();
4469 preempt_enable();
John Fastabendd4455162017-07-17 09:26:45 -07004470
John Fastabend6103aa92017-07-17 09:27:50 -07004471 /* Consider XDP consuming the packet a success from
4472 * the netdev point of view we do not want to count
4473 * this as an error.
4474 */
John Fastabendd4455162017-07-17 09:26:45 -07004475 if (ret != XDP_PASS)
John Fastabend6103aa92017-07-17 09:27:50 -07004476 return NET_RX_SUCCESS;
John Fastabendd4455162017-07-17 09:26:45 -07004477 }
4478
Eric Dumazetdf334542010-03-24 19:13:54 +00004479#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004480 if (static_key_false(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004481 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004482 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Changli Gaocece1942010-08-07 20:35:43 -07004484 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004485 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004486
4487 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004488 if (cpu < 0)
4489 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004490
4491 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4492
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004493 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004494 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004495 } else
4496#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004497 {
4498 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004499
Tom Herbertfec5e652010-04-16 16:01:27 -07004500 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4501 put_cpu();
4502 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004505
4506/**
4507 * netif_rx - post buffer to the network code
4508 * @skb: buffer to post
4509 *
4510 * This function receives a packet from a device driver and queues it for
4511 * the upper (protocol) levels to process. It always succeeds. The buffer
4512 * may be dropped during processing for congestion control or by the
4513 * protocol layers.
4514 *
4515 * return values:
4516 * NET_RX_SUCCESS (no congestion)
4517 * NET_RX_DROP (packet was dropped)
4518 *
4519 */
4520
4521int netif_rx(struct sk_buff *skb)
4522{
4523 trace_netif_rx_entry(skb);
4524
4525 return netif_rx_internal(skb);
4526}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004527EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529int netif_rx_ni(struct sk_buff *skb)
4530{
4531 int err;
4532
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004533 trace_netif_rx_ni_entry(skb);
4534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004536 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 if (local_softirq_pending())
4538 do_softirq();
4539 preempt_enable();
4540
4541 return err;
4542}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543EXPORT_SYMBOL(netif_rx_ni);
4544
Emese Revfy0766f782016-06-20 20:42:34 +02004545static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004547 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
4549 if (sd->completion_queue) {
4550 struct sk_buff *clist;
4551
4552 local_irq_disable();
4553 clist = sd->completion_queue;
4554 sd->completion_queue = NULL;
4555 local_irq_enable();
4556
4557 while (clist) {
4558 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 clist = clist->next;
4561
Reshetova, Elena63354792017-06-30 13:07:58 +03004562 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004563 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4564 trace_consume_skb(skb);
4565 else
4566 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004567
4568 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4569 __kfree_skb(skb);
4570 else
4571 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004573
4574 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 }
4576
4577 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004578 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 local_irq_disable();
4581 head = sd->output_queue;
4582 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004583 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 local_irq_enable();
4585
4586 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004587 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004588 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 head = head->next_sched;
4591
John Fastabend6b3ba912017-12-07 09:54:25 -08004592 if (!(q->flags & TCQ_F_NOLOCK)) {
4593 root_lock = qdisc_lock(q);
4594 spin_lock(root_lock);
4595 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004596 /* We need to make sure head->next_sched is read
4597 * before clearing __QDISC_STATE_SCHED
4598 */
4599 smp_mb__before_atomic();
4600 clear_bit(__QDISC_STATE_SCHED, &q->state);
4601 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004602 if (root_lock)
4603 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
4605 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004606
4607 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608}
4609
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004610#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004611/* This hook is defined here for ATM LANE */
4612int (*br_fdb_test_addr_hook)(struct net_device *dev,
4613 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004614EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004617static inline struct sk_buff *
4618sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4619 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004620{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004621#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004622 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004623 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004624
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004625 /* If there's at least one ingress present somewhere (so
4626 * we get here via enabled static key), remaining devices
4627 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004628 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004629 */
Jiri Pirko46209402017-11-03 11:46:25 +01004630 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004631 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004632
Herbert Xuf697c3e2007-10-14 00:38:47 -07004633 if (*pt_prev) {
4634 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4635 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004636 }
4637
Florian Westphal33654952015-05-14 00:36:28 +02004638 qdisc_skb_cb(skb)->pkt_len = skb->len;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004639 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004640 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004641
Jiri Pirko46209402017-11-03 11:46:25 +01004642 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004643 case TC_ACT_OK:
4644 case TC_ACT_RECLASSIFY:
4645 skb->tc_index = TC_H_MIN(cl_res.classid);
4646 break;
4647 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004648 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004649 kfree_skb(skb);
4650 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004651 case TC_ACT_STOLEN:
4652 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004653 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004654 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004655 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004656 case TC_ACT_REDIRECT:
4657 /* skb_mac_header check was done by cls/act_bpf, so
4658 * we can safely push the L2 header back before
4659 * redirecting to another netdev
4660 */
4661 __skb_push(skb, skb->mac_len);
4662 skb_do_redirect(skb);
4663 return NULL;
Paolo Abenicd11b1642018-07-30 14:30:44 +02004664 case TC_ACT_REINSERT:
4665 /* this does not scrub the packet, and updates stats on error */
4666 skb_tc_reinsert(skb, &cl_res);
4667 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004668 default:
4669 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004670 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004671#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004672 return skb;
4673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004675/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004676 * netdev_is_rx_handler_busy - check if receive handler is registered
4677 * @dev: device to check
4678 *
4679 * Check if a receive handler is already registered for a given device.
4680 * Return true if there one.
4681 *
4682 * The caller must hold the rtnl_mutex.
4683 */
4684bool netdev_is_rx_handler_busy(struct net_device *dev)
4685{
4686 ASSERT_RTNL();
4687 return dev && rtnl_dereference(dev->rx_handler);
4688}
4689EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4690
4691/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004692 * netdev_rx_handler_register - register receive handler
4693 * @dev: device to register a handler for
4694 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004695 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004696 *
Masanari Iidae2278672014-02-18 22:54:36 +09004697 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004698 * called from __netif_receive_skb. A negative errno code is returned
4699 * on a failure.
4700 *
4701 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004702 *
4703 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004704 */
4705int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004706 rx_handler_func_t *rx_handler,
4707 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004708{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004709 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004710 return -EBUSY;
4711
Paolo Abenif54262502018-03-09 10:39:24 +01004712 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4713 return -EINVAL;
4714
Eric Dumazet00cfec32013-03-29 03:01:22 +00004715 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004716 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004717 rcu_assign_pointer(dev->rx_handler, rx_handler);
4718
4719 return 0;
4720}
4721EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4722
4723/**
4724 * netdev_rx_handler_unregister - unregister receive handler
4725 * @dev: device to unregister a handler from
4726 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004727 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004728 *
4729 * The caller must hold the rtnl_mutex.
4730 */
4731void netdev_rx_handler_unregister(struct net_device *dev)
4732{
4733
4734 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004735 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004736 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4737 * section has a guarantee to see a non NULL rx_handler_data
4738 * as well.
4739 */
4740 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004741 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004742}
4743EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4744
Mel Gormanb4b9e352012-07-31 16:44:26 -07004745/*
4746 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4747 * the special handling of PFMEMALLOC skbs.
4748 */
4749static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4750{
4751 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004752 case htons(ETH_P_ARP):
4753 case htons(ETH_P_IP):
4754 case htons(ETH_P_IPV6):
4755 case htons(ETH_P_8021Q):
4756 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004757 return true;
4758 default:
4759 return false;
4760 }
4761}
4762
Pablo Neirae687ad62015-05-13 18:19:38 +02004763static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4764 int *ret, struct net_device *orig_dev)
4765{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004766#ifdef CONFIG_NETFILTER_INGRESS
Pablo Neirae687ad62015-05-13 18:19:38 +02004767 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004768 int ingress_retval;
4769
Pablo Neirae687ad62015-05-13 18:19:38 +02004770 if (*pt_prev) {
4771 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4772 *pt_prev = NULL;
4773 }
4774
Aaron Conole2c1e2702016-09-21 11:35:03 -04004775 rcu_read_lock();
4776 ingress_retval = nf_hook_ingress(skb);
4777 rcu_read_unlock();
4778 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004779 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004780#endif /* CONFIG_NETFILTER_INGRESS */
Pablo Neirae687ad62015-05-13 18:19:38 +02004781 return 0;
4782}
Pablo Neirae687ad62015-05-13 18:19:38 +02004783
Edward Cree88eb1942018-07-02 16:13:56 +01004784static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
4785 struct packet_type **ppt_prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
4787 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004788 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004789 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004790 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004792 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Eric Dumazet588f0332011-11-15 04:12:55 +00004794 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07004795
Koki Sanagicf66ba52010-08-23 18:45:02 +09004796 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08004797
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07004798 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00004799
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07004800 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00004801 if (!skb_transport_header_was_set(skb))
4802 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00004803 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 pt_prev = NULL;
4806
David S. Miller63d8ea72011-02-28 10:48:59 -08004807another_round:
David S. Millerb6858172012-07-23 16:27:54 -07004808 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08004809
4810 __this_cpu_inc(softnet_data.processed);
4811
Patrick McHardy8ad227f2013-04-19 02:04:31 +00004812 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4813 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004814 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004815 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004816 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004817 }
4818
Willem de Bruijne7246e12017-01-07 17:06:35 -05004819 if (skb_skip_tc_classify(skb))
4820 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
David S. Miller9754e292013-02-14 15:57:38 -05004822 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07004823 goto skip_taps;
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08004826 if (pt_prev)
4827 ret = deliver_skb(skb, pt_prev, orig_dev);
4828 pt_prev = ptype;
4829 }
4830
4831 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
4832 if (pt_prev)
4833 ret = deliver_skb(skb, pt_prev, orig_dev);
4834 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 }
4836
Mel Gormanb4b9e352012-07-31 16:44:26 -07004837skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02004838#ifdef CONFIG_NET_INGRESS
Davidlohr Buesoaabf6772018-05-08 09:07:00 -07004839 if (static_branch_unlikely(&ingress_needed_key)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004840 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02004841 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004842 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02004843
4844 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004845 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02004846 }
Pablo Neira1cf519002015-05-13 18:19:37 +02004847#endif
Willem de Bruijna5135bc2017-01-07 17:06:36 -05004848 skb_reset_tc(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05004849skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05004850 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07004851 goto drop;
4852
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004853 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00004854 if (pt_prev) {
4855 ret = deliver_skb(skb, pt_prev, orig_dev);
4856 pt_prev = NULL;
4857 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004858 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00004859 goto another_round;
4860 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004861 goto out;
John Fastabend24257172011-10-10 09:16:41 +00004862 }
4863
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004864 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004865 if (rx_handler) {
4866 if (pt_prev) {
4867 ret = deliver_skb(skb, pt_prev, orig_dev);
4868 pt_prev = NULL;
4869 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004870 switch (rx_handler(&skb)) {
4871 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00004872 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004873 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004874 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08004875 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004876 case RX_HANDLER_EXACT:
4877 deliver_exact = true;
4878 case RX_HANDLER_PASS:
4879 break;
4880 default:
4881 BUG();
4882 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004885 if (unlikely(skb_vlan_tag_present(skb))) {
4886 if (skb_vlan_tag_get_id(skb))
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004887 skb->pkt_type = PACKET_OTHERHOST;
4888 /* Note: we might in the future use prio bits
4889 * and set skb->priority like in vlan_do_receive()
4890 * For the time being, just ignore Priority Code Point
4891 */
Michał Mirosławb18175242018-11-09 00:18:02 +01004892 __vlan_hwaccel_clear_tag(skb);
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004893 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08004896
4897 /* deliver only exact match when indicated */
4898 if (likely(!deliver_exact)) {
4899 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4900 &ptype_base[ntohs(type) &
4901 PTYPE_HASH_MASK]);
4902 }
4903
4904 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4905 &orig_dev->ptype_specific);
4906
4907 if (unlikely(skb->dev != orig_dev)) {
4908 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4909 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 }
4911
4912 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004913 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00004914 goto drop;
Edward Cree88eb1942018-07-02 16:13:56 +01004915 *ppt_prev = pt_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07004917drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05004918 if (!deliver_exact)
4919 atomic_long_inc(&skb->dev->rx_dropped);
4920 else
4921 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 kfree_skb(skb);
4923 /* Jamal, now you will not able to escape explaining
4924 * me how you were going to use this. :-)
4925 */
4926 ret = NET_RX_DROP;
4927 }
4928
Julian Anastasov2c17d272015-07-09 09:59:10 +03004929out:
David S. Miller9754e292013-02-14 15:57:38 -05004930 return ret;
4931}
4932
Edward Cree88eb1942018-07-02 16:13:56 +01004933static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
4934{
4935 struct net_device *orig_dev = skb->dev;
4936 struct packet_type *pt_prev = NULL;
4937 int ret;
4938
4939 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
4940 if (pt_prev)
4941 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
4942 return ret;
4943}
4944
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02004945/**
4946 * netif_receive_skb_core - special purpose version of netif_receive_skb
4947 * @skb: buffer to process
4948 *
4949 * More direct receive version of netif_receive_skb(). It should
4950 * only be used by callers that have a need to skip RPS and Generic XDP.
4951 * Caller must also take care of handling if (page_is_)pfmemalloc.
4952 *
4953 * This function may only be called from softirq context and interrupts
4954 * should be enabled.
4955 *
4956 * Return values (usually ignored):
4957 * NET_RX_SUCCESS: no congestion
4958 * NET_RX_DROP: packet was dropped
4959 */
4960int netif_receive_skb_core(struct sk_buff *skb)
4961{
4962 int ret;
4963
4964 rcu_read_lock();
Edward Cree88eb1942018-07-02 16:13:56 +01004965 ret = __netif_receive_skb_one_core(skb, false);
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02004966 rcu_read_unlock();
4967
4968 return ret;
4969}
4970EXPORT_SYMBOL(netif_receive_skb_core);
4971
Edward Cree88eb1942018-07-02 16:13:56 +01004972static inline void __netif_receive_skb_list_ptype(struct list_head *head,
4973 struct packet_type *pt_prev,
4974 struct net_device *orig_dev)
Edward Cree4ce00172018-07-02 16:13:40 +01004975{
4976 struct sk_buff *skb, *next;
4977
Edward Cree88eb1942018-07-02 16:13:56 +01004978 if (!pt_prev)
4979 return;
4980 if (list_empty(head))
4981 return;
Edward Cree17266ee2018-07-02 16:14:12 +01004982 if (pt_prev->list_func != NULL)
4983 pt_prev->list_func(head, pt_prev, orig_dev);
4984 else
4985 list_for_each_entry_safe(skb, next, head, list)
4986 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Edward Cree88eb1942018-07-02 16:13:56 +01004987}
4988
4989static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
4990{
4991 /* Fast-path assumptions:
4992 * - There is no RX handler.
4993 * - Only one packet_type matches.
4994 * If either of these fails, we will end up doing some per-packet
4995 * processing in-line, then handling the 'last ptype' for the whole
4996 * sublist. This can't cause out-of-order delivery to any single ptype,
4997 * because the 'last ptype' must be constant across the sublist, and all
4998 * other ptypes are handled per-packet.
4999 */
5000 /* Current (common) ptype of sublist */
5001 struct packet_type *pt_curr = NULL;
5002 /* Current (common) orig_dev of sublist */
5003 struct net_device *od_curr = NULL;
5004 struct list_head sublist;
5005 struct sk_buff *skb, *next;
5006
Edward Cree9af86f92018-07-09 18:10:19 +01005007 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005008 list_for_each_entry_safe(skb, next, head, list) {
5009 struct net_device *orig_dev = skb->dev;
5010 struct packet_type *pt_prev = NULL;
5011
Edward Cree9af86f92018-07-09 18:10:19 +01005012 list_del(&skb->list);
Edward Cree88eb1942018-07-02 16:13:56 +01005013 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
Edward Cree9af86f92018-07-09 18:10:19 +01005014 if (!pt_prev)
5015 continue;
Edward Cree88eb1942018-07-02 16:13:56 +01005016 if (pt_curr != pt_prev || od_curr != orig_dev) {
5017 /* dispatch old sublist */
Edward Cree88eb1942018-07-02 16:13:56 +01005018 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5019 /* start new sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005020 INIT_LIST_HEAD(&sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005021 pt_curr = pt_prev;
5022 od_curr = orig_dev;
5023 }
Edward Cree9af86f92018-07-09 18:10:19 +01005024 list_add_tail(&skb->list, &sublist);
Edward Cree88eb1942018-07-02 16:13:56 +01005025 }
5026
5027 /* dispatch final sublist */
Edward Cree9af86f92018-07-09 18:10:19 +01005028 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
Edward Cree4ce00172018-07-02 16:13:40 +01005029}
5030
David S. Miller9754e292013-02-14 15:57:38 -05005031static int __netif_receive_skb(struct sk_buff *skb)
5032{
5033 int ret;
5034
5035 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005036 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05005037
5038 /*
5039 * PFMEMALLOC skbs are special, they should
5040 * - be delivered to SOCK_MEMALLOC sockets only
5041 * - stay away from userspace
5042 * - have bounded memory usage
5043 *
5044 * Use PF_MEMALLOC as this saves us from propagating the allocation
5045 * context down to all allocation sites.
5046 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005047 noreclaim_flag = memalloc_noreclaim_save();
Edward Cree88eb1942018-07-02 16:13:56 +01005048 ret = __netif_receive_skb_one_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07005049 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05005050 } else
Edward Cree88eb1942018-07-02 16:13:56 +01005051 ret = __netif_receive_skb_one_core(skb, false);
David S. Miller9754e292013-02-14 15:57:38 -05005052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return ret;
5054}
Tom Herbert0a9627f2010-03-16 08:03:29 +00005055
Edward Cree4ce00172018-07-02 16:13:40 +01005056static void __netif_receive_skb_list(struct list_head *head)
5057{
5058 unsigned long noreclaim_flag = 0;
5059 struct sk_buff *skb, *next;
5060 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5061
5062 list_for_each_entry_safe(skb, next, head, list) {
5063 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5064 struct list_head sublist;
5065
5066 /* Handle the previous sublist */
5067 list_cut_before(&sublist, head, &skb->list);
Edward Creeb9f463d2018-07-02 16:14:44 +01005068 if (!list_empty(&sublist))
5069 __netif_receive_skb_list_core(&sublist, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005070 pfmemalloc = !pfmemalloc;
5071 /* See comments in __netif_receive_skb */
5072 if (pfmemalloc)
5073 noreclaim_flag = memalloc_noreclaim_save();
5074 else
5075 memalloc_noreclaim_restore(noreclaim_flag);
5076 }
5077 }
5078 /* Handle the remaining sublist */
Edward Creeb9f463d2018-07-02 16:14:44 +01005079 if (!list_empty(head))
5080 __netif_receive_skb_list_core(head, pfmemalloc);
Edward Cree4ce00172018-07-02 16:13:40 +01005081 /* Restore pflags */
5082 if (pfmemalloc)
5083 memalloc_noreclaim_restore(noreclaim_flag);
5084}
5085
Jakub Kicinskif4e63522017-11-03 13:56:16 -07005086static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04005087{
Martin KaFai Lau58038692017-06-15 17:29:09 -07005088 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04005089 struct bpf_prog *new = xdp->prog;
5090 int ret = 0;
5091
5092 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07005093 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04005094 rcu_assign_pointer(dev->xdp_prog, new);
5095 if (old)
5096 bpf_prog_put(old);
5097
5098 if (old && !new) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005099 static_branch_dec(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005100 } else if (new && !old) {
Davidlohr Bueso02786472018-05-08 09:07:02 -07005101 static_branch_inc(&generic_xdp_needed_key);
David S. Millerb5cdae32017-04-18 15:36:58 -04005102 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05005103 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04005104 }
5105 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04005106
5107 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07005108 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04005109 break;
5110
5111 default:
5112 ret = -EINVAL;
5113 break;
5114 }
5115
5116 return ret;
5117}
5118
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005119static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00005120{
Julian Anastasov2c17d272015-07-09 09:59:10 +03005121 int ret;
5122
Eric Dumazet588f0332011-11-15 04:12:55 +00005123 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07005124
Richard Cochranc1f19b52010-07-17 08:49:36 +00005125 if (skb_defer_rx_timestamp(skb))
5126 return NET_RX_SUCCESS;
5127
Davidlohr Bueso02786472018-05-08 09:07:02 -07005128 if (static_branch_unlikely(&generic_xdp_needed_key)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07005129 int ret;
David S. Millerb5cdae32017-04-18 15:36:58 -04005130
John Fastabendbbbe2112017-09-08 14:00:30 -07005131 preempt_disable();
5132 rcu_read_lock();
5133 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5134 rcu_read_unlock();
5135 preempt_enable();
5136
5137 if (ret != XDP_PASS)
John Fastabendd4455162017-07-17 09:26:45 -07005138 return NET_RX_DROP;
David S. Millerb5cdae32017-04-18 15:36:58 -04005139 }
5140
John Fastabendbbbe2112017-09-08 14:00:30 -07005141 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00005142#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01005143 if (static_key_false(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07005144 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03005145 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07005146
Eric Dumazet3b098e22010-05-15 23:57:10 -07005147 if (cpu >= 0) {
5148 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5149 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00005150 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07005151 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005152 }
Tom Herbert1e94d722010-03-18 17:45:44 -07005153#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03005154 ret = __netif_receive_skb(skb);
5155 rcu_read_unlock();
5156 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005157}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005158
Edward Cree7da517a2018-07-02 16:13:24 +01005159static void netif_receive_skb_list_internal(struct list_head *head)
5160{
5161 struct bpf_prog *xdp_prog = NULL;
5162 struct sk_buff *skb, *next;
Edward Cree8c057ef2018-07-09 18:09:54 +01005163 struct list_head sublist;
Edward Cree7da517a2018-07-02 16:13:24 +01005164
Edward Cree8c057ef2018-07-09 18:09:54 +01005165 INIT_LIST_HEAD(&sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005166 list_for_each_entry_safe(skb, next, head, list) {
5167 net_timestamp_check(netdev_tstamp_prequeue, skb);
Edward Cree8c057ef2018-07-09 18:09:54 +01005168 list_del(&skb->list);
5169 if (!skb_defer_rx_timestamp(skb))
5170 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005171 }
Edward Cree8c057ef2018-07-09 18:09:54 +01005172 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005173
5174 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5175 preempt_disable();
5176 rcu_read_lock();
5177 list_for_each_entry_safe(skb, next, head, list) {
5178 xdp_prog = rcu_dereference(skb->dev->xdp_prog);
Edward Cree8c057ef2018-07-09 18:09:54 +01005179 list_del(&skb->list);
5180 if (do_xdp_generic(xdp_prog, skb) == XDP_PASS)
5181 list_add_tail(&skb->list, &sublist);
Edward Cree7da517a2018-07-02 16:13:24 +01005182 }
5183 rcu_read_unlock();
5184 preempt_enable();
Edward Cree8c057ef2018-07-09 18:09:54 +01005185 /* Put passed packets back on main list */
5186 list_splice_init(&sublist, head);
Edward Cree7da517a2018-07-02 16:13:24 +01005187 }
5188
5189 rcu_read_lock();
5190#ifdef CONFIG_RPS
5191 if (static_key_false(&rps_needed)) {
5192 list_for_each_entry_safe(skb, next, head, list) {
5193 struct rps_dev_flow voidflow, *rflow = &voidflow;
5194 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5195
5196 if (cpu >= 0) {
Edward Cree8c057ef2018-07-09 18:09:54 +01005197 /* Will be handled, remove from list */
Edward Cree7da517a2018-07-02 16:13:24 +01005198 list_del(&skb->list);
Edward Cree8c057ef2018-07-09 18:09:54 +01005199 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
Edward Cree7da517a2018-07-02 16:13:24 +01005200 }
5201 }
5202 }
5203#endif
5204 __netif_receive_skb_list(head);
5205 rcu_read_unlock();
5206}
5207
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005208/**
5209 * netif_receive_skb - process receive buffer from network
5210 * @skb: buffer to process
5211 *
5212 * netif_receive_skb() is the main receive data processing function.
5213 * It always succeeds. The buffer may be dropped during processing
5214 * for congestion control or by the protocol layers.
5215 *
5216 * This function may only be called from softirq context and interrupts
5217 * should be enabled.
5218 *
5219 * Return values (usually ignored):
5220 * NET_RX_SUCCESS: no congestion
5221 * NET_RX_DROP: packet was dropped
5222 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005223int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005224{
5225 trace_netif_receive_skb_entry(skb);
5226
5227 return netif_receive_skb_internal(skb);
5228}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05005229EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
Edward Creef6ad8c12018-07-02 16:12:45 +01005231/**
5232 * netif_receive_skb_list - process many receive buffers from network
5233 * @head: list of skbs to process.
5234 *
Edward Cree7da517a2018-07-02 16:13:24 +01005235 * Since return value of netif_receive_skb() is normally ignored, and
5236 * wouldn't be meaningful for a list, this function returns void.
Edward Creef6ad8c12018-07-02 16:12:45 +01005237 *
5238 * This function may only be called from softirq context and interrupts
5239 * should be enabled.
5240 */
5241void netif_receive_skb_list(struct list_head *head)
5242{
Edward Cree7da517a2018-07-02 16:13:24 +01005243 struct sk_buff *skb;
Edward Creef6ad8c12018-07-02 16:12:45 +01005244
Edward Creeb9f463d2018-07-02 16:14:44 +01005245 if (list_empty(head))
5246 return;
Edward Cree920572b2018-07-02 16:13:11 +01005247 list_for_each_entry(skb, head, list)
5248 trace_netif_receive_skb_list_entry(skb);
Edward Cree7da517a2018-07-02 16:13:24 +01005249 netif_receive_skb_list_internal(head);
Edward Creef6ad8c12018-07-02 16:12:45 +01005250}
5251EXPORT_SYMBOL(netif_receive_skb_list);
5252
Eric Dumazet41852492016-08-26 12:50:39 -07005253DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005254
5255/* Network device is going away, flush any packets still pending */
5256static void flush_backlog(struct work_struct *work)
5257{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005258 struct sk_buff *skb, *tmp;
5259 struct softnet_data *sd;
5260
5261 local_bh_disable();
5262 sd = this_cpu_ptr(&softnet_data);
5263
5264 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005265 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005266 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005267 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005268 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005269 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005270 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005271 }
Changli Gao6e7676c2010-04-27 15:07:33 -07005272 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00005273 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005274 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005275
5276 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07005277 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07005278 __skb_unlink(skb, &sd->process_queue);
5279 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005280 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07005281 }
5282 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005283 local_bh_enable();
5284}
5285
Eric Dumazet41852492016-08-26 12:50:39 -07005286static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005287{
5288 unsigned int cpu;
5289
5290 get_online_cpus();
5291
Eric Dumazet41852492016-08-26 12:50:39 -07005292 for_each_online_cpu(cpu)
5293 queue_work_on(cpu, system_highpri_wq,
5294 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005295
5296 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07005297 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005298
5299 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005300}
5301
Herbert Xud565b0a2008-12-15 23:38:52 -08005302static int napi_gro_complete(struct sk_buff *skb)
5303{
Vlad Yasevich22061d82012-11-15 08:49:11 +00005304 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005305 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005306 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08005307 int err = -ENOENT;
5308
Eric Dumazetc3c7c252012-12-06 13:54:59 +00005309 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5310
Herbert Xufc59f9a2009-04-14 15:11:06 -07005311 if (NAPI_GRO_CB(skb)->count == 1) {
5312 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005313 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07005314 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005315
5316 rcu_read_lock();
5317 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005318 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08005319 continue;
5320
Jerry Chu299603e82013-12-11 20:53:45 -08005321 err = ptype->callbacks.gro_complete(skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08005322 break;
5323 }
5324 rcu_read_unlock();
5325
5326 if (err) {
5327 WARN_ON(&ptype->list == head);
5328 kfree_skb(skb);
5329 return NET_RX_SUCCESS;
5330 }
5331
5332out:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005333 return netif_receive_skb_internal(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005334}
5335
Li RongQing6312fe72018-07-05 14:34:32 +08005336static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
David Miller07d78362018-06-24 14:14:02 +09005337 bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08005338{
Li RongQing6312fe72018-07-05 14:34:32 +08005339 struct list_head *head = &napi->gro_hash[index].list;
David Millerd4546c22018-06-24 14:13:49 +09005340 struct sk_buff *skb, *p;
Herbert Xud565b0a2008-12-15 23:38:52 -08005341
David Miller07d78362018-06-24 14:14:02 +09005342 list_for_each_entry_safe_reverse(skb, p, head, list) {
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005343 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5344 return;
David S. Miller992cba72018-07-31 15:27:56 -07005345 skb_list_del_init(skb);
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005346 napi_gro_complete(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08005347 napi->gro_hash[index].count--;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005348 }
Li RongQingd9f37d02018-07-13 14:41:36 +08005349
5350 if (!napi->gro_hash[index].count)
5351 __clear_bit(index, &napi->gro_bitmask);
Herbert Xud565b0a2008-12-15 23:38:52 -08005352}
David Miller07d78362018-06-24 14:14:02 +09005353
Li RongQing6312fe72018-07-05 14:34:32 +08005354/* napi->gro_hash[].list contains packets ordered by age.
David Miller07d78362018-06-24 14:14:02 +09005355 * youngest packets at the head of it.
5356 * Complete skbs in reverse order to reduce latencies.
5357 */
5358void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5359{
Li RongQing6312fe72018-07-05 14:34:32 +08005360 u32 i;
David Miller07d78362018-06-24 14:14:02 +09005361
Li RongQingd9f37d02018-07-13 14:41:36 +08005362 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
5363 if (test_bit(i, &napi->gro_bitmask))
5364 __napi_gro_flush_chain(napi, i, flush_old);
5365 }
David Miller07d78362018-06-24 14:14:02 +09005366}
Eric Dumazet86cac582010-08-31 18:25:32 +00005367EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08005368
David Miller07d78362018-06-24 14:14:02 +09005369static struct list_head *gro_list_prepare(struct napi_struct *napi,
5370 struct sk_buff *skb)
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005371{
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005372 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08005373 u32 hash = skb_get_hash_raw(skb);
David Miller07d78362018-06-24 14:14:02 +09005374 struct list_head *head;
David Millerd4546c22018-06-24 14:13:49 +09005375 struct sk_buff *p;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005376
Li RongQing6312fe72018-07-05 14:34:32 +08005377 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
David Miller07d78362018-06-24 14:14:02 +09005378 list_for_each_entry(p, head, list) {
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005379 unsigned long diffs;
5380
Tom Herbert0b4cec82014-01-15 08:58:06 -08005381 NAPI_GRO_CB(p)->flush = 0;
5382
5383 if (hash != skb_get_hash_raw(p)) {
5384 NAPI_GRO_CB(p)->same_flow = 0;
5385 continue;
5386 }
5387
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005388 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
Michał Mirosławb18175242018-11-09 00:18:02 +01005389 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5390 if (skb_vlan_tag_present(p))
5391 diffs |= p->vlan_tci ^ skb->vlan_tci;
Jesse Grossce87fc62016-01-20 17:59:49 -08005392 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005393 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005394 if (maclen == ETH_HLEN)
5395 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005396 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005397 else if (!diffs)
5398 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07005399 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005400 maclen);
5401 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005402 }
David Miller07d78362018-06-24 14:14:02 +09005403
5404 return head;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005405}
5406
Jerry Chu299603e82013-12-11 20:53:45 -08005407static void skb_gro_reset_offset(struct sk_buff *skb)
5408{
5409 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5410 const skb_frag_t *frag0 = &pinfo->frags[0];
5411
5412 NAPI_GRO_CB(skb)->data_offset = 0;
5413 NAPI_GRO_CB(skb)->frag0 = NULL;
5414 NAPI_GRO_CB(skb)->frag0_len = 0;
5415
5416 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
5417 pinfo->nr_frags &&
5418 !PageHighMem(skb_frag_page(frag0))) {
5419 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08005420 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5421 skb_frag_size(frag0),
5422 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08005423 }
5424}
5425
Eric Dumazeta50e2332014-03-29 21:28:21 -07005426static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5427{
5428 struct skb_shared_info *pinfo = skb_shinfo(skb);
5429
5430 BUG_ON(skb->end - skb->tail < grow);
5431
5432 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5433
5434 skb->data_len -= grow;
5435 skb->tail += grow;
5436
5437 pinfo->frags[0].page_offset += grow;
5438 skb_frag_size_sub(&pinfo->frags[0], grow);
5439
5440 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5441 skb_frag_unref(skb, 0);
5442 memmove(pinfo->frags, pinfo->frags + 1,
5443 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5444 }
5445}
5446
Li RongQing6312fe72018-07-05 14:34:32 +08005447static void gro_flush_oldest(struct list_head *head)
David Miller07d78362018-06-24 14:14:02 +09005448{
Li RongQing6312fe72018-07-05 14:34:32 +08005449 struct sk_buff *oldest;
David Miller07d78362018-06-24 14:14:02 +09005450
Li RongQing6312fe72018-07-05 14:34:32 +08005451 oldest = list_last_entry(head, struct sk_buff, list);
David Miller07d78362018-06-24 14:14:02 +09005452
Li RongQing6312fe72018-07-05 14:34:32 +08005453 /* We are called with head length >= MAX_GRO_SKBS, so this is
David Miller07d78362018-06-24 14:14:02 +09005454 * impossible.
5455 */
5456 if (WARN_ON_ONCE(!oldest))
5457 return;
5458
Li RongQingd9f37d02018-07-13 14:41:36 +08005459 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5460 * SKB to the chain.
David Miller07d78362018-06-24 14:14:02 +09005461 */
David S. Millerece23712018-10-28 10:35:12 -07005462 skb_list_del_init(oldest);
David Miller07d78362018-06-24 14:14:02 +09005463 napi_gro_complete(oldest);
5464}
5465
Rami Rosenbb728822012-11-28 21:55:25 +00005466static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08005467{
Li RongQing6312fe72018-07-05 14:34:32 +08005468 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
David Millerd4546c22018-06-24 14:13:49 +09005469 struct list_head *head = &offload_base;
Vlad Yasevich22061d82012-11-15 08:49:11 +00005470 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08005471 __be16 type = skb->protocol;
David Miller07d78362018-06-24 14:14:02 +09005472 struct list_head *gro_head;
David Millerd4546c22018-06-24 14:13:49 +09005473 struct sk_buff *pp = NULL;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005474 enum gro_result ret;
David Millerd4546c22018-06-24 14:13:49 +09005475 int same_flow;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005476 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08005477
David S. Millerb5cdae32017-04-18 15:36:58 -04005478 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08005479 goto normal;
5480
David Miller07d78362018-06-24 14:14:02 +09005481 gro_head = gro_list_prepare(napi, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005482
Herbert Xud565b0a2008-12-15 23:38:52 -08005483 rcu_read_lock();
5484 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00005485 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08005486 continue;
5487
Herbert Xu86911732009-01-29 14:19:50 +00005488 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00005489 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005490 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08005491 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08005492 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07005493 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02005494 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07005495 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04005496 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08005497 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005498
Tom Herbert662880f2014-08-27 21:26:56 -07005499 /* Setup for GRO checksum validation */
5500 switch (skb->ip_summed) {
5501 case CHECKSUM_COMPLETE:
5502 NAPI_GRO_CB(skb)->csum = skb->csum;
5503 NAPI_GRO_CB(skb)->csum_valid = 1;
5504 NAPI_GRO_CB(skb)->csum_cnt = 0;
5505 break;
5506 case CHECKSUM_UNNECESSARY:
5507 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5508 NAPI_GRO_CB(skb)->csum_valid = 0;
5509 break;
5510 default:
5511 NAPI_GRO_CB(skb)->csum_cnt = 0;
5512 NAPI_GRO_CB(skb)->csum_valid = 0;
5513 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005514
David Miller07d78362018-06-24 14:14:02 +09005515 pp = ptype->callbacks.gro_receive(gro_head, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005516 break;
5517 }
5518 rcu_read_unlock();
5519
5520 if (&ptype->list == head)
5521 goto normal;
5522
Steffen Klassert25393d32017-02-15 09:39:44 +01005523 if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
5524 ret = GRO_CONSUMED;
5525 goto ok;
5526 }
5527
Herbert Xu0da2afd52008-12-26 14:57:42 -08005528 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005529 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08005530
Herbert Xud565b0a2008-12-15 23:38:52 -08005531 if (pp) {
David S. Miller992cba72018-07-31 15:27:56 -07005532 skb_list_del_init(pp);
David Millerd4546c22018-06-24 14:13:49 +09005533 napi_gro_complete(pp);
Li RongQing6312fe72018-07-05 14:34:32 +08005534 napi->gro_hash[hash].count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08005535 }
5536
Herbert Xu0da2afd52008-12-26 14:57:42 -08005537 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08005538 goto ok;
5539
Eric Dumazet600adc12014-01-09 14:12:19 -08005540 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08005541 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08005542
Li RongQing6312fe72018-07-05 14:34:32 +08005543 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
5544 gro_flush_oldest(gro_head);
Eric Dumazet600adc12014-01-09 14:12:19 -08005545 } else {
Li RongQing6312fe72018-07-05 14:34:32 +08005546 napi->gro_hash[hash].count++;
Eric Dumazet600adc12014-01-09 14:12:19 -08005547 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005548 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00005549 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07005550 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00005551 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
David Miller07d78362018-06-24 14:14:02 +09005552 list_add(&skb->list, gro_head);
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005553 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08005554
Herbert Xuad0f99042009-02-01 01:24:55 -08005555pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005556 grow = skb_gro_offset(skb) - skb_headlen(skb);
5557 if (grow > 0)
5558 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08005559ok:
Li RongQingd9f37d02018-07-13 14:41:36 +08005560 if (napi->gro_hash[hash].count) {
5561 if (!test_bit(hash, &napi->gro_bitmask))
5562 __set_bit(hash, &napi->gro_bitmask);
5563 } else if (test_bit(hash, &napi->gro_bitmask)) {
5564 __clear_bit(hash, &napi->gro_bitmask);
5565 }
5566
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005567 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08005568
5569normal:
Herbert Xuad0f99042009-02-01 01:24:55 -08005570 ret = GRO_NORMAL;
5571 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08005572}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005573
Jerry Chubf5a7552014-01-07 10:23:19 -08005574struct packet_offload *gro_find_receive_by_type(__be16 type)
5575{
5576 struct list_head *offload_head = &offload_base;
5577 struct packet_offload *ptype;
5578
5579 list_for_each_entry_rcu(ptype, offload_head, list) {
5580 if (ptype->type != type || !ptype->callbacks.gro_receive)
5581 continue;
5582 return ptype;
5583 }
5584 return NULL;
5585}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005586EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08005587
5588struct packet_offload *gro_find_complete_by_type(__be16 type)
5589{
5590 struct list_head *offload_head = &offload_base;
5591 struct packet_offload *ptype;
5592
5593 list_for_each_entry_rcu(ptype, offload_head, list) {
5594 if (ptype->type != type || !ptype->callbacks.gro_complete)
5595 continue;
5596 return ptype;
5597 }
5598 return NULL;
5599}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005600EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005601
Michal Kubečeke44699d2017-06-29 11:13:36 +02005602static void napi_skb_free_stolen_head(struct sk_buff *skb)
5603{
5604 skb_dst_drop(skb);
5605 secpath_reset(skb);
5606 kmem_cache_free(skbuff_head_cache, skb);
5607}
5608
Rami Rosenbb728822012-11-28 21:55:25 +00005609static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08005610{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005611 switch (ret) {
5612 case GRO_NORMAL:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005613 if (netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005614 ret = GRO_DROP;
5615 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005616
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005617 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08005618 kfree_skb(skb);
5619 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005620
Eric Dumazetdaa86542012-04-19 07:07:40 +00005621 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02005622 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5623 napi_skb_free_stolen_head(skb);
5624 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00005625 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00005626 break;
5627
Ben Hutchings5b252f02009-10-29 07:17:09 +00005628 case GRO_HELD:
5629 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005630 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005631 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005632 }
5633
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005634 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005635}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005636
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005637gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005638{
Eric Dumazet93f93a42015-11-18 06:30:59 -08005639 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005640 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00005641
Eric Dumazeta50e2332014-03-29 21:28:21 -07005642 skb_gro_reset_offset(skb);
5643
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005644 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005645}
5646EXPORT_SYMBOL(napi_gro_receive);
5647
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00005648static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005649{
Eric Dumazet93a35f52014-10-23 06:30:30 -07005650 if (unlikely(skb->pfmemalloc)) {
5651 consume_skb(skb);
5652 return;
5653 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005654 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00005655 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5656 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Michał Mirosławb18175242018-11-09 00:18:02 +01005657 __vlan_hwaccel_clear_tag(skb);
Herbert Xu66c46d72011-01-29 20:44:54 -08005658 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08005659 skb->skb_iif = 0;
Jerry Chuc3caf112014-07-14 15:54:46 -07005660 skb->encapsulation = 0;
5661 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07005662 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Steffen Klassertf991bb92017-01-30 06:45:38 +01005663 secpath_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005664
5665 napi->skb = skb;
5666}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005667
Herbert Xu76620aa2009-04-16 02:02:07 -07005668struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005669{
Herbert Xu5d38a072009-01-04 16:13:40 -08005670 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005671
5672 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005673 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005674 if (skb) {
5675 napi->skb = skb;
5676 skb_mark_napi_id(skb, napi);
5677 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005678 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005679 return skb;
5680}
Herbert Xu76620aa2009-04-16 02:02:07 -07005681EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005682
Eric Dumazeta50e2332014-03-29 21:28:21 -07005683static gro_result_t napi_frags_finish(struct napi_struct *napi,
5684 struct sk_buff *skb,
5685 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005686{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005687 switch (ret) {
5688 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005689 case GRO_HELD:
5690 __skb_push(skb, ETH_HLEN);
5691 skb->protocol = eth_type_trans(skb, skb->dev);
5692 if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005693 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00005694 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005695
5696 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005697 napi_reuse_skb(napi, skb);
5698 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005699
Michal Kubečeke44699d2017-06-29 11:13:36 +02005700 case GRO_MERGED_FREE:
5701 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5702 napi_skb_free_stolen_head(skb);
5703 else
5704 napi_reuse_skb(napi, skb);
5705 break;
5706
Ben Hutchings5b252f02009-10-29 07:17:09 +00005707 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005708 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005709 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005710 }
5711
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005712 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005713}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005714
Eric Dumazeta50e2332014-03-29 21:28:21 -07005715/* Upper GRO stack assumes network header starts at gro_offset=0
5716 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5717 * We copy ethernet header into skb->data to have a common layout.
5718 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005719static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005720{
Herbert Xu76620aa2009-04-16 02:02:07 -07005721 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005722 const struct ethhdr *eth;
5723 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005724
5725 napi->skb = NULL;
5726
Eric Dumazeta50e2332014-03-29 21:28:21 -07005727 skb_reset_mac_header(skb);
5728 skb_gro_reset_offset(skb);
5729
5730 eth = skb_gro_header_fast(skb, 0);
5731 if (unlikely(skb_gro_header_hard(skb, hlen))) {
5732 eth = skb_gro_header_slow(skb, hlen, 0);
5733 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04005734 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
5735 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005736 napi_reuse_skb(napi, skb);
5737 return NULL;
5738 }
5739 } else {
5740 gro_pull_from_frag0(skb, hlen);
5741 NAPI_GRO_CB(skb)->frag0 += hlen;
5742 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07005743 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07005744 __skb_pull(skb, hlen);
5745
5746 /*
5747 * This works because the only protocols we care about don't require
5748 * special handling.
5749 * We'll fix it up properly in napi_frags_finish()
5750 */
5751 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07005752
Herbert Xu76620aa2009-04-16 02:02:07 -07005753 return skb;
5754}
Herbert Xu76620aa2009-04-16 02:02:07 -07005755
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005756gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07005757{
5758 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005759
5760 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005761 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08005762
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005763 trace_napi_gro_frags_entry(skb);
5764
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005765 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
Herbert Xu5d38a072009-01-04 16:13:40 -08005766}
5767EXPORT_SYMBOL(napi_gro_frags);
5768
Tom Herbert573e8fc2014-08-22 13:33:47 -07005769/* Compute the checksum from gro_offset and return the folded value
5770 * after adding in any pseudo checksum.
5771 */
5772__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
5773{
5774 __wsum wsum;
5775 __sum16 sum;
5776
5777 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
5778
5779 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
5780 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
5781 if (likely(!sum)) {
5782 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
5783 !skb->csum_complete_sw)
5784 netdev_rx_csum_fault(skb->dev);
5785 }
5786
5787 NAPI_GRO_CB(skb)->csum = wsum;
5788 NAPI_GRO_CB(skb)->csum_valid = 1;
5789
5790 return sum;
5791}
5792EXPORT_SYMBOL(__skb_gro_checksum_complete);
5793
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305794static void net_rps_send_ipi(struct softnet_data *remsd)
5795{
5796#ifdef CONFIG_RPS
5797 while (remsd) {
5798 struct softnet_data *next = remsd->rps_ipi_next;
5799
5800 if (cpu_online(remsd->cpu))
5801 smp_call_function_single_async(remsd->cpu, &remsd->csd);
5802 remsd = next;
5803 }
5804#endif
5805}
5806
Eric Dumazete326bed2010-04-22 00:22:45 -07005807/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08005808 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07005809 * Note: called with local irq disabled, but exits with local irq enabled.
5810 */
5811static void net_rps_action_and_irq_enable(struct softnet_data *sd)
5812{
5813#ifdef CONFIG_RPS
5814 struct softnet_data *remsd = sd->rps_ipi_list;
5815
5816 if (remsd) {
5817 sd->rps_ipi_list = NULL;
5818
5819 local_irq_enable();
5820
5821 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305822 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07005823 } else
5824#endif
5825 local_irq_enable();
5826}
5827
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005828static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5829{
5830#ifdef CONFIG_RPS
5831 return sd->rps_ipi_list != NULL;
5832#else
5833 return false;
5834#endif
5835}
5836
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005837static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005839 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005840 bool again = true;
5841 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Eric Dumazete326bed2010-04-22 00:22:45 -07005843 /* Check if we have pending ipi, its better to send them now,
5844 * not waiting net_rx_action() end.
5845 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005846 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07005847 local_irq_disable();
5848 net_rps_action_and_irq_enable(sd);
5849 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005850
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01005851 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005852 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Changli Gao6e7676c2010-04-27 15:07:33 -07005855 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03005856 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07005857 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03005858 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00005859 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005860 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00005861 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005862
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005865 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005866 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07005867 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005868 /*
5869 * Inline a custom version of __napi_complete().
5870 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07005871 * and NAPI_STATE_SCHED is the only possible flag set
5872 * on backlog.
5873 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005874 * and we dont need an smp_mb() memory barrier.
5875 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005876 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005877 again = false;
5878 } else {
5879 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5880 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07005881 }
5882 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005883 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005886 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887}
5888
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005889/**
5890 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07005891 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005892 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005893 * The entry's receive function will be scheduled to run.
5894 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005895 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08005896void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005897{
5898 unsigned long flags;
5899
5900 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05005901 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005902 local_irq_restore(flags);
5903}
5904EXPORT_SYMBOL(__napi_schedule);
5905
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005906/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08005907 * napi_schedule_prep - check if napi can be scheduled
5908 * @n: napi context
5909 *
5910 * Test if NAPI routine is already running, and if not mark
5911 * it as running. This is used as a condition variable
5912 * insure only one NAPI poll instance runs. We also make
5913 * sure there is no pending NAPI disable.
5914 */
5915bool napi_schedule_prep(struct napi_struct *n)
5916{
5917 unsigned long val, new;
5918
5919 do {
5920 val = READ_ONCE(n->state);
5921 if (unlikely(val & NAPIF_STATE_DISABLE))
5922 return false;
5923 new = val | NAPIF_STATE_SCHED;
5924
5925 /* Sets STATE_MISSED bit if STATE_SCHED was already set
5926 * This was suggested by Alexander Duyck, as compiler
5927 * emits better code than :
5928 * if (val & NAPIF_STATE_SCHED)
5929 * new |= NAPIF_STATE_MISSED;
5930 */
5931 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5932 NAPIF_STATE_MISSED;
5933 } while (cmpxchg(&n->state, val, new) != val);
5934
5935 return !(val & NAPIF_STATE_SCHED);
5936}
5937EXPORT_SYMBOL(napi_schedule_prep);
5938
5939/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005940 * __napi_schedule_irqoff - schedule for receive
5941 * @n: entry to schedule
5942 *
5943 * Variant of __napi_schedule() assuming hard irqs are masked
5944 */
5945void __napi_schedule_irqoff(struct napi_struct *n)
5946{
5947 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5948}
5949EXPORT_SYMBOL(__napi_schedule_irqoff);
5950
Eric Dumazet364b6052016-11-15 10:15:13 -08005951bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08005952{
Eric Dumazet39e6c822017-02-28 10:34:50 -08005953 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08005954
5955 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08005956 * 1) Don't let napi dequeue from the cpu poll list
5957 * just in case its running on a different cpu.
5958 * 2) If we are busy polling, do nothing here, we have
5959 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08005960 */
Eric Dumazet217f6972016-11-15 10:15:11 -08005961 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
5962 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08005963 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08005964
Li RongQingd9f37d02018-07-13 14:41:36 +08005965 if (n->gro_bitmask) {
Eric Dumazet3b47d302014-11-06 21:09:44 -08005966 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005967
Eric Dumazet3b47d302014-11-06 21:09:44 -08005968 if (work_done)
5969 timeout = n->dev->gro_flush_timeout;
5970
5971 if (timeout)
5972 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5973 HRTIMER_MODE_REL_PINNED);
5974 else
5975 napi_gro_flush(n, false);
5976 }
Eric Dumazet02c16022017-02-04 15:25:02 -08005977 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005978 /* If n->poll_list is not empty, we need to mask irqs */
5979 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08005980 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005981 local_irq_restore(flags);
5982 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08005983
5984 do {
5985 val = READ_ONCE(n->state);
5986
5987 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
5988
5989 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
5990
5991 /* If STATE_MISSED was set, leave STATE_SCHED set,
5992 * because we will call napi->poll() one more time.
5993 * This C code was suggested by Alexander Duyck to help gcc.
5994 */
5995 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
5996 NAPIF_STATE_SCHED;
5997 } while (cmpxchg(&n->state, val, new) != val);
5998
5999 if (unlikely(val & NAPIF_STATE_MISSED)) {
6000 __napi_schedule(n);
6001 return false;
6002 }
6003
Eric Dumazet364b6052016-11-15 10:15:13 -08006004 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08006005}
Eric Dumazet3b47d302014-11-06 21:09:44 -08006006EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08006007
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006008/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08006009static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006010{
6011 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6012 struct napi_struct *napi;
6013
6014 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6015 if (napi->napi_id == napi_id)
6016 return napi;
6017
6018 return NULL;
6019}
Eric Dumazet02d62e82015-11-18 06:30:52 -08006020
6021#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08006022
Eric Dumazetce6aea92015-11-18 06:30:54 -08006023#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08006024
6025static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6026{
6027 int rc;
6028
Eric Dumazet39e6c822017-02-28 10:34:50 -08006029 /* Busy polling means there is a high chance device driver hard irq
6030 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6031 * set in napi_schedule_prep().
6032 * Since we are about to call napi->poll() once more, we can safely
6033 * clear NAPI_STATE_MISSED.
6034 *
6035 * Note: x86 could use a single "lock and ..." instruction
6036 * to perform these two clear_bit()
6037 */
6038 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08006039 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6040
6041 local_bh_disable();
6042
6043 /* All we really want here is to re-enable device interrupts.
6044 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6045 */
6046 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02006047 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006048 netpoll_poll_unlock(have_poll_lock);
6049 if (rc == BUSY_POLL_BUDGET)
6050 __napi_schedule(napi);
6051 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006052}
6053
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006054void napi_busy_loop(unsigned int napi_id,
6055 bool (*loop_end)(void *, unsigned long),
6056 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08006057{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006058 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08006059 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08006060 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006061 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08006062
6063restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08006064 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006065
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006066 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006067
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006068 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006069 if (!napi)
6070 goto out;
6071
Eric Dumazet217f6972016-11-15 10:15:11 -08006072 preempt_disable();
6073 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006074 int work = 0;
6075
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006076 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08006077 if (!napi_poll) {
6078 unsigned long val = READ_ONCE(napi->state);
6079
6080 /* If multiple threads are competing for this napi,
6081 * we avoid dirtying napi->state as much as we can.
6082 */
6083 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6084 NAPIF_STATE_IN_BUSY_POLL))
6085 goto count;
6086 if (cmpxchg(&napi->state, val,
6087 val | NAPIF_STATE_IN_BUSY_POLL |
6088 NAPIF_STATE_SCHED) != val)
6089 goto count;
6090 have_poll_lock = netpoll_poll_lock(napi);
6091 napi_poll = napi->poll;
6092 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006093 work = napi_poll(napi, BUSY_POLL_BUDGET);
6094 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08006095count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006096 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006097 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006098 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006099 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006100
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006101 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08006102 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08006103
Eric Dumazet217f6972016-11-15 10:15:11 -08006104 if (unlikely(need_resched())) {
6105 if (napi_poll)
6106 busy_poll_stop(napi, have_poll_lock);
6107 preempt_enable();
6108 rcu_read_unlock();
6109 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006110 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07006111 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08006112 goto restart;
6113 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08006114 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08006115 }
6116 if (napi_poll)
6117 busy_poll_stop(napi, have_poll_lock);
6118 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006119out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08006120 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08006121}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07006122EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08006123
6124#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006125
Eric Dumazet149d6ad2016-11-08 11:07:28 -08006126static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006127{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08006128 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6129 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006130 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006131
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006132 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006133
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006134 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006135 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07006136 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6137 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006138 } while (napi_by_id(napi_gen_id));
6139 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006140
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006141 hlist_add_head_rcu(&napi->napi_hash_node,
6142 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006143
Eric Dumazet52bd2d62015-11-18 06:30:50 -08006144 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006145}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006146
6147/* Warning : caller is responsible to make sure rcu grace period
6148 * is respected before freeing memory containing @napi
6149 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08006150bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006151{
Eric Dumazet34cbe272015-11-18 06:31:02 -08006152 bool rcu_sync_needed = false;
6153
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006154 spin_lock(&napi_hash_lock);
6155
Eric Dumazet34cbe272015-11-18 06:31:02 -08006156 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6157 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006158 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006159 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006160 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08006161 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03006162}
6163EXPORT_SYMBOL_GPL(napi_hash_del);
6164
Eric Dumazet3b47d302014-11-06 21:09:44 -08006165static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6166{
6167 struct napi_struct *napi;
6168
6169 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08006170
6171 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6172 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6173 */
Li RongQingd9f37d02018-07-13 14:41:36 +08006174 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
Eric Dumazet39e6c822017-02-28 10:34:50 -08006175 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6176 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006177
6178 return HRTIMER_NORESTART;
6179}
6180
David S. Miller7c4ec742018-07-20 23:37:55 -07006181static void init_gro_hash(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08006182{
David Miller07d78362018-06-24 14:14:02 +09006183 int i;
6184
Li RongQing6312fe72018-07-05 14:34:32 +08006185 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6186 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6187 napi->gro_hash[i].count = 0;
6188 }
David S. Miller7c4ec742018-07-20 23:37:55 -07006189 napi->gro_bitmask = 0;
6190}
6191
6192void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6193 int (*poll)(struct napi_struct *, int), int weight)
6194{
6195 INIT_LIST_HEAD(&napi->poll_list);
6196 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6197 napi->timer.function = napi_watchdog;
6198 init_gro_hash(napi);
Herbert Xu5d38a072009-01-04 16:13:40 -08006199 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08006200 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00006201 if (weight > NAPI_POLL_WEIGHT)
6202 pr_err_once("netif_napi_add() called with weight %d on device %s\n",
6203 weight, dev->name);
Herbert Xud565b0a2008-12-15 23:38:52 -08006204 napi->weight = weight;
6205 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08006206 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08006207#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08006208 napi->poll_owner = -1;
6209#endif
6210 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08006211 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006212}
6213EXPORT_SYMBOL(netif_napi_add);
6214
Eric Dumazet3b47d302014-11-06 21:09:44 -08006215void napi_disable(struct napi_struct *n)
6216{
6217 might_sleep();
6218 set_bit(NAPI_STATE_DISABLE, &n->state);
6219
6220 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6221 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04006222 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6223 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08006224
6225 hrtimer_cancel(&n->timer);
6226
6227 clear_bit(NAPI_STATE_DISABLE, &n->state);
6228}
6229EXPORT_SYMBOL(napi_disable);
6230
David Miller07d78362018-06-24 14:14:02 +09006231static void flush_gro_hash(struct napi_struct *napi)
David Millerd4546c22018-06-24 14:13:49 +09006232{
David Miller07d78362018-06-24 14:14:02 +09006233 int i;
David Millerd4546c22018-06-24 14:13:49 +09006234
David Miller07d78362018-06-24 14:14:02 +09006235 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6236 struct sk_buff *skb, *n;
6237
Li RongQing6312fe72018-07-05 14:34:32 +08006238 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
David Miller07d78362018-06-24 14:14:02 +09006239 kfree_skb(skb);
Li RongQing6312fe72018-07-05 14:34:32 +08006240 napi->gro_hash[i].count = 0;
David Miller07d78362018-06-24 14:14:02 +09006241 }
David Millerd4546c22018-06-24 14:13:49 +09006242}
6243
Eric Dumazet93d05d42015-11-18 06:31:03 -08006244/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08006245void netif_napi_del(struct napi_struct *napi)
6246{
Eric Dumazet93d05d42015-11-18 06:31:03 -08006247 might_sleep();
6248 if (napi_hash_del(napi))
6249 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08006250 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07006251 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08006252
David Miller07d78362018-06-24 14:14:02 +09006253 flush_gro_hash(napi);
Li RongQingd9f37d02018-07-13 14:41:36 +08006254 napi->gro_bitmask = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08006255}
6256EXPORT_SYMBOL(netif_napi_del);
6257
Herbert Xu726ce702014-12-21 07:16:21 +11006258static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6259{
6260 void *have;
6261 int work, weight;
6262
6263 list_del_init(&n->poll_list);
6264
6265 have = netpoll_poll_lock(n);
6266
6267 weight = n->weight;
6268
6269 /* This NAPI_STATE_SCHED test is for avoiding a race
6270 * with netpoll's poll_napi(). Only the entity which
6271 * obtains the lock and sees NAPI_STATE_SCHED set will
6272 * actually make the ->poll() call. Therefore we avoid
6273 * accidentally calling ->poll() when NAPI is not scheduled.
6274 */
6275 work = 0;
6276 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6277 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02006278 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11006279 }
6280
6281 WARN_ON_ONCE(work > weight);
6282
6283 if (likely(work < weight))
6284 goto out_unlock;
6285
6286 /* Drivers must not modify the NAPI state if they
6287 * consume the entire weight. In such cases this code
6288 * still "owns" the NAPI instance and therefore can
6289 * move the instance around on the list at-will.
6290 */
6291 if (unlikely(napi_disable_pending(n))) {
6292 napi_complete(n);
6293 goto out_unlock;
6294 }
6295
Li RongQingd9f37d02018-07-13 14:41:36 +08006296 if (n->gro_bitmask) {
Herbert Xu726ce702014-12-21 07:16:21 +11006297 /* flush too old packets
6298 * If HZ < 1000, flush all packets.
6299 */
6300 napi_gro_flush(n, HZ >= 1000);
6301 }
6302
Herbert Xu001ce542014-12-21 07:16:22 +11006303 /* Some drivers may have called napi_schedule
6304 * prior to exhausting their budget.
6305 */
6306 if (unlikely(!list_empty(&n->poll_list))) {
6307 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6308 n->dev ? n->dev->name : "backlog");
6309 goto out_unlock;
6310 }
6311
Herbert Xu726ce702014-12-21 07:16:21 +11006312 list_add_tail(&n->poll_list, repoll);
6313
6314out_unlock:
6315 netpoll_poll_unlock(have);
6316
6317 return work;
6318}
6319
Emese Revfy0766f782016-06-20 20:42:34 +02006320static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321{
Christoph Lameter903ceff2014-08-17 12:30:35 -05006322 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04006323 unsigned long time_limit = jiffies +
6324 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07006325 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006326 LIST_HEAD(list);
6327 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07006328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006330 list_splice_init(&sd->poll_list, &list);
6331 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006333 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006334 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006336 if (list_empty(&list)) {
6337 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006338 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006339 break;
6340 }
6341
Herbert Xu6bd373e2014-12-21 07:16:24 +11006342 n = list_first_entry(&list, struct napi_struct, poll_list);
6343 budget -= napi_poll(n, &repoll);
6344
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006345 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08006346 * Allow this to run for 2 jiffies since which will allow
6347 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006348 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11006349 if (unlikely(budget <= 0 ||
6350 time_after_eq(jiffies, time_limit))) {
6351 sd->time_squeeze++;
6352 break;
6353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006355
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08006356 local_irq_disable();
6357
6358 list_splice_tail_init(&sd->poll_list, &list);
6359 list_splice_tail(&repoll, &list);
6360 list_splice(&list, &sd->poll_list);
6361 if (!list_empty(&sd->poll_list))
6362 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6363
Eric Dumazete326bed2010-04-22 00:22:45 -07006364 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08006365out:
6366 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367}
6368
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006369struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006370 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006371
6372 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006373 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006374
Veaceslav Falico5d261912013-08-28 23:25:05 +02006375 /* counter for the number of times this device was added to us */
6376 u16 ref_nr;
6377
Veaceslav Falico402dae92013-09-25 09:20:09 +02006378 /* private field for the users */
6379 void *private;
6380
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006381 struct list_head list;
6382 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006383};
6384
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006385static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006386 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006387{
Veaceslav Falico5d261912013-08-28 23:25:05 +02006388 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006389
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006390 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02006391 if (adj->dev == adj_dev)
6392 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006393 }
6394 return NULL;
6395}
6396
David Ahernf1170fd2016-10-17 19:15:51 -07006397static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
6398{
6399 struct net_device *dev = data;
6400
6401 return upper_dev == dev;
6402}
6403
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006404/**
6405 * netdev_has_upper_dev - Check if device is linked to an upper device
6406 * @dev: device
6407 * @upper_dev: upper device to check
6408 *
6409 * Find out if a device is linked to specified upper device and return true
6410 * in case it is. Note that this checks only immediate upper device,
6411 * not through a complete stack of devices. The caller must hold the RTNL lock.
6412 */
6413bool netdev_has_upper_dev(struct net_device *dev,
6414 struct net_device *upper_dev)
6415{
6416 ASSERT_RTNL();
6417
David Ahernf1170fd2016-10-17 19:15:51 -07006418 return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
6419 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006420}
6421EXPORT_SYMBOL(netdev_has_upper_dev);
6422
6423/**
David Ahern1a3f0602016-10-17 19:15:44 -07006424 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6425 * @dev: device
6426 * @upper_dev: upper device to check
6427 *
6428 * Find out if a device is linked to specified upper device and return true
6429 * in case it is. Note that this checks the entire upper device chain.
6430 * The caller must hold rcu lock.
6431 */
6432
David Ahern1a3f0602016-10-17 19:15:44 -07006433bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6434 struct net_device *upper_dev)
6435{
6436 return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
6437 upper_dev);
6438}
6439EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6440
6441/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006442 * netdev_has_any_upper_dev - Check if device is linked to some device
6443 * @dev: device
6444 *
6445 * Find out if a device is linked to an upper device and return true in case
6446 * it is. The caller must hold the RTNL lock.
6447 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006448bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006449{
6450 ASSERT_RTNL();
6451
David Ahernf1170fd2016-10-17 19:15:51 -07006452 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006453}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02006454EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006455
6456/**
6457 * netdev_master_upper_dev_get - Get master upper device
6458 * @dev: device
6459 *
6460 * Find a master upper device and return pointer to it or NULL in case
6461 * it's not there. The caller must hold the RTNL lock.
6462 */
6463struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6464{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006465 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006466
6467 ASSERT_RTNL();
6468
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006469 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006470 return NULL;
6471
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006472 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006473 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006474 if (likely(upper->master))
6475 return upper->dev;
6476 return NULL;
6477}
6478EXPORT_SYMBOL(netdev_master_upper_dev_get);
6479
David Ahern0f524a82016-10-17 19:15:52 -07006480/**
6481 * netdev_has_any_lower_dev - Check if device is linked to some device
6482 * @dev: device
6483 *
6484 * Find out if a device is linked to a lower device and return true in case
6485 * it is. The caller must hold the RTNL lock.
6486 */
6487static bool netdev_has_any_lower_dev(struct net_device *dev)
6488{
6489 ASSERT_RTNL();
6490
6491 return !list_empty(&dev->adj_list.lower);
6492}
6493
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02006494void *netdev_adjacent_get_private(struct list_head *adj_list)
6495{
6496 struct netdev_adjacent *adj;
6497
6498 adj = list_entry(adj_list, struct netdev_adjacent, list);
6499
6500 return adj->private;
6501}
6502EXPORT_SYMBOL(netdev_adjacent_get_private);
6503
Veaceslav Falico31088a12013-09-25 09:20:12 +02006504/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04006505 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6506 * @dev: device
6507 * @iter: list_head ** of the current position
6508 *
6509 * Gets the next device from the dev's upper list, starting from iter
6510 * position. The caller must hold RCU read lock.
6511 */
6512struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6513 struct list_head **iter)
6514{
6515 struct netdev_adjacent *upper;
6516
6517 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6518
6519 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6520
6521 if (&upper->list == &dev->adj_list.upper)
6522 return NULL;
6523
6524 *iter = &upper->list;
6525
6526 return upper->dev;
6527}
6528EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6529
David Ahern1a3f0602016-10-17 19:15:44 -07006530static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6531 struct list_head **iter)
6532{
6533 struct netdev_adjacent *upper;
6534
6535 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6536
6537 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6538
6539 if (&upper->list == &dev->adj_list.upper)
6540 return NULL;
6541
6542 *iter = &upper->list;
6543
6544 return upper->dev;
6545}
6546
6547int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6548 int (*fn)(struct net_device *dev,
6549 void *data),
6550 void *data)
6551{
6552 struct net_device *udev;
6553 struct list_head *iter;
6554 int ret;
6555
6556 for (iter = &dev->adj_list.upper,
6557 udev = netdev_next_upper_dev_rcu(dev, &iter);
6558 udev;
6559 udev = netdev_next_upper_dev_rcu(dev, &iter)) {
6560 /* first is the upper device itself */
6561 ret = fn(udev, data);
6562 if (ret)
6563 return ret;
6564
6565 /* then look at all of its upper devices */
6566 ret = netdev_walk_all_upper_dev_rcu(udev, fn, data);
6567 if (ret)
6568 return ret;
6569 }
6570
6571 return 0;
6572}
6573EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6574
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006575/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02006576 * netdev_lower_get_next_private - Get the next ->private from the
6577 * lower neighbour list
6578 * @dev: device
6579 * @iter: list_head ** of the current position
6580 *
6581 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6582 * list, starting from iter position. The caller must hold either hold the
6583 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006584 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02006585 */
6586void *netdev_lower_get_next_private(struct net_device *dev,
6587 struct list_head **iter)
6588{
6589 struct netdev_adjacent *lower;
6590
6591 lower = list_entry(*iter, struct netdev_adjacent, list);
6592
6593 if (&lower->list == &dev->adj_list.lower)
6594 return NULL;
6595
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006596 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006597
6598 return lower->private;
6599}
6600EXPORT_SYMBOL(netdev_lower_get_next_private);
6601
6602/**
6603 * netdev_lower_get_next_private_rcu - Get the next ->private from the
6604 * lower neighbour list, RCU
6605 * variant
6606 * @dev: device
6607 * @iter: list_head ** of the current position
6608 *
6609 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6610 * list, starting from iter position. The caller must hold RCU read lock.
6611 */
6612void *netdev_lower_get_next_private_rcu(struct net_device *dev,
6613 struct list_head **iter)
6614{
6615 struct netdev_adjacent *lower;
6616
6617 WARN_ON_ONCE(!rcu_read_lock_held());
6618
6619 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6620
6621 if (&lower->list == &dev->adj_list.lower)
6622 return NULL;
6623
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006624 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006625
6626 return lower->private;
6627}
6628EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
6629
6630/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006631 * netdev_lower_get_next - Get the next device from the lower neighbour
6632 * list
6633 * @dev: device
6634 * @iter: list_head ** of the current position
6635 *
6636 * Gets the next netdev_adjacent from the dev's lower neighbour
6637 * list, starting from iter position. The caller must hold RTNL lock or
6638 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006639 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006640 */
6641void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
6642{
6643 struct netdev_adjacent *lower;
6644
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006645 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006646
6647 if (&lower->list == &dev->adj_list.lower)
6648 return NULL;
6649
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006650 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006651
6652 return lower->dev;
6653}
6654EXPORT_SYMBOL(netdev_lower_get_next);
6655
David Ahern1a3f0602016-10-17 19:15:44 -07006656static struct net_device *netdev_next_lower_dev(struct net_device *dev,
6657 struct list_head **iter)
6658{
6659 struct netdev_adjacent *lower;
6660
David Ahern46b5ab12016-10-26 13:21:33 -07006661 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07006662
6663 if (&lower->list == &dev->adj_list.lower)
6664 return NULL;
6665
David Ahern46b5ab12016-10-26 13:21:33 -07006666 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07006667
6668 return lower->dev;
6669}
6670
6671int netdev_walk_all_lower_dev(struct net_device *dev,
6672 int (*fn)(struct net_device *dev,
6673 void *data),
6674 void *data)
6675{
6676 struct net_device *ldev;
6677 struct list_head *iter;
6678 int ret;
6679
6680 for (iter = &dev->adj_list.lower,
6681 ldev = netdev_next_lower_dev(dev, &iter);
6682 ldev;
6683 ldev = netdev_next_lower_dev(dev, &iter)) {
6684 /* first is the lower device itself */
6685 ret = fn(ldev, data);
6686 if (ret)
6687 return ret;
6688
6689 /* then look at all of its lower devices */
6690 ret = netdev_walk_all_lower_dev(ldev, fn, data);
6691 if (ret)
6692 return ret;
6693 }
6694
6695 return 0;
6696}
6697EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
6698
David Ahern1a3f0602016-10-17 19:15:44 -07006699static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
6700 struct list_head **iter)
6701{
6702 struct netdev_adjacent *lower;
6703
6704 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6705 if (&lower->list == &dev->adj_list.lower)
6706 return NULL;
6707
6708 *iter = &lower->list;
6709
6710 return lower->dev;
6711}
6712
6713int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
6714 int (*fn)(struct net_device *dev,
6715 void *data),
6716 void *data)
6717{
6718 struct net_device *ldev;
6719 struct list_head *iter;
6720 int ret;
6721
6722 for (iter = &dev->adj_list.lower,
6723 ldev = netdev_next_lower_dev_rcu(dev, &iter);
6724 ldev;
6725 ldev = netdev_next_lower_dev_rcu(dev, &iter)) {
6726 /* first is the lower device itself */
6727 ret = fn(ldev, data);
6728 if (ret)
6729 return ret;
6730
6731 /* then look at all of its lower devices */
6732 ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data);
6733 if (ret)
6734 return ret;
6735 }
6736
6737 return 0;
6738}
6739EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
6740
Jiri Pirko7ce856a2016-07-04 08:23:12 +02006741/**
dingtianhonge001bfa2013-12-13 10:19:55 +08006742 * netdev_lower_get_first_private_rcu - Get the first ->private from the
6743 * lower neighbour list, RCU
6744 * variant
6745 * @dev: device
6746 *
6747 * Gets the first netdev_adjacent->private from the dev's lower neighbour
6748 * list. The caller must hold RCU read lock.
6749 */
6750void *netdev_lower_get_first_private_rcu(struct net_device *dev)
6751{
6752 struct netdev_adjacent *lower;
6753
6754 lower = list_first_or_null_rcu(&dev->adj_list.lower,
6755 struct netdev_adjacent, list);
6756 if (lower)
6757 return lower->private;
6758 return NULL;
6759}
6760EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
6761
6762/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006763 * netdev_master_upper_dev_get_rcu - Get master upper device
6764 * @dev: device
6765 *
6766 * Find a master upper device and return pointer to it or NULL in case
6767 * it's not there. The caller must hold the RCU read lock.
6768 */
6769struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
6770{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006771 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006772
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006773 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006774 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006775 if (upper && likely(upper->master))
6776 return upper->dev;
6777 return NULL;
6778}
6779EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
6780
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306781static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006782 struct net_device *adj_dev,
6783 struct list_head *dev_list)
6784{
6785 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006786
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006787 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6788 "upper_%s" : "lower_%s", adj_dev->name);
6789 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
6790 linkname);
6791}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306792static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006793 char *name,
6794 struct list_head *dev_list)
6795{
6796 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006797
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006798 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6799 "upper_%s" : "lower_%s", name);
6800 sysfs_remove_link(&(dev->dev.kobj), linkname);
6801}
6802
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006803static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
6804 struct net_device *adj_dev,
6805 struct list_head *dev_list)
6806{
6807 return (dev_list == &dev->adj_list.upper ||
6808 dev_list == &dev->adj_list.lower) &&
6809 net_eq(dev_net(dev), dev_net(adj_dev));
6810}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006811
Veaceslav Falico5d261912013-08-28 23:25:05 +02006812static int __netdev_adjacent_dev_insert(struct net_device *dev,
6813 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02006814 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006815 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006816{
6817 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006818 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006819
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006820 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006821
6822 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07006823 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07006824 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
6825 dev->name, adj_dev->name, adj->ref_nr);
6826
Veaceslav Falico5d261912013-08-28 23:25:05 +02006827 return 0;
6828 }
6829
6830 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
6831 if (!adj)
6832 return -ENOMEM;
6833
6834 adj->dev = adj_dev;
6835 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07006836 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006837 adj->private = private;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006838 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006839
David Ahern67b62f92016-10-17 19:15:53 -07006840 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
6841 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006842
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006843 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006844 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006845 if (ret)
6846 goto free_adj;
6847 }
6848
Veaceslav Falico7863c052013-09-25 09:20:06 +02006849 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006850 if (master) {
6851 ret = sysfs_create_link(&(dev->dev.kobj),
6852 &(adj_dev->dev.kobj), "master");
6853 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02006854 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006855
Veaceslav Falico7863c052013-09-25 09:20:06 +02006856 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006857 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02006858 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006859 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006860
6861 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006862
Veaceslav Falico5831d662013-09-25 09:20:32 +02006863remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006864 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006865 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006866free_adj:
6867 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02006868 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006869
6870 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006871}
6872
stephen hemminger1d143d92013-12-29 14:01:29 -08006873static void __netdev_adjacent_dev_remove(struct net_device *dev,
6874 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006875 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006876 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006877{
6878 struct netdev_adjacent *adj;
6879
David Ahern67b62f92016-10-17 19:15:53 -07006880 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
6881 dev->name, adj_dev->name, ref_nr);
6882
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006883 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006884
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006885 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07006886 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006887 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07006888 WARN_ON(1);
6889 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006890 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006891
Andrew Collins93409032016-10-03 13:43:02 -06006892 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07006893 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
6894 dev->name, adj_dev->name, ref_nr,
6895 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06006896 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006897 return;
6898 }
6899
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006900 if (adj->master)
6901 sysfs_remove_link(&(dev->dev.kobj), "master");
6902
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006903 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006904 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006905
Veaceslav Falico5d261912013-08-28 23:25:05 +02006906 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07006907 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006908 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006909 dev_put(adj_dev);
6910 kfree_rcu(adj, rcu);
6911}
6912
stephen hemminger1d143d92013-12-29 14:01:29 -08006913static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
6914 struct net_device *upper_dev,
6915 struct list_head *up_list,
6916 struct list_head *down_list,
6917 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006918{
6919 int ret;
6920
David Ahern790510d2016-10-17 19:15:43 -07006921 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06006922 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006923 if (ret)
6924 return ret;
6925
David Ahern790510d2016-10-17 19:15:43 -07006926 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06006927 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006928 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07006929 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006930 return ret;
6931 }
6932
6933 return 0;
6934}
6935
stephen hemminger1d143d92013-12-29 14:01:29 -08006936static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
6937 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006938 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006939 struct list_head *up_list,
6940 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006941{
Andrew Collins93409032016-10-03 13:43:02 -06006942 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
6943 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006944}
6945
stephen hemminger1d143d92013-12-29 14:01:29 -08006946static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
6947 struct net_device *upper_dev,
6948 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006949{
David Ahernf1170fd2016-10-17 19:15:51 -07006950 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
6951 &dev->adj_list.upper,
6952 &upper_dev->adj_list.lower,
6953 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006954}
6955
stephen hemminger1d143d92013-12-29 14:01:29 -08006956static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
6957 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006958{
Andrew Collins93409032016-10-03 13:43:02 -06006959 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006960 &dev->adj_list.upper,
6961 &upper_dev->adj_list.lower);
6962}
Veaceslav Falico5d261912013-08-28 23:25:05 +02006963
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006964static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006965 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07006966 void *upper_priv, void *upper_info,
6967 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006968{
David Ahern51d0c0472017-10-04 17:48:45 -07006969 struct netdev_notifier_changeupper_info changeupper_info = {
6970 .info = {
6971 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07006972 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07006973 },
6974 .upper_dev = upper_dev,
6975 .master = master,
6976 .linking = true,
6977 .upper_info = upper_info,
6978 };
Mike Manning50d629e2018-02-26 23:49:30 +00006979 struct net_device *master_dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006980 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006981
6982 ASSERT_RTNL();
6983
6984 if (dev == upper_dev)
6985 return -EBUSY;
6986
6987 /* To prevent loops, check if dev is not upper device to upper_dev. */
David Ahernf1170fd2016-10-17 19:15:51 -07006988 if (netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006989 return -EBUSY;
6990
Mike Manning50d629e2018-02-26 23:49:30 +00006991 if (!master) {
6992 if (netdev_has_upper_dev(dev, upper_dev))
6993 return -EEXIST;
6994 } else {
6995 master_dev = netdev_master_upper_dev_get(dev);
6996 if (master_dev)
6997 return master_dev == upper_dev ? -EEXIST : -EBUSY;
6998 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006999
David Ahern51d0c0472017-10-04 17:48:45 -07007000 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007001 &changeupper_info.info);
7002 ret = notifier_to_errno(ret);
7003 if (ret)
7004 return ret;
7005
Jiri Pirko6dffb042015-12-03 12:12:10 +01007006 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02007007 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007008 if (ret)
7009 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007010
David Ahern51d0c0472017-10-04 17:48:45 -07007011 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01007012 &changeupper_info.info);
7013 ret = notifier_to_errno(ret);
7014 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07007015 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01007016
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007017 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02007018
David Ahernf1170fd2016-10-17 19:15:51 -07007019rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007020 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007021
7022 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007023}
7024
7025/**
7026 * netdev_upper_dev_link - Add a link to the upper device
7027 * @dev: device
7028 * @upper_dev: new upper device
Florian Fainelli7a006d52018-01-22 19:14:28 -08007029 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007030 *
7031 * Adds a link to device which is upper to this one. The caller must hold
7032 * the RTNL lock. On a failure a negative errno code is returned.
7033 * On success the reference counts are adjusted and the function
7034 * returns zero.
7035 */
7036int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007037 struct net_device *upper_dev,
7038 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007039{
David Ahern42ab19e2017-10-04 17:48:47 -07007040 return __netdev_upper_dev_link(dev, upper_dev, false,
7041 NULL, NULL, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007042}
7043EXPORT_SYMBOL(netdev_upper_dev_link);
7044
7045/**
7046 * netdev_master_upper_dev_link - Add a master link to the upper device
7047 * @dev: device
7048 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01007049 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007050 * @upper_info: upper info to be passed down via notifier
Florian Fainelli7a006d52018-01-22 19:14:28 -08007051 * @extack: netlink extended ack
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007052 *
7053 * Adds a link to device which is upper to this one. In this case, only
7054 * one master upper device can be linked, although other non-master devices
7055 * might be linked as well. The caller must hold the RTNL lock.
7056 * On a failure a negative errno code is returned. On success the reference
7057 * counts are adjusted and the function returns zero.
7058 */
7059int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01007060 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07007061 void *upper_priv, void *upper_info,
7062 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007063{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01007064 return __netdev_upper_dev_link(dev, upper_dev, true,
David Ahern42ab19e2017-10-04 17:48:47 -07007065 upper_priv, upper_info, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007066}
7067EXPORT_SYMBOL(netdev_master_upper_dev_link);
7068
7069/**
7070 * netdev_upper_dev_unlink - Removes a link to upper device
7071 * @dev: device
7072 * @upper_dev: new upper device
7073 *
7074 * Removes a link to device which is upper to this one. The caller must hold
7075 * the RTNL lock.
7076 */
7077void netdev_upper_dev_unlink(struct net_device *dev,
7078 struct net_device *upper_dev)
7079{
David Ahern51d0c0472017-10-04 17:48:45 -07007080 struct netdev_notifier_changeupper_info changeupper_info = {
7081 .info = {
7082 .dev = dev,
7083 },
7084 .upper_dev = upper_dev,
7085 .linking = false,
7086 };
tchardingf4563a72017-02-09 17:56:07 +11007087
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007088 ASSERT_RTNL();
7089
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007090 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007091
David Ahern51d0c0472017-10-04 17:48:45 -07007092 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02007093 &changeupper_info.info);
7094
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007095 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02007096
David Ahern51d0c0472017-10-04 17:48:45 -07007097 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02007098 &changeupper_info.info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007099}
7100EXPORT_SYMBOL(netdev_upper_dev_unlink);
7101
Moni Shoua61bd3852015-02-03 16:48:29 +02007102/**
7103 * netdev_bonding_info_change - Dispatch event about slave change
7104 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09007105 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02007106 *
7107 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7108 * The caller must hold the RTNL lock.
7109 */
7110void netdev_bonding_info_change(struct net_device *dev,
7111 struct netdev_bonding_info *bonding_info)
7112{
David Ahern51d0c0472017-10-04 17:48:45 -07007113 struct netdev_notifier_bonding_info info = {
7114 .info.dev = dev,
7115 };
Moni Shoua61bd3852015-02-03 16:48:29 +02007116
7117 memcpy(&info.bonding_info, bonding_info,
7118 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07007119 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02007120 &info.info);
7121}
7122EXPORT_SYMBOL(netdev_bonding_info_change);
7123
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007124static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007125{
7126 struct netdev_adjacent *iter;
7127
7128 struct net *net = dev_net(dev);
7129
7130 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007131 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007132 continue;
7133 netdev_adjacent_sysfs_add(iter->dev, dev,
7134 &iter->dev->adj_list.lower);
7135 netdev_adjacent_sysfs_add(dev, iter->dev,
7136 &dev->adj_list.upper);
7137 }
7138
7139 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007140 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007141 continue;
7142 netdev_adjacent_sysfs_add(iter->dev, dev,
7143 &iter->dev->adj_list.upper);
7144 netdev_adjacent_sysfs_add(dev, iter->dev,
7145 &dev->adj_list.lower);
7146 }
7147}
7148
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007149static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007150{
7151 struct netdev_adjacent *iter;
7152
7153 struct net *net = dev_net(dev);
7154
7155 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007156 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007157 continue;
7158 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7159 &iter->dev->adj_list.lower);
7160 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7161 &dev->adj_list.upper);
7162 }
7163
7164 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007165 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007166 continue;
7167 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7168 &iter->dev->adj_list.upper);
7169 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7170 &dev->adj_list.lower);
7171 }
7172}
7173
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007174void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02007175{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007176 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02007177
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007178 struct net *net = dev_net(dev);
7179
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007180 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007181 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007182 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007183 netdev_adjacent_sysfs_del(iter->dev, oldname,
7184 &iter->dev->adj_list.lower);
7185 netdev_adjacent_sysfs_add(iter->dev, dev,
7186 &iter->dev->adj_list.lower);
7187 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007188
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007189 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08007190 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04007191 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01007192 netdev_adjacent_sysfs_del(iter->dev, oldname,
7193 &iter->dev->adj_list.upper);
7194 netdev_adjacent_sysfs_add(iter->dev, dev,
7195 &iter->dev->adj_list.upper);
7196 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02007197}
Veaceslav Falico402dae92013-09-25 09:20:09 +02007198
7199void *netdev_lower_dev_get_private(struct net_device *dev,
7200 struct net_device *lower_dev)
7201{
7202 struct netdev_adjacent *lower;
7203
7204 if (!lower_dev)
7205 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02007206 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02007207 if (!lower)
7208 return NULL;
7209
7210 return lower->private;
7211}
7212EXPORT_SYMBOL(netdev_lower_dev_get_private);
7213
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007214
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02007215int dev_get_nest_level(struct net_device *dev)
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007216{
7217 struct net_device *lower = NULL;
7218 struct list_head *iter;
7219 int max_nest = -1;
7220 int nest;
7221
7222 ASSERT_RTNL();
7223
7224 netdev_for_each_lower_dev(dev, lower, iter) {
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02007225 nest = dev_get_nest_level(lower);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007226 if (max_nest < nest)
7227 max_nest = nest;
7228 }
7229
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02007230 return max_nest + 1;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04007231}
7232EXPORT_SYMBOL(dev_get_nest_level);
7233
Jiri Pirko04d48262015-12-03 12:12:15 +01007234/**
7235 * netdev_lower_change - Dispatch event about lower device state change
7236 * @lower_dev: device
7237 * @lower_state_info: state to dispatch
7238 *
7239 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7240 * The caller must hold the RTNL lock.
7241 */
7242void netdev_lower_state_changed(struct net_device *lower_dev,
7243 void *lower_state_info)
7244{
David Ahern51d0c0472017-10-04 17:48:45 -07007245 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7246 .info.dev = lower_dev,
7247 };
Jiri Pirko04d48262015-12-03 12:12:15 +01007248
7249 ASSERT_RTNL();
7250 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07007251 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01007252 &changelowerstate_info.info);
7253}
7254EXPORT_SYMBOL(netdev_lower_state_changed);
7255
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007256static void dev_change_rx_flags(struct net_device *dev, int flags)
7257{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007258 const struct net_device_ops *ops = dev->netdev_ops;
7259
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05007260 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007261 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007262}
7263
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007264static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07007265{
Eric Dumazetb536db92011-11-30 21:42:26 +00007266 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06007267 kuid_t uid;
7268 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07007269
Patrick McHardy24023452007-07-14 18:51:31 -07007270 ASSERT_RTNL();
7271
Wang Chendad9b332008-06-18 01:48:28 -07007272 dev->flags |= IFF_PROMISC;
7273 dev->promiscuity += inc;
7274 if (dev->promiscuity == 0) {
7275 /*
7276 * Avoid overflow.
7277 * If inc causes overflow, untouch promisc and return error.
7278 */
7279 if (inc < 0)
7280 dev->flags &= ~IFF_PROMISC;
7281 else {
7282 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007283 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7284 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007285 return -EOVERFLOW;
7286 }
7287 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007288 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007289 pr_info("device %s %s promiscuous mode\n",
7290 dev->name,
7291 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11007292 if (audit_enabled) {
7293 current_uid_gid(&uid, &gid);
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04007294 audit_log(audit_context(), GFP_ATOMIC,
7295 AUDIT_ANOM_PROMISCUOUS,
7296 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7297 dev->name, (dev->flags & IFF_PROMISC),
7298 (old_flags & IFF_PROMISC),
7299 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7300 from_kuid(&init_user_ns, uid),
7301 from_kgid(&init_user_ns, gid),
7302 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11007303 }
Patrick McHardy24023452007-07-14 18:51:31 -07007304
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007305 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07007306 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007307 if (notify)
7308 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07007309 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007310}
7311
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312/**
7313 * dev_set_promiscuity - update promiscuity count on a device
7314 * @dev: device
7315 * @inc: modifier
7316 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07007317 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 * remains above zero the interface remains promiscuous. Once it hits zero
7319 * the device reverts back to normal filtering operation. A negative inc
7320 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07007321 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 */
Wang Chendad9b332008-06-18 01:48:28 -07007323int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324{
Eric Dumazetb536db92011-11-30 21:42:26 +00007325 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07007326 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007328 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07007329 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07007330 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07007331 if (dev->flags != old_flags)
7332 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07007333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007335EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007337static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007339 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
Patrick McHardy24023452007-07-14 18:51:31 -07007341 ASSERT_RTNL();
7342
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07007344 dev->allmulti += inc;
7345 if (dev->allmulti == 0) {
7346 /*
7347 * Avoid overflow.
7348 * If inc causes overflow, untouch allmulti and return error.
7349 */
7350 if (inc < 0)
7351 dev->flags &= ~IFF_ALLMULTI;
7352 else {
7353 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007354 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
7355 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07007356 return -EOVERFLOW;
7357 }
7358 }
Patrick McHardy24023452007-07-14 18:51:31 -07007359 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007360 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07007361 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007362 if (notify)
7363 __dev_notify_flags(dev, old_flags,
7364 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07007365 }
Wang Chendad9b332008-06-18 01:48:28 -07007366 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07007367}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007368
7369/**
7370 * dev_set_allmulti - update allmulti count on a device
7371 * @dev: device
7372 * @inc: modifier
7373 *
7374 * Add or remove reception of all multicast frames to a device. While the
7375 * count in the device remains above zero the interface remains listening
7376 * to all interfaces. Once it hits zero the device reverts back to normal
7377 * filtering operation. A negative @inc value is used to drop the counter
7378 * when releasing a resource needing all multicasts.
7379 * Return 0 if successful or a negative errno code on error.
7380 */
7381
7382int dev_set_allmulti(struct net_device *dev, int inc)
7383{
7384 return __dev_set_allmulti(dev, inc, true);
7385}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007386EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07007387
7388/*
7389 * Upload unicast and multicast address lists to device and
7390 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08007391 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07007392 * are present.
7393 */
7394void __dev_set_rx_mode(struct net_device *dev)
7395{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007396 const struct net_device_ops *ops = dev->netdev_ops;
7397
Patrick McHardy4417da62007-06-27 01:28:10 -07007398 /* dev_open will call this function so the list will stay sane. */
7399 if (!(dev->flags&IFF_UP))
7400 return;
7401
7402 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09007403 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07007404
Jiri Pirko01789342011-08-16 06:29:00 +00007405 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07007406 /* Unicast addresses changes may only happen under the rtnl,
7407 * therefore calling __dev_set_promiscuity here is safe.
7408 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08007409 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007410 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07007411 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08007412 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007413 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07007414 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07007415 }
Patrick McHardy4417da62007-06-27 01:28:10 -07007416 }
Jiri Pirko01789342011-08-16 06:29:00 +00007417
7418 if (ops->ndo_set_rx_mode)
7419 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07007420}
7421
7422void dev_set_rx_mode(struct net_device *dev)
7423{
David S. Millerb9e40852008-07-15 00:15:08 -07007424 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07007425 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07007426 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427}
7428
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007429/**
7430 * dev_get_flags - get flags reported to userspace
7431 * @dev: device
7432 *
7433 * Get the combination of flag bits exported through APIs to userspace.
7434 */
Eric Dumazet95c96172012-04-15 05:58:06 +00007435unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436{
Eric Dumazet95c96172012-04-15 05:58:06 +00007437 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438
7439 flags = (dev->flags & ~(IFF_PROMISC |
7440 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08007441 IFF_RUNNING |
7442 IFF_LOWER_UP |
7443 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 (dev->gflags & (IFF_PROMISC |
7445 IFF_ALLMULTI));
7446
Stefan Rompfb00055a2006-03-20 17:09:11 -08007447 if (netif_running(dev)) {
7448 if (netif_oper_up(dev))
7449 flags |= IFF_RUNNING;
7450 if (netif_carrier_ok(dev))
7451 flags |= IFF_LOWER_UP;
7452 if (netif_dormant(dev))
7453 flags |= IFF_DORMANT;
7454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
7456 return flags;
7457}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007458EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459
Patrick McHardybd380812010-02-26 06:34:53 +00007460int __dev_change_flags(struct net_device *dev, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461{
Eric Dumazetb536db92011-11-30 21:42:26 +00007462 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00007463 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
Patrick McHardy24023452007-07-14 18:51:31 -07007465 ASSERT_RTNL();
7466
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 /*
7468 * Set the flags on our device.
7469 */
7470
7471 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
7472 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
7473 IFF_AUTOMEDIA)) |
7474 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
7475 IFF_ALLMULTI));
7476
7477 /*
7478 * Load in the correct multicast list now the flags have changed.
7479 */
7480
Patrick McHardyb6c40d62008-10-07 15:26:48 -07007481 if ((old_flags ^ flags) & IFF_MULTICAST)
7482 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07007483
Patrick McHardy4417da62007-06-27 01:28:10 -07007484 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
7486 /*
7487 * Have we downed the interface. We handle IFF_UP ourselves
7488 * according to user attempts to set it, rather than blindly
7489 * setting it.
7490 */
7491
7492 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07007493 if ((old_flags ^ flags) & IFF_UP) {
7494 if (old_flags & IFF_UP)
7495 __dev_close(dev);
7496 else
7497 ret = __dev_open(dev);
7498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007501 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007502 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007503
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007505
7506 if (__dev_set_promiscuity(dev, inc, false) >= 0)
7507 if (dev->flags != old_flags)
7508 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 }
7510
7511 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11007512 * is important. Some (broken) drivers set IFF_PROMISC, when
7513 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 */
7515 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007516 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
7517
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007519 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 }
7521
Patrick McHardybd380812010-02-26 06:34:53 +00007522 return ret;
7523}
7524
Nicolas Dichtela528c212013-09-25 12:02:44 +02007525void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
7526 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00007527{
7528 unsigned int changes = dev->flags ^ old_flags;
7529
Nicolas Dichtela528c212013-09-25 12:02:44 +02007530 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07007531 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02007532
Patrick McHardybd380812010-02-26 06:34:53 +00007533 if (changes & IFF_UP) {
7534 if (dev->flags & IFF_UP)
7535 call_netdevice_notifiers(NETDEV_UP, dev);
7536 else
7537 call_netdevice_notifiers(NETDEV_DOWN, dev);
7538 }
7539
7540 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00007541 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07007542 struct netdev_notifier_change_info change_info = {
7543 .info = {
7544 .dev = dev,
7545 },
7546 .flags_changed = changes,
7547 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00007548
David Ahern51d0c0472017-10-04 17:48:45 -07007549 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00007550 }
Patrick McHardybd380812010-02-26 06:34:53 +00007551}
7552
7553/**
7554 * dev_change_flags - change device settings
7555 * @dev: device
7556 * @flags: device state flags
7557 *
7558 * Change settings on device based state flags. The flags are
7559 * in the userspace exported format.
7560 */
Eric Dumazetb536db92011-11-30 21:42:26 +00007561int dev_change_flags(struct net_device *dev, unsigned int flags)
Patrick McHardybd380812010-02-26 06:34:53 +00007562{
Eric Dumazetb536db92011-11-30 21:42:26 +00007563 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007564 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00007565
7566 ret = __dev_change_flags(dev, flags);
7567 if (ret < 0)
7568 return ret;
7569
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02007570 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02007571 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 return ret;
7573}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007574EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
WANG Congf51048c2017-07-06 15:01:57 -07007576int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007577{
7578 const struct net_device_ops *ops = dev->netdev_ops;
7579
7580 if (ops->ndo_change_mtu)
7581 return ops->ndo_change_mtu(dev, new_mtu);
7582
7583 dev->mtu = new_mtu;
7584 return 0;
7585}
WANG Congf51048c2017-07-06 15:01:57 -07007586EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007587
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007588/**
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007589 * dev_set_mtu_ext - Change maximum transfer unit
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007590 * @dev: device
7591 * @new_mtu: new transfer unit
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007592 * @extack: netlink extended ack
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007593 *
7594 * Change the maximum transfer size of the network device.
7595 */
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007596int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
7597 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007599 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600
7601 if (new_mtu == dev->mtu)
7602 return 0;
7603
Jarod Wilson61e84622016-10-07 22:04:33 -04007604 /* MTU must be positive, and in range */
7605 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007606 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 return -EINVAL;
Jarod Wilson61e84622016-10-07 22:04:33 -04007608 }
7609
7610 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007611 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
Jarod Wilson61e84622016-10-07 22:04:33 -04007612 return -EINVAL;
7613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
7615 if (!netif_device_present(dev))
7616 return -ENODEV;
7617
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01007618 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
7619 err = notifier_to_errno(err);
7620 if (err)
7621 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007622
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007623 orig_mtu = dev->mtu;
7624 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007626 if (!err) {
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02007627 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
7628 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007629 err = notifier_to_errno(err);
7630 if (err) {
7631 /* setting mtu back and notifying everyone again,
7632 * so that they have a chance to revert changes.
7633 */
7634 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02007635 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
7636 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007637 }
7638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639 return err;
7640}
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007641
7642int dev_set_mtu(struct net_device *dev, int new_mtu)
7643{
7644 struct netlink_ext_ack extack;
7645 int err;
7646
Li RongQinga6bcfc82018-08-03 15:45:21 +08007647 memset(&extack, 0, sizeof(extack));
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007648 err = dev_set_mtu_ext(dev, new_mtu, &extack);
Li RongQinga6bcfc82018-08-03 15:45:21 +08007649 if (err && extack._msg)
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07007650 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
7651 return err;
7652}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007653EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007655/**
Cong Wang6a643dd2018-01-25 18:26:22 -08007656 * dev_change_tx_queue_len - Change TX queue length of a netdevice
7657 * @dev: device
7658 * @new_len: new tx queue length
7659 */
7660int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
7661{
7662 unsigned int orig_len = dev->tx_queue_len;
7663 int res;
7664
7665 if (new_len != (unsigned int)new_len)
7666 return -ERANGE;
7667
7668 if (new_len != orig_len) {
7669 dev->tx_queue_len = new_len;
7670 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
7671 res = notifier_to_errno(res);
Tariq Toukan7effaf02018-07-24 14:12:20 +03007672 if (res)
7673 goto err_rollback;
7674 res = dev_qdisc_change_tx_queue_len(dev);
7675 if (res)
7676 goto err_rollback;
Cong Wang6a643dd2018-01-25 18:26:22 -08007677 }
7678
7679 return 0;
Tariq Toukan7effaf02018-07-24 14:12:20 +03007680
7681err_rollback:
7682 netdev_err(dev, "refused to change device tx_queue_len\n");
7683 dev->tx_queue_len = orig_len;
7684 return res;
Cong Wang6a643dd2018-01-25 18:26:22 -08007685}
7686
7687/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00007688 * dev_set_group - Change group this device belongs to
7689 * @dev: device
7690 * @new_group: group this device should belong to
7691 */
7692void dev_set_group(struct net_device *dev, int new_group)
7693{
7694 dev->group = new_group;
7695}
7696EXPORT_SYMBOL(dev_set_group);
7697
7698/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007699 * dev_set_mac_address - Change Media Access Control Address
7700 * @dev: device
7701 * @sa: new address
7702 *
7703 * Change the hardware (MAC) address of the device
7704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
7706{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007707 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 int err;
7709
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007710 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 return -EOPNOTSUPP;
7712 if (sa->sa_family != dev->type)
7713 return -EINVAL;
7714 if (!netif_device_present(dev))
7715 return -ENODEV;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007716 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00007717 if (err)
7718 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00007719 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00007720 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04007721 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00007722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007724EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
Jiri Pirko4bf84c32012-12-27 23:49:37 +00007726/**
7727 * dev_change_carrier - Change device carrier
7728 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00007729 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00007730 *
7731 * Change device carrier
7732 */
7733int dev_change_carrier(struct net_device *dev, bool new_carrier)
7734{
7735 const struct net_device_ops *ops = dev->netdev_ops;
7736
7737 if (!ops->ndo_change_carrier)
7738 return -EOPNOTSUPP;
7739 if (!netif_device_present(dev))
7740 return -ENODEV;
7741 return ops->ndo_change_carrier(dev, new_carrier);
7742}
7743EXPORT_SYMBOL(dev_change_carrier);
7744
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02007746 * dev_get_phys_port_id - Get device physical port ID
7747 * @dev: device
7748 * @ppid: port ID
7749 *
7750 * Get device physical port ID
7751 */
7752int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01007753 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02007754{
7755 const struct net_device_ops *ops = dev->netdev_ops;
7756
7757 if (!ops->ndo_get_phys_port_id)
7758 return -EOPNOTSUPP;
7759 return ops->ndo_get_phys_port_id(dev, ppid);
7760}
7761EXPORT_SYMBOL(dev_get_phys_port_id);
7762
7763/**
David Aherndb24a902015-03-17 20:23:15 -06007764 * dev_get_phys_port_name - Get device physical port name
7765 * @dev: device
7766 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00007767 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06007768 *
7769 * Get device physical port name
7770 */
7771int dev_get_phys_port_name(struct net_device *dev,
7772 char *name, size_t len)
7773{
7774 const struct net_device_ops *ops = dev->netdev_ops;
7775
7776 if (!ops->ndo_get_phys_port_name)
7777 return -EOPNOTSUPP;
7778 return ops->ndo_get_phys_port_name(dev, name, len);
7779}
7780EXPORT_SYMBOL(dev_get_phys_port_name);
7781
7782/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007783 * dev_change_proto_down - update protocol port state information
7784 * @dev: device
7785 * @proto_down: new value
7786 *
7787 * This info can be used by switch drivers to set the phys state of the
7788 * port.
7789 */
7790int dev_change_proto_down(struct net_device *dev, bool proto_down)
7791{
7792 const struct net_device_ops *ops = dev->netdev_ops;
7793
7794 if (!ops->ndo_change_proto_down)
7795 return -EOPNOTSUPP;
7796 if (!netif_device_present(dev))
7797 return -ENODEV;
7798 return ops->ndo_change_proto_down(dev, proto_down);
7799}
7800EXPORT_SYMBOL(dev_change_proto_down);
7801
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007802u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
7803 enum bpf_netdev_command cmd)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007804{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007805 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007806
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007807 if (!bpf_op)
7808 return 0;
Martin KaFai Lau58038692017-06-15 17:29:09 -07007809
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007810 memset(&xdp, 0, sizeof(xdp));
7811 xdp.command = cmd;
7812
7813 /* Query must always succeed. */
7814 WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007815
Jakub Kicinski6b867582018-07-11 20:36:39 -07007816 return xdp.prog_id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007817}
7818
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007819static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07007820 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007821 struct bpf_prog *prog)
7822{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007823 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007824
7825 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07007826 if (flags & XDP_FLAGS_HW_MODE)
7827 xdp.command = XDP_SETUP_PROG_HW;
7828 else
7829 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007830 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07007831 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007832 xdp.prog = prog;
7833
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007834 return bpf_op(dev, &xdp);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007835}
7836
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007837static void dev_xdp_uninstall(struct net_device *dev)
7838{
7839 struct netdev_bpf xdp;
7840 bpf_op_t ndo_bpf;
7841
7842 /* Remove generic XDP */
7843 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
7844
7845 /* Remove from the driver */
7846 ndo_bpf = dev->netdev_ops->ndo_bpf;
7847 if (!ndo_bpf)
7848 return;
7849
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007850 memset(&xdp, 0, sizeof(xdp));
7851 xdp.command = XDP_QUERY_PROG;
7852 WARN_ON(ndo_bpf(dev, &xdp));
7853 if (xdp.prog_id)
7854 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
7855 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007856
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007857 /* Remove HW offload */
7858 memset(&xdp, 0, sizeof(xdp));
7859 xdp.command = XDP_QUERY_PROG_HW;
7860 if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
7861 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
7862 NULL));
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007863}
7864
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007865/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07007866 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
7867 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07007868 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07007869 * @fd: new program fd or negative value to clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01007870 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07007871 *
7872 * Set or clear a bpf program for a device
7873 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07007874int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
7875 int fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07007876{
7877 const struct net_device_ops *ops = dev->netdev_ops;
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007878 enum bpf_netdev_command query;
Brenden Blancoa7862b42016-07-19 12:16:48 -07007879 struct bpf_prog *prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007880 bpf_op_t bpf_op, bpf_chk;
Brenden Blancoa7862b42016-07-19 12:16:48 -07007881 int err;
7882
Daniel Borkmann85de8572016-11-28 23:16:54 +01007883 ASSERT_RTNL();
7884
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007885 query = flags & XDP_FLAGS_HW_MODE ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
7886
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007887 bpf_op = bpf_chk = ops->ndo_bpf;
7888 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE)))
Daniel Borkmann0489df92017-05-12 01:04:45 +02007889 return -EOPNOTSUPP;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007890 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
7891 bpf_op = generic_xdp_install;
7892 if (bpf_op == bpf_chk)
7893 bpf_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04007894
Brenden Blancoa7862b42016-07-19 12:16:48 -07007895 if (fd >= 0) {
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007896 if (__dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG) ||
7897 __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG_HW))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007898 return -EEXIST;
7899 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
Jakub Kicinskia25717d2018-07-11 20:36:41 -07007900 __dev_xdp_query(dev, bpf_op, query))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007901 return -EBUSY;
Daniel Borkmann85de8572016-11-28 23:16:54 +01007902
Jakub Kicinski288b3de2017-11-20 15:21:54 -08007903 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
7904 bpf_op == ops->ndo_bpf);
Brenden Blancoa7862b42016-07-19 12:16:48 -07007905 if (IS_ERR(prog))
7906 return PTR_ERR(prog);
Jakub Kicinski441a3302017-11-20 15:21:55 -08007907
7908 if (!(flags & XDP_FLAGS_HW_MODE) &&
7909 bpf_prog_is_dev_bound(prog->aux)) {
7910 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
7911 bpf_prog_put(prog);
7912 return -EINVAL;
7913 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07007914 }
7915
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007916 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07007917 if (err < 0 && prog)
7918 bpf_prog_put(prog);
7919
7920 return err;
7921}
Brenden Blancoa7862b42016-07-19 12:16:48 -07007922
7923/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07007925 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 *
7927 * Returns a suitable unique value for a new device interface
7928 * number. The caller must hold the rtnl semaphore or the
7929 * dev_base_lock to be sure it remains unique.
7930 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07007931static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00007933 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11007934
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 for (;;) {
7936 if (++ifindex <= 0)
7937 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07007938 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00007939 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 }
7941}
7942
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08007944static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07007945DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946
Stephen Hemminger6f05f622007-03-08 20:46:03 -08007947static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07007950 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951}
7952
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007953static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007954{
Krishna Kumare93737b2009-12-08 22:26:02 +00007955 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007956 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007957
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007958 BUG_ON(dev_boot_phase);
7959 ASSERT_RTNL();
7960
Krishna Kumare93737b2009-12-08 22:26:02 +00007961 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007962 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00007963 * for initialization unwind. Remove those
7964 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007965 */
7966 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007967 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
7968 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007969
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007970 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00007971 list_del(&dev->unreg_list);
7972 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007973 }
Eric Dumazet449f4542011-05-19 12:24:16 +00007974 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007975 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00007976 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007977
Octavian Purdila44345722010-12-13 12:44:07 +00007978 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007979 list_for_each_entry(dev, head, unreg_list)
7980 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04007981 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007982
Octavian Purdila44345722010-12-13 12:44:07 +00007983 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007984 /* And unlink it from device chain. */
7985 unlist_netdevice(dev);
7986
7987 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007988 }
Eric Dumazet41852492016-08-26 12:50:39 -07007989 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007990
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007991 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007992
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007993 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007994 struct sk_buff *skb = NULL;
7995
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007996 /* Shutdown queueing discipline. */
7997 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007998
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007999 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008000
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008001 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008002 * this device. They should clean all the things.
8003 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008004 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
8005
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008006 if (!dev->rtnl_link_ops ||
8007 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04008008 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01008009 GFP_KERNEL, NULL, 0);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008010
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008011 /*
8012 * Flush the unicast and multicast chains
8013 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008014 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008015 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008016
8017 if (dev->netdev_ops->ndo_uninit)
8018 dev->netdev_ops->ndo_uninit(dev);
8019
Mahesh Bandewar395eea62014-12-03 13:46:24 -08008020 if (skb)
8021 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07008022
Jiri Pirko9ff162a2013-01-03 22:48:49 +00008023 /* Notifier chain MUST detach us all upper devices. */
8024 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07008025 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008026
8027 /* Remove entries from kobject tree */
8028 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00008029#ifdef CONFIG_XPS
8030 /* Remove XPS queueing entries */
8031 netif_reset_xps_queues_gt(dev, 0);
8032#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008033 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008034
Eric W. Biederman850a5452011-10-13 22:25:23 +00008035 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008036
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00008037 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008038 dev_put(dev);
8039}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008040
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008041static void rollback_registered(struct net_device *dev)
8042{
8043 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008044
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008045 list_add(&dev->unreg_list, &single);
8046 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00008047 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008048}
8049
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008050static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
8051 struct net_device *upper, netdev_features_t features)
8052{
8053 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8054 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008055 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008056
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008057 for_each_netdev_feature(&upper_disables, feature_bit) {
8058 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008059 if (!(upper->wanted_features & feature)
8060 && (features & feature)) {
8061 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
8062 &feature, upper->name);
8063 features &= ~feature;
8064 }
8065 }
8066
8067 return features;
8068}
8069
8070static void netdev_sync_lower_features(struct net_device *upper,
8071 struct net_device *lower, netdev_features_t features)
8072{
8073 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8074 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008075 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008076
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05008077 for_each_netdev_feature(&upper_disables, feature_bit) {
8078 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008079 if (!(features & feature) && (lower->features & feature)) {
8080 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
8081 &feature, lower->name);
8082 lower->wanted_features &= ~feature;
8083 netdev_update_features(lower);
8084
8085 if (unlikely(lower->features & feature))
8086 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
8087 &feature, lower->name);
8088 }
8089 }
8090}
8091
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008092static netdev_features_t netdev_fix_features(struct net_device *dev,
8093 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07008094{
Michał Mirosław57422dc2011-01-22 12:14:12 +00008095 /* Fix illegal checksum combinations */
8096 if ((features & NETIF_F_HW_CSUM) &&
8097 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008098 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00008099 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
8100 }
8101
Herbert Xub63365a2008-10-23 01:11:29 -07008102 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008103 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008104 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00008105 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07008106 }
8107
Pravin B Shelarec5f0612013-03-07 09:28:01 +00008108 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
8109 !(features & NETIF_F_IP_CSUM)) {
8110 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
8111 features &= ~NETIF_F_TSO;
8112 features &= ~NETIF_F_TSO_ECN;
8113 }
8114
8115 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
8116 !(features & NETIF_F_IPV6_CSUM)) {
8117 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
8118 features &= ~NETIF_F_TSO6;
8119 }
8120
Alexander Duyckb1dc4972016-05-02 09:38:24 -07008121 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8122 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
8123 features &= ~NETIF_F_TSO_MANGLEID;
8124
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00008125 /* TSO ECN requires that TSO is present as well. */
8126 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
8127 features &= ~NETIF_F_TSO_ECN;
8128
Michał Mirosław212b5732011-02-15 16:59:16 +00008129 /* Software GSO depends on SG. */
8130 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04008131 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00008132 features &= ~NETIF_F_GSO;
8133 }
8134
Alexander Duyck802ab552016-04-10 21:45:03 -04008135 /* GSO partial features require GSO partial be set */
8136 if ((features & dev->gso_partial_features) &&
8137 !(features & NETIF_F_GSO_PARTIAL)) {
8138 netdev_dbg(dev,
8139 "Dropping partially supported GSO features since no GSO partial.\n");
8140 features &= ~dev->gso_partial_features;
8141 }
8142
Michael Chanfb1f5f72017-12-16 03:09:40 -05008143 if (!(features & NETIF_F_RXCSUM)) {
8144 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8145 * successfully merged by hardware must also have the
8146 * checksum verified by hardware. If the user does not
8147 * want to enable RXCSUM, logically, we should disable GRO_HW.
8148 */
8149 if (features & NETIF_F_GRO_HW) {
8150 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8151 features &= ~NETIF_F_GRO_HW;
8152 }
8153 }
8154
Gal Pressmande8d5ab2018-03-12 11:48:49 +02008155 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8156 if (features & NETIF_F_RXFCS) {
8157 if (features & NETIF_F_LRO) {
8158 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
8159 features &= ~NETIF_F_LRO;
8160 }
8161
8162 if (features & NETIF_F_GRO_HW) {
8163 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8164 features &= ~NETIF_F_GRO_HW;
8165 }
Gal Pressmane6c6a922018-03-04 14:12:04 +02008166 }
8167
Herbert Xub63365a2008-10-23 01:11:29 -07008168 return features;
8169}
Herbert Xub63365a2008-10-23 01:11:29 -07008170
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008171int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00008172{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008173 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008174 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008175 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05008176 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00008177
Michał Mirosław87267482011-04-12 09:56:38 +00008178 ASSERT_RTNL();
8179
Michał Mirosław5455c692011-02-15 16:59:17 +00008180 features = netdev_get_wanted_features(dev);
8181
8182 if (dev->netdev_ops->ndo_fix_features)
8183 features = dev->netdev_ops->ndo_fix_features(dev, features);
8184
8185 /* driver might be less strict about feature dependencies */
8186 features = netdev_fix_features(dev, features);
8187
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008188 /* some features can't be enabled if they're off an an upper device */
8189 netdev_for_each_upper_dev_rcu(dev, upper, iter)
8190 features = netdev_sync_upper_features(dev, upper, features);
8191
Michał Mirosław5455c692011-02-15 16:59:17 +00008192 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05008193 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00008194
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008195 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
8196 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00008197
8198 if (dev->netdev_ops->ndo_set_features)
8199 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01008200 else
8201 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00008202
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008203 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00008204 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008205 "set_features() failed (%d); wanted %pNF, left %pNF\n",
8206 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01008207 /* return non-0 since some features might have changed and
8208 * it's better to fire a spurious notification than miss it
8209 */
8210 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008211 }
8212
Jarod Wilsone7868a82015-11-03 23:09:32 -05008213sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05008214 /* some features must be disabled on lower devices when disabled
8215 * on an upper device (think: bonding master or bridge)
8216 */
8217 netdev_for_each_lower_dev(dev, lower, iter)
8218 netdev_sync_lower_features(dev, lower, features);
8219
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02008220 if (!err) {
8221 netdev_features_t diff = features ^ dev->features;
8222
8223 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
8224 /* udp_tunnel_{get,drop}_rx_info both need
8225 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
8226 * device, or they won't do anything.
8227 * Thus we need to update dev->features
8228 * *before* calling udp_tunnel_get_rx_info,
8229 * but *after* calling udp_tunnel_drop_rx_info.
8230 */
8231 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
8232 dev->features = features;
8233 udp_tunnel_get_rx_info(dev);
8234 } else {
8235 udp_tunnel_drop_rx_info(dev);
8236 }
8237 }
8238
Gal Pressman9daae9b2018-03-28 17:46:54 +03008239 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
8240 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
8241 dev->features = features;
8242 err |= vlan_get_rx_ctag_filter_info(dev);
8243 } else {
8244 vlan_drop_rx_ctag_filter_info(dev);
8245 }
8246 }
8247
8248 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
8249 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
8250 dev->features = features;
8251 err |= vlan_get_rx_stag_filter_info(dev);
8252 } else {
8253 vlan_drop_rx_stag_filter_info(dev);
8254 }
8255 }
8256
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008257 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02008258 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008259
Jarod Wilsone7868a82015-11-03 23:09:32 -05008260 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008261}
8262
Michał Mirosławafe12cc2011-05-07 03:22:17 +00008263/**
8264 * netdev_update_features - recalculate device features
8265 * @dev: the device to check
8266 *
8267 * Recalculate dev->features set and send notifications if it
8268 * has changed. Should be called after driver or hardware dependent
8269 * conditions might have changed that influence the features.
8270 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008271void netdev_update_features(struct net_device *dev)
8272{
8273 if (__netdev_update_features(dev))
8274 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00008275}
8276EXPORT_SYMBOL(netdev_update_features);
8277
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00008279 * netdev_change_features - recalculate device features
8280 * @dev: the device to check
8281 *
8282 * Recalculate dev->features set and send notifications even
8283 * if they have not changed. Should be called instead of
8284 * netdev_update_features() if also dev->vlan_features might
8285 * have changed to allow the changes to be propagated to stacked
8286 * VLAN devices.
8287 */
8288void netdev_change_features(struct net_device *dev)
8289{
8290 __netdev_update_features(dev);
8291 netdev_features_change(dev);
8292}
8293EXPORT_SYMBOL(netdev_change_features);
8294
8295/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08008296 * netif_stacked_transfer_operstate - transfer operstate
8297 * @rootdev: the root or lower level device to transfer state from
8298 * @dev: the device to transfer operstate to
8299 *
8300 * Transfer operational state from root to device. This is normally
8301 * called when a stacking relationship exists between the root
8302 * device and the device(a leaf device).
8303 */
8304void netif_stacked_transfer_operstate(const struct net_device *rootdev,
8305 struct net_device *dev)
8306{
8307 if (rootdev->operstate == IF_OPER_DORMANT)
8308 netif_dormant_on(dev);
8309 else
8310 netif_dormant_off(dev);
8311
Zhang Shengju0575c862017-04-26 17:49:38 +08008312 if (netif_carrier_ok(rootdev))
8313 netif_carrier_on(dev);
8314 else
8315 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08008316}
8317EXPORT_SYMBOL(netif_stacked_transfer_operstate);
8318
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008319static int netif_alloc_rx_queues(struct net_device *dev)
8320{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008321 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00008322 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05308323 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008324 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008325
Tom Herbertbd25fa72010-10-18 18:00:16 +00008326 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008327
Michal Hockodcda9b02017-07-12 14:36:45 -07008328 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07008329 if (!rx)
8330 return -ENOMEM;
8331
Tom Herbertbd25fa72010-10-18 18:00:16 +00008332 dev->_rx = rx;
8333
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008334 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00008335 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008336
8337 /* XDP RX-queue setup */
8338 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
8339 if (err < 0)
8340 goto err_rxq_info;
8341 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008342 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008343
8344err_rxq_info:
8345 /* Rollback successful reg's and free other resources */
8346 while (i--)
8347 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08008348 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008349 dev->_rx = NULL;
8350 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008351}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008352
8353static void netif_free_rx_queues(struct net_device *dev)
8354{
8355 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008356
8357 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
8358 if (!dev->_rx)
8359 return;
8360
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008361 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08008362 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
8363
8364 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008365}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00008366
Changli Gaoaa942102010-12-04 02:31:41 +00008367static void netdev_init_one_queue(struct net_device *dev,
8368 struct netdev_queue *queue, void *_unused)
8369{
8370 /* Initialize queue lock */
8371 spin_lock_init(&queue->_xmit_lock);
8372 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
8373 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00008374 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00008375 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00008376#ifdef CONFIG_BQL
8377 dql_init(&queue->dql, HZ);
8378#endif
Changli Gaoaa942102010-12-04 02:31:41 +00008379}
8380
Eric Dumazet60877a32013-06-20 01:15:51 -07008381static void netif_free_tx_queues(struct net_device *dev)
8382{
WANG Cong4cb28972014-06-02 15:55:22 -07008383 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07008384}
8385
Tom Herberte6484932010-10-18 18:04:39 +00008386static int netif_alloc_netdev_queues(struct net_device *dev)
8387{
8388 unsigned int count = dev->num_tx_queues;
8389 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07008390 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00008391
Eric Dumazetd3397272015-07-06 17:13:26 +02008392 if (count < 1 || count > 0xffff)
8393 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00008394
Michal Hockodcda9b02017-07-12 14:36:45 -07008395 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07008396 if (!tx)
8397 return -ENOMEM;
8398
Tom Herberte6484932010-10-18 18:04:39 +00008399 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00008400
Tom Herberte6484932010-10-18 18:04:39 +00008401 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
8402 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00008403
8404 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00008405}
8406
Denys Vlasenkoa2029242015-05-11 21:17:53 +02008407void netif_tx_stop_all_queues(struct net_device *dev)
8408{
8409 unsigned int i;
8410
8411 for (i = 0; i < dev->num_tx_queues; i++) {
8412 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11008413
Denys Vlasenkoa2029242015-05-11 21:17:53 +02008414 netif_tx_stop_queue(txq);
8415 }
8416}
8417EXPORT_SYMBOL(netif_tx_stop_all_queues);
8418
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08008419/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 * register_netdevice - register a network device
8421 * @dev: device to register
8422 *
8423 * Take a completed network device structure and add it to the kernel
8424 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
8425 * chain. 0 is returned on success. A negative errno code is returned
8426 * on a failure to set up the device, or if the name is a duplicate.
8427 *
8428 * Callers must hold the rtnl semaphore. You may want
8429 * register_netdev() instead of this.
8430 *
8431 * BUGS:
8432 * The locking appears insufficient to guarantee two parallel registers
8433 * will not get the same name.
8434 */
8435
8436int register_netdevice(struct net_device *dev)
8437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008439 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440
Florian Fainellie283de32018-04-30 14:20:05 -07008441 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
8442 NETDEV_FEATURE_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 BUG_ON(dev_boot_phase);
8444 ASSERT_RTNL();
8445
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008446 might_sleep();
8447
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448 /* When net_device's are persistent, this will be fatal. */
8449 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008450 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451
David S. Millerf1f28aa2008-07-15 00:08:33 -07008452 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07008453 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454
Gao feng828de4f2012-09-13 20:58:27 +00008455 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00008456 if (ret < 0)
8457 goto out;
8458
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008460 if (dev->netdev_ops->ndo_init) {
8461 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 if (ret) {
8463 if (ret > 0)
8464 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08008465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 }
8467 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008468
Patrick McHardyf6469682013-04-19 02:04:27 +00008469 if (((dev->hw_features | dev->features) &
8470 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00008471 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
8472 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
8473 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
8474 ret = -EINVAL;
8475 goto err_uninit;
8476 }
8477
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00008478 ret = -EBUSY;
8479 if (!dev->ifindex)
8480 dev->ifindex = dev_new_index(net);
8481 else if (__dev_get_by_index(net, dev->ifindex))
8482 goto err_uninit;
8483
Michał Mirosław5455c692011-02-15 16:59:17 +00008484 /* Transfer changeable features to wanted_features and enable
8485 * software offloads (GSO and GRO).
8486 */
8487 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00008488 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02008489
8490 if (dev->netdev_ops->ndo_udp_tunnel_add) {
8491 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
8492 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
8493 }
8494
Michał Mirosław14d12322011-02-22 16:52:28 +00008495 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496
Alexander Duyckcbc53e02016-04-10 21:44:51 -04008497 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00008498 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04008499
Alexander Duyck7f348a62016-04-20 16:51:00 -04008500 /* If IPv4 TCP segmentation offload is supported we should also
8501 * allow the device to enable segmenting the frame with the option
8502 * of ignoring a static IP ID value. This doesn't enable the
8503 * feature itself but allows the user to enable it later.
8504 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04008505 if (dev->hw_features & NETIF_F_TSO)
8506 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04008507 if (dev->vlan_features & NETIF_F_TSO)
8508 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
8509 if (dev->mpls_features & NETIF_F_TSO)
8510 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
8511 if (dev->hw_enc_features & NETIF_F_TSO)
8512 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07008513
Michał Mirosław1180e7d2011-07-14 14:41:11 -07008514 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00008515 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07008516 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00008517
Pravin B Shelaree579672013-03-07 09:28:08 +00008518 /* Make NETIF_F_SG inheritable to tunnel devices.
8519 */
Alexander Duyck802ab552016-04-10 21:45:03 -04008520 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00008521
Simon Horman0d89d202013-05-23 21:02:52 +00008522 /* Make NETIF_F_SG inheritable to MPLS.
8523 */
8524 dev->mpls_features |= NETIF_F_SG;
8525
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00008526 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
8527 ret = notifier_to_errno(ret);
8528 if (ret)
8529 goto err_uninit;
8530
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008531 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008532 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07008533 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008534 dev->reg_state = NETREG_REGISTERED;
8535
Michał Mirosław6cb6a272011-04-02 22:48:47 -07008536 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00008537
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 /*
8539 * Default initial state at registry is that the
8540 * device is present.
8541 */
8542
8543 set_bit(__LINK_STATE_PRESENT, &dev->state);
8544
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01008545 linkwatch_init_dev(dev);
8546
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008549 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04008550 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
Jiri Pirko948b3372013-01-08 01:38:25 +00008552 /* If the device has permanent device address, driver should
8553 * set dev_addr and also addr_assign_type should be set to
8554 * NET_ADDR_PERM (default value).
8555 */
8556 if (dev->addr_assign_type == NET_ADDR_PERM)
8557 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
8558
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07008560 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07008561 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07008562 if (ret) {
8563 rollback_registered(dev);
8564 dev->reg_state = NETREG_UNREGISTERED;
8565 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008566 /*
8567 * Prevent userspace races by waiting until the network
8568 * device is fully setup before sending notifications.
8569 */
Patrick McHardya2835762010-02-26 06:34:51 +00008570 if (!dev->rtnl_link_ops ||
8571 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008572 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573
8574out:
8575 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07008576
8577err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08008578 if (dev->netdev_ops->ndo_uninit)
8579 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04008580 if (dev->priv_destructor)
8581 dev->priv_destructor(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07008582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008584EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585
8586/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08008587 * init_dummy_netdev - init a dummy network device for NAPI
8588 * @dev: device to init
8589 *
8590 * This takes a network device structure and initialize the minimum
8591 * amount of fields so it can be used to schedule NAPI polls without
8592 * registering a full blown interface. This is to be used by drivers
8593 * that need to tie several hardware interfaces to a single NAPI
8594 * poll scheduler due to HW limitations.
8595 */
8596int init_dummy_netdev(struct net_device *dev)
8597{
8598 /* Clear everything. Note we don't initialize spinlocks
8599 * are they aren't supposed to be taken by any of the
8600 * NAPI code and this dummy netdev is supposed to be
8601 * only ever used for NAPI polls
8602 */
8603 memset(dev, 0, sizeof(struct net_device));
8604
8605 /* make sure we BUG if trying to hit standard
8606 * register/unregister code path
8607 */
8608 dev->reg_state = NETREG_DUMMY;
8609
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08008610 /* NAPI wants this */
8611 INIT_LIST_HEAD(&dev->napi_list);
8612
8613 /* a dummy interface is started by default */
8614 set_bit(__LINK_STATE_PRESENT, &dev->state);
8615 set_bit(__LINK_STATE_START, &dev->state);
8616
Eric Dumazet29b44332010-10-11 10:22:12 +00008617 /* Note : We dont allocate pcpu_refcnt for dummy devices,
8618 * because users of this 'device' dont need to change
8619 * its refcount.
8620 */
8621
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08008622 return 0;
8623}
8624EXPORT_SYMBOL_GPL(init_dummy_netdev);
8625
8626
8627/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 * register_netdev - register a network device
8629 * @dev: device to register
8630 *
8631 * Take a completed network device structure and add it to the kernel
8632 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
8633 * chain. 0 is returned on success. A negative errno code is returned
8634 * on a failure to set up the device, or if the name is a duplicate.
8635 *
Borislav Petkov38b4da382007-04-20 22:14:10 -07008636 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637 * and expands the device name if you passed a format string to
8638 * alloc_netdev.
8639 */
8640int register_netdev(struct net_device *dev)
8641{
8642 int err;
8643
Kirill Tkhaib0f3deb2018-03-14 22:17:28 +03008644 if (rtnl_lock_killable())
8645 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 rtnl_unlock();
8648 return err;
8649}
8650EXPORT_SYMBOL(register_netdev);
8651
Eric Dumazet29b44332010-10-11 10:22:12 +00008652int netdev_refcnt_read(const struct net_device *dev)
8653{
8654 int i, refcnt = 0;
8655
8656 for_each_possible_cpu(i)
8657 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
8658 return refcnt;
8659}
8660EXPORT_SYMBOL(netdev_refcnt_read);
8661
Ben Hutchings2c530402012-07-10 10:55:09 +00008662/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00008664 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 *
8666 * This is called when unregistering network devices.
8667 *
8668 * Any protocol or device that holds a reference should register
8669 * for netdevice notification, and cleanup and put back the
8670 * reference if they receive an UNREGISTER event.
8671 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008672 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 */
8674static void netdev_wait_allrefs(struct net_device *dev)
8675{
8676 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00008677 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678
Eric Dumazete014deb2009-11-17 05:59:21 +00008679 linkwatch_forget_dev(dev);
8680
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00008682 refcnt = netdev_refcnt_read(dev);
8683
8684 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08008686 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687
8688 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07008689 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690
Eric Dumazet748e2d92012-08-22 21:50:59 +00008691 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008692 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00008693 rtnl_lock();
8694
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
8696 &dev->state)) {
8697 /* We must not have linkwatch events
8698 * pending on unregister. If this
8699 * happens, we simply run the queue
8700 * unscheduled, resulting in a noop
8701 * for this device.
8702 */
8703 linkwatch_run_queue();
8704 }
8705
Stephen Hemminger6756ae42006-03-20 22:23:58 -08008706 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707
8708 rebroadcast_time = jiffies;
8709 }
8710
8711 msleep(250);
8712
Eric Dumazet29b44332010-10-11 10:22:12 +00008713 refcnt = netdev_refcnt_read(dev);
8714
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715 if (time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008716 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
8717 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 warning_time = jiffies;
8719 }
8720 }
8721}
8722
8723/* The sequence is:
8724 *
8725 * rtnl_lock();
8726 * ...
8727 * register_netdevice(x1);
8728 * register_netdevice(x2);
8729 * ...
8730 * unregister_netdevice(y1);
8731 * unregister_netdevice(y2);
8732 * ...
8733 * rtnl_unlock();
8734 * free_netdev(y1);
8735 * free_netdev(y2);
8736 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07008737 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008739 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 * without deadlocking with linkwatch via keventd.
8741 * 2) Since we run with the RTNL semaphore not held, we can sleep
8742 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07008743 *
8744 * We must not return until all unregister events added during
8745 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747void netdev_run_todo(void)
8748{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008749 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008752 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07008753
8754 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008755
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008756
8757 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00008758 if (!list_empty(&list))
8759 rcu_barrier();
8760
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761 while (!list_empty(&list)) {
8762 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00008763 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 list_del(&dev->todo_list);
8765
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008766 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008767 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008769 dump_stack();
8770 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008772
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008773 dev->reg_state = NETREG_UNREGISTERED;
8774
8775 netdev_wait_allrefs(dev);
8776
8777 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00008778 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08008779 BUG_ON(!list_empty(&dev->ptype_all));
8780 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00008781 WARN_ON(rcu_access_pointer(dev->ip_ptr));
8782 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
David Ahern330c7272018-02-13 08:52:00 -08008783#if IS_ENABLED(CONFIG_DECNET)
Ilpo Järvinen547b7922008-07-25 21:43:18 -07008784 WARN_ON(dev->dn_ptr);
David Ahern330c7272018-02-13 08:52:00 -08008785#endif
David S. Millercf124db2017-05-08 12:52:56 -04008786 if (dev->priv_destructor)
8787 dev->priv_destructor(dev);
8788 if (dev->needs_free_netdev)
8789 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07008790
Eric W. Biederman50624c92013-09-23 21:19:49 -07008791 /* Report a network device has been unregistered */
8792 rtnl_lock();
8793 dev_net(dev)->dev_unreg_count--;
8794 __rtnl_unlock();
8795 wake_up(&netdev_unregistering_wq);
8796
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07008797 /* Free network device */
8798 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800}
8801
Jarod Wilson92566452016-02-01 18:51:04 -05008802/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
8803 * all the same fields in the same order as net_device_stats, with only
8804 * the type differing, but rtnl_link_stats64 may have additional fields
8805 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00008806 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00008807void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
8808 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00008809{
8810#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05008811 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02008812 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05008813 /* zero out counters that only exist in rtnl_link_stats64 */
8814 memset((char *)stats64 + sizeof(*netdev_stats), 0,
8815 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008816#else
Jarod Wilson92566452016-02-01 18:51:04 -05008817 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00008818 const unsigned long *src = (const unsigned long *)netdev_stats;
8819 u64 *dst = (u64 *)stats64;
8820
Jarod Wilson92566452016-02-01 18:51:04 -05008821 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008822 for (i = 0; i < n; i++)
8823 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05008824 /* zero out counters that only exist in rtnl_link_stats64 */
8825 memset((char *)stats64 + n * sizeof(u64), 0,
8826 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008827#endif
8828}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00008829EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00008830
Eric Dumazetd83345a2009-11-16 03:36:51 +00008831/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008832 * dev_get_stats - get network device statistics
8833 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07008834 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008835 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00008836 * Get network statistics from device. Return @storage.
8837 * The device driver may provide its own method by setting
8838 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
8839 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008840 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00008841struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
8842 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00008843{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008844 const struct net_device_ops *ops = dev->netdev_ops;
8845
Eric Dumazet28172732010-07-07 14:58:56 -07008846 if (ops->ndo_get_stats64) {
8847 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00008848 ops->ndo_get_stats64(dev, storage);
8849 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00008850 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00008851 } else {
8852 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07008853 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07008854 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
8855 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
8856 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07008857 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07008858}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008859EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07008860
Eric Dumazet24824a02010-10-02 06:11:55 +00008861struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07008862{
Eric Dumazet24824a02010-10-02 06:11:55 +00008863 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07008864
Eric Dumazet24824a02010-10-02 06:11:55 +00008865#ifdef CONFIG_NET_CLS_ACT
8866 if (queue)
8867 return queue;
8868 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
8869 if (!queue)
8870 return NULL;
8871 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008872 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00008873 queue->qdisc_sleeping = &noop_qdisc;
8874 rcu_assign_pointer(dev->ingress_queue, queue);
8875#endif
8876 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07008877}
8878
Eric Dumazet2c60db02012-09-16 09:17:26 +00008879static const struct ethtool_ops default_ethtool_ops;
8880
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00008881void netdev_set_default_ethtool_ops(struct net_device *dev,
8882 const struct ethtool_ops *ops)
8883{
8884 if (dev->ethtool_ops == &default_ethtool_ops)
8885 dev->ethtool_ops = ops;
8886}
8887EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
8888
Eric Dumazet74d332c2013-10-30 13:10:44 -07008889void netdev_freemem(struct net_device *dev)
8890{
8891 char *addr = (char *)dev - dev->padded;
8892
WANG Cong4cb28972014-06-02 15:55:22 -07008893 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07008894}
8895
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896/**
tcharding722c9a02017-02-09 17:56:04 +11008897 * alloc_netdev_mqs - allocate network device
8898 * @sizeof_priv: size of private data to allocate space for
8899 * @name: device name format string
8900 * @name_assign_type: origin of device name
8901 * @setup: callback to initialize device
8902 * @txqs: the number of TX subqueues to allocate
8903 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 *
tcharding722c9a02017-02-09 17:56:04 +11008905 * Allocates a struct net_device with private data area for driver use
8906 * and performs basic initialization. Also allocates subqueue structs
8907 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 */
Tom Herbert36909ea2011-01-09 19:36:31 +00008909struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02008910 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00008911 void (*setup)(struct net_device *),
8912 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03008915 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008916 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07008918 BUG_ON(strlen(name) >= sizeof(dev->name));
8919
Tom Herbert36909ea2011-01-09 19:36:31 +00008920 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008921 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00008922 return NULL;
8923 }
8924
Tom Herbert36909ea2011-01-09 19:36:31 +00008925 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008926 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00008927 return NULL;
8928 }
Tom Herbert36909ea2011-01-09 19:36:31 +00008929
David S. Millerfd2ea0a2008-07-17 01:56:23 -07008930 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07008931 if (sizeof_priv) {
8932 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008933 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07008934 alloc_size += sizeof_priv;
8935 }
8936 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008937 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938
Michal Hockodcda9b02017-07-12 14:36:45 -07008939 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00008940 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008942
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008943 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008945
Eric Dumazet29b44332010-10-11 10:22:12 +00008946 dev->pcpu_refcnt = alloc_percpu(int);
8947 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07008948 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008949
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00008951 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952
Jiri Pirko22bedad32010-04-01 21:22:57 +00008953 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008954 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00008955
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09008956 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008957
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07008958 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00008959 dev->gso_max_segs = GSO_MAX_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
Herbert Xud565b0a2008-12-15 23:38:52 -08008961 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00008962 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008963 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00008964 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008965 INIT_LIST_HEAD(&dev->adj_list.upper);
8966 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08008967 INIT_LIST_HEAD(&dev->ptype_all);
8968 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02008969#ifdef CONFIG_NET_SCHED
8970 hash_init(dev->qdisc_hash);
8971#endif
Eric Dumazet02875872014-10-05 18:38:35 -07008972 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008974
Phil Suttera8131042016-02-17 15:37:43 +01008975 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02008976 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01008977 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01008978 }
Phil Sutter906470c2015-08-18 10:30:48 +02008979
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008980 dev->num_tx_queues = txqs;
8981 dev->real_num_tx_queues = txqs;
8982 if (netif_alloc_netdev_queues(dev))
8983 goto free_all;
8984
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008985 dev->num_rx_queues = rxqs;
8986 dev->real_num_rx_queues = rxqs;
8987 if (netif_alloc_rx_queues(dev))
8988 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008989
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02008991 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008992 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00008993 if (!dev->ethtool_ops)
8994 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02008995
8996 nf_hook_ingress_init(dev);
8997
Linus Torvalds1da177e2005-04-16 15:20:36 -07008998 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008999
David S. Miller8d3bdbd2011-02-08 15:02:50 -08009000free_all:
9001 free_netdev(dev);
9002 return NULL;
9003
Eric Dumazet29b44332010-10-11 10:22:12 +00009004free_pcpu:
9005 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07009006free_dev:
9007 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00009008 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009}
Tom Herbert36909ea2011-01-09 19:36:31 +00009010EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011
9012/**
tcharding722c9a02017-02-09 17:56:04 +11009013 * free_netdev - free network device
9014 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 *
tcharding722c9a02017-02-09 17:56:04 +11009016 * This function does the last stage of destroying an allocated device
9017 * interface. The reference to the device object is released. If this
9018 * is the last reference then it will be freed.Must be called in process
9019 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020 */
9021void free_netdev(struct net_device *dev)
9022{
Herbert Xud565b0a2008-12-15 23:38:52 -08009023 struct napi_struct *p, *n;
9024
Eric Dumazet93d05d42015-11-18 06:31:03 -08009025 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07009026 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01009027 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -07009028
Eric Dumazet33d480c2011-08-11 19:30:52 +00009029 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00009030
Jiri Pirkof001fde2009-05-05 02:48:28 +00009031 /* Flush device addresses */
9032 dev_addr_flush(dev);
9033
Herbert Xud565b0a2008-12-15 23:38:52 -08009034 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
9035 netif_napi_del(p);
9036
Eric Dumazet29b44332010-10-11 10:22:12 +00009037 free_percpu(dev->pcpu_refcnt);
9038 dev->pcpu_refcnt = NULL;
9039
Stephen Hemminger3041a062006-05-26 13:25:24 -07009040 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07009042 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 return;
9044 }
9045
9046 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
9047 dev->reg_state = NETREG_RELEASED;
9048
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07009049 /* will free via device release */
9050 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009052EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009053
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009054/**
9055 * synchronize_net - Synchronize with packet receive processing
9056 *
9057 * Wait for packets currently being received to be done.
9058 * Does not block later packets from starting.
9059 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09009060void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061{
9062 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00009063 if (rtnl_is_locked())
9064 synchronize_rcu_expedited();
9065 else
9066 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07009068EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069
9070/**
Eric Dumazet44a08732009-10-27 07:03:04 +00009071 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00009073 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08009074 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009076 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00009077 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 *
9079 * Callers must hold the rtnl semaphore. You may want
9080 * unregister_netdev() instead of this.
9081 */
9082
Eric Dumazet44a08732009-10-27 07:03:04 +00009083void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084{
Herbert Xua6620712007-12-12 19:21:56 -08009085 ASSERT_RTNL();
9086
Eric Dumazet44a08732009-10-27 07:03:04 +00009087 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00009088 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00009089 } else {
9090 rollback_registered(dev);
9091 /* Finish processing unregister after unlock */
9092 net_set_todo(dev);
9093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094}
Eric Dumazet44a08732009-10-27 07:03:04 +00009095EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096
9097/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009098 * unregister_netdevice_many - unregister many devices
9099 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07009100 *
9101 * Note: As most callers use a stack allocated list_head,
9102 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009103 */
9104void unregister_netdevice_many(struct list_head *head)
9105{
9106 struct net_device *dev;
9107
9108 if (!list_empty(head)) {
9109 rollback_registered_many(head);
9110 list_for_each_entry(dev, head, unreg_list)
9111 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07009112 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009113 }
9114}
Eric Dumazet63c80992009-10-27 07:06:49 +00009115EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00009116
9117/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 * unregister_netdev - remove device from the kernel
9119 * @dev: device
9120 *
9121 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08009122 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 *
9124 * This is just a wrapper for unregister_netdevice that takes
9125 * the rtnl semaphore. In general you want to use this and not
9126 * unregister_netdevice.
9127 */
9128void unregister_netdev(struct net_device *dev)
9129{
9130 rtnl_lock();
9131 unregister_netdevice(dev);
9132 rtnl_unlock();
9133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134EXPORT_SYMBOL(unregister_netdev);
9135
Eric W. Biedermance286d32007-09-12 13:53:49 +02009136/**
9137 * dev_change_net_namespace - move device to different nethost namespace
9138 * @dev: device
9139 * @net: network namespace
9140 * @pat: If not NULL name pattern to try if the current device name
9141 * is already taken in the destination network namespace.
9142 *
9143 * This function shuts down a device interface and moves it
9144 * to a new network namespace. On success 0 is returned, on
9145 * a failure a netagive errno code is returned.
9146 *
9147 * Callers must hold the rtnl semaphore.
9148 */
9149
9150int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
9151{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009152 int err, new_nsid, new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009153
9154 ASSERT_RTNL();
9155
9156 /* Don't allow namespace local devices to be moved. */
9157 err = -EINVAL;
9158 if (dev->features & NETIF_F_NETNS_LOCAL)
9159 goto out;
9160
9161 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009162 if (dev->reg_state != NETREG_REGISTERED)
9163 goto out;
9164
9165 /* Get out if there is nothing todo */
9166 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09009167 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02009168 goto out;
9169
9170 /* Pick the destination device name, and ensure
9171 * we can use it in the destination network namespace.
9172 */
9173 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00009174 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009175 /* We get here if we can't use the current device name */
9176 if (!pat)
9177 goto out;
Li RongQing7892bd02018-06-19 17:23:17 +08009178 err = dev_get_valid_name(net, dev, pat);
9179 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02009180 goto out;
9181 }
9182
9183 /*
9184 * And now a mini version of register_netdevice unregister_netdevice.
9185 */
9186
9187 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07009188 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009189
9190 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009191 unlist_netdevice(dev);
9192
9193 synchronize_net();
9194
9195 /* Shutdown queueing discipline. */
9196 dev_shutdown(dev);
9197
9198 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11009199 * this device. They should clean all the things.
9200 *
9201 * Note that dev->reg_state stays at NETREG_REGISTERED.
9202 * This is wanted because this way 8021q and macvlan know
9203 * the device is just moving and can keep their slaves up.
9204 */
Eric W. Biedermance286d32007-09-12 13:53:49 +02009205 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +00009206 rcu_barrier();
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009207
Nicolas Dichtelc36ac8e2018-01-25 15:01:38 +01009208 new_nsid = peernet2id_alloc(dev_net(dev), net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009209 /* If there is an ifindex conflict assign a new one */
9210 if (__dev_get_by_index(net, dev->ifindex))
9211 new_ifindex = dev_new_index(net);
9212 else
9213 new_ifindex = dev->ifindex;
9214
9215 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
9216 new_ifindex);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009217
9218 /*
9219 * Flush the unicast and multicast chains
9220 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00009221 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00009222 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009223
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009224 /* Send a netdev-removed uevent to the old namespace */
9225 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04009226 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009227
Eric W. Biedermance286d32007-09-12 13:53:49 +02009228 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09009229 dev_net_set(dev, net);
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01009230 dev->ifindex = new_ifindex;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009231
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009232 /* Send a netdev-add uevent to the new namespace */
9233 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04009234 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00009235
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009236 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07009237 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009238 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009239
9240 /* Add the device back in the hashes */
9241 list_netdevice(dev);
9242
9243 /* Notify protocols, that a new device appeared. */
9244 call_netdevice_notifiers(NETDEV_REGISTER, dev);
9245
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009246 /*
9247 * Prevent userspace races by waiting until the network
9248 * device is fully setup before sending notifications.
9249 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -07009250 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +00009251
Eric W. Biedermance286d32007-09-12 13:53:49 +02009252 synchronize_net();
9253 err = 0;
9254out:
9255 return err;
9256}
Johannes Berg463d0182009-07-14 00:33:35 +02009257EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009258
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009259static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260{
9261 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009263 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +05309264 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 local_irq_disable();
9267 cpu = smp_processor_id();
9268 sd = &per_cpu(softnet_data, cpu);
9269 oldsd = &per_cpu(softnet_data, oldcpu);
9270
9271 /* Find end of our completion_queue. */
9272 list_skb = &sd->completion_queue;
9273 while (*list_skb)
9274 list_skb = &(*list_skb)->next;
9275 /* Append completion queue from offline CPU. */
9276 *list_skb = oldsd->completion_queue;
9277 oldsd->completion_queue = NULL;
9278
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00009280 if (oldsd->output_queue) {
9281 *sd->output_queue_tailp = oldsd->output_queue;
9282 sd->output_queue_tailp = oldsd->output_queue_tailp;
9283 oldsd->output_queue = NULL;
9284 oldsd->output_queue_tailp = &oldsd->output_queue;
9285 }
Eric Dumazetac64da02015-01-15 17:04:22 -08009286 /* Append NAPI poll list from offline CPU, with one exception :
9287 * process_backlog() must be called by cpu owning percpu backlog.
9288 * We properly handle process_queue & input_pkt_queue later.
9289 */
9290 while (!list_empty(&oldsd->poll_list)) {
9291 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
9292 struct napi_struct,
9293 poll_list);
9294
9295 list_del_init(&napi->poll_list);
9296 if (napi->poll == process_backlog)
9297 napi->state = 0;
9298 else
9299 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +00009300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
9302 raise_softirq_irqoff(NET_TX_SOFTIRQ);
9303 local_irq_enable();
9304
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05309305#ifdef CONFIG_RPS
9306 remsd = oldsd->rps_ipi_list;
9307 oldsd->rps_ipi_list = NULL;
9308#endif
9309 /* send out pending IPI's on offline CPU */
9310 net_rps_send_ipi(remsd);
9311
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00009313 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08009314 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00009315 input_queue_head_incr(oldsd);
9316 }
Eric Dumazetac64da02015-01-15 17:04:22 -08009317 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08009318 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00009319 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07009320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324
Herbert Xu7f353bf2007-08-10 15:47:58 -07009325/**
Herbert Xub63365a2008-10-23 01:11:29 -07009326 * netdev_increment_features - increment feature set by one
9327 * @all: current feature set
9328 * @one: new feature set
9329 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07009330 *
9331 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07009332 * @one to the master device with current feature set @all. Will not
9333 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07009334 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00009335netdev_features_t netdev_increment_features(netdev_features_t all,
9336 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07009337{
Tom Herbertc8cd0982015-12-14 11:19:44 -08009338 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -08009339 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +00009340 mask |= NETIF_F_VLAN_CHALLENGED;
9341
Tom Herberta1882222015-12-14 11:19:43 -08009342 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +00009343 all &= one | ~NETIF_F_ALL_FOR_ALL;
9344
Michał Mirosław1742f182011-04-22 06:31:16 +00009345 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -08009346 if (all & NETIF_F_HW_CSUM)
9347 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -07009348
9349 return all;
9350}
Herbert Xub63365a2008-10-23 01:11:29 -07009351EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07009352
Baruch Siach430f03c2013-06-02 20:43:55 +00009353static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009354{
9355 int i;
9356 struct hlist_head *hash;
9357
Kees Cook6da2ec52018-06-12 13:55:00 -07009358 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009359 if (hash != NULL)
9360 for (i = 0; i < NETDEV_HASHENTRIES; i++)
9361 INIT_HLIST_HEAD(&hash[i]);
9362
9363 return hash;
9364}
9365
Eric W. Biederman881d9662007-09-17 11:56:21 -07009366/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07009367static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07009368{
Li RongQingd9f37d02018-07-13 14:41:36 +08009369 BUILD_BUG_ON(GRO_HASH_BUCKETS >
David S. Millerccdb5172018-07-16 17:02:04 -07009370 8 * FIELD_SIZEOF(struct napi_struct, gro_bitmask));
Li RongQingd9f37d02018-07-13 14:41:36 +08009371
Rustad, Mark D734b6542012-07-18 09:06:07 +00009372 if (net != &init_net)
9373 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07009374
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009375 net->dev_name_head = netdev_create_hash();
9376 if (net->dev_name_head == NULL)
9377 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009378
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009379 net->dev_index_head = netdev_create_hash();
9380 if (net->dev_index_head == NULL)
9381 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009382
9383 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07009384
9385err_idx:
9386 kfree(net->dev_name_head);
9387err_name:
9388 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07009389}
9390
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009391/**
9392 * netdev_drivername - network driver for the device
9393 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07009394 *
9395 * Determine network driver for device.
9396 */
David S. Miller3019de12011-06-06 16:41:33 -07009397const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -07009398{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07009399 const struct device_driver *driver;
9400 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -07009401 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -07009402
9403 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07009404 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -07009405 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07009406
9407 driver = parent->driver;
9408 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -07009409 return driver->name;
9410 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07009411}
9412
Joe Perches6ea754e2014-09-22 11:10:50 -07009413static void __netdev_printk(const char *level, const struct net_device *dev,
9414 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +00009415{
Joe Perchesb004ff42012-09-12 20:12:19 -07009416 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -07009417 dev_printk_emit(level[1] - '0',
9418 dev->dev.parent,
9419 "%s %s %s%s: %pV",
9420 dev_driver_string(dev->dev.parent),
9421 dev_name(dev->dev.parent),
9422 netdev_name(dev), netdev_reg_state(dev),
9423 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009424 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -07009425 printk("%s%s%s: %pV",
9426 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009427 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -07009428 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009429 }
Joe Perches256df2f2010-06-27 01:02:35 +00009430}
9431
Joe Perches6ea754e2014-09-22 11:10:50 -07009432void netdev_printk(const char *level, const struct net_device *dev,
9433 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +00009434{
9435 struct va_format vaf;
9436 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +00009437
9438 va_start(args, format);
9439
9440 vaf.fmt = format;
9441 vaf.va = &args;
9442
Joe Perches6ea754e2014-09-22 11:10:50 -07009443 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07009444
Joe Perches256df2f2010-06-27 01:02:35 +00009445 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +00009446}
9447EXPORT_SYMBOL(netdev_printk);
9448
9449#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -07009450void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +00009451{ \
Joe Perches256df2f2010-06-27 01:02:35 +00009452 struct va_format vaf; \
9453 va_list args; \
9454 \
9455 va_start(args, fmt); \
9456 \
9457 vaf.fmt = fmt; \
9458 vaf.va = &args; \
9459 \
Joe Perches6ea754e2014-09-22 11:10:50 -07009460 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -07009461 \
Joe Perches256df2f2010-06-27 01:02:35 +00009462 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +00009463} \
9464EXPORT_SYMBOL(func);
9465
9466define_netdev_printk_level(netdev_emerg, KERN_EMERG);
9467define_netdev_printk_level(netdev_alert, KERN_ALERT);
9468define_netdev_printk_level(netdev_crit, KERN_CRIT);
9469define_netdev_printk_level(netdev_err, KERN_ERR);
9470define_netdev_printk_level(netdev_warn, KERN_WARNING);
9471define_netdev_printk_level(netdev_notice, KERN_NOTICE);
9472define_netdev_printk_level(netdev_info, KERN_INFO);
9473
Pavel Emelyanov46650792007-10-08 20:38:39 -07009474static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07009475{
9476 kfree(net->dev_name_head);
9477 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +03009478 if (net != &init_net)
9479 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -07009480}
9481
Denis V. Lunev022cbae2007-11-13 03:23:50 -08009482static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07009483 .init = netdev_init,
9484 .exit = netdev_exit,
9485};
9486
Pavel Emelyanov46650792007-10-08 20:38:39 -07009487static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02009488{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009489 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02009490 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009491 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02009492 * initial network namespace
9493 */
9494 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009495 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009496 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07009497 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02009498
9499 /* Ignore unmoveable devices (i.e. loopback) */
9500 if (dev->features & NETIF_F_NETNS_LOCAL)
9501 continue;
9502
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00009503 /* Leave virtual devices for the generic cleanup */
9504 if (dev->rtnl_link_ops)
9505 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08009506
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009507 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07009508 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
9509 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02009510 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00009511 pr_emerg("%s: failed to move %s to init_net: %d\n",
9512 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07009513 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02009514 }
9515 }
9516 rtnl_unlock();
9517}
9518
Eric W. Biederman50624c92013-09-23 21:19:49 -07009519static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
9520{
9521 /* Return with the rtnl_lock held when there are no network
9522 * devices unregistering in any network namespace in net_list.
9523 */
9524 struct net *net;
9525 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +01009526 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009527
Peter Zijlstraff960a72014-10-29 17:04:56 +01009528 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009529 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -07009530 unregistering = false;
9531 rtnl_lock();
9532 list_for_each_entry(net, net_list, exit_list) {
9533 if (net->dev_unreg_count > 0) {
9534 unregistering = true;
9535 break;
9536 }
9537 }
9538 if (!unregistering)
9539 break;
9540 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +01009541
9542 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009543 }
Peter Zijlstraff960a72014-10-29 17:04:56 +01009544 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07009545}
9546
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009547static void __net_exit default_device_exit_batch(struct list_head *net_list)
9548{
9549 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04009550 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009551 * Do this across as many network namespaces as possible to
9552 * improve batching efficiency.
9553 */
9554 struct net_device *dev;
9555 struct net *net;
9556 LIST_HEAD(dev_kill_list);
9557
Eric W. Biederman50624c92013-09-23 21:19:49 -07009558 /* To prevent network device cleanup code from dereferencing
9559 * loopback devices or network devices that have been freed
9560 * wait here for all pending unregistrations to complete,
9561 * before unregistring the loopback device and allowing the
9562 * network namespace be freed.
9563 *
9564 * The netdev todo list containing all network devices
9565 * unregistrations that happen in default_device_exit_batch
9566 * will run in the rtnl_unlock() at the end of
9567 * default_device_exit_batch.
9568 */
9569 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009570 list_for_each_entry(net, net_list, exit_list) {
9571 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02009572 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009573 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
9574 else
9575 unregister_netdevice_queue(dev, &dev_kill_list);
9576 }
9577 }
9578 unregister_netdevice_many(&dev_kill_list);
9579 rtnl_unlock();
9580}
9581
Denis V. Lunev022cbae2007-11-13 03:23:50 -08009582static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02009583 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00009584 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02009585};
9586
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587/*
9588 * Initialize the DEV module. At boot time this walks the device list and
9589 * unhooks any devices that fail to initialise (normally hardware not
9590 * present) and leaves us with a valid list of present and active devices.
9591 *
9592 */
9593
9594/*
9595 * This is called single threaded during boot, so no need
9596 * to take the rtnl semaphore.
9597 */
9598static int __init net_dev_init(void)
9599{
9600 int i, rc = -ENOMEM;
9601
9602 BUG_ON(!dev_boot_phase);
9603
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 if (dev_proc_init())
9605 goto out;
9606
Eric W. Biederman8b41d182007-09-26 22:02:53 -07009607 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608 goto out;
9609
9610 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08009611 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612 INIT_LIST_HEAD(&ptype_base[i]);
9613
Vlad Yasevich62532da2012-11-15 08:49:10 +00009614 INIT_LIST_HEAD(&offload_base);
9615
Eric W. Biederman881d9662007-09-17 11:56:21 -07009616 if (register_pernet_subsys(&netdev_net_ops))
9617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618
9619 /*
9620 * Initialise the packet receive queues.
9621 */
9622
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07009623 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -07009624 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009625 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626
Eric Dumazet41852492016-08-26 12:50:39 -07009627 INIT_WORK(flush, flush_backlog);
9628
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009629 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07009630 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +01009631#ifdef CONFIG_XFRM_OFFLOAD
9632 skb_queue_head_init(&sd->xfrm_backlog);
9633#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009634 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00009635 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00009636#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009637 sd->csd.func = rps_trigger_softirq;
9638 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009639 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07009640#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00009641
David S. Miller7c4ec742018-07-20 23:37:55 -07009642 init_gro_hash(&sd->backlog);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00009643 sd->backlog.poll = process_backlog;
9644 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009645 }
9646
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 dev_boot_phase = 0;
9648
Eric W. Biederman505d4f72008-11-07 22:54:20 -08009649 /* The loopback device is special if any other network devices
9650 * is present in a network namespace the loopback device must
9651 * be present. Since we now dynamically allocate and free the
9652 * loopback device ensure this invariant is maintained by
9653 * keeping the loopback device as the first device on the
9654 * list of network devices. Ensuring the loopback devices
9655 * is the first device that appears and the last network device
9656 * that disappears.
9657 */
9658 if (register_pernet_device(&loopback_net_ops))
9659 goto out;
9660
9661 if (register_pernet_device(&default_device_ops))
9662 goto out;
9663
Carlos R. Mafra962cf362008-05-15 11:15:37 -03009664 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
9665 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01009667 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
9668 NULL, dev_cpu_dead);
9669 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 rc = 0;
9671out:
9672 return rc;
9673}
9674
9675subsys_initcall(net_dev_init);