blob: 94435cd09072b861fb77934599bfe625084684a7 [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/notifier.h>
97#include <linux/skbuff.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070098#include <linux/bpf.h>
David S. Millerb5cdae32017-04-18 15:36:58 -040099#include <linux/bpf_trace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200100#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -0800102#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700106#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <net/pkt_sched.h>
Jiri Pirko87d83092017-05-17 11:07:54 +0200108#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000110#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <linux/highmem.h>
112#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <linux/netpoll.h>
115#include <linux/rcupdate.h>
116#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500119#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700120#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700121#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700122#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700123#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700124#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700125#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700126#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700127#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700128#include <linux/ipv6.h>
129#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700130#include <linux/jhash.h>
131#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700132#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900133#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900134#include <trace/events/skb.h>
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +0000135#include <linux/pci.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700136#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000137#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100138#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300139#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700140#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100141#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400142#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800143#include <linux/hrtimer.h>
Pablo Neirae687ad62015-05-13 18:19:38 +0200144#include <linux/netfilter_ingress.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530145#include <linux/crash_dump.h>
Davide Carattib72b5bf2017-05-18 15:44:38 +0200146#include <linux/sctp.h>
Sabrina Dubrocaae847f42017-07-21 12:49:31 +0200147#include <net/udp_tunnel.h>
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200148#include <linux/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700150#include "net-sysfs.h"
151
Herbert Xud565b0a2008-12-15 23:38:52 -0800152/* Instead of increasing this, you should create a hash table. */
153#define MAX_GRO_SKBS 8
154
Herbert Xu5d38a072009-01-04 16:13:40 -0800155/* This should be increased if a protocol with a bigger head is added. */
156#define GRO_MAX_HEAD (MAX_HEADER + 128)
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000159static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000160struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
161struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000162static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000164static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli54951192014-07-01 21:39:43 -0700165static int call_netdevice_notifiers_info(unsigned long val,
Loic Prylli54951192014-07-01 21:39:43 -0700166 struct netdev_notifier_info *info);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200167static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700170 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * semaphore.
172 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800173 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700176 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * actual updates. This allows pure readers to access the list even
178 * while a writer is preparing to update it.
179 *
180 * To put it another way, dev_base_lock is held for writing only to
181 * protect against pure readers; the rtnl semaphore provides the
182 * protection against other writers.
183 *
184 * See, for example usages, register_netdevice() and
185 * unregister_netdevice(), which must be called with the rtnl
186 * semaphore held.
187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189EXPORT_SYMBOL(dev_base_lock);
190
Florian Westphal6c557002017-10-02 23:50:05 +0200191static DEFINE_MUTEX(ifalias_mutex);
192
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300193/* protects napi_hash addition/deletion and napi_gen_id */
194static DEFINE_SPINLOCK(napi_hash_lock);
195
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800196static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800197static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300198
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200199static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000200
Thomas Graf4e985ad2011-06-21 03:11:20 +0000201static inline void dev_base_seq_inc(struct net *net)
202{
tcharding643aa9c2017-02-09 17:56:05 +1100203 while (++net->dev_base_seq == 0)
204 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000205}
206
Eric W. Biederman881d9662007-09-17 11:56:21 -0700207static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700209 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000210
stephen hemminger08e98972009-11-10 07:20:34 +0000211 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Eric W. Biederman881d9662007-09-17 11:56:21 -0700214static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700216 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000219static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000220{
221#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000222 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000223#endif
224}
225
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000226static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000227{
228#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000229 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000230#endif
231}
232
Eric W. Biedermance286d32007-09-12 13:53:49 +0200233/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000234static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200235{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900236 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200237
238 ASSERT_RTNL();
239
240 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800241 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000242 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000243 hlist_add_head_rcu(&dev->index_hlist,
244 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200245 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000246
247 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200248}
249
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000250/* Device list removal
251 * caller must respect a RCU grace period before freeing/reusing dev
252 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200253static void unlist_netdevice(struct net_device *dev)
254{
255 ASSERT_RTNL();
256
257 /* Unlink dev from the device chain */
258 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800259 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000260 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000261 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200262 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000263
264 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * Our notifier list
269 */
270
Alan Sternf07d5b92006-05-09 15:23:03 -0700271static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*
274 * Device drivers call our routines to queue packets here. We empty the
275 * queue in the local softnet handler.
276 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700277
Eric Dumazet9958da02010-04-17 04:17:02 +0000278DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700279EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David S. Millercf508b12008-07-22 14:16:42 -0700281#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700282/*
David S. Millerc773e842008-07-08 23:13:53 -0700283 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700284 * according to dev->type
285 */
tcharding643aa9c2017-02-09 17:56:05 +1100286static const unsigned short netdev_lock_type[] = {
287 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700288 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
289 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
290 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
291 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
292 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
293 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
294 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
295 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
296 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
297 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
298 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
Paul Gortmaker211ed862012-05-10 17:14:35 -0400299 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
300 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
301 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700302
tcharding643aa9c2017-02-09 17:56:05 +1100303static const char *const netdev_lock_name[] = {
304 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
305 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
306 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
307 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
308 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
309 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
310 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
311 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
312 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
313 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
314 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
315 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
316 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
317 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
318 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700319
320static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700321static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700322
323static inline unsigned short netdev_lock_pos(unsigned short dev_type)
324{
325 int i;
326
327 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
328 if (netdev_lock_type[i] == dev_type)
329 return i;
330 /* the last key is used by default */
331 return ARRAY_SIZE(netdev_lock_type) - 1;
332}
333
David S. Millercf508b12008-07-22 14:16:42 -0700334static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
335 unsigned short dev_type)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700336{
337 int i;
338
339 i = netdev_lock_pos(dev_type);
340 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
341 netdev_lock_name[i]);
342}
David S. Millercf508b12008-07-22 14:16:42 -0700343
344static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
345{
346 int i;
347
348 i = netdev_lock_pos(dev->type);
349 lockdep_set_class_and_name(&dev->addr_list_lock,
350 &netdev_addr_lock_key[i],
351 netdev_lock_name[i]);
352}
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700353#else
David S. Millercf508b12008-07-22 14:16:42 -0700354static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
355 unsigned short dev_type)
356{
357}
358static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700359{
360}
361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100364 *
365 * Protocol management and registration routines
366 *
367 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Add a protocol ID to the list. Now that the input handler is
372 * smarter we can dispense with all the messy stuff that used to be
373 * here.
374 *
375 * BEWARE!!! Protocol handlers, mangling input packets,
376 * MUST BE last in hash buckets and checking protocol handlers
377 * MUST start from promiscuous ptype_all chain in net_bh.
378 * It is true now, do not change it.
379 * Explanation follows: if protocol handler, mangling packet, will
380 * be the first on list, it is not able to sense, that packet
381 * is cloned and should be copied-on-write, so that it will
382 * change it and subsequent readers will get broken packet.
383 * --ANK (980803)
384 */
385
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000386static inline struct list_head *ptype_head(const struct packet_type *pt)
387{
388 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800389 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000390 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800391 return pt->dev ? &pt->dev->ptype_specific :
392 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
396 * dev_add_pack - add packet handler
397 * @pt: packet type declaration
398 *
399 * Add a protocol handler to the networking stack. The passed &packet_type
400 * is linked into kernel lists and may not be freed until it has been
401 * removed from the kernel lists.
402 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900403 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * guarantee all CPU's that are in middle of receiving packets
405 * will see the new packet type (until the next received packet).
406 */
407
408void dev_add_pack(struct packet_type *pt)
409{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000410 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000412 spin_lock(&ptype_lock);
413 list_add_rcu(&pt->list, head);
414 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700416EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * __dev_remove_pack - remove packet handler
420 * @pt: packet type declaration
421 *
422 * Remove a protocol handler that was previously added to the kernel
423 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
424 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900425 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
427 * The packet type might still be in use by receivers
428 * and must not be freed until after all the CPU's have gone
429 * through a quiescent state.
430 */
431void __dev_remove_pack(struct packet_type *pt)
432{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000433 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct packet_type *pt1;
435
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000436 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 list_for_each_entry(pt1, head, list) {
439 if (pt == pt1) {
440 list_del_rcu(&pt->list);
441 goto out;
442 }
443 }
444
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000445 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000447 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700449EXPORT_SYMBOL(__dev_remove_pack);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * dev_remove_pack - remove packet handler
453 * @pt: packet type declaration
454 *
455 * Remove a protocol handler that was previously added to the kernel
456 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
457 * from the kernel lists and can be freed or reused once this function
458 * returns.
459 *
460 * This call sleeps to guarantee that no CPU is looking at the packet
461 * type after return.
462 */
463void dev_remove_pack(struct packet_type *pt)
464{
465 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 synchronize_net();
468}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700469EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Vlad Yasevich62532da2012-11-15 08:49:10 +0000471
472/**
473 * dev_add_offload - register offload handlers
474 * @po: protocol offload declaration
475 *
476 * Add protocol offload handlers to the networking stack. The passed
477 * &proto_offload is linked into kernel lists and may not be freed until
478 * it has been removed from the kernel lists.
479 *
480 * This call does not sleep therefore it can not
481 * guarantee all CPU's that are in middle of receiving packets
482 * will see the new offload handlers (until the next received packet).
483 */
484void dev_add_offload(struct packet_offload *po)
485{
David S. Millerbdef7de2015-06-01 14:56:09 -0700486 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000487
488 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700489 list_for_each_entry(elem, &offload_base, list) {
490 if (po->priority < elem->priority)
491 break;
492 }
493 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000494 spin_unlock(&offload_lock);
495}
496EXPORT_SYMBOL(dev_add_offload);
497
498/**
499 * __dev_remove_offload - remove offload handler
500 * @po: packet offload declaration
501 *
502 * Remove a protocol offload handler that was previously added to the
503 * kernel offload handlers by dev_add_offload(). The passed &offload_type
504 * is removed from the kernel lists and can be freed or reused once this
505 * function returns.
506 *
507 * The packet type might still be in use by receivers
508 * and must not be freed until after all the CPU's have gone
509 * through a quiescent state.
510 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800511static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000512{
513 struct list_head *head = &offload_base;
514 struct packet_offload *po1;
515
Eric Dumazetc53aa502012-11-16 08:08:23 +0000516 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000517
518 list_for_each_entry(po1, head, list) {
519 if (po == po1) {
520 list_del_rcu(&po->list);
521 goto out;
522 }
523 }
524
525 pr_warn("dev_remove_offload: %p not found\n", po);
526out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000527 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000528}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000529
530/**
531 * dev_remove_offload - remove packet offload handler
532 * @po: packet offload declaration
533 *
534 * Remove a packet offload handler that was previously added to the kernel
535 * offload handlers by dev_add_offload(). The passed &offload_type is
536 * removed from the kernel lists and can be freed or reused once this
537 * function returns.
538 *
539 * This call sleeps to guarantee that no CPU is looking at the packet
540 * type after return.
541 */
542void dev_remove_offload(struct packet_offload *po)
543{
544 __dev_remove_offload(po);
545
546 synchronize_net();
547}
548EXPORT_SYMBOL(dev_remove_offload);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100551 *
552 * Device Boot-time Settings Routines
553 *
554 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556/* Boot time configuration table */
557static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
558
559/**
560 * netdev_boot_setup_add - add new setup entry
561 * @name: name of the device
562 * @map: configured settings for the device
563 *
564 * Adds new setup entry to the dev_boot_setup list. The function
565 * returns 0 on error and 1 on success. This is a generic routine to
566 * all netdevices.
567 */
568static int netdev_boot_setup_add(char *name, struct ifmap *map)
569{
570 struct netdev_boot_setup *s;
571 int i;
572
573 s = dev_boot_setup;
574 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
575 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
576 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700577 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 memcpy(&s[i].map, map, sizeof(s[i].map));
579 break;
580 }
581 }
582
583 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
584}
585
586/**
tcharding722c9a02017-02-09 17:56:04 +1100587 * netdev_boot_setup_check - check boot time settings
588 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 *
tcharding722c9a02017-02-09 17:56:04 +1100590 * Check boot time settings for the device.
591 * The found settings are set for the device to be used
592 * later in the device probing.
593 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595int netdev_boot_setup_check(struct net_device *dev)
596{
597 struct netdev_boot_setup *s = dev_boot_setup;
598 int i;
599
600 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
601 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700602 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100603 dev->irq = s[i].map.irq;
604 dev->base_addr = s[i].map.base_addr;
605 dev->mem_start = s[i].map.mem_start;
606 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 1;
608 }
609 }
610 return 0;
611}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700612EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614
615/**
tcharding722c9a02017-02-09 17:56:04 +1100616 * netdev_boot_base - get address from boot time settings
617 * @prefix: prefix for network device
618 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
tcharding722c9a02017-02-09 17:56:04 +1100620 * Check boot time settings for the base address of device.
621 * The found settings are set for the device to be used
622 * later in the device probing.
623 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
625unsigned long netdev_boot_base(const char *prefix, int unit)
626{
627 const struct netdev_boot_setup *s = dev_boot_setup;
628 char name[IFNAMSIZ];
629 int i;
630
631 sprintf(name, "%s%d", prefix, unit);
632
633 /*
634 * If device already registered then return base of 1
635 * to indicate not to probe for this interface
636 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700637 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 1;
639
640 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
641 if (!strcmp(name, s[i].name))
642 return s[i].map.base_addr;
643 return 0;
644}
645
646/*
647 * Saves at boot time configured settings for any netdevice.
648 */
649int __init netdev_boot_setup(char *str)
650{
651 int ints[5];
652 struct ifmap map;
653
654 str = get_options(str, ARRAY_SIZE(ints), ints);
655 if (!str || !*str)
656 return 0;
657
658 /* Save settings */
659 memset(&map, 0, sizeof(map));
660 if (ints[0] > 0)
661 map.irq = ints[1];
662 if (ints[0] > 1)
663 map.base_addr = ints[2];
664 if (ints[0] > 2)
665 map.mem_start = ints[3];
666 if (ints[0] > 3)
667 map.mem_end = ints[4];
668
669 /* Add new entry to the list */
670 return netdev_boot_setup_add(str, &map);
671}
672
673__setup("netdev=", netdev_boot_setup);
674
675/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100676 *
677 * Device Interface Subroutines
678 *
679 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200682 * dev_get_iflink - get 'iflink' value of a interface
683 * @dev: targeted interface
684 *
685 * Indicates the ifindex the interface is linked to.
686 * Physical interfaces have the same 'ifindex' and 'iflink' values.
687 */
688
689int dev_get_iflink(const struct net_device *dev)
690{
691 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
692 return dev->netdev_ops->ndo_get_iflink(dev);
693
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200694 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200695}
696EXPORT_SYMBOL(dev_get_iflink);
697
698/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700699 * dev_fill_metadata_dst - Retrieve tunnel egress information.
700 * @dev: targeted interface
701 * @skb: The packet.
702 *
703 * For better visibility of tunnel traffic OVS needs to retrieve
704 * egress tunnel information for a packet. Following API allows
705 * user to get this info.
706 */
707int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
708{
709 struct ip_tunnel_info *info;
710
711 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
712 return -EINVAL;
713
714 info = skb_tunnel_info_unclone(skb);
715 if (!info)
716 return -ENOMEM;
717 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
718 return -EINVAL;
719
720 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
721}
722EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
723
724/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700726 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * @name: name to find
728 *
729 * Find an interface by name. Must be called under RTNL semaphore
730 * or @dev_base_lock. If the name is found a pointer to the device
731 * is returned. If the name is not found then %NULL is returned. The
732 * reference counters are not incremented so the caller must be
733 * careful with locks.
734 */
735
Eric W. Biederman881d9662007-09-17 11:56:21 -0700736struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700738 struct net_device *dev;
739 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Sasha Levinb67bfe02013-02-27 17:06:00 -0800741 hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!strncmp(dev->name, name, IFNAMSIZ))
743 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return NULL;
746}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700747EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/**
tcharding722c9a02017-02-09 17:56:04 +1100750 * dev_get_by_name_rcu - find a device by its name
751 * @net: the applicable net namespace
752 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000753 *
tcharding722c9a02017-02-09 17:56:04 +1100754 * Find an interface by name.
755 * If the name is found a pointer to the device is returned.
756 * If the name is not found then %NULL is returned.
757 * The reference counters are not incremented so the caller must be
758 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000759 */
760
761struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
762{
Eric Dumazet72c95282009-10-30 07:11:27 +0000763 struct net_device *dev;
764 struct hlist_head *head = dev_name_hash(net, name);
765
Sasha Levinb67bfe02013-02-27 17:06:00 -0800766 hlist_for_each_entry_rcu(dev, head, name_hlist)
Eric Dumazet72c95282009-10-30 07:11:27 +0000767 if (!strncmp(dev->name, name, IFNAMSIZ))
768 return dev;
769
770 return NULL;
771}
772EXPORT_SYMBOL(dev_get_by_name_rcu);
773
774/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700776 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * @name: name to find
778 *
779 * Find an interface by name. This can be called from any
780 * context and does its own locking. The returned handle has
781 * the usage count incremented and the caller must use dev_put() to
782 * release it when it is no longer needed. %NULL is returned if no
783 * matching device is found.
784 */
785
Eric W. Biederman881d9662007-09-17 11:56:21 -0700786struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct net_device *dev;
789
Eric Dumazet72c95282009-10-30 07:11:27 +0000790 rcu_read_lock();
791 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (dev)
793 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000794 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return dev;
796}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700797EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799/**
800 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700801 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * @ifindex: index of device
803 *
804 * Search for an interface by index. Returns %NULL if the device
805 * is not found or a pointer to the device. The device has not
806 * had its reference counter increased so the caller must be careful
807 * about locking. The caller must hold either the RTNL semaphore
808 * or @dev_base_lock.
809 */
810
Eric W. Biederman881d9662007-09-17 11:56:21 -0700811struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700813 struct net_device *dev;
814 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Sasha Levinb67bfe02013-02-27 17:06:00 -0800816 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (dev->ifindex == ifindex)
818 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return NULL;
821}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700822EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000824/**
825 * dev_get_by_index_rcu - find a device by its ifindex
826 * @net: the applicable net namespace
827 * @ifindex: index of device
828 *
829 * Search for an interface by index. Returns %NULL if the device
830 * is not found or a pointer to the device. The device has not
831 * had its reference counter increased so the caller must be careful
832 * about locking. The caller must hold RCU lock.
833 */
834
835struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
836{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000837 struct net_device *dev;
838 struct hlist_head *head = dev_index_hash(net, ifindex);
839
Sasha Levinb67bfe02013-02-27 17:06:00 -0800840 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000841 if (dev->ifindex == ifindex)
842 return dev;
843
844 return NULL;
845}
846EXPORT_SYMBOL(dev_get_by_index_rcu);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849/**
850 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700851 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * @ifindex: index of device
853 *
854 * Search for an interface by index. Returns NULL if the device
855 * is not found or a pointer to the device. The device returned has
856 * had a reference added and the pointer is safe until the user calls
857 * dev_put to indicate they have finished with it.
858 */
859
Eric W. Biederman881d9662007-09-17 11:56:21 -0700860struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct net_device *dev;
863
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000864 rcu_read_lock();
865 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (dev)
867 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000868 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return dev;
870}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700871EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200874 * dev_get_by_napi_id - find a device by napi_id
875 * @napi_id: ID of the NAPI struct
876 *
877 * Search for an interface by NAPI ID. Returns %NULL if the device
878 * is not found or a pointer to the device. The device has not had
879 * its reference counter increased so the caller must be careful
880 * about locking. The caller must hold RCU lock.
881 */
882
883struct net_device *dev_get_by_napi_id(unsigned int napi_id)
884{
885 struct napi_struct *napi;
886
887 WARN_ON_ONCE(!rcu_read_lock_held());
888
889 if (napi_id < MIN_NAPI_ID)
890 return NULL;
891
892 napi = napi_by_id(napi_id);
893
894 return napi ? napi->dev : NULL;
895}
896EXPORT_SYMBOL(dev_get_by_napi_id);
897
898/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200899 * netdev_get_name - get a netdevice name, knowing its ifindex.
900 * @net: network namespace
901 * @name: a pointer to the buffer where the name will be stored.
902 * @ifindex: the ifindex of the interface to get the name from.
903 *
904 * The use of raw_seqcount_begin() and cond_resched() before
905 * retrying is required as we want to give the writers a chance
906 * to complete when CONFIG_PREEMPT is not set.
907 */
908int netdev_get_name(struct net *net, char *name, int ifindex)
909{
910 struct net_device *dev;
911 unsigned int seq;
912
913retry:
914 seq = raw_seqcount_begin(&devnet_rename_seq);
915 rcu_read_lock();
916 dev = dev_get_by_index_rcu(net, ifindex);
917 if (!dev) {
918 rcu_read_unlock();
919 return -ENODEV;
920 }
921
922 strcpy(name, dev->name);
923 rcu_read_unlock();
924 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
925 cond_resched();
926 goto retry;
927 }
928
929 return 0;
930}
931
932/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000933 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700934 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * @type: media type of device
936 * @ha: hardware address
937 *
938 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800939 * is not found or a pointer to the device.
940 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000941 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * and the caller must therefore be careful about locking
943 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
945
Eric Dumazet941666c2010-12-05 01:23:53 +0000946struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
947 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 struct net_device *dev;
950
Eric Dumazet941666c2010-12-05 01:23:53 +0000951 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (dev->type == type &&
953 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700954 return dev;
955
956 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
Eric Dumazet941666c2010-12-05 01:23:53 +0000958EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300959
Eric W. Biederman881d9662007-09-17 11:56:21 -0700960struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700961{
962 struct net_device *dev;
963
964 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700965 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700966 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700967 return dev;
968
969 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700970}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700971EXPORT_SYMBOL(__dev_getfirstbyhwtype);
972
Eric W. Biederman881d9662007-09-17 11:56:21 -0700973struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000975 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000977 rcu_read_lock();
978 for_each_netdev_rcu(net, dev)
979 if (dev->type == type) {
980 dev_hold(dev);
981 ret = dev;
982 break;
983 }
984 rcu_read_unlock();
985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987EXPORT_SYMBOL(dev_getfirstbyhwtype);
988
989/**
WANG Cong6c555492014-09-11 15:35:09 -0700990 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700991 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * @if_flags: IFF_* values
993 * @mask: bitmask of bits in if_flags to check
994 *
995 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000996 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -0700997 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999
WANG Cong6c555492014-09-11 15:35:09 -07001000struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1001 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001003 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
WANG Cong6c555492014-09-11 15:35:09 -07001005 ASSERT_RTNL();
1006
Pavel Emelianov7562f872007-05-03 15:13:45 -07001007 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001008 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001010 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
1012 }
1013 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001014 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
WANG Cong6c555492014-09-11 15:35:09 -07001016EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018/**
1019 * dev_valid_name - check if name is okay for network device
1020 * @name: name string
1021 *
1022 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -07001023 * to allow sysfs to work. We also disallow any kind of
1024 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
David S. Miller95f050b2012-03-06 16:12:15 -05001026bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001028 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001029 return false;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07001030 if (strlen(name) >= IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001031 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001032 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001033 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001034
1035 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001036 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001037 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001038 name++;
1039 }
David S. Miller95f050b2012-03-06 16:12:15 -05001040 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001042EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001045 * __dev_alloc_name - allocate a name for a device
1046 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001048 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
1050 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001051 * id. It scans list of devices to build up a free map, then chooses
1052 * the first empty slot. The caller must hold the dev_base or rtnl lock
1053 * while allocating the name and adding the device in order to avoid
1054 * duplicates.
1055 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1056 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
1058
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001059static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 const char *p;
1063 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001064 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct net_device *d;
1066
Rasmus Villemoes93809102017-11-13 00:15:08 +01001067 if (!dev_valid_name(name))
1068 return -EINVAL;
1069
Rasmus Villemoes51f299d2017-11-13 00:15:04 +01001070 p = strchr(name, '%');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (p) {
1072 /*
1073 * Verify the string as this thing may have come from
1074 * the user. There must be either one "%d" and no other "%"
1075 * characters.
1076 */
1077 if (p[1] != 'd' || strchr(p + 2, '%'))
1078 return -EINVAL;
1079
1080 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001081 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!inuse)
1083 return -ENOMEM;
1084
Eric W. Biederman881d9662007-09-17 11:56:21 -07001085 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!sscanf(d->name, name, &i))
1087 continue;
1088 if (i < 0 || i >= max_netdevices)
1089 continue;
1090
1091 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001092 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!strncmp(buf, d->name, IFNAMSIZ))
1094 set_bit(i, inuse);
1095 }
1096
1097 i = find_first_zero_bit(inuse, max_netdevices);
1098 free_page((unsigned long) inuse);
1099 }
1100
Rasmus Villemoes6224abd2017-11-13 00:15:07 +01001101 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001102 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /* It is possible to run out of possible slots
1106 * when the name is long and there isn't enough space left
1107 * for the digits, or if all bits are used.
1108 */
Johannes Berg029b6d12017-12-02 08:41:55 +01001109 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001112static int dev_alloc_name_ns(struct net *net,
1113 struct net_device *dev,
1114 const char *name)
1115{
1116 char buf[IFNAMSIZ];
1117 int ret;
1118
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001119 BUG_ON(!net);
Rasmus Villemoes2c88b852017-11-13 00:15:05 +01001120 ret = __dev_alloc_name(net, name, buf);
1121 if (ret >= 0)
1122 strlcpy(dev->name, buf, IFNAMSIZ);
1123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001126/**
1127 * dev_alloc_name - allocate a name for a device
1128 * @dev: device
1129 * @name: name format string
1130 *
1131 * Passed a format string - eg "lt%d" it will try and find a suitable
1132 * id. It scans list of devices to build up a free map, then chooses
1133 * the first empty slot. The caller must hold the dev_base or rtnl lock
1134 * while allocating the name and adding the device in order to avoid
1135 * duplicates.
1136 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1137 * Returns the number of the unit assigned or a negative errno code.
1138 */
1139
1140int dev_alloc_name(struct net_device *dev, const char *name)
1141{
Rasmus Villemoesc46d7642017-11-13 00:15:06 +01001142 return dev_alloc_name_ns(dev_net(dev), dev, name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001143}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001144EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001145
Cong Wang0ad646c2017-10-13 11:58:53 -07001146int dev_get_valid_name(struct net *net, struct net_device *dev,
1147 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001148{
David S. Miller55a5ec92018-01-02 11:45:07 -05001149 BUG_ON(!net);
1150
1151 if (!dev_valid_name(name))
1152 return -EINVAL;
1153
1154 if (strchr(name, '%'))
1155 return dev_alloc_name_ns(net, dev, name);
1156 else if (__dev_get_by_name(net, name))
1157 return -EEXIST;
1158 else if (dev->name != name)
1159 strlcpy(dev->name, name, IFNAMSIZ);
1160
1161 return 0;
Octavian Purdilad9031022009-11-18 02:36:59 +00001162}
Cong Wang0ad646c2017-10-13 11:58:53 -07001163EXPORT_SYMBOL(dev_get_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165/**
1166 * dev_change_name - change name of a device
1167 * @dev: device
1168 * @newname: name (or format string) must be at least IFNAMSIZ
1169 *
1170 * Change name of a device, can pass format strings "eth%d".
1171 * for wildcarding.
1172 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001173int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Tom Gundersen238fa362014-07-14 16:37:23 +02001175 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001176 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001178 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001179 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001182 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001184 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (dev->flags & IFF_UP)
1186 return -EBUSY;
1187
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001188 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001189
1190 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001191 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001192 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001193 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001194
Herbert Xufcc5a032007-07-30 17:03:38 -07001195 memcpy(oldname, dev->name, IFNAMSIZ);
1196
Gao feng828de4f2012-09-13 20:58:27 +00001197 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001198 if (err < 0) {
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001199 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001200 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001203 if (oldname[0] && !strchr(oldname, '%'))
1204 netdev_info(dev, "renamed from %s\n", oldname);
1205
Tom Gundersen238fa362014-07-14 16:37:23 +02001206 old_assign_type = dev->name_assign_type;
1207 dev->name_assign_type = NET_NAME_RENAMED;
1208
Herbert Xufcc5a032007-07-30 17:03:38 -07001209rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001210 ret = device_rename(&dev->dev, dev->name);
1211 if (ret) {
1212 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001213 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001214 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001215 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001216 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001217
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001218 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001219
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001220 netdev_adjacent_rename_links(dev, oldname);
1221
Herbert Xu7f988ea2007-07-30 16:35:46 -07001222 write_lock_bh(&dev_base_lock);
Eric Dumazet372b2312011-05-17 13:56:59 -04001223 hlist_del_rcu(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001224 write_unlock_bh(&dev_base_lock);
1225
1226 synchronize_rcu();
1227
1228 write_lock_bh(&dev_base_lock);
1229 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001230 write_unlock_bh(&dev_base_lock);
1231
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001232 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001233 ret = notifier_to_errno(ret);
1234
1235 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001236 /* err >= 0 after dev_alloc_name() or stores the first errno */
1237 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001238 err = ret;
Eric Dumazet30e6c9fa2012-12-20 17:25:08 +00001239 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001240 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001241 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001242 dev->name_assign_type = old_assign_type;
1243 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001244 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001245 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001246 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001247 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001248 }
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 return err;
1252}
1253
1254/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001255 * dev_set_alias - change ifalias of a device
1256 * @dev: device
1257 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001258 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001259 *
1260 * Set ifalias for a device,
1261 */
1262int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1263{
Florian Westphal6c557002017-10-02 23:50:05 +02001264 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001265
1266 if (len >= IFALIASZ)
1267 return -EINVAL;
1268
Florian Westphal6c557002017-10-02 23:50:05 +02001269 if (len) {
1270 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1271 if (!new_alias)
1272 return -ENOMEM;
1273
1274 memcpy(new_alias->ifalias, alias, len);
1275 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a2c2008-09-23 21:23:19 -07001276 }
1277
Florian Westphal6c557002017-10-02 23:50:05 +02001278 mutex_lock(&ifalias_mutex);
1279 rcu_swap_protected(dev->ifalias, new_alias,
1280 mutex_is_locked(&ifalias_mutex));
1281 mutex_unlock(&ifalias_mutex);
1282
1283 if (new_alias)
1284 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001285
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001286 return len;
1287}
1288
Florian Westphal6c557002017-10-02 23:50:05 +02001289/**
1290 * dev_get_alias - get ifalias of a device
1291 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001292 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001293 * @len: size of buffer
1294 *
1295 * get ifalias for a device. Caller must make sure dev cannot go
1296 * away, e.g. rcu read lock or own a reference count to device.
1297 */
1298int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1299{
1300 const struct dev_ifalias *alias;
1301 int ret = 0;
1302
1303 rcu_read_lock();
1304 alias = rcu_dereference(dev->ifalias);
1305 if (alias)
1306 ret = snprintf(name, len, "%s", alias->ifalias);
1307 rcu_read_unlock();
1308
1309 return ret;
1310}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001311
1312/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001313 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001314 * @dev: device to cause notification
1315 *
1316 * Called to indicate a device has changed features.
1317 */
1318void netdev_features_change(struct net_device *dev)
1319{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001320 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001321}
1322EXPORT_SYMBOL(netdev_features_change);
1323
1324/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * netdev_state_change - device changes state
1326 * @dev: device to cause notification
1327 *
1328 * Called to indicate a device has changed state. This function calls
1329 * the notifier chains for netdev_chain and sends a NEWLINK message
1330 * to the routing socket.
1331 */
1332void netdev_state_change(struct net_device *dev)
1333{
1334 if (dev->flags & IFF_UP) {
David Ahern51d0c0472017-10-04 17:48:45 -07001335 struct netdev_notifier_change_info change_info = {
1336 .info.dev = dev,
1337 };
Loic Prylli54951192014-07-01 21:39:43 -07001338
David Ahern51d0c0472017-10-04 17:48:45 -07001339 call_netdevice_notifiers_info(NETDEV_CHANGE,
Loic Prylli54951192014-07-01 21:39:43 -07001340 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001341 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001344EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Amerigo Wangee89bab2012-08-09 22:14:56 +00001346/**
tcharding722c9a02017-02-09 17:56:04 +11001347 * netdev_notify_peers - notify network peers about existence of @dev
1348 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001349 *
1350 * Generate traffic such that interested network peers are aware of
1351 * @dev, such as by generating a gratuitous ARP. This may be used when
1352 * a device wants to inform the rest of the network about some sort of
1353 * reconfiguration such as a failover event or virtual machine
1354 * migration.
1355 */
1356void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001357{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001358 rtnl_lock();
1359 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001360 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001361 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001362}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001363EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001364
Patrick McHardybd380812010-02-26 06:34:53 +00001365static int __dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001367 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001368 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001370 ASSERT_RTNL();
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!netif_device_present(dev))
1373 return -ENODEV;
1374
Neil Hormanca99ca12013-02-05 08:05:43 +00001375 /* Block netpoll from trying to do any rx path servicing.
1376 * If we don't do this there is a chance ndo_poll_controller
1377 * or ndo_poll may be running while we open the device
1378 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001379 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001380
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001381 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1382 ret = notifier_to_errno(ret);
1383 if (ret)
1384 return ret;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001387
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001388 if (ops->ndo_validate_addr)
1389 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001390
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001391 if (!ret && ops->ndo_open)
1392 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Eric W. Biederman66b55522014-03-27 15:39:03 -07001394 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001395
Jeff Garzikbada3392007-10-23 20:19:37 -07001396 if (ret)
1397 clear_bit(__LINK_STATE_START, &dev->state);
1398 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001400 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001402 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return ret;
1406}
Patrick McHardybd380812010-02-26 06:34:53 +00001407
1408/**
1409 * dev_open - prepare an interface for use.
1410 * @dev: device to open
1411 *
1412 * Takes a device from down to up state. The device's private open
1413 * function is invoked and then the multicast lists are loaded. Finally
1414 * the device is moved into the up state and a %NETDEV_UP message is
1415 * sent to the netdev notifier chain.
1416 *
1417 * Calling this function on an active interface is a nop. On a failure
1418 * a negative errno code is returned.
1419 */
1420int dev_open(struct net_device *dev)
1421{
1422 int ret;
1423
Patrick McHardybd380812010-02-26 06:34:53 +00001424 if (dev->flags & IFF_UP)
1425 return 0;
1426
Patrick McHardybd380812010-02-26 06:34:53 +00001427 ret = __dev_open(dev);
1428 if (ret < 0)
1429 return ret;
1430
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001431 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001432 call_netdevice_notifiers(NETDEV_UP, dev);
1433
1434 return ret;
1435}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001436EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
stephen hemminger7051b882017-07-18 15:59:27 -07001438static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Octavian Purdila44345722010-12-13 12:44:07 +00001440 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001441
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001442 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001443 might_sleep();
1444
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001445 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001446 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001447 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001448
Octavian Purdila44345722010-12-13 12:44:07 +00001449 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Octavian Purdila44345722010-12-13 12:44:07 +00001451 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Octavian Purdila44345722010-12-13 12:44:07 +00001453 /* Synchronize to scheduled poll. We cannot touch poll list, it
1454 * can be even on different cpu. So just clear netif_running().
1455 *
1456 * dev->stop() will invoke napi_disable() on all of it's
1457 * napi_struct instances on this device.
1458 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001459 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Octavian Purdila44345722010-12-13 12:44:07 +00001462 dev_deactivate_many(head);
1463
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001464 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001465 const struct net_device_ops *ops = dev->netdev_ops;
1466
1467 /*
1468 * Call the device specific close. This cannot fail.
1469 * Only if device is UP
1470 *
1471 * We allow it to be called even after a DETACH hot-plug
1472 * event.
1473 */
1474 if (ops->ndo_stop)
1475 ops->ndo_stop(dev);
1476
Octavian Purdila44345722010-12-13 12:44:07 +00001477 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001478 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001479 }
Octavian Purdila44345722010-12-13 12:44:07 +00001480}
1481
stephen hemminger7051b882017-07-18 15:59:27 -07001482static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001483{
1484 LIST_HEAD(single);
1485
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001486 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001487 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001488 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001489}
1490
stephen hemminger7051b882017-07-18 15:59:27 -07001491void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001492{
1493 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001494
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001495 /* Remove the devices that don't need to be closed */
1496 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001497 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001498 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001499
1500 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001501
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001502 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001503 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001504 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001505 if (unlink)
1506 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
David S. Miller99c4a262015-03-18 22:52:33 -04001509EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001510
1511/**
1512 * dev_close - shutdown an interface.
1513 * @dev: device to shutdown
1514 *
1515 * This function moves an active device into down state. A
1516 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1517 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1518 * chain.
1519 */
stephen hemminger7051b882017-07-18 15:59:27 -07001520void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001521{
Eric Dumazete14a5992011-05-10 12:26:06 -07001522 if (dev->flags & IFF_UP) {
1523 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001524
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001525 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001526 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001527 list_del(&single);
1528 }
Patrick McHardybd380812010-02-26 06:34:53 +00001529}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001530EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001533/**
1534 * dev_disable_lro - disable Large Receive Offload on a device
1535 * @dev: device
1536 *
1537 * Disable Large Receive Offload (LRO) on a net device. Must be
1538 * called under RTNL. This is needed if received packets may be
1539 * forwarded to another interface.
1540 */
1541void dev_disable_lro(struct net_device *dev)
1542{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001543 struct net_device *lower_dev;
1544 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001545
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001546 dev->wanted_features &= ~NETIF_F_LRO;
1547 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001548
Michał Mirosław22d59692011-04-21 12:42:15 +00001549 if (unlikely(dev->features & NETIF_F_LRO))
1550 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001551
1552 netdev_for_each_lower_dev(dev, lower_dev, iter)
1553 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001554}
1555EXPORT_SYMBOL(dev_disable_lro);
1556
Michael Chan56f5aa72017-12-16 03:09:41 -05001557/**
1558 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1559 * @dev: device
1560 *
1561 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1562 * called under RTNL. This is needed if Generic XDP is installed on
1563 * the device.
1564 */
1565static void dev_disable_gro_hw(struct net_device *dev)
1566{
1567 dev->wanted_features &= ~NETIF_F_GRO_HW;
1568 netdev_update_features(dev);
1569
1570 if (unlikely(dev->features & NETIF_F_GRO_HW))
1571 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1572}
1573
Jiri Pirko351638e2013-05-28 01:30:21 +00001574static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1575 struct net_device *dev)
1576{
David Ahern51d0c0472017-10-04 17:48:45 -07001577 struct netdev_notifier_info info = {
1578 .dev = dev,
1579 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001580
Jiri Pirko351638e2013-05-28 01:30:21 +00001581 return nb->notifier_call(nb, val, &info);
1582}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001583
Eric W. Biederman881d9662007-09-17 11:56:21 -07001584static int dev_boot_phase = 1;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/**
tcharding722c9a02017-02-09 17:56:04 +11001587 * register_netdevice_notifier - register a network notifier block
1588 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *
tcharding722c9a02017-02-09 17:56:04 +11001590 * Register a notifier to be called when network device events occur.
1591 * The notifier passed is linked into the kernel structures and must
1592 * not be reused until it has been unregistered. A negative errno code
1593 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 *
tcharding722c9a02017-02-09 17:56:04 +11001595 * When registered all registration and up events are replayed
1596 * to the new notifier to allow device to have a race free
1597 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
1599
1600int register_netdevice_notifier(struct notifier_block *nb)
1601{
1602 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001603 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001604 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int err;
1606
1607 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001608 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001609 if (err)
1610 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001611 if (dev_boot_phase)
1612 goto unlock;
1613 for_each_net(net) {
1614 for_each_netdev(net, dev) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001615 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001616 err = notifier_to_errno(err);
1617 if (err)
1618 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Eric W. Biederman881d9662007-09-17 11:56:21 -07001620 if (!(dev->flags & IFF_UP))
1621 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001622
Jiri Pirko351638e2013-05-28 01:30:21 +00001623 call_netdevice_notifier(nb, NETDEV_UP, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001626
1627unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 rtnl_unlock();
1629 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001630
1631rollback:
1632 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001633 for_each_net(net) {
1634 for_each_netdev(net, dev) {
1635 if (dev == last)
RongQing.Li8f891482011-11-30 23:43:07 -05001636 goto outroll;
Herbert Xufcc5a032007-07-30 17:03:38 -07001637
Eric W. Biederman881d9662007-09-17 11:56:21 -07001638 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001639 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1640 dev);
1641 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001642 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001643 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001644 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001645 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001646
RongQing.Li8f891482011-11-30 23:43:07 -05001647outroll:
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001648 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001649 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001651EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653/**
tcharding722c9a02017-02-09 17:56:04 +11001654 * unregister_netdevice_notifier - unregister a network notifier block
1655 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 *
tcharding722c9a02017-02-09 17:56:04 +11001657 * Unregister a notifier previously registered by
1658 * register_netdevice_notifier(). The notifier is unlinked into the
1659 * kernel structures and may then be reused. A negative errno code
1660 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001661 *
tcharding722c9a02017-02-09 17:56:04 +11001662 * After unregistering unregister and down device events are synthesized
1663 * for all devices on the device list to the removed notifier to remove
1664 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
1666
1667int unregister_netdevice_notifier(struct notifier_block *nb)
1668{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001669 struct net_device *dev;
1670 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001671 int err;
1672
1673 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001674 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001675 if (err)
1676 goto unlock;
1677
1678 for_each_net(net) {
1679 for_each_netdev(net, dev) {
1680 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001681 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1682 dev);
1683 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001684 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001685 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001686 }
1687 }
1688unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001689 rtnl_unlock();
1690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001692EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001695 * call_netdevice_notifiers_info - call all network notifier blocks
1696 * @val: value passed unmodified to notifier function
1697 * @dev: net_device pointer passed unmodified to notifier function
1698 * @info: notifier information data
1699 *
1700 * Call all network notifier blocks. Parameters and return value
1701 * are as for raw_notifier_call_chain().
1702 */
1703
stephen hemminger1d143d92013-12-29 14:01:29 -08001704static int call_netdevice_notifiers_info(unsigned long val,
stephen hemminger1d143d92013-12-29 14:01:29 -08001705 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001706{
1707 ASSERT_RTNL();
Jiri Pirko351638e2013-05-28 01:30:21 +00001708 return raw_notifier_call_chain(&netdev_chain, val, info);
1709}
Jiri Pirko351638e2013-05-28 01:30:21 +00001710
1711/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * call_netdevice_notifiers - call all network notifier blocks
1713 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001714 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 *
1716 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001717 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 */
1719
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001720int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
David Ahern51d0c0472017-10-04 17:48:45 -07001722 struct netdev_notifier_info info = {
1723 .dev = dev,
1724 };
Jiri Pirko351638e2013-05-28 01:30:21 +00001725
David Ahern51d0c0472017-10-04 17:48:45 -07001726 return call_netdevice_notifiers_info(val, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001728EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Pablo Neira1cf519002015-05-13 18:19:37 +02001730#ifdef CONFIG_NET_INGRESS
Daniel Borkmann45771392015-04-10 23:07:54 +02001731static struct static_key ingress_needed __read_mostly;
1732
1733void net_inc_ingress_queue(void)
1734{
1735 static_key_slow_inc(&ingress_needed);
1736}
1737EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1738
1739void net_dec_ingress_queue(void)
1740{
1741 static_key_slow_dec(&ingress_needed);
1742}
1743EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1744#endif
1745
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001746#ifdef CONFIG_NET_EGRESS
1747static struct static_key egress_needed __read_mostly;
1748
1749void net_inc_egress_queue(void)
1750{
1751 static_key_slow_inc(&egress_needed);
1752}
1753EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1754
1755void net_dec_egress_queue(void)
1756{
1757 static_key_slow_dec(&egress_needed);
1758}
1759EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1760#endif
1761
Ingo Molnarc5905af2012-02-24 08:31:31 +01001762static struct static_key netstamp_needed __read_mostly;
Eric Dumazetb90e5792011-11-28 11:16:50 +00001763#ifdef HAVE_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00001764static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08001765static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001766static void netstamp_clear(struct work_struct *work)
1767{
1768 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08001769 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001770
Eric Dumazet13baa002017-03-01 14:28:39 -08001771 wanted = atomic_add_return(deferred, &netstamp_wanted);
1772 if (wanted > 0)
1773 static_key_enable(&netstamp_needed);
1774 else
1775 static_key_disable(&netstamp_needed);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001776}
1777static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780void net_enable_timestamp(void)
1781{
Eric Dumazet13baa002017-03-01 14:28:39 -08001782#ifdef HAVE_JUMP_LABEL
1783 int wanted;
1784
1785 while (1) {
1786 wanted = atomic_read(&netstamp_wanted);
1787 if (wanted <= 0)
1788 break;
1789 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1790 return;
1791 }
1792 atomic_inc(&netstamp_needed_deferred);
1793 schedule_work(&netstamp_work);
1794#else
Ingo Molnarc5905af2012-02-24 08:31:31 +01001795 static_key_slow_inc(&netstamp_needed);
Eric Dumazet13baa002017-03-01 14:28:39 -08001796#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001798EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800void net_disable_timestamp(void)
1801{
Eric Dumazetb90e5792011-11-28 11:16:50 +00001802#ifdef HAVE_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08001803 int wanted;
1804
1805 while (1) {
1806 wanted = atomic_read(&netstamp_wanted);
1807 if (wanted <= 1)
1808 break;
1809 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1810 return;
1811 }
1812 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001813 schedule_work(&netstamp_work);
1814#else
Ingo Molnarc5905af2012-02-24 08:31:31 +01001815 static_key_slow_dec(&netstamp_needed);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001818EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Eric Dumazet3b098e22010-05-15 23:57:10 -07001820static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Thomas Gleixner2456e852016-12-25 11:38:40 +01001822 skb->tstamp = 0;
Ingo Molnarc5905af2012-02-24 08:31:31 +01001823 if (static_key_false(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001824 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Eric Dumazet588f0332011-11-15 04:12:55 +00001827#define net_timestamp_check(COND, SKB) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001828 if (static_key_false(&netstamp_needed)) { \
Thomas Gleixner2456e852016-12-25 11:38:40 +01001829 if ((COND) && !(SKB)->tstamp) \
Eric Dumazet588f0332011-11-15 04:12:55 +00001830 __net_timestamp(SKB); \
1831 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07001832
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02001833bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001834{
1835 unsigned int len;
1836
1837 if (!(dev->flags & IFF_UP))
1838 return false;
1839
1840 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1841 if (skb->len <= len)
1842 return true;
1843
1844 /* if TSO is enabled, we don't care about the length as the packet
1845 * could be forwarded without being segmented before
1846 */
1847 if (skb_is_gso(skb))
1848 return true;
1849
1850 return false;
1851}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04001852EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001853
Herbert Xua0265d22014-04-17 13:45:03 +08001854int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1855{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001856 int ret = ____dev_forward_skb(dev, skb);
1857
1858 if (likely(!ret)) {
1859 skb->protocol = eth_type_trans(skb, dev);
1860 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08001861 }
1862
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001863 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08001864}
1865EXPORT_SYMBOL_GPL(__dev_forward_skb);
1866
Arnd Bergmann44540962009-11-26 06:07:08 +00001867/**
1868 * dev_forward_skb - loopback an skb to another netif
1869 *
1870 * @dev: destination network device
1871 * @skb: buffer to forward
1872 *
1873 * return values:
1874 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001875 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001876 *
1877 * dev_forward_skb can be used for injecting an skb from the
1878 * start_xmit function of one device into the receive queue
1879 * of another device.
1880 *
1881 * The receiving device may be in another namespace, so
1882 * we have to clear all information in the skb that could
1883 * impact namespace isolation.
1884 */
1885int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1886{
Herbert Xua0265d22014-04-17 13:45:03 +08001887 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001888}
1889EXPORT_SYMBOL_GPL(dev_forward_skb);
1890
Changli Gao71d9dec2010-12-15 19:57:25 +00001891static inline int deliver_skb(struct sk_buff *skb,
1892 struct packet_type *pt_prev,
1893 struct net_device *orig_dev)
1894{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001895 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00001896 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03001897 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00001898 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1899}
1900
Salam Noureddine7866a622015-01-27 11:35:48 -08001901static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1902 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001903 struct net_device *orig_dev,
1904 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08001905 struct list_head *ptype_list)
1906{
1907 struct packet_type *ptype, *pt_prev = *pt;
1908
1909 list_for_each_entry_rcu(ptype, ptype_list, list) {
1910 if (ptype->type != type)
1911 continue;
1912 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001913 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08001914 pt_prev = ptype;
1915 }
1916 *pt = pt_prev;
1917}
1918
Eric Leblondc0de08d2012-08-16 22:02:58 +00001919static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1920{
Eric Leblonda3d744e2012-11-06 02:10:10 +00001921 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001922 return false;
1923
1924 if (ptype->id_match)
1925 return ptype->id_match(ptype, skb->sk);
1926 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1927 return true;
1928
1929 return false;
1930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
1933 * Support routine. Sends outgoing frames to any network
1934 * taps currently in use.
1935 */
1936
David Ahern74b20582016-05-10 11:19:50 -07001937void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00001940 struct sk_buff *skb2 = NULL;
1941 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08001942 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08001945again:
1946 list_for_each_entry_rcu(ptype, ptype_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* Never send packets back to the socket
1948 * they originated from - MvS (miquels@drinkel.ow.org)
1949 */
Salam Noureddine7866a622015-01-27 11:35:48 -08001950 if (skb_loop_sk(ptype, skb))
1951 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00001952
Salam Noureddine7866a622015-01-27 11:35:48 -08001953 if (pt_prev) {
1954 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00001955 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08001956 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Salam Noureddine7866a622015-01-27 11:35:48 -08001958
1959 /* need to clone skb, done only once */
1960 skb2 = skb_clone(skb, GFP_ATOMIC);
1961 if (!skb2)
1962 goto out_unlock;
1963
1964 net_timestamp_set(skb2);
1965
1966 /* skb->nh should be correctly
1967 * set by sender, so that the second statement is
1968 * just protection against buggy protocols.
1969 */
1970 skb_reset_mac_header(skb2);
1971
1972 if (skb_network_header(skb2) < skb2->data ||
1973 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
1974 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1975 ntohs(skb2->protocol),
1976 dev->name);
1977 skb_reset_network_header(skb2);
1978 }
1979
1980 skb2->transport_header = skb2->network_header;
1981 skb2->pkt_type = PACKET_OUTGOING;
1982 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Salam Noureddine7866a622015-01-27 11:35:48 -08001984
1985 if (ptype_list == &ptype_all) {
1986 ptype_list = &dev->ptype_all;
1987 goto again;
1988 }
1989out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04001990 if (pt_prev) {
1991 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
1992 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1993 else
1994 kfree_skb(skb2);
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 rcu_read_unlock();
1997}
David Ahern74b20582016-05-10 11:19:50 -07001998EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Ben Hutchings2c530402012-07-10 10:55:09 +00002000/**
2001 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00002002 * @dev: Network device
2003 * @txq: number of queues available
2004 *
2005 * If real_num_tx_queues is changed the tc mappings may no longer be
2006 * valid. To resolve this verify the tc mapping remains valid and if
2007 * not NULL the mapping. With no priorities mapping to this
2008 * offset/count pair it will no longer be used. In the worst case TC0
2009 * is invalid nothing can be done so disable priority mappings. If is
2010 * expected that drivers will fix this mapping if they can before
2011 * calling netif_set_real_num_tx_queues.
2012 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002013static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002014{
2015 int i;
2016 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2017
2018 /* If TC0 is invalidated disable TC mapping */
2019 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002020 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002021 dev->num_tc = 0;
2022 return;
2023 }
2024
2025 /* Invalidated prio to tc mappings set to TC0 */
2026 for (i = 1; i < TC_BITMASK + 1; i++) {
2027 int q = netdev_get_prio_tc_map(dev, i);
2028
2029 tc = &dev->tc_to_txq[q];
2030 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002031 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2032 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002033 netdev_set_prio_tc_map(dev, i, 0);
2034 }
2035 }
2036}
2037
Alexander Duyck8d059b02016-10-28 11:43:49 -04002038int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2039{
2040 if (dev->num_tc) {
2041 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2042 int i;
2043
2044 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2045 if ((txq - tc->offset) < tc->count)
2046 return i;
2047 }
2048
2049 return -1;
2050 }
2051
2052 return 0;
2053}
Henrik Austad8a5f2162017-10-17 12:10:10 +02002054EXPORT_SYMBOL(netdev_txq_to_tc);
Alexander Duyck8d059b02016-10-28 11:43:49 -04002055
Alexander Duyck537c00d2013-01-10 08:57:02 +00002056#ifdef CONFIG_XPS
2057static DEFINE_MUTEX(xps_map_mutex);
2058#define xmap_dereference(P) \
2059 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2060
Alexander Duyck6234f872016-10-28 11:46:49 -04002061static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2062 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002063{
2064 struct xps_map *map = NULL;
2065 int pos;
2066
2067 if (dev_maps)
Alexander Duyck6234f872016-10-28 11:46:49 -04002068 map = xmap_dereference(dev_maps->cpu_map[tci]);
2069 if (!map)
2070 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002071
Alexander Duyck6234f872016-10-28 11:46:49 -04002072 for (pos = map->len; pos--;) {
2073 if (map->queues[pos] != index)
2074 continue;
2075
2076 if (map->len > 1) {
2077 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002078 break;
2079 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002080
2081 RCU_INIT_POINTER(dev_maps->cpu_map[tci], NULL);
2082 kfree_rcu(map, rcu);
2083 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002084 }
2085
Alexander Duyck6234f872016-10-28 11:46:49 -04002086 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002087}
2088
Alexander Duyck6234f872016-10-28 11:46:49 -04002089static bool remove_xps_queue_cpu(struct net_device *dev,
2090 struct xps_dev_maps *dev_maps,
2091 int cpu, u16 offset, u16 count)
2092{
Alexander Duyck184c4492016-10-28 11:50:13 -04002093 int num_tc = dev->num_tc ? : 1;
2094 bool active = false;
2095 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002096
Alexander Duyck184c4492016-10-28 11:50:13 -04002097 for (tci = cpu * num_tc; num_tc--; tci++) {
2098 int i, j;
2099
2100 for (i = count, j = offset; i--; j++) {
2101 if (!remove_xps_queue(dev_maps, cpu, j))
2102 break;
2103 }
2104
2105 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002106 }
2107
Alexander Duyck184c4492016-10-28 11:50:13 -04002108 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002109}
2110
2111static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2112 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002113{
2114 struct xps_dev_maps *dev_maps;
Alexander Duyck024e9672013-01-10 08:57:46 +00002115 int cpu, i;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002116 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002117
2118 mutex_lock(&xps_map_mutex);
2119 dev_maps = xmap_dereference(dev->xps_maps);
2120
2121 if (!dev_maps)
2122 goto out_no_maps;
2123
Alexander Duyck6234f872016-10-28 11:46:49 -04002124 for_each_possible_cpu(cpu)
2125 active |= remove_xps_queue_cpu(dev, dev_maps, cpu,
2126 offset, count);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002127
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002128 if (!active) {
Alexander Duyck537c00d2013-01-10 08:57:02 +00002129 RCU_INIT_POINTER(dev->xps_maps, NULL);
2130 kfree_rcu(dev_maps, rcu);
2131 }
2132
Alexander Duyck6234f872016-10-28 11:46:49 -04002133 for (i = offset + (count - 1); count--; i--)
Alexander Duyck024e9672013-01-10 08:57:46 +00002134 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
2135 NUMA_NO_NODE);
2136
Alexander Duyck537c00d2013-01-10 08:57:02 +00002137out_no_maps:
2138 mutex_unlock(&xps_map_mutex);
2139}
2140
Alexander Duyck6234f872016-10-28 11:46:49 -04002141static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2142{
2143 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2144}
2145
Alexander Duyck01c5f862013-01-10 08:57:35 +00002146static struct xps_map *expand_xps_map(struct xps_map *map,
2147 int cpu, u16 index)
2148{
2149 struct xps_map *new_map;
2150 int alloc_len = XPS_MIN_MAP_ALLOC;
2151 int i, pos;
2152
2153 for (pos = 0; map && pos < map->len; pos++) {
2154 if (map->queues[pos] != index)
2155 continue;
2156 return map;
2157 }
2158
2159 /* Need to add queue to this CPU's existing map */
2160 if (map) {
2161 if (pos < map->alloc_len)
2162 return map;
2163
2164 alloc_len = map->alloc_len * 2;
2165 }
2166
2167 /* Need to allocate new map to store queue on this CPU's map */
2168 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2169 cpu_to_node(cpu));
2170 if (!new_map)
2171 return NULL;
2172
2173 for (i = 0; i < pos; i++)
2174 new_map->queues[i] = map->queues[i];
2175 new_map->alloc_len = alloc_len;
2176 new_map->len = pos;
2177
2178 return new_map;
2179}
2180
Michael S. Tsirkin35735402013-10-02 09:14:06 +03002181int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2182 u16 index)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002183{
Alexander Duyck01c5f862013-01-10 08:57:35 +00002184 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Alexander Duyck184c4492016-10-28 11:50:13 -04002185 int i, cpu, tci, numa_node_id = -2;
2186 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002187 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002188 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002189
Alexander Duyck184c4492016-10-28 11:50:13 -04002190 if (dev->num_tc) {
2191 num_tc = dev->num_tc;
2192 tc = netdev_txq_to_tc(dev, index);
2193 if (tc < 0)
2194 return -EINVAL;
2195 }
2196
2197 maps_sz = XPS_DEV_MAPS_SIZE(num_tc);
2198 if (maps_sz < L1_CACHE_BYTES)
2199 maps_sz = L1_CACHE_BYTES;
2200
Alexander Duyck537c00d2013-01-10 08:57:02 +00002201 mutex_lock(&xps_map_mutex);
2202
2203 dev_maps = xmap_dereference(dev->xps_maps);
2204
Alexander Duyck01c5f862013-01-10 08:57:35 +00002205 /* allocate memory for queue storage */
Alexander Duyck184c4492016-10-28 11:50:13 -04002206 for_each_cpu_and(cpu, cpu_online_mask, mask) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002207 if (!new_dev_maps)
2208 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002209 if (!new_dev_maps) {
2210 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002211 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002212 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002213
Alexander Duyck184c4492016-10-28 11:50:13 -04002214 tci = cpu * num_tc + tc;
2215 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002216 NULL;
2217
2218 map = expand_xps_map(map, cpu, index);
2219 if (!map)
2220 goto error;
2221
Alexander Duyck184c4492016-10-28 11:50:13 -04002222 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002223 }
2224
2225 if (!new_dev_maps)
2226 goto out_no_new_maps;
2227
2228 for_each_possible_cpu(cpu) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002229 /* copy maps belonging to foreign traffic classes */
2230 for (i = tc, tci = cpu * num_tc; dev_maps && i--; tci++) {
2231 /* fill in the new device map from the old device map */
2232 map = xmap_dereference(dev_maps->cpu_map[tci]);
2233 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2234 }
2235
2236 /* We need to explicitly update tci as prevous loop
2237 * could break out early if dev_maps is NULL.
2238 */
2239 tci = cpu * num_tc + tc;
2240
Alexander Duyck01c5f862013-01-10 08:57:35 +00002241 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
2242 /* add queue to CPU maps */
2243 int pos = 0;
2244
Alexander Duyck184c4492016-10-28 11:50:13 -04002245 map = xmap_dereference(new_dev_maps->cpu_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002246 while ((pos < map->len) && (map->queues[pos] != index))
2247 pos++;
2248
2249 if (pos == map->len)
2250 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002251#ifdef CONFIG_NUMA
Alexander Duyck537c00d2013-01-10 08:57:02 +00002252 if (numa_node_id == -2)
2253 numa_node_id = cpu_to_node(cpu);
2254 else if (numa_node_id != cpu_to_node(cpu))
2255 numa_node_id = -1;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002256#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002257 } else if (dev_maps) {
2258 /* fill in the new device map from the old device map */
Alexander Duyck184c4492016-10-28 11:50:13 -04002259 map = xmap_dereference(dev_maps->cpu_map[tci]);
2260 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002261 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002262
Alexander Duyck184c4492016-10-28 11:50:13 -04002263 /* copy maps belonging to foreign traffic classes */
2264 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2265 /* fill in the new device map from the old device map */
2266 map = xmap_dereference(dev_maps->cpu_map[tci]);
2267 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2268 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002269 }
2270
Alexander Duyck01c5f862013-01-10 08:57:35 +00002271 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2272
Alexander Duyck537c00d2013-01-10 08:57:02 +00002273 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002274 if (!dev_maps)
2275 goto out_no_old_maps;
2276
2277 for_each_possible_cpu(cpu) {
2278 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2279 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2280 map = xmap_dereference(dev_maps->cpu_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002281 if (map && map != new_map)
2282 kfree_rcu(map, rcu);
2283 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002284 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002285
Alexander Duyck184c4492016-10-28 11:50:13 -04002286 kfree_rcu(dev_maps, rcu);
2287
2288out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002289 dev_maps = new_dev_maps;
2290 active = true;
2291
2292out_no_new_maps:
2293 /* update Tx queue numa node */
Alexander Duyck537c00d2013-01-10 08:57:02 +00002294 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2295 (numa_node_id >= 0) ? numa_node_id :
2296 NUMA_NO_NODE);
2297
Alexander Duyck01c5f862013-01-10 08:57:35 +00002298 if (!dev_maps)
2299 goto out_no_maps;
2300
2301 /* removes queue from unused CPUs */
2302 for_each_possible_cpu(cpu) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002303 for (i = tc, tci = cpu * num_tc; i--; tci++)
2304 active |= remove_xps_queue(dev_maps, tci, index);
2305 if (!cpumask_test_cpu(cpu, mask) || !cpu_online(cpu))
2306 active |= remove_xps_queue(dev_maps, tci, index);
2307 for (i = num_tc - tc, tci++; --i; tci++)
2308 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002309 }
2310
2311 /* free map if not active */
2312 if (!active) {
2313 RCU_INIT_POINTER(dev->xps_maps, NULL);
2314 kfree_rcu(dev_maps, rcu);
2315 }
2316
2317out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002318 mutex_unlock(&xps_map_mutex);
2319
2320 return 0;
2321error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002322 /* remove any maps that we added */
2323 for_each_possible_cpu(cpu) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002324 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2325 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2326 map = dev_maps ?
2327 xmap_dereference(dev_maps->cpu_map[tci]) :
2328 NULL;
2329 if (new_map && new_map != map)
2330 kfree(new_map);
2331 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002332 }
2333
Alexander Duyck537c00d2013-01-10 08:57:02 +00002334 mutex_unlock(&xps_map_mutex);
2335
Alexander Duyck537c00d2013-01-10 08:57:02 +00002336 kfree(new_dev_maps);
2337 return -ENOMEM;
2338}
2339EXPORT_SYMBOL(netif_set_xps_queue);
2340
2341#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002342void netdev_reset_tc(struct net_device *dev)
2343{
Alexander Duyck6234f872016-10-28 11:46:49 -04002344#ifdef CONFIG_XPS
2345 netif_reset_xps_queues_gt(dev, 0);
2346#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002347 dev->num_tc = 0;
2348 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2349 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2350}
2351EXPORT_SYMBOL(netdev_reset_tc);
2352
2353int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2354{
2355 if (tc >= dev->num_tc)
2356 return -EINVAL;
2357
Alexander Duyck6234f872016-10-28 11:46:49 -04002358#ifdef CONFIG_XPS
2359 netif_reset_xps_queues(dev, offset, count);
2360#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002361 dev->tc_to_txq[tc].count = count;
2362 dev->tc_to_txq[tc].offset = offset;
2363 return 0;
2364}
2365EXPORT_SYMBOL(netdev_set_tc_queue);
2366
2367int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2368{
2369 if (num_tc > TC_MAX_QUEUE)
2370 return -EINVAL;
2371
Alexander Duyck6234f872016-10-28 11:46:49 -04002372#ifdef CONFIG_XPS
2373 netif_reset_xps_queues_gt(dev, 0);
2374#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002375 dev->num_tc = num_tc;
2376 return 0;
2377}
2378EXPORT_SYMBOL(netdev_set_num_tc);
2379
John Fastabendf0796d52010-07-01 13:21:57 +00002380/*
2381 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2382 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2383 */
Tom Herberte6484932010-10-18 18:04:39 +00002384int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002385{
Tom Herbert1d24eb42010-11-21 13:17:27 +00002386 int rc;
2387
Tom Herberte6484932010-10-18 18:04:39 +00002388 if (txq < 1 || txq > dev->num_tx_queues)
2389 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002390
Ben Hutchings5c565802011-02-15 19:39:21 +00002391 if (dev->reg_state == NETREG_REGISTERED ||
2392 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002393 ASSERT_RTNL();
2394
Tom Herbert1d24eb42010-11-21 13:17:27 +00002395 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2396 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002397 if (rc)
2398 return rc;
2399
John Fastabend4f57c082011-01-17 08:06:04 +00002400 if (dev->num_tc)
2401 netif_setup_tc(dev, txq);
2402
Alexander Duyck024e9672013-01-10 08:57:46 +00002403 if (txq < dev->real_num_tx_queues) {
Tom Herberte6484932010-10-18 18:04:39 +00002404 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002405#ifdef CONFIG_XPS
2406 netif_reset_xps_queues_gt(dev, txq);
2407#endif
2408 }
John Fastabendf0796d52010-07-01 13:21:57 +00002409 }
Tom Herberte6484932010-10-18 18:04:39 +00002410
2411 dev->real_num_tx_queues = txq;
2412 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002413}
2414EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002415
Michael Daltona953be52014-01-16 22:23:28 -08002416#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002417/**
2418 * netif_set_real_num_rx_queues - set actual number of RX queues used
2419 * @dev: Network device
2420 * @rxq: Actual number of RX queues
2421 *
2422 * This must be called either with the rtnl_lock held or before
2423 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002424 * negative error code. If called before registration, it always
2425 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002426 */
2427int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2428{
2429 int rc;
2430
Tom Herbertbd25fa72010-10-18 18:00:16 +00002431 if (rxq < 1 || rxq > dev->num_rx_queues)
2432 return -EINVAL;
2433
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002434 if (dev->reg_state == NETREG_REGISTERED) {
2435 ASSERT_RTNL();
2436
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002437 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2438 rxq);
2439 if (rc)
2440 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002441 }
2442
2443 dev->real_num_rx_queues = rxq;
2444 return 0;
2445}
2446EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2447#endif
2448
Ben Hutchings2c530402012-07-10 10:55:09 +00002449/**
2450 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002451 *
2452 * This routine should set an upper limit on the number of RSS queues
2453 * used by default by multiqueue devices.
2454 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002455int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002456{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302457 return is_kdump_kernel() ?
2458 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002459}
2460EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2461
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002462static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002463{
2464 struct softnet_data *sd;
2465 unsigned long flags;
2466
2467 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002468 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002469 q->next_sched = NULL;
2470 *sd->output_queue_tailp = q;
2471 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002472 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2473 local_irq_restore(flags);
2474}
2475
David S. Miller37437bb2008-07-16 02:15:04 -07002476void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002477{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002478 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2479 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002480}
2481EXPORT_SYMBOL(__netif_schedule);
2482
Eric Dumazete6247022013-12-05 04:45:08 -08002483struct dev_kfree_skb_cb {
2484 enum skb_free_reason reason;
2485};
2486
2487static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002488{
Eric Dumazete6247022013-12-05 04:45:08 -08002489 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002490}
Denis Vlasenko56079432006-03-29 15:57:29 -08002491
John Fastabend46e5da40a2014-09-12 20:04:52 -07002492void netif_schedule_queue(struct netdev_queue *txq)
2493{
2494 rcu_read_lock();
2495 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2496 struct Qdisc *q = rcu_dereference(txq->qdisc);
2497
2498 __netif_schedule(q);
2499 }
2500 rcu_read_unlock();
2501}
2502EXPORT_SYMBOL(netif_schedule_queue);
2503
John Fastabend46e5da40a2014-09-12 20:04:52 -07002504void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2505{
2506 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2507 struct Qdisc *q;
2508
2509 rcu_read_lock();
2510 q = rcu_dereference(dev_queue->qdisc);
2511 __netif_schedule(q);
2512 rcu_read_unlock();
2513 }
2514}
2515EXPORT_SYMBOL(netif_tx_wake_queue);
2516
Eric Dumazete6247022013-12-05 04:45:08 -08002517void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2518{
2519 unsigned long flags;
2520
Myungho Jung98998862017-04-25 11:58:15 -07002521 if (unlikely(!skb))
2522 return;
2523
Reshetova, Elena63354792017-06-30 13:07:58 +03002524 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08002525 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03002526 refcount_set(&skb->users, 0);
2527 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08002528 return;
2529 }
2530 get_kfree_skb_cb(skb)->reason = reason;
2531 local_irq_save(flags);
2532 skb->next = __this_cpu_read(softnet_data.completion_queue);
2533 __this_cpu_write(softnet_data.completion_queue, skb);
2534 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2535 local_irq_restore(flags);
2536}
2537EXPORT_SYMBOL(__dev_kfree_skb_irq);
2538
2539void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08002540{
2541 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08002542 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08002543 else
2544 dev_kfree_skb(skb);
2545}
Eric Dumazete6247022013-12-05 04:45:08 -08002546EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08002547
2548
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002549/**
2550 * netif_device_detach - mark device as removed
2551 * @dev: network device
2552 *
2553 * Mark device as removed from system and therefore no longer available.
2554 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002555void netif_device_detach(struct net_device *dev)
2556{
2557 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2558 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002559 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002560 }
2561}
2562EXPORT_SYMBOL(netif_device_detach);
2563
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002564/**
2565 * netif_device_attach - mark device as attached
2566 * @dev: network device
2567 *
2568 * Mark device as attached from system and restart if needed.
2569 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002570void netif_device_attach(struct net_device *dev)
2571{
2572 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2573 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002574 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002575 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002576 }
2577}
2578EXPORT_SYMBOL(netif_device_attach);
2579
Jiri Pirko5605c762015-05-12 14:56:12 +02002580/*
2581 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2582 * to be used as a distribution range.
2583 */
2584u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
2585 unsigned int num_tx_queues)
2586{
2587 u32 hash;
2588 u16 qoffset = 0;
2589 u16 qcount = num_tx_queues;
2590
2591 if (skb_rx_queue_recorded(skb)) {
2592 hash = skb_get_rx_queue(skb);
2593 while (unlikely(hash >= num_tx_queues))
2594 hash -= num_tx_queues;
2595 return hash;
2596 }
2597
2598 if (dev->num_tc) {
2599 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
tchardingf4563a72017-02-09 17:56:07 +11002600
Jiri Pirko5605c762015-05-12 14:56:12 +02002601 qoffset = dev->tc_to_txq[tc].offset;
2602 qcount = dev->tc_to_txq[tc].count;
2603 }
2604
2605 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2606}
2607EXPORT_SYMBOL(__skb_tx_hash);
2608
Ben Hutchings36c92472012-01-17 07:57:56 +00002609static void skb_warn_bad_offload(const struct sk_buff *skb)
2610{
Wei Tang84d15ae2016-06-16 21:17:49 +08002611 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00002612 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01002613 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00002614
Ben Greearc846ad92013-04-19 10:45:52 +00002615 if (!net_ratelimit())
2616 return;
2617
Bjørn Mork88ad4172015-11-16 19:16:40 +01002618 if (dev) {
2619 if (dev->dev.parent)
2620 name = dev_driver_string(dev->dev.parent);
2621 else
2622 name = netdev_name(dev);
2623 }
Ben Hutchings36c92472012-01-17 07:57:56 +00002624 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2625 "gso_type=%d ip_summed=%d\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01002626 name, dev ? &dev->features : &null_features,
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002627 skb->sk ? &skb->sk->sk_route_caps : &null_features,
Ben Hutchings36c92472012-01-17 07:57:56 +00002628 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2629 skb_shinfo(skb)->gso_type, skb->ip_summed);
2630}
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632/*
2633 * Invalidate hardware checksum when packet is to be mangled, and
2634 * complete checksum manually on outgoing path.
2635 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002636int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Al Virod3bc23e2006-11-14 21:24:49 -08002638 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07002639 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Patrick McHardy84fa7932006-08-29 16:44:56 -07002641 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07002642 goto out_set_summed;
2643
2644 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00002645 skb_warn_bad_offload(skb);
2646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
Eric Dumazetcef401d2013-01-25 20:34:37 +00002649 /* Before computing a checksum, we should make sure no frag could
2650 * be modified by an external entity : checksum could be wrong.
2651 */
2652 if (skb_has_shared_frag(skb)) {
2653 ret = __skb_linearize(skb);
2654 if (ret)
2655 goto out;
2656 }
2657
Michał Mirosław55508d62010-12-14 15:24:08 +00002658 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07002659 BUG_ON(offset >= skb_headlen(skb));
2660 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2661
2662 offset += skb->csum_offset;
2663 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2664
2665 if (skb_cloned(skb) &&
2666 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2668 if (ret)
2669 goto out;
2670 }
2671
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07002672 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07002673out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002675out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return ret;
2677}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002678EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Davide Carattib72b5bf2017-05-18 15:44:38 +02002680int skb_crc32c_csum_help(struct sk_buff *skb)
2681{
2682 __le32 crc32c_csum;
2683 int ret = 0, offset, start;
2684
2685 if (skb->ip_summed != CHECKSUM_PARTIAL)
2686 goto out;
2687
2688 if (unlikely(skb_is_gso(skb)))
2689 goto out;
2690
2691 /* Before computing a checksum, we should make sure no frag could
2692 * be modified by an external entity : checksum could be wrong.
2693 */
2694 if (unlikely(skb_has_shared_frag(skb))) {
2695 ret = __skb_linearize(skb);
2696 if (ret)
2697 goto out;
2698 }
2699 start = skb_checksum_start_offset(skb);
2700 offset = start + offsetof(struct sctphdr, checksum);
2701 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
2702 ret = -EINVAL;
2703 goto out;
2704 }
2705 if (skb_cloned(skb) &&
2706 !skb_clone_writable(skb, offset + sizeof(__le32))) {
2707 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2708 if (ret)
2709 goto out;
2710 }
2711 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
2712 skb->len - start, ~(__u32)0,
2713 crc32c_csum_stub));
2714 *(__le32 *)(skb->data + offset) = crc32c_csum;
2715 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02002716 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02002717out:
2718 return ret;
2719}
2720
Vlad Yasevich53d64712014-03-27 17:26:18 -04002721__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002722{
2723 __be16 type = skb->protocol;
2724
Pravin B Shelar19acc322013-05-07 20:41:07 +00002725 /* Tunnel gso handlers can set protocol to ethernet. */
2726 if (type == htons(ETH_P_TEB)) {
2727 struct ethhdr *eth;
2728
2729 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2730 return 0;
2731
2732 eth = (struct ethhdr *)skb_mac_header(skb);
2733 type = eth->h_proto;
2734 }
2735
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09002736 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002737}
2738
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002739/**
2740 * skb_mac_gso_segment - mac layer segmentation handler.
2741 * @skb: buffer to segment
2742 * @features: features for the output path (see dev->features)
2743 */
2744struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2745 netdev_features_t features)
2746{
2747 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2748 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002749 int vlan_depth = skb->mac_len;
2750 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002751
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002752 if (unlikely(!type))
2753 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002754
Vlad Yasevich53d64712014-03-27 17:26:18 -04002755 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002756
2757 rcu_read_lock();
2758 list_for_each_entry_rcu(ptype, &offload_base, list) {
2759 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002760 segs = ptype->callbacks.gso_segment(skb, features);
2761 break;
2762 }
2763 }
2764 rcu_read_unlock();
2765
2766 __skb_push(skb, skb->data - skb_mac_header(skb));
2767
2768 return segs;
2769}
2770EXPORT_SYMBOL(skb_mac_gso_segment);
2771
2772
Cong Wang12b00042013-02-05 16:36:38 +00002773/* openvswitch calls this on rx path, so we need a different check.
2774 */
2775static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
2776{
2777 if (tx_path)
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05002778 return skb->ip_summed != CHECKSUM_PARTIAL &&
2779 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08002780
2781 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00002782}
2783
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002784/**
Cong Wang12b00042013-02-05 16:36:38 +00002785 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002786 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002787 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00002788 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002789 *
2790 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07002791 *
2792 * It may return NULL if the skb requires no segmentation. This is
2793 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03002794 *
2795 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002796 */
Cong Wang12b00042013-02-05 16:36:38 +00002797struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2798 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002799{
Eric Dumazetb2504a52017-01-31 10:20:32 -08002800 struct sk_buff *segs;
2801
Cong Wang12b00042013-02-05 16:36:38 +00002802 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002803 int err;
2804
Eric Dumazetb2504a52017-01-31 10:20:32 -08002805 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02002806 err = skb_cow_head(skb, 0);
2807 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07002808 return ERR_PTR(err);
2809 }
2810
Alexander Duyck802ab552016-04-10 21:45:03 -04002811 /* Only report GSO partial support if it will enable us to
2812 * support segmentation on this frame without needing additional
2813 * work.
2814 */
2815 if (features & NETIF_F_GSO_PARTIAL) {
2816 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
2817 struct net_device *dev = skb->dev;
2818
2819 partial_features |= dev->features & dev->gso_partial_features;
2820 if (!skb_gso_ok(skb, features | partial_features))
2821 features &= ~NETIF_F_GSO_PARTIAL;
2822 }
2823
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03002824 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
2825 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
2826
Pravin B Shelar68c33162013-02-14 14:02:41 +00002827 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07002828 SKB_GSO_CB(skb)->encap_level = 0;
2829
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002830 skb_reset_mac_header(skb);
2831 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002832
Eric Dumazetb2504a52017-01-31 10:20:32 -08002833 segs = skb_mac_gso_segment(skb, features);
2834
Willem de Bruijn8d74e9f2017-12-12 11:39:04 -05002835 if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazetb2504a52017-01-31 10:20:32 -08002836 skb_warn_bad_offload(skb);
2837
2838 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002839}
Cong Wang12b00042013-02-05 16:36:38 +00002840EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002841
Herbert Xufb286bb2005-11-10 13:01:24 -08002842/* Take action when hardware reception checksum errors are detected. */
2843#ifdef CONFIG_BUG
2844void netdev_rx_csum_fault(struct net_device *dev)
2845{
2846 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002847 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08002848 dump_stack();
2849 }
2850}
2851EXPORT_SYMBOL(netdev_rx_csum_fault);
2852#endif
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854/* Actually, we should eliminate this check as soon as we know, that:
2855 * 1. IOMMU is present and allows to map all the memory.
2856 * 2. No high memory really exists on this machine.
2857 */
2858
Florian Westphalc1e756b2014-05-05 15:00:44 +02002859static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Herbert Xu3d3a8532006-06-27 13:33:10 -07002861#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 int i;
tchardingf4563a72017-02-09 17:56:07 +11002863
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002864 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002865 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2866 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11002867
Ian Campbellea2ab692011-08-22 23:44:58 +00002868 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002869 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00002870 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002873 if (PCI_DMA_BUS_IS_PHYS) {
2874 struct device *pdev = dev->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Eric Dumazet9092c652010-04-02 13:34:49 -07002876 if (!pdev)
2877 return 0;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002878 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002879 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2880 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
tchardingf4563a72017-02-09 17:56:07 +11002881
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002882 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2883 return 1;
2884 }
2885 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07002886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return 0;
2888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Simon Horman3b392dd2014-06-04 08:53:17 +09002890/* If MPLS offload request, verify we are testing hardware MPLS features
2891 * instead of standard features for the netdev.
2892 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08002893#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09002894static netdev_features_t net_mpls_features(struct sk_buff *skb,
2895 netdev_features_t features,
2896 __be16 type)
2897{
Simon Horman25cd9ba2014-10-06 05:05:13 -07002898 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09002899 features &= skb->dev->mpls_features;
2900
2901 return features;
2902}
2903#else
2904static netdev_features_t net_mpls_features(struct sk_buff *skb,
2905 netdev_features_t features,
2906 __be16 type)
2907{
2908 return features;
2909}
2910#endif
2911
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002912static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02002913 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00002914{
Vlad Yasevich53d64712014-03-27 17:26:18 -04002915 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09002916 __be16 type;
2917
2918 type = skb_network_protocol(skb, &tmp);
2919 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002920
Ed Cashinc0d680e2012-09-19 15:49:00 +00002921 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09002922 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07002923 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00002924 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08002925 if (illegal_highdma(skb->dev, skb))
2926 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00002927
2928 return features;
2929}
2930
Toshiaki Makitae38f3022015-03-27 14:31:13 +09002931netdev_features_t passthru_features_check(struct sk_buff *skb,
2932 struct net_device *dev,
2933 netdev_features_t features)
2934{
2935 return features;
2936}
2937EXPORT_SYMBOL(passthru_features_check);
2938
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09002939static netdev_features_t dflt_features_check(const struct sk_buff *skb,
2940 struct net_device *dev,
2941 netdev_features_t features)
2942{
2943 return vlan_features_check(skb, features);
2944}
2945
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002946static netdev_features_t gso_features_check(const struct sk_buff *skb,
2947 struct net_device *dev,
2948 netdev_features_t features)
2949{
2950 u16 gso_segs = skb_shinfo(skb)->gso_segs;
2951
2952 if (gso_segs > dev->gso_max_segs)
2953 return features & ~NETIF_F_GSO_MASK;
2954
Alexander Duyck802ab552016-04-10 21:45:03 -04002955 /* Support for GSO partial features requires software
2956 * intervention before we can actually process the packets
2957 * so we need to strip support for any partial features now
2958 * and we can pull them back in after we have partially
2959 * segmented the frame.
2960 */
2961 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
2962 features &= ~dev->gso_partial_features;
2963
2964 /* Make sure to clear the IPv4 ID mangling feature if the
2965 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002966 */
2967 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2968 struct iphdr *iph = skb->encapsulation ?
2969 inner_ip_hdr(skb) : ip_hdr(skb);
2970
2971 if (!(iph->frag_off & htons(IP_DF)))
2972 features &= ~NETIF_F_TSO_MANGLEID;
2973 }
2974
2975 return features;
2976}
2977
Florian Westphalc1e756b2014-05-05 15:00:44 +02002978netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00002979{
Jesse Gross5f352272014-12-23 22:37:26 -08002980 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07002981 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00002982
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002983 if (skb_is_gso(skb))
2984 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00002985
Jesse Gross5f352272014-12-23 22:37:26 -08002986 /* If encapsulation offload request, verify we are testing
2987 * hardware encapsulation features instead of standard
2988 * features for the netdev
2989 */
2990 if (skb->encapsulation)
2991 features &= dev->hw_enc_features;
2992
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09002993 if (skb_vlan_tagged(skb))
2994 features = netdev_intersect_features(features,
2995 dev->vlan_features |
2996 NETIF_F_HW_VLAN_CTAG_TX |
2997 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00002998
Jesse Gross5f352272014-12-23 22:37:26 -08002999 if (dev->netdev_ops->ndo_features_check)
3000 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3001 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09003002 else
3003 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08003004
Florian Westphalc1e756b2014-05-05 15:00:44 +02003005 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003006}
Florian Westphalc1e756b2014-05-05 15:00:44 +02003007EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00003008
David S. Miller2ea25512014-08-29 21:10:01 -07003009static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07003010 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003011{
David S. Miller2ea25512014-08-29 21:10:01 -07003012 unsigned int len;
3013 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003014
Salam Noureddine7866a622015-01-27 11:35:48 -08003015 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
David S. Miller2ea25512014-08-29 21:10:01 -07003016 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003017
David S. Miller2ea25512014-08-29 21:10:01 -07003018 len = skb->len;
3019 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003020 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003021 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003022
Patrick McHardy572a9d72009-11-10 06:14:14 +00003023 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003024}
David S. Miller2ea25512014-08-29 21:10:01 -07003025
David S. Miller8dcda222014-09-01 15:06:40 -07003026struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3027 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003028{
3029 struct sk_buff *skb = first;
3030 int rc = NETDEV_TX_OK;
3031
3032 while (skb) {
3033 struct sk_buff *next = skb->next;
3034
3035 skb->next = NULL;
David S. Miller95f6b3d2014-08-29 21:57:30 -07003036 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003037 if (unlikely(!dev_xmit_complete(rc))) {
3038 skb->next = next;
3039 goto out;
3040 }
3041
3042 skb = next;
3043 if (netif_xmit_stopped(txq) && skb) {
3044 rc = NETDEV_TX_BUSY;
3045 break;
3046 }
3047 }
3048
3049out:
3050 *ret = rc;
3051 return skb;
3052}
3053
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003054static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3055 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003056{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003057 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003058 !vlan_hw_offload_capable(features, skb->vlan_proto))
3059 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003060 return skb;
3061}
3062
Davide Caratti43c26a12017-05-18 15:44:41 +02003063int skb_csum_hwoffload_help(struct sk_buff *skb,
3064 const netdev_features_t features)
3065{
3066 if (unlikely(skb->csum_not_inet))
3067 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3068 skb_crc32c_csum_help(skb);
3069
3070 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3071}
3072EXPORT_SYMBOL(skb_csum_hwoffload_help);
3073
Steffen Klassertf53c7232017-12-20 10:41:36 +01003074static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
David S. Millereae3f882014-08-30 15:17:13 -07003075{
3076 netdev_features_t features;
3077
David S. Millereae3f882014-08-30 15:17:13 -07003078 features = netif_skb_features(skb);
3079 skb = validate_xmit_vlan(skb, features);
3080 if (unlikely(!skb))
3081 goto out_null;
3082
Johannes Berg8b86a612015-04-17 15:45:04 +02003083 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003084 struct sk_buff *segs;
3085
3086 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003087 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003088 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003089 } else if (segs) {
3090 consume_skb(skb);
3091 skb = segs;
3092 }
David S. Millereae3f882014-08-30 15:17:13 -07003093 } else {
3094 if (skb_needs_linearize(skb, features) &&
3095 __skb_linearize(skb))
3096 goto out_kfree_skb;
3097
3098 /* If packet is not checksummed and device does not
3099 * support checksumming for this protocol, complete
3100 * checksumming here.
3101 */
3102 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3103 if (skb->encapsulation)
3104 skb_set_inner_transport_header(skb,
3105 skb_checksum_start_offset(skb));
3106 else
3107 skb_set_transport_header(skb,
3108 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003109 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003110 goto out_kfree_skb;
3111 }
3112 }
3113
Steffen Klassertf53c7232017-12-20 10:41:36 +01003114 skb = validate_xmit_xfrm(skb, features, again);
Steffen Klassert3dca3f32017-12-20 10:41:31 +01003115
David S. Millereae3f882014-08-30 15:17:13 -07003116 return skb;
3117
3118out_kfree_skb:
3119 kfree_skb(skb);
3120out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003121 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003122 return NULL;
3123}
3124
Steffen Klassertf53c7232017-12-20 10:41:36 +01003125struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
Eric Dumazet55a93b32014-10-03 15:31:07 -07003126{
3127 struct sk_buff *next, *head = NULL, *tail;
3128
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003129 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003130 next = skb->next;
3131 skb->next = NULL;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003132
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003133 /* in case skb wont be segmented, point to itself */
3134 skb->prev = skb;
3135
Steffen Klassertf53c7232017-12-20 10:41:36 +01003136 skb = validate_xmit_skb(skb, dev, again);
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003137 if (!skb)
3138 continue;
3139
3140 if (!head)
3141 head = skb;
3142 else
3143 tail->next = skb;
3144 /* If skb was segmented, skb->prev points to
3145 * the last segment. If not, it still contains skb.
3146 */
3147 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003148 }
3149 return head;
3150}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003151EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003152
Eric Dumazet1def9232013-01-10 12:36:42 +00003153static void qdisc_pkt_len_init(struct sk_buff *skb)
3154{
3155 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3156
3157 qdisc_skb_cb(skb)->pkt_len = skb->len;
3158
3159 /* To get more precise estimation of bytes sent on wire,
3160 * we add to pkt_len the headers size of all segments
3161 */
3162 if (shinfo->gso_size) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003163 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003164 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003165
Eric Dumazet757b8b12013-01-15 21:14:21 -08003166 /* mac layer + network layer */
3167 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3168
3169 /* + transport layer */
Eric Dumazet1def9232013-01-10 12:36:42 +00003170 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3171 hdr_len += tcp_hdrlen(skb);
3172 else
3173 hdr_len += sizeof(struct udphdr);
Jason Wang15e5a032013-03-25 20:19:59 +00003174
3175 if (shinfo->gso_type & SKB_GSO_DODGY)
3176 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3177 shinfo->gso_size);
3178
3179 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003180 }
3181}
3182
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003183static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3184 struct net_device *dev,
3185 struct netdev_queue *txq)
3186{
3187 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003188 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003189 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003190 int rc;
3191
Eric Dumazeta2da5702011-01-20 03:48:19 +00003192 qdisc_calculate_pkt_len(skb, q);
John Fastabend6b3ba912017-12-07 09:54:25 -08003193
3194 if (q->flags & TCQ_F_NOLOCK) {
3195 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3196 __qdisc_drop(skb, &to_free);
3197 rc = NET_XMIT_DROP;
3198 } else {
3199 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
3200 __qdisc_run(q);
3201 }
3202
3203 if (unlikely(to_free))
3204 kfree_skb_list(to_free);
3205 return rc;
3206 }
3207
Eric Dumazet79640a42010-06-02 05:09:29 -07003208 /*
3209 * Heuristic to force contended enqueues to serialize on a
3210 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003211 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003212 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003213 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003214 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003215 if (unlikely(contended))
3216 spin_lock(&q->busylock);
3217
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003218 spin_lock(root_lock);
3219 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003220 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003221 rc = NET_XMIT_DROP;
3222 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003223 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003224 /*
3225 * This is a work-conserving queue; there are no old skbs
3226 * waiting to be sent out; and the qdisc is not running -
3227 * xmit the skb directly.
3228 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003229
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003230 qdisc_bstats_update(q, skb);
3231
Eric Dumazet55a93b32014-10-03 15:31:07 -07003232 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003233 if (unlikely(contended)) {
3234 spin_unlock(&q->busylock);
3235 contended = false;
3236 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003237 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003238 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003239
John Fastabend6c148182017-12-07 09:54:06 -08003240 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003241 rc = NET_XMIT_SUCCESS;
3242 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003243 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003244 if (qdisc_run_begin(q)) {
3245 if (unlikely(contended)) {
3246 spin_unlock(&q->busylock);
3247 contended = false;
3248 }
3249 __qdisc_run(q);
John Fastabend6c148182017-12-07 09:54:06 -08003250 qdisc_run_end(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003251 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003252 }
3253 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003254 if (unlikely(to_free))
3255 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003256 if (unlikely(contended))
3257 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003258 return rc;
3259}
3260
Daniel Borkmann86f85152013-12-29 17:27:11 +01003261#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003262static void skb_update_prio(struct sk_buff *skb)
3263{
Igor Maravic6977a792011-11-25 07:44:54 +00003264 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
Neil Horman5bc14212011-11-22 05:10:51 +00003265
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003266 if (!skb->priority && skb->sk && map) {
Tejun Heo2a56a1f2015-12-07 17:38:52 -05003267 unsigned int prioidx =
3268 sock_cgroup_prioidx(&skb->sk->sk_cgrp_data);
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003269
3270 if (prioidx < map->priomap_len)
3271 skb->priority = map->priomap[prioidx];
3272 }
Neil Horman5bc14212011-11-22 05:10:51 +00003273}
3274#else
3275#define skb_update_prio(skb)
3276#endif
3277
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +02003278DEFINE_PER_CPU(int, xmit_recursion);
3279EXPORT_SYMBOL(xmit_recursion);
3280
Dave Jonesd29f7492008-07-22 14:09:06 -07003281/**
Michel Machado95603e22012-06-12 10:16:35 +00003282 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003283 * @net: network namespace this loopback is happening in
3284 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003285 * @skb: buffer to transmit
3286 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003287int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003288{
3289 skb_reset_mac_header(skb);
3290 __skb_pull(skb, skb_network_offset(skb));
3291 skb->pkt_type = PACKET_LOOPBACK;
3292 skb->ip_summed = CHECKSUM_UNNECESSARY;
3293 WARN_ON(!skb_dst(skb));
3294 skb_dst_force(skb);
3295 netif_rx_ni(skb);
3296 return 0;
3297}
3298EXPORT_SYMBOL(dev_loopback_xmit);
3299
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003300#ifdef CONFIG_NET_EGRESS
3301static struct sk_buff *
3302sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3303{
Jiri Pirko46209402017-11-03 11:46:25 +01003304 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003305 struct tcf_result cl_res;
3306
Jiri Pirko46209402017-11-03 11:46:25 +01003307 if (!miniq)
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003308 return skb;
3309
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003310 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Jiri Pirko46209402017-11-03 11:46:25 +01003311 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003312
Jiri Pirko46209402017-11-03 11:46:25 +01003313 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003314 case TC_ACT_OK:
3315 case TC_ACT_RECLASSIFY:
3316 skb->tc_index = TC_H_MIN(cl_res.classid);
3317 break;
3318 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01003319 mini_qdisc_qstats_cpu_drop(miniq);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003320 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003321 kfree_skb(skb);
3322 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003323 case TC_ACT_STOLEN:
3324 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003325 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003326 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003327 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003328 return NULL;
3329 case TC_ACT_REDIRECT:
3330 /* No need to push/pop skb's mac_header here on egress! */
3331 skb_do_redirect(skb);
3332 *ret = NET_XMIT_SUCCESS;
3333 return NULL;
3334 default:
3335 break;
3336 }
3337
3338 return skb;
3339}
3340#endif /* CONFIG_NET_EGRESS */
3341
Jiri Pirko638b2a62015-05-12 14:56:13 +02003342static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
3343{
3344#ifdef CONFIG_XPS
3345 struct xps_dev_maps *dev_maps;
3346 struct xps_map *map;
3347 int queue_index = -1;
3348
3349 rcu_read_lock();
3350 dev_maps = rcu_dereference(dev->xps_maps);
3351 if (dev_maps) {
Alexander Duyck184c4492016-10-28 11:50:13 -04003352 unsigned int tci = skb->sender_cpu - 1;
3353
3354 if (dev->num_tc) {
3355 tci *= dev->num_tc;
3356 tci += netdev_get_prio_tc_map(dev, skb->priority);
3357 }
3358
3359 map = rcu_dereference(dev_maps->cpu_map[tci]);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003360 if (map) {
3361 if (map->len == 1)
3362 queue_index = map->queues[0];
3363 else
3364 queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
3365 map->len)];
3366 if (unlikely(queue_index >= dev->real_num_tx_queues))
3367 queue_index = -1;
3368 }
3369 }
3370 rcu_read_unlock();
3371
3372 return queue_index;
3373#else
3374 return -1;
3375#endif
3376}
3377
3378static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
3379{
3380 struct sock *sk = skb->sk;
3381 int queue_index = sk_tx_queue_get(sk);
3382
3383 if (queue_index < 0 || skb->ooo_okay ||
3384 queue_index >= dev->real_num_tx_queues) {
3385 int new_index = get_xps_queue(dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003386
Jiri Pirko638b2a62015-05-12 14:56:13 +02003387 if (new_index < 0)
3388 new_index = skb_tx_hash(dev, skb);
3389
3390 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003391 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003392 rcu_access_pointer(sk->sk_dst_cache))
3393 sk_tx_queue_set(sk, new_index);
3394
3395 queue_index = new_index;
3396 }
3397
3398 return queue_index;
3399}
3400
3401struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3402 struct sk_buff *skb,
3403 void *accel_priv)
3404{
3405 int queue_index = 0;
3406
3407#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003408 u32 sender_cpu = skb->sender_cpu - 1;
3409
3410 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003411 skb->sender_cpu = raw_smp_processor_id() + 1;
3412#endif
3413
3414 if (dev->real_num_tx_queues != 1) {
3415 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003416
Jiri Pirko638b2a62015-05-12 14:56:13 +02003417 if (ops->ndo_select_queue)
3418 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
3419 __netdev_pick_tx);
3420 else
3421 queue_index = __netdev_pick_tx(dev, skb);
3422
Alexander Duyckd5845272017-11-22 10:57:41 -08003423 queue_index = netdev_cap_txqueue(dev, queue_index);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003424 }
3425
3426 skb_set_queue_mapping(skb, queue_index);
3427 return netdev_get_tx_queue(dev, queue_index);
3428}
3429
Michel Machado95603e22012-06-12 10:16:35 +00003430/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003431 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003432 * @skb: buffer to transmit
Jason Wang9d08dd32014-01-20 11:25:13 +08003433 * @accel_priv: private data used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003434 *
3435 * Queue a buffer for transmission to a network device. The caller must
3436 * have set the device and priority and built the buffer before calling
3437 * this function. The function can be called from an interrupt.
3438 *
3439 * A negative errno code is returned on a failure. A success does not
3440 * guarantee the frame will be transmitted as it may be dropped due
3441 * to congestion or traffic shaping.
3442 *
3443 * -----------------------------------------------------------------------------------
3444 * I notice this method can also return errors from the queue disciplines,
3445 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3446 * be positive.
3447 *
3448 * Regardless of the return value, the skb is consumed, so it is currently
3449 * difficult to retry a send to this method. (You can bump the ref count
3450 * before sending to hold a reference for retry if you are careful.)
3451 *
3452 * When calling this method, interrupts MUST be enabled. This is because
3453 * the BH enable code must have IRQs enabled so that it will not deadlock.
3454 * --BLG
3455 */
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05303456static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003459 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 struct Qdisc *q;
3461 int rc = -ENOMEM;
Steffen Klassertf53c7232017-12-20 10:41:36 +01003462 bool again = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003464 skb_reset_mac_header(skb);
3465
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003466 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3467 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3468
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003469 /* Disable soft irqs for various locks below. Also
3470 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003472 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Neil Horman5bc14212011-11-22 05:10:51 +00003474 skb_update_prio(skb);
3475
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003476 qdisc_pkt_len_init(skb);
3477#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003478 skb->tc_at_ingress = 0;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003479# ifdef CONFIG_NET_EGRESS
3480 if (static_key_false(&egress_needed)) {
3481 skb = sch_handle_egress(skb, &rc, dev);
3482 if (!skb)
3483 goto out;
3484 }
3485# endif
3486#endif
Eric Dumazet02875872014-10-05 18:38:35 -07003487 /* If device/qdisc don't need skb->dst, release it right now while
3488 * its hot in this cpu cache.
3489 */
3490 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3491 skb_dst_drop(skb);
3492 else
3493 skb_dst_force(skb);
3494
Jason Wangf663dd92014-01-10 16:18:26 +08003495 txq = netdev_pick_tx(dev, skb, accel_priv);
Paul E. McKenneya898def2010-02-22 17:04:49 -08003496 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07003497
Koki Sanagicf66ba52010-08-23 18:45:02 +09003498 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003500 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07003501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503
3504 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11003505 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
tchardingeb13da12017-02-09 17:56:06 +11003507 * Really, it is unlikely that netif_tx_lock protection is necessary
3508 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3509 * counters.)
3510 * However, it is possible, that they rely on protection
3511 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
tchardingeb13da12017-02-09 17:56:06 +11003513 * Check this and shot the lock. It is not prone from deadlocks.
3514 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 */
3516 if (dev->flags & IFF_UP) {
3517 int cpu = smp_processor_id(); /* ok because BHs are off */
3518
David S. Millerc773e842008-07-08 23:13:53 -07003519 if (txq->xmit_lock_owner != cpu) {
Daniel Borkmanna70b5062016-06-10 21:19:06 +02003520 if (unlikely(__this_cpu_read(xmit_recursion) >
3521 XMIT_RECURSION_LIMIT))
Eric Dumazet745e20f2010-09-29 13:23:09 -07003522 goto recursion_alert;
3523
Steffen Klassertf53c7232017-12-20 10:41:36 +01003524 skb = validate_xmit_skb(skb, dev, &again);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003525 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07003526 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003527
David S. Millerc773e842008-07-08 23:13:53 -07003528 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Tom Herbert734664982011-11-28 16:32:44 +00003530 if (!netif_xmit_stopped(txq)) {
Eric Dumazet745e20f2010-09-29 13:23:09 -07003531 __this_cpu_inc(xmit_recursion);
David S. Millerce937182014-08-30 19:22:20 -07003532 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Eric Dumazet745e20f2010-09-29 13:23:09 -07003533 __this_cpu_dec(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00003534 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07003535 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 goto out;
3537 }
3538 }
David S. Millerc773e842008-07-08 23:13:53 -07003539 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00003540 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3541 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 } else {
3543 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07003544 * unfortunately
3545 */
3546recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00003547 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3548 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
3550 }
3551
3552 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07003553 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Eric Dumazet015f0682014-03-27 08:45:56 -07003555 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003556 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return rc;
3558out:
Herbert Xud4828d82006-06-22 02:28:18 -07003559 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return rc;
3561}
Jason Wangf663dd92014-01-10 16:18:26 +08003562
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003563int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08003564{
3565 return __dev_queue_xmit(skb, NULL);
3566}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003567EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Jason Wangf663dd92014-01-10 16:18:26 +08003569int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
3570{
3571 return __dev_queue_xmit(skb, accel_priv);
3572}
3573EXPORT_SYMBOL(dev_queue_xmit_accel);
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
tchardingeb13da12017-02-09 17:56:06 +11003576/*************************************************************************
3577 * Receiver routines
3578 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003580int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00003581EXPORT_SYMBOL(netdev_max_backlog);
3582
Eric Dumazet3b098e22010-05-15 23:57:10 -07003583int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003584int netdev_budget __read_mostly = 300;
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04003585unsigned int __read_mostly netdev_budget_usecs = 2000;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01003586int weight_p __read_mostly = 64; /* old backlog weight */
3587int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
3588int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
3589int dev_rx_weight __read_mostly = 64;
3590int dev_tx_weight __read_mostly = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003592/* Called with irq disabled */
3593static inline void ____napi_schedule(struct softnet_data *sd,
3594 struct napi_struct *napi)
3595{
3596 list_add_tail(&napi->poll_list, &sd->poll_list);
3597 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3598}
3599
Eric Dumazetdf334542010-03-24 19:13:54 +00003600#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07003601
3602/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00003603struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07003604EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003605u32 rps_cpu_mask __read_mostly;
3606EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07003607
Ingo Molnarc5905af2012-02-24 08:31:31 +01003608struct static_key rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04003609EXPORT_SYMBOL(rps_needed);
Eric Dumazet13bfff22016-12-07 08:29:10 -08003610struct static_key rfs_needed __read_mostly;
3611EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00003612
Ben Hutchingsc4454772011-01-19 11:03:53 +00003613static struct rps_dev_flow *
3614set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3615 struct rps_dev_flow *rflow, u16 next_cpu)
3616{
Eric Dumazeta31196b2015-04-25 09:35:24 -07003617 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00003618#ifdef CONFIG_RFS_ACCEL
3619 struct netdev_rx_queue *rxqueue;
3620 struct rps_dev_flow_table *flow_table;
3621 struct rps_dev_flow *old_rflow;
3622 u32 flow_id;
3623 u16 rxq_index;
3624 int rc;
3625
3626 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00003627 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3628 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00003629 goto out;
3630 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3631 if (rxq_index == skb_get_rx_queue(skb))
3632 goto out;
3633
3634 rxqueue = dev->_rx + rxq_index;
3635 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3636 if (!flow_table)
3637 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07003638 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003639 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3640 rxq_index, flow_id);
3641 if (rc < 0)
3642 goto out;
3643 old_rflow = rflow;
3644 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00003645 rflow->filter = rc;
3646 if (old_rflow->filter == rflow->filter)
3647 old_rflow->filter = RPS_NO_FILTER;
3648 out:
3649#endif
3650 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00003651 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003652 }
3653
Ben Hutchings09994d12011-10-03 04:42:46 +00003654 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003655 return rflow;
3656}
3657
Tom Herbert0a9627f2010-03-16 08:03:29 +00003658/*
3659 * get_rps_cpu is called from netif_receive_skb and returns the target
3660 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003661 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00003662 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003663static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3664 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003665{
Eric Dumazet567e4b72015-02-06 12:59:01 -08003666 const struct rps_sock_flow_table *sock_flow_table;
3667 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07003668 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003669 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003670 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003671 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07003672 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003673
Tom Herbert0a9627f2010-03-16 08:03:29 +00003674 if (skb_rx_queue_recorded(skb)) {
3675 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003676
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003677 if (unlikely(index >= dev->real_num_rx_queues)) {
3678 WARN_ONCE(dev->real_num_rx_queues > 1,
3679 "%s received packet on queue %u, but number "
3680 "of RX queues is %u\n",
3681 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003682 goto done;
3683 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08003684 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003685 }
3686
Eric Dumazet567e4b72015-02-06 12:59:01 -08003687 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
3688
3689 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3690 map = rcu_dereference(rxqueue->rps_map);
3691 if (!flow_table && !map)
3692 goto done;
3693
Changli Gao2d47b452010-08-17 19:00:56 +00003694 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07003695 hash = skb_get_hash(skb);
3696 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003697 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003698
Tom Herbertfec5e652010-04-16 16:01:27 -07003699 sock_flow_table = rcu_dereference(rps_sock_flow_table);
3700 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003701 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003702 u32 next_cpu;
3703 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07003704
Eric Dumazet567e4b72015-02-06 12:59:01 -08003705 /* First check into global flow table if there is a match */
3706 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
3707 if ((ident ^ hash) & ~rps_cpu_mask)
3708 goto try_rps;
3709
3710 next_cpu = ident & rps_cpu_mask;
3711
3712 /* OK, now we know there is a match,
3713 * we can look at the local (per receive queue) flow table
3714 */
Tom Herbert61b905d2014-03-24 15:34:47 -07003715 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07003716 tcpu = rflow->cpu;
3717
Tom Herbertfec5e652010-04-16 16:01:27 -07003718 /*
3719 * If the desired CPU (where last recvmsg was done) is
3720 * different from current CPU (one in the rx-queue flow
3721 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07003722 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07003723 * - Current CPU is offline.
3724 * - The current CPU's queue tail has advanced beyond the
3725 * last packet that was enqueued using this table entry.
3726 * This guarantees that all previous packets for the flow
3727 * have been dequeued, thus preserving in order delivery.
3728 */
3729 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07003730 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07003731 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00003732 rflow->last_qtail)) >= 0)) {
3733 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003734 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00003735 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00003736
Eric Dumazeta31196b2015-04-25 09:35:24 -07003737 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003738 *rflowp = rflow;
3739 cpu = tcpu;
3740 goto done;
3741 }
3742 }
3743
Eric Dumazet567e4b72015-02-06 12:59:01 -08003744try_rps:
3745
Tom Herbert0a9627f2010-03-16 08:03:29 +00003746 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02003747 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00003748 if (cpu_online(tcpu)) {
3749 cpu = tcpu;
3750 goto done;
3751 }
3752 }
3753
3754done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00003755 return cpu;
3756}
3757
Ben Hutchingsc4454772011-01-19 11:03:53 +00003758#ifdef CONFIG_RFS_ACCEL
3759
3760/**
3761 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3762 * @dev: Device on which the filter was set
3763 * @rxq_index: RX queue index
3764 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3765 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3766 *
3767 * Drivers that implement ndo_rx_flow_steer() should periodically call
3768 * this function for each installed filter and remove the filters for
3769 * which it returns %true.
3770 */
3771bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3772 u32 flow_id, u16 filter_id)
3773{
3774 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3775 struct rps_dev_flow_table *flow_table;
3776 struct rps_dev_flow *rflow;
3777 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07003778 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003779
3780 rcu_read_lock();
3781 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3782 if (flow_table && flow_id <= flow_table->mask) {
3783 rflow = &flow_table->flows[flow_id];
Mark Rutland6aa7de02017-10-23 14:07:29 -07003784 cpu = READ_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07003785 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00003786 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3787 rflow->last_qtail) <
3788 (int)(10 * flow_table->mask)))
3789 expire = false;
3790 }
3791 rcu_read_unlock();
3792 return expire;
3793}
3794EXPORT_SYMBOL(rps_may_expire_flow);
3795
3796#endif /* CONFIG_RFS_ACCEL */
3797
Tom Herbert0a9627f2010-03-16 08:03:29 +00003798/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003799static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003800{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003801 struct softnet_data *sd = data;
3802
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003803 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00003804 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003805}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003806
Tom Herbertfec5e652010-04-16 16:01:27 -07003807#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00003808
3809/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003810 * Check if this softnet_data structure is another cpu one
3811 * If yes, queue it to our IPI list and return 1
3812 * If no, return 0
3813 */
3814static int rps_ipi_queued(struct softnet_data *sd)
3815{
3816#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05003817 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003818
3819 if (sd != mysd) {
3820 sd->rps_ipi_next = mysd->rps_ipi_list;
3821 mysd->rps_ipi_list = sd;
3822
3823 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3824 return 1;
3825 }
3826#endif /* CONFIG_RPS */
3827 return 0;
3828}
3829
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003830#ifdef CONFIG_NET_FLOW_LIMIT
3831int netdev_flow_limit_table_len __read_mostly = (1 << 12);
3832#endif
3833
3834static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
3835{
3836#ifdef CONFIG_NET_FLOW_LIMIT
3837 struct sd_flow_limit *fl;
3838 struct softnet_data *sd;
3839 unsigned int old_flow, new_flow;
3840
3841 if (qlen < (netdev_max_backlog >> 1))
3842 return false;
3843
Christoph Lameter903ceff2014-08-17 12:30:35 -05003844 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003845
3846 rcu_read_lock();
3847 fl = rcu_dereference(sd->flow_limit);
3848 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08003849 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003850 old_flow = fl->history[fl->history_head];
3851 fl->history[fl->history_head] = new_flow;
3852
3853 fl->history_head++;
3854 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
3855
3856 if (likely(fl->buckets[old_flow]))
3857 fl->buckets[old_flow]--;
3858
3859 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
3860 fl->count++;
3861 rcu_read_unlock();
3862 return true;
3863 }
3864 }
3865 rcu_read_unlock();
3866#endif
3867 return false;
3868}
3869
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003870/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00003871 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3872 * queue (may be a remote CPU queue).
3873 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003874static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3875 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003876{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003877 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003878 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003879 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003880
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003881 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003882
3883 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003884
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003885 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03003886 if (!netif_running(skb->dev))
3887 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003888 qlen = skb_queue_len(&sd->input_pkt_queue);
3889 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08003890 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00003891enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003892 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003893 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003894 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00003895 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003896 return NET_RX_SUCCESS;
3897 }
3898
Eric Dumazetebda37c22010-05-06 23:51:21 +00003899 /* Schedule NAPI for backlog device
3900 * We can use non atomic operation since we own the queue lock
3901 */
3902 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003903 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003904 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003905 }
3906 goto enqueue;
3907 }
3908
Julian Anastasove9e4dd32015-07-09 09:59:09 +03003909drop:
Changli Gaodee42872010-05-02 05:42:16 +00003910 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003911 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003912
Tom Herbert0a9627f2010-03-16 08:03:29 +00003913 local_irq_restore(flags);
3914
Eric Dumazetcaf586e2010-09-30 21:06:55 +00003915 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003916 kfree_skb(skb);
3917 return NET_RX_DROP;
3918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01003920static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
3921{
3922 struct net_device *dev = skb->dev;
3923 struct netdev_rx_queue *rxqueue;
3924
3925 rxqueue = dev->_rx;
3926
3927 if (skb_rx_queue_recorded(skb)) {
3928 u16 index = skb_get_rx_queue(skb);
3929
3930 if (unlikely(index >= dev->real_num_rx_queues)) {
3931 WARN_ONCE(dev->real_num_rx_queues > 1,
3932 "%s received packet on queue %u, but number "
3933 "of RX queues is %u\n",
3934 dev->name, index, dev->real_num_rx_queues);
3935
3936 return rxqueue; /* Return first rxqueue */
3937 }
3938 rxqueue += index;
3939 }
3940 return rxqueue;
3941}
3942
John Fastabendd4455162017-07-17 09:26:45 -07003943static u32 netif_receive_generic_xdp(struct sk_buff *skb,
3944 struct bpf_prog *xdp_prog)
3945{
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01003946 struct netdev_rx_queue *rxqueue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003947 u32 metalen, act = XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07003948 struct xdp_buff xdp;
John Fastabendd4455162017-07-17 09:26:45 -07003949 void *orig_data;
3950 int hlen, off;
3951 u32 mac_len;
3952
3953 /* Reinjected packets coming from act_mirred or similar should
3954 * not get XDP generic processing.
3955 */
3956 if (skb_cloned(skb))
3957 return XDP_PASS;
3958
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003959 /* XDP packets must be linear and must have sufficient headroom
3960 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
3961 * native XDP provides, thus we need to do it here as well.
3962 */
3963 if (skb_is_nonlinear(skb) ||
3964 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
3965 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
3966 int troom = skb->tail + skb->data_len - skb->end;
3967
3968 /* In case we have to go down the path and also linearize,
3969 * then lets do the pskb_expand_head() work just once here.
3970 */
3971 if (pskb_expand_head(skb,
3972 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
3973 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
3974 goto do_drop;
Song Liu2d17d8d2017-12-14 17:17:56 -08003975 if (skb_linearize(skb))
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003976 goto do_drop;
3977 }
John Fastabendd4455162017-07-17 09:26:45 -07003978
3979 /* The XDP program wants to see the packet starting at the MAC
3980 * header.
3981 */
3982 mac_len = skb->data - skb_mac_header(skb);
3983 hlen = skb_headlen(skb) + mac_len;
3984 xdp.data = skb->data - mac_len;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003985 xdp.data_meta = xdp.data;
John Fastabendd4455162017-07-17 09:26:45 -07003986 xdp.data_end = xdp.data + hlen;
3987 xdp.data_hard_start = skb->data - skb_headroom(skb);
3988 orig_data = xdp.data;
3989
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01003990 rxqueue = netif_get_rxqueue(skb);
3991 xdp.rxq = &rxqueue->xdp_rxq;
3992
John Fastabendd4455162017-07-17 09:26:45 -07003993 act = bpf_prog_run_xdp(xdp_prog, &xdp);
3994
3995 off = xdp.data - orig_data;
3996 if (off > 0)
3997 __skb_pull(skb, off);
3998 else if (off < 0)
3999 __skb_push(skb, -off);
Edward Cree92dd5452017-09-19 18:45:56 +01004000 skb->mac_header += off;
John Fastabendd4455162017-07-17 09:26:45 -07004001
4002 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07004003 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07004004 case XDP_TX:
4005 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07004006 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004007 case XDP_PASS:
4008 metalen = xdp.data - xdp.data_meta;
4009 if (metalen)
4010 skb_metadata_set(skb, metalen);
4011 break;
John Fastabendd4455162017-07-17 09:26:45 -07004012 default:
4013 bpf_warn_invalid_xdp_action(act);
4014 /* fall through */
4015 case XDP_ABORTED:
4016 trace_xdp_exception(skb->dev, xdp_prog, act);
4017 /* fall through */
4018 case XDP_DROP:
4019 do_drop:
4020 kfree_skb(skb);
4021 break;
4022 }
4023
4024 return act;
4025}
4026
4027/* When doing generic XDP we have to bypass the qdisc layer and the
4028 * network taps in order to match in-driver-XDP behavior.
4029 */
Jason Wang7c497472017-08-11 19:41:17 +08004030void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07004031{
4032 struct net_device *dev = skb->dev;
4033 struct netdev_queue *txq;
4034 bool free_skb = true;
4035 int cpu, rc;
4036
4037 txq = netdev_pick_tx(dev, skb, NULL);
4038 cpu = smp_processor_id();
4039 HARD_TX_LOCK(dev, txq, cpu);
4040 if (!netif_xmit_stopped(txq)) {
4041 rc = netdev_start_xmit(skb, dev, txq, 0);
4042 if (dev_xmit_complete(rc))
4043 free_skb = false;
4044 }
4045 HARD_TX_UNLOCK(dev, txq);
4046 if (free_skb) {
4047 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4048 kfree_skb(skb);
4049 }
4050}
Jason Wang7c497472017-08-11 19:41:17 +08004051EXPORT_SYMBOL_GPL(generic_xdp_tx);
John Fastabendd4455162017-07-17 09:26:45 -07004052
4053static struct static_key generic_xdp_needed __read_mostly;
4054
Jason Wang7c497472017-08-11 19:41:17 +08004055int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004056{
John Fastabendd4455162017-07-17 09:26:45 -07004057 if (xdp_prog) {
4058 u32 act = netif_receive_generic_xdp(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004059 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004060
4061 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004062 switch (act) {
4063 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004064 err = xdp_do_generic_redirect(skb->dev, skb,
4065 xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004066 if (err)
4067 goto out_redir;
4068 /* fallthru to submit skb */
4069 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004070 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004071 break;
4072 }
John Fastabendd4455162017-07-17 09:26:45 -07004073 return XDP_DROP;
4074 }
4075 }
4076 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004077out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004078 kfree_skb(skb);
4079 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004080}
Jason Wang7c497472017-08-11 19:41:17 +08004081EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004082
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004083static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004085 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Eric Dumazet588f0332011-11-15 04:12:55 +00004087 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Koki Sanagicf66ba52010-08-23 18:45:02 +09004089 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004090
4091 if (static_key_false(&generic_xdp_needed)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07004092 int ret;
4093
4094 preempt_disable();
4095 rcu_read_lock();
4096 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4097 rcu_read_unlock();
4098 preempt_enable();
John Fastabendd4455162017-07-17 09:26:45 -07004099
John Fastabend6103aa92017-07-17 09:27:50 -07004100 /* Consider XDP consuming the packet a success from
4101 * the netdev point of view we do not want to count
4102 * this as an error.
4103 */
John Fastabendd4455162017-07-17 09:26:45 -07004104 if (ret != XDP_PASS)
John Fastabend6103aa92017-07-17 09:27:50 -07004105 return NET_RX_SUCCESS;
John Fastabendd4455162017-07-17 09:26:45 -07004106 }
4107
Eric Dumazetdf334542010-03-24 19:13:54 +00004108#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004109 if (static_key_false(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004110 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004111 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Changli Gaocece1942010-08-07 20:35:43 -07004113 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004114 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004115
4116 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004117 if (cpu < 0)
4118 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004119
4120 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4121
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004122 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004123 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004124 } else
4125#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004126 {
4127 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004128
Tom Herbertfec5e652010-04-16 16:01:27 -07004129 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4130 put_cpu();
4131 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004134
4135/**
4136 * netif_rx - post buffer to the network code
4137 * @skb: buffer to post
4138 *
4139 * This function receives a packet from a device driver and queues it for
4140 * the upper (protocol) levels to process. It always succeeds. The buffer
4141 * may be dropped during processing for congestion control or by the
4142 * protocol layers.
4143 *
4144 * return values:
4145 * NET_RX_SUCCESS (no congestion)
4146 * NET_RX_DROP (packet was dropped)
4147 *
4148 */
4149
4150int netif_rx(struct sk_buff *skb)
4151{
4152 trace_netif_rx_entry(skb);
4153
4154 return netif_rx_internal(skb);
4155}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004156EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
4158int netif_rx_ni(struct sk_buff *skb)
4159{
4160 int err;
4161
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004162 trace_netif_rx_ni_entry(skb);
4163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004165 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 if (local_softirq_pending())
4167 do_softirq();
4168 preempt_enable();
4169
4170 return err;
4171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172EXPORT_SYMBOL(netif_rx_ni);
4173
Emese Revfy0766f782016-06-20 20:42:34 +02004174static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004176 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 if (sd->completion_queue) {
4179 struct sk_buff *clist;
4180
4181 local_irq_disable();
4182 clist = sd->completion_queue;
4183 sd->completion_queue = NULL;
4184 local_irq_enable();
4185
4186 while (clist) {
4187 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 clist = clist->next;
4190
Reshetova, Elena63354792017-06-30 13:07:58 +03004191 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004192 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4193 trace_consume_skb(skb);
4194 else
4195 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004196
4197 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4198 __kfree_skb(skb);
4199 else
4200 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004202
4203 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 }
4205
4206 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004207 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209 local_irq_disable();
4210 head = sd->output_queue;
4211 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004212 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 local_irq_enable();
4214
4215 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004216 struct Qdisc *q = head;
John Fastabend6b3ba912017-12-07 09:54:25 -08004217 spinlock_t *root_lock = NULL;
David S. Miller37437bb2008-07-16 02:15:04 -07004218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 head = head->next_sched;
4220
John Fastabend6b3ba912017-12-07 09:54:25 -08004221 if (!(q->flags & TCQ_F_NOLOCK)) {
4222 root_lock = qdisc_lock(q);
4223 spin_lock(root_lock);
4224 }
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004225 /* We need to make sure head->next_sched is read
4226 * before clearing __QDISC_STATE_SCHED
4227 */
4228 smp_mb__before_atomic();
4229 clear_bit(__QDISC_STATE_SCHED, &q->state);
4230 qdisc_run(q);
John Fastabend6b3ba912017-12-07 09:54:25 -08004231 if (root_lock)
4232 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
4234 }
Steffen Klassertf53c7232017-12-20 10:41:36 +01004235
4236 xfrm_dev_backlog(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004239#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004240/* This hook is defined here for ATM LANE */
4241int (*br_fdb_test_addr_hook)(struct net_device *dev,
4242 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004243EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004246static inline struct sk_buff *
4247sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4248 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004249{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004250#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko46209402017-11-03 11:46:25 +01004251 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004252 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004253
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004254 /* If there's at least one ingress present somewhere (so
4255 * we get here via enabled static key), remaining devices
4256 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004257 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004258 */
Jiri Pirko46209402017-11-03 11:46:25 +01004259 if (!miniq)
Daniel Borkmann45771392015-04-10 23:07:54 +02004260 return skb;
Jiri Pirko46209402017-11-03 11:46:25 +01004261
Herbert Xuf697c3e2007-10-14 00:38:47 -07004262 if (*pt_prev) {
4263 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4264 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004265 }
4266
Florian Westphal33654952015-05-14 00:36:28 +02004267 qdisc_skb_cb(skb)->pkt_len = skb->len;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004268 skb->tc_at_ingress = 1;
Jiri Pirko46209402017-11-03 11:46:25 +01004269 mini_qdisc_bstats_cpu_update(miniq, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004270
Jiri Pirko46209402017-11-03 11:46:25 +01004271 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004272 case TC_ACT_OK:
4273 case TC_ACT_RECLASSIFY:
4274 skb->tc_index = TC_H_MIN(cl_res.classid);
4275 break;
4276 case TC_ACT_SHOT:
Jiri Pirko46209402017-11-03 11:46:25 +01004277 mini_qdisc_qstats_cpu_drop(miniq);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004278 kfree_skb(skb);
4279 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004280 case TC_ACT_STOLEN:
4281 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004282 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004283 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004284 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004285 case TC_ACT_REDIRECT:
4286 /* skb_mac_header check was done by cls/act_bpf, so
4287 * we can safely push the L2 header back before
4288 * redirecting to another netdev
4289 */
4290 __skb_push(skb, skb->mac_len);
4291 skb_do_redirect(skb);
4292 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004293 default:
4294 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004295 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004296#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004297 return skb;
4298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004300/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004301 * netdev_is_rx_handler_busy - check if receive handler is registered
4302 * @dev: device to check
4303 *
4304 * Check if a receive handler is already registered for a given device.
4305 * Return true if there one.
4306 *
4307 * The caller must hold the rtnl_mutex.
4308 */
4309bool netdev_is_rx_handler_busy(struct net_device *dev)
4310{
4311 ASSERT_RTNL();
4312 return dev && rtnl_dereference(dev->rx_handler);
4313}
4314EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4315
4316/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004317 * netdev_rx_handler_register - register receive handler
4318 * @dev: device to register a handler for
4319 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004320 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004321 *
Masanari Iidae2278672014-02-18 22:54:36 +09004322 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004323 * called from __netif_receive_skb. A negative errno code is returned
4324 * on a failure.
4325 *
4326 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004327 *
4328 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004329 */
4330int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004331 rx_handler_func_t *rx_handler,
4332 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004333{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004334 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004335 return -EBUSY;
4336
Eric Dumazet00cfec32013-03-29 03:01:22 +00004337 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004338 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004339 rcu_assign_pointer(dev->rx_handler, rx_handler);
4340
4341 return 0;
4342}
4343EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4344
4345/**
4346 * netdev_rx_handler_unregister - unregister receive handler
4347 * @dev: device to unregister a handler from
4348 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004349 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004350 *
4351 * The caller must hold the rtnl_mutex.
4352 */
4353void netdev_rx_handler_unregister(struct net_device *dev)
4354{
4355
4356 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004357 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004358 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4359 * section has a guarantee to see a non NULL rx_handler_data
4360 * as well.
4361 */
4362 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004363 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004364}
4365EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4366
Mel Gormanb4b9e352012-07-31 16:44:26 -07004367/*
4368 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4369 * the special handling of PFMEMALLOC skbs.
4370 */
4371static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4372{
4373 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004374 case htons(ETH_P_ARP):
4375 case htons(ETH_P_IP):
4376 case htons(ETH_P_IPV6):
4377 case htons(ETH_P_8021Q):
4378 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004379 return true;
4380 default:
4381 return false;
4382 }
4383}
4384
Pablo Neirae687ad62015-05-13 18:19:38 +02004385static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4386 int *ret, struct net_device *orig_dev)
4387{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004388#ifdef CONFIG_NETFILTER_INGRESS
Pablo Neirae687ad62015-05-13 18:19:38 +02004389 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004390 int ingress_retval;
4391
Pablo Neirae687ad62015-05-13 18:19:38 +02004392 if (*pt_prev) {
4393 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4394 *pt_prev = NULL;
4395 }
4396
Aaron Conole2c1e2702016-09-21 11:35:03 -04004397 rcu_read_lock();
4398 ingress_retval = nf_hook_ingress(skb);
4399 rcu_read_unlock();
4400 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004401 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004402#endif /* CONFIG_NETFILTER_INGRESS */
Pablo Neirae687ad62015-05-13 18:19:38 +02004403 return 0;
4404}
Pablo Neirae687ad62015-05-13 18:19:38 +02004405
David S. Miller9754e292013-02-14 15:57:38 -05004406static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
4408 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004409 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004410 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004411 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004413 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Eric Dumazet588f0332011-11-15 04:12:55 +00004415 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07004416
Koki Sanagicf66ba52010-08-23 18:45:02 +09004417 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08004418
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07004419 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00004420
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07004421 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00004422 if (!skb_transport_header_was_set(skb))
4423 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00004424 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
4426 pt_prev = NULL;
4427
David S. Miller63d8ea72011-02-28 10:48:59 -08004428another_round:
David S. Millerb6858172012-07-23 16:27:54 -07004429 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08004430
4431 __this_cpu_inc(softnet_data.processed);
4432
Patrick McHardy8ad227f2013-04-19 02:04:31 +00004433 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4434 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004435 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004436 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004437 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004438 }
4439
Willem de Bruijne7246e12017-01-07 17:06:35 -05004440 if (skb_skip_tc_classify(skb))
4441 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
David S. Miller9754e292013-02-14 15:57:38 -05004443 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07004444 goto skip_taps;
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08004447 if (pt_prev)
4448 ret = deliver_skb(skb, pt_prev, orig_dev);
4449 pt_prev = ptype;
4450 }
4451
4452 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
4453 if (pt_prev)
4454 ret = deliver_skb(skb, pt_prev, orig_dev);
4455 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 }
4457
Mel Gormanb4b9e352012-07-31 16:44:26 -07004458skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02004459#ifdef CONFIG_NET_INGRESS
Daniel Borkmann45771392015-04-10 23:07:54 +02004460 if (static_key_false(&ingress_needed)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004461 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02004462 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004463 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02004464
4465 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004466 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02004467 }
Pablo Neira1cf519002015-05-13 18:19:37 +02004468#endif
Willem de Bruijna5135bc2017-01-07 17:06:36 -05004469 skb_reset_tc(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05004470skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05004471 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07004472 goto drop;
4473
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004474 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00004475 if (pt_prev) {
4476 ret = deliver_skb(skb, pt_prev, orig_dev);
4477 pt_prev = NULL;
4478 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004479 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00004480 goto another_round;
4481 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004482 goto out;
John Fastabend24257172011-10-10 09:16:41 +00004483 }
4484
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004485 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004486 if (rx_handler) {
4487 if (pt_prev) {
4488 ret = deliver_skb(skb, pt_prev, orig_dev);
4489 pt_prev = NULL;
4490 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004491 switch (rx_handler(&skb)) {
4492 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00004493 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004494 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004495 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08004496 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004497 case RX_HANDLER_EXACT:
4498 deliver_exact = true;
4499 case RX_HANDLER_PASS:
4500 break;
4501 default:
4502 BUG();
4503 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004506 if (unlikely(skb_vlan_tag_present(skb))) {
4507 if (skb_vlan_tag_get_id(skb))
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004508 skb->pkt_type = PACKET_OTHERHOST;
4509 /* Note: we might in the future use prio bits
4510 * and set skb->priority like in vlan_do_receive()
4511 * For the time being, just ignore Priority Code Point
4512 */
4513 skb->vlan_tci = 0;
4514 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08004517
4518 /* deliver only exact match when indicated */
4519 if (likely(!deliver_exact)) {
4520 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4521 &ptype_base[ntohs(type) &
4522 PTYPE_HASH_MASK]);
4523 }
4524
4525 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4526 &orig_dev->ptype_specific);
4527
4528 if (unlikely(skb->dev != orig_dev)) {
4529 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4530 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 }
4532
4533 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004534 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00004535 goto drop;
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00004536 else
4537 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07004539drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05004540 if (!deliver_exact)
4541 atomic_long_inc(&skb->dev->rx_dropped);
4542 else
4543 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 kfree_skb(skb);
4545 /* Jamal, now you will not able to escape explaining
4546 * me how you were going to use this. :-)
4547 */
4548 ret = NET_RX_DROP;
4549 }
4550
Julian Anastasov2c17d272015-07-09 09:59:10 +03004551out:
David S. Miller9754e292013-02-14 15:57:38 -05004552 return ret;
4553}
4554
Jesper Dangaard Brouer1c601d82017-10-16 12:19:39 +02004555/**
4556 * netif_receive_skb_core - special purpose version of netif_receive_skb
4557 * @skb: buffer to process
4558 *
4559 * More direct receive version of netif_receive_skb(). It should
4560 * only be used by callers that have a need to skip RPS and Generic XDP.
4561 * Caller must also take care of handling if (page_is_)pfmemalloc.
4562 *
4563 * This function may only be called from softirq context and interrupts
4564 * should be enabled.
4565 *
4566 * Return values (usually ignored):
4567 * NET_RX_SUCCESS: no congestion
4568 * NET_RX_DROP: packet was dropped
4569 */
4570int netif_receive_skb_core(struct sk_buff *skb)
4571{
4572 int ret;
4573
4574 rcu_read_lock();
4575 ret = __netif_receive_skb_core(skb, false);
4576 rcu_read_unlock();
4577
4578 return ret;
4579}
4580EXPORT_SYMBOL(netif_receive_skb_core);
4581
David S. Miller9754e292013-02-14 15:57:38 -05004582static int __netif_receive_skb(struct sk_buff *skb)
4583{
4584 int ret;
4585
4586 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07004587 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05004588
4589 /*
4590 * PFMEMALLOC skbs are special, they should
4591 * - be delivered to SOCK_MEMALLOC sockets only
4592 * - stay away from userspace
4593 * - have bounded memory usage
4594 *
4595 * Use PF_MEMALLOC as this saves us from propagating the allocation
4596 * context down to all allocation sites.
4597 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07004598 noreclaim_flag = memalloc_noreclaim_save();
David S. Miller9754e292013-02-14 15:57:38 -05004599 ret = __netif_receive_skb_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07004600 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05004601 } else
4602 ret = __netif_receive_skb_core(skb, false);
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return ret;
4605}
Tom Herbert0a9627f2010-03-16 08:03:29 +00004606
Jakub Kicinskif4e63522017-11-03 13:56:16 -07004607static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
David S. Millerb5cdae32017-04-18 15:36:58 -04004608{
Martin KaFai Lau58038692017-06-15 17:29:09 -07004609 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04004610 struct bpf_prog *new = xdp->prog;
4611 int ret = 0;
4612
4613 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07004614 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04004615 rcu_assign_pointer(dev->xdp_prog, new);
4616 if (old)
4617 bpf_prog_put(old);
4618
4619 if (old && !new) {
4620 static_key_slow_dec(&generic_xdp_needed);
4621 } else if (new && !old) {
4622 static_key_slow_inc(&generic_xdp_needed);
4623 dev_disable_lro(dev);
Michael Chan56f5aa72017-12-16 03:09:41 -05004624 dev_disable_gro_hw(dev);
David S. Millerb5cdae32017-04-18 15:36:58 -04004625 }
4626 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04004627
4628 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07004629 xdp->prog_attached = !!old;
4630 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04004631 break;
4632
4633 default:
4634 ret = -EINVAL;
4635 break;
4636 }
4637
4638 return ret;
4639}
4640
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004641static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004642{
Julian Anastasov2c17d272015-07-09 09:59:10 +03004643 int ret;
4644
Eric Dumazet588f0332011-11-15 04:12:55 +00004645 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07004646
Richard Cochranc1f19b52010-07-17 08:49:36 +00004647 if (skb_defer_rx_timestamp(skb))
4648 return NET_RX_SUCCESS;
4649
David S. Millerb5cdae32017-04-18 15:36:58 -04004650 if (static_key_false(&generic_xdp_needed)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07004651 int ret;
David S. Millerb5cdae32017-04-18 15:36:58 -04004652
John Fastabendbbbe2112017-09-08 14:00:30 -07004653 preempt_disable();
4654 rcu_read_lock();
4655 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4656 rcu_read_unlock();
4657 preempt_enable();
4658
4659 if (ret != XDP_PASS)
John Fastabendd4455162017-07-17 09:26:45 -07004660 return NET_RX_DROP;
David S. Millerb5cdae32017-04-18 15:36:58 -04004661 }
4662
John Fastabendbbbe2112017-09-08 14:00:30 -07004663 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00004664#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004665 if (static_key_false(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07004666 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004667 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07004668
Eric Dumazet3b098e22010-05-15 23:57:10 -07004669 if (cpu >= 0) {
4670 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4671 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00004672 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07004673 }
Tom Herbertfec5e652010-04-16 16:01:27 -07004674 }
Tom Herbert1e94d722010-03-18 17:45:44 -07004675#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03004676 ret = __netif_receive_skb(skb);
4677 rcu_read_unlock();
4678 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004679}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004680
4681/**
4682 * netif_receive_skb - process receive buffer from network
4683 * @skb: buffer to process
4684 *
4685 * netif_receive_skb() is the main receive data processing function.
4686 * It always succeeds. The buffer may be dropped during processing
4687 * for congestion control or by the protocol layers.
4688 *
4689 * This function may only be called from softirq context and interrupts
4690 * should be enabled.
4691 *
4692 * Return values (usually ignored):
4693 * NET_RX_SUCCESS: no congestion
4694 * NET_RX_DROP: packet was dropped
4695 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05004696int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004697{
4698 trace_netif_receive_skb_entry(skb);
4699
4700 return netif_receive_skb_internal(skb);
4701}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05004702EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Eric Dumazet41852492016-08-26 12:50:39 -07004704DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004705
4706/* Network device is going away, flush any packets still pending */
4707static void flush_backlog(struct work_struct *work)
4708{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004709 struct sk_buff *skb, *tmp;
4710 struct softnet_data *sd;
4711
4712 local_bh_disable();
4713 sd = this_cpu_ptr(&softnet_data);
4714
4715 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004716 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07004717 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07004718 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004719 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004720 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004721 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004722 }
Changli Gao6e7676c2010-04-27 15:07:33 -07004723 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004724 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004725 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07004726
4727 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07004728 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07004729 __skb_unlink(skb, &sd->process_queue);
4730 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004731 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07004732 }
4733 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004734 local_bh_enable();
4735}
4736
Eric Dumazet41852492016-08-26 12:50:39 -07004737static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004738{
4739 unsigned int cpu;
4740
4741 get_online_cpus();
4742
Eric Dumazet41852492016-08-26 12:50:39 -07004743 for_each_online_cpu(cpu)
4744 queue_work_on(cpu, system_highpri_wq,
4745 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004746
4747 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07004748 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004749
4750 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004751}
4752
Herbert Xud565b0a2008-12-15 23:38:52 -08004753static int napi_gro_complete(struct sk_buff *skb)
4754{
Vlad Yasevich22061d82012-11-15 08:49:11 +00004755 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08004756 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004757 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08004758 int err = -ENOENT;
4759
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004760 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
4761
Herbert Xufc59f9a2009-04-14 15:11:06 -07004762 if (NAPI_GRO_CB(skb)->count == 1) {
4763 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004764 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07004765 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004766
4767 rcu_read_lock();
4768 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004769 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08004770 continue;
4771
Jerry Chu299603e82013-12-11 20:53:45 -08004772 err = ptype->callbacks.gro_complete(skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08004773 break;
4774 }
4775 rcu_read_unlock();
4776
4777 if (err) {
4778 WARN_ON(&ptype->list == head);
4779 kfree_skb(skb);
4780 return NET_RX_SUCCESS;
4781 }
4782
4783out:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004784 return netif_receive_skb_internal(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004785}
4786
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004787/* napi->gro_list contains packets ordered by age.
4788 * youngest packets at the head of it.
4789 * Complete skbs in reverse order to reduce latencies.
4790 */
4791void napi_gro_flush(struct napi_struct *napi, bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08004792{
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004793 struct sk_buff *skb, *prev = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08004794
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004795 /* scan list and build reverse chain */
4796 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
4797 skb->prev = prev;
4798 prev = skb;
Herbert Xud565b0a2008-12-15 23:38:52 -08004799 }
4800
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004801 for (skb = prev; skb; skb = prev) {
4802 skb->next = NULL;
4803
4804 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
4805 return;
4806
4807 prev = skb->prev;
4808 napi_gro_complete(skb);
4809 napi->gro_count--;
4810 }
4811
Herbert Xud565b0a2008-12-15 23:38:52 -08004812 napi->gro_list = NULL;
4813}
Eric Dumazet86cac582010-08-31 18:25:32 +00004814EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08004815
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004816static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
4817{
4818 struct sk_buff *p;
4819 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08004820 u32 hash = skb_get_hash_raw(skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004821
4822 for (p = napi->gro_list; p; p = p->next) {
4823 unsigned long diffs;
4824
Tom Herbert0b4cec82014-01-15 08:58:06 -08004825 NAPI_GRO_CB(p)->flush = 0;
4826
4827 if (hash != skb_get_hash_raw(p)) {
4828 NAPI_GRO_CB(p)->same_flow = 0;
4829 continue;
4830 }
4831
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004832 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
4833 diffs |= p->vlan_tci ^ skb->vlan_tci;
Jesse Grossce87fc62016-01-20 17:59:49 -08004834 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004835 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004836 if (maclen == ETH_HLEN)
4837 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07004838 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004839 else if (!diffs)
4840 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07004841 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004842 maclen);
4843 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004844 }
4845}
4846
Jerry Chu299603e82013-12-11 20:53:45 -08004847static void skb_gro_reset_offset(struct sk_buff *skb)
4848{
4849 const struct skb_shared_info *pinfo = skb_shinfo(skb);
4850 const skb_frag_t *frag0 = &pinfo->frags[0];
4851
4852 NAPI_GRO_CB(skb)->data_offset = 0;
4853 NAPI_GRO_CB(skb)->frag0 = NULL;
4854 NAPI_GRO_CB(skb)->frag0_len = 0;
4855
4856 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
4857 pinfo->nr_frags &&
4858 !PageHighMem(skb_frag_page(frag0))) {
4859 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08004860 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
4861 skb_frag_size(frag0),
4862 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08004863 }
4864}
4865
Eric Dumazeta50e2332014-03-29 21:28:21 -07004866static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
4867{
4868 struct skb_shared_info *pinfo = skb_shinfo(skb);
4869
4870 BUG_ON(skb->end - skb->tail < grow);
4871
4872 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
4873
4874 skb->data_len -= grow;
4875 skb->tail += grow;
4876
4877 pinfo->frags[0].page_offset += grow;
4878 skb_frag_size_sub(&pinfo->frags[0], grow);
4879
4880 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
4881 skb_frag_unref(skb, 0);
4882 memmove(pinfo->frags, pinfo->frags + 1,
4883 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
4884 }
4885}
4886
Rami Rosenbb728822012-11-28 21:55:25 +00004887static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08004888{
4889 struct sk_buff **pp = NULL;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004890 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08004891 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004892 struct list_head *head = &offload_base;
Herbert Xu0da2afd52008-12-26 14:57:42 -08004893 int same_flow;
Ben Hutchings5b252f02009-10-29 07:17:09 +00004894 enum gro_result ret;
Eric Dumazeta50e2332014-03-29 21:28:21 -07004895 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08004896
David S. Millerb5cdae32017-04-18 15:36:58 -04004897 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08004898 goto normal;
4899
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004900 gro_list_prepare(napi, skb);
4901
Herbert Xud565b0a2008-12-15 23:38:52 -08004902 rcu_read_lock();
4903 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004904 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08004905 continue;
4906
Herbert Xu86911732009-01-29 14:19:50 +00004907 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00004908 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004909 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08004910 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08004911 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07004912 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02004913 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07004914 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04004915 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08004916 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004917
Tom Herbert662880f2014-08-27 21:26:56 -07004918 /* Setup for GRO checksum validation */
4919 switch (skb->ip_summed) {
4920 case CHECKSUM_COMPLETE:
4921 NAPI_GRO_CB(skb)->csum = skb->csum;
4922 NAPI_GRO_CB(skb)->csum_valid = 1;
4923 NAPI_GRO_CB(skb)->csum_cnt = 0;
4924 break;
4925 case CHECKSUM_UNNECESSARY:
4926 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
4927 NAPI_GRO_CB(skb)->csum_valid = 0;
4928 break;
4929 default:
4930 NAPI_GRO_CB(skb)->csum_cnt = 0;
4931 NAPI_GRO_CB(skb)->csum_valid = 0;
4932 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004933
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004934 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004935 break;
4936 }
4937 rcu_read_unlock();
4938
4939 if (&ptype->list == head)
4940 goto normal;
4941
Steffen Klassert25393d32017-02-15 09:39:44 +01004942 if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
4943 ret = GRO_CONSUMED;
4944 goto ok;
4945 }
4946
Herbert Xu0da2afd52008-12-26 14:57:42 -08004947 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004948 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08004949
Herbert Xud565b0a2008-12-15 23:38:52 -08004950 if (pp) {
4951 struct sk_buff *nskb = *pp;
4952
4953 *pp = nskb->next;
4954 nskb->next = NULL;
4955 napi_gro_complete(nskb);
Herbert Xu4ae55442009-02-08 18:00:36 +00004956 napi->gro_count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08004957 }
4958
Herbert Xu0da2afd52008-12-26 14:57:42 -08004959 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08004960 goto ok;
4961
Eric Dumazet600adc12014-01-09 14:12:19 -08004962 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08004963 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08004964
Eric Dumazet600adc12014-01-09 14:12:19 -08004965 if (unlikely(napi->gro_count >= MAX_GRO_SKBS)) {
4966 struct sk_buff *nskb = napi->gro_list;
4967
4968 /* locate the end of the list to select the 'oldest' flow */
4969 while (nskb->next) {
4970 pp = &nskb->next;
4971 nskb = *pp;
4972 }
4973 *pp = NULL;
4974 nskb->next = NULL;
4975 napi_gro_complete(nskb);
4976 } else {
4977 napi->gro_count++;
4978 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004979 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004980 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07004981 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00004982 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004983 skb->next = napi->gro_list;
4984 napi->gro_list = skb;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004985 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08004986
Herbert Xuad0f9902009-02-01 01:24:55 -08004987pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07004988 grow = skb_gro_offset(skb) - skb_headlen(skb);
4989 if (grow > 0)
4990 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08004991ok:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004992 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08004993
4994normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08004995 ret = GRO_NORMAL;
4996 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08004997}
Herbert Xu96e93ea2009-01-06 10:49:34 -08004998
Jerry Chubf5a7552014-01-07 10:23:19 -08004999struct packet_offload *gro_find_receive_by_type(__be16 type)
5000{
5001 struct list_head *offload_head = &offload_base;
5002 struct packet_offload *ptype;
5003
5004 list_for_each_entry_rcu(ptype, offload_head, list) {
5005 if (ptype->type != type || !ptype->callbacks.gro_receive)
5006 continue;
5007 return ptype;
5008 }
5009 return NULL;
5010}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005011EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08005012
5013struct packet_offload *gro_find_complete_by_type(__be16 type)
5014{
5015 struct list_head *offload_head = &offload_base;
5016 struct packet_offload *ptype;
5017
5018 list_for_each_entry_rcu(ptype, offload_head, list) {
5019 if (ptype->type != type || !ptype->callbacks.gro_complete)
5020 continue;
5021 return ptype;
5022 }
5023 return NULL;
5024}
Or Gerlitze27a2f82014-01-20 13:59:20 +02005025EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005026
Michal Kubečeke44699d2017-06-29 11:13:36 +02005027static void napi_skb_free_stolen_head(struct sk_buff *skb)
5028{
5029 skb_dst_drop(skb);
5030 secpath_reset(skb);
5031 kmem_cache_free(skbuff_head_cache, skb);
5032}
5033
Rami Rosenbb728822012-11-28 21:55:25 +00005034static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08005035{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005036 switch (ret) {
5037 case GRO_NORMAL:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005038 if (netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005039 ret = GRO_DROP;
5040 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005041
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005042 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08005043 kfree_skb(skb);
5044 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005045
Eric Dumazetdaa86542012-04-19 07:07:40 +00005046 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02005047 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5048 napi_skb_free_stolen_head(skb);
5049 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00005050 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00005051 break;
5052
Ben Hutchings5b252f02009-10-29 07:17:09 +00005053 case GRO_HELD:
5054 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005055 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005056 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08005057 }
5058
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005059 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005060}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005061
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005062gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005063{
Eric Dumazet93f93a42015-11-18 06:30:59 -08005064 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005065 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00005066
Eric Dumazeta50e2332014-03-29 21:28:21 -07005067 skb_gro_reset_offset(skb);
5068
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005069 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08005070}
5071EXPORT_SYMBOL(napi_gro_receive);
5072
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00005073static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005074{
Eric Dumazet93a35f52014-10-23 06:30:30 -07005075 if (unlikely(skb->pfmemalloc)) {
5076 consume_skb(skb);
5077 return;
5078 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005079 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00005080 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5081 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Jesse Gross3701e512010-10-20 13:56:06 +00005082 skb->vlan_tci = 0;
Herbert Xu66c46d72011-01-29 20:44:54 -08005083 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08005084 skb->skb_iif = 0;
Jerry Chuc3caf112014-07-14 15:54:46 -07005085 skb->encapsulation = 0;
5086 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07005087 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Steffen Klassertf991bb92017-01-30 06:45:38 +01005088 secpath_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005089
5090 napi->skb = skb;
5091}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005092
Herbert Xu76620aa2009-04-16 02:02:07 -07005093struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005094{
Herbert Xu5d38a072009-01-04 16:13:40 -08005095 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005096
5097 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005098 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005099 if (skb) {
5100 napi->skb = skb;
5101 skb_mark_napi_id(skb, napi);
5102 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005103 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005104 return skb;
5105}
Herbert Xu76620aa2009-04-16 02:02:07 -07005106EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005107
Eric Dumazeta50e2332014-03-29 21:28:21 -07005108static gro_result_t napi_frags_finish(struct napi_struct *napi,
5109 struct sk_buff *skb,
5110 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005111{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005112 switch (ret) {
5113 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005114 case GRO_HELD:
5115 __skb_push(skb, ETH_HLEN);
5116 skb->protocol = eth_type_trans(skb, skb->dev);
5117 if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005118 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00005119 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005120
5121 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005122 napi_reuse_skb(napi, skb);
5123 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005124
Michal Kubečeke44699d2017-06-29 11:13:36 +02005125 case GRO_MERGED_FREE:
5126 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5127 napi_skb_free_stolen_head(skb);
5128 else
5129 napi_reuse_skb(napi, skb);
5130 break;
5131
Ben Hutchings5b252f02009-10-29 07:17:09 +00005132 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005133 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005134 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005135 }
5136
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005137 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005138}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005139
Eric Dumazeta50e2332014-03-29 21:28:21 -07005140/* Upper GRO stack assumes network header starts at gro_offset=0
5141 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5142 * We copy ethernet header into skb->data to have a common layout.
5143 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005144static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005145{
Herbert Xu76620aa2009-04-16 02:02:07 -07005146 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005147 const struct ethhdr *eth;
5148 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005149
5150 napi->skb = NULL;
5151
Eric Dumazeta50e2332014-03-29 21:28:21 -07005152 skb_reset_mac_header(skb);
5153 skb_gro_reset_offset(skb);
5154
5155 eth = skb_gro_header_fast(skb, 0);
5156 if (unlikely(skb_gro_header_hard(skb, hlen))) {
5157 eth = skb_gro_header_slow(skb, hlen, 0);
5158 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04005159 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
5160 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005161 napi_reuse_skb(napi, skb);
5162 return NULL;
5163 }
5164 } else {
5165 gro_pull_from_frag0(skb, hlen);
5166 NAPI_GRO_CB(skb)->frag0 += hlen;
5167 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07005168 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07005169 __skb_pull(skb, hlen);
5170
5171 /*
5172 * This works because the only protocols we care about don't require
5173 * special handling.
5174 * We'll fix it up properly in napi_frags_finish()
5175 */
5176 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07005177
Herbert Xu76620aa2009-04-16 02:02:07 -07005178 return skb;
5179}
Herbert Xu76620aa2009-04-16 02:02:07 -07005180
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005181gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07005182{
5183 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005184
5185 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005186 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08005187
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005188 trace_napi_gro_frags_entry(skb);
5189
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005190 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
Herbert Xu5d38a072009-01-04 16:13:40 -08005191}
5192EXPORT_SYMBOL(napi_gro_frags);
5193
Tom Herbert573e8fc2014-08-22 13:33:47 -07005194/* Compute the checksum from gro_offset and return the folded value
5195 * after adding in any pseudo checksum.
5196 */
5197__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
5198{
5199 __wsum wsum;
5200 __sum16 sum;
5201
5202 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
5203
5204 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
5205 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
5206 if (likely(!sum)) {
5207 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
5208 !skb->csum_complete_sw)
5209 netdev_rx_csum_fault(skb->dev);
5210 }
5211
5212 NAPI_GRO_CB(skb)->csum = wsum;
5213 NAPI_GRO_CB(skb)->csum_valid = 1;
5214
5215 return sum;
5216}
5217EXPORT_SYMBOL(__skb_gro_checksum_complete);
5218
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305219static void net_rps_send_ipi(struct softnet_data *remsd)
5220{
5221#ifdef CONFIG_RPS
5222 while (remsd) {
5223 struct softnet_data *next = remsd->rps_ipi_next;
5224
5225 if (cpu_online(remsd->cpu))
5226 smp_call_function_single_async(remsd->cpu, &remsd->csd);
5227 remsd = next;
5228 }
5229#endif
5230}
5231
Eric Dumazete326bed2010-04-22 00:22:45 -07005232/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08005233 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07005234 * Note: called with local irq disabled, but exits with local irq enabled.
5235 */
5236static void net_rps_action_and_irq_enable(struct softnet_data *sd)
5237{
5238#ifdef CONFIG_RPS
5239 struct softnet_data *remsd = sd->rps_ipi_list;
5240
5241 if (remsd) {
5242 sd->rps_ipi_list = NULL;
5243
5244 local_irq_enable();
5245
5246 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305247 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07005248 } else
5249#endif
5250 local_irq_enable();
5251}
5252
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005253static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5254{
5255#ifdef CONFIG_RPS
5256 return sd->rps_ipi_list != NULL;
5257#else
5258 return false;
5259#endif
5260}
5261
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005262static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005264 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005265 bool again = true;
5266 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
Eric Dumazete326bed2010-04-22 00:22:45 -07005268 /* Check if we have pending ipi, its better to send them now,
5269 * not waiting net_rx_action() end.
5270 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005271 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07005272 local_irq_disable();
5273 net_rps_action_and_irq_enable(sd);
5274 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005275
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01005276 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005277 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Changli Gao6e7676c2010-04-27 15:07:33 -07005280 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03005281 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07005282 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03005283 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00005284 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005285 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00005286 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005287
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005290 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005291 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07005292 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005293 /*
5294 * Inline a custom version of __napi_complete().
5295 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07005296 * and NAPI_STATE_SCHED is the only possible flag set
5297 * on backlog.
5298 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005299 * and we dont need an smp_mb() memory barrier.
5300 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005301 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005302 again = false;
5303 } else {
5304 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5305 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07005306 }
5307 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005308 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005311 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312}
5313
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005314/**
5315 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07005316 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005317 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005318 * The entry's receive function will be scheduled to run.
5319 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005320 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08005321void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005322{
5323 unsigned long flags;
5324
5325 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05005326 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005327 local_irq_restore(flags);
5328}
5329EXPORT_SYMBOL(__napi_schedule);
5330
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005331/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08005332 * napi_schedule_prep - check if napi can be scheduled
5333 * @n: napi context
5334 *
5335 * Test if NAPI routine is already running, and if not mark
5336 * it as running. This is used as a condition variable
5337 * insure only one NAPI poll instance runs. We also make
5338 * sure there is no pending NAPI disable.
5339 */
5340bool napi_schedule_prep(struct napi_struct *n)
5341{
5342 unsigned long val, new;
5343
5344 do {
5345 val = READ_ONCE(n->state);
5346 if (unlikely(val & NAPIF_STATE_DISABLE))
5347 return false;
5348 new = val | NAPIF_STATE_SCHED;
5349
5350 /* Sets STATE_MISSED bit if STATE_SCHED was already set
5351 * This was suggested by Alexander Duyck, as compiler
5352 * emits better code than :
5353 * if (val & NAPIF_STATE_SCHED)
5354 * new |= NAPIF_STATE_MISSED;
5355 */
5356 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5357 NAPIF_STATE_MISSED;
5358 } while (cmpxchg(&n->state, val, new) != val);
5359
5360 return !(val & NAPIF_STATE_SCHED);
5361}
5362EXPORT_SYMBOL(napi_schedule_prep);
5363
5364/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005365 * __napi_schedule_irqoff - schedule for receive
5366 * @n: entry to schedule
5367 *
5368 * Variant of __napi_schedule() assuming hard irqs are masked
5369 */
5370void __napi_schedule_irqoff(struct napi_struct *n)
5371{
5372 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5373}
5374EXPORT_SYMBOL(__napi_schedule_irqoff);
5375
Eric Dumazet364b6052016-11-15 10:15:13 -08005376bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08005377{
Eric Dumazet39e6c822017-02-28 10:34:50 -08005378 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08005379
5380 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08005381 * 1) Don't let napi dequeue from the cpu poll list
5382 * just in case its running on a different cpu.
5383 * 2) If we are busy polling, do nothing here, we have
5384 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08005385 */
Eric Dumazet217f6972016-11-15 10:15:11 -08005386 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
5387 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08005388 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08005389
Eric Dumazet3b47d302014-11-06 21:09:44 -08005390 if (n->gro_list) {
5391 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005392
Eric Dumazet3b47d302014-11-06 21:09:44 -08005393 if (work_done)
5394 timeout = n->dev->gro_flush_timeout;
5395
5396 if (timeout)
5397 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5398 HRTIMER_MODE_REL_PINNED);
5399 else
5400 napi_gro_flush(n, false);
5401 }
Eric Dumazet02c16022017-02-04 15:25:02 -08005402 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005403 /* If n->poll_list is not empty, we need to mask irqs */
5404 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08005405 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005406 local_irq_restore(flags);
5407 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08005408
5409 do {
5410 val = READ_ONCE(n->state);
5411
5412 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
5413
5414 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
5415
5416 /* If STATE_MISSED was set, leave STATE_SCHED set,
5417 * because we will call napi->poll() one more time.
5418 * This C code was suggested by Alexander Duyck to help gcc.
5419 */
5420 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
5421 NAPIF_STATE_SCHED;
5422 } while (cmpxchg(&n->state, val, new) != val);
5423
5424 if (unlikely(val & NAPIF_STATE_MISSED)) {
5425 __napi_schedule(n);
5426 return false;
5427 }
5428
Eric Dumazet364b6052016-11-15 10:15:13 -08005429 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08005430}
Eric Dumazet3b47d302014-11-06 21:09:44 -08005431EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08005432
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005433/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08005434static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005435{
5436 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
5437 struct napi_struct *napi;
5438
5439 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
5440 if (napi->napi_id == napi_id)
5441 return napi;
5442
5443 return NULL;
5444}
Eric Dumazet02d62e82015-11-18 06:30:52 -08005445
5446#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08005447
Eric Dumazetce6aea92015-11-18 06:30:54 -08005448#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08005449
5450static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
5451{
5452 int rc;
5453
Eric Dumazet39e6c822017-02-28 10:34:50 -08005454 /* Busy polling means there is a high chance device driver hard irq
5455 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
5456 * set in napi_schedule_prep().
5457 * Since we are about to call napi->poll() once more, we can safely
5458 * clear NAPI_STATE_MISSED.
5459 *
5460 * Note: x86 could use a single "lock and ..." instruction
5461 * to perform these two clear_bit()
5462 */
5463 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08005464 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
5465
5466 local_bh_disable();
5467
5468 /* All we really want here is to re-enable device interrupts.
5469 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
5470 */
5471 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02005472 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08005473 netpoll_poll_unlock(have_poll_lock);
5474 if (rc == BUSY_POLL_BUDGET)
5475 __napi_schedule(napi);
5476 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08005477}
5478
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005479void napi_busy_loop(unsigned int napi_id,
5480 bool (*loop_end)(void *, unsigned long),
5481 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08005482{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005483 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08005484 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08005485 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005486 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08005487
5488restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08005489 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005490
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005491 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005492
Alexander Duyck545cd5e2017-03-24 10:07:53 -07005493 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08005494 if (!napi)
5495 goto out;
5496
Eric Dumazet217f6972016-11-15 10:15:11 -08005497 preempt_disable();
5498 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005499 int work = 0;
5500
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005501 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08005502 if (!napi_poll) {
5503 unsigned long val = READ_ONCE(napi->state);
5504
5505 /* If multiple threads are competing for this napi,
5506 * we avoid dirtying napi->state as much as we can.
5507 */
5508 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
5509 NAPIF_STATE_IN_BUSY_POLL))
5510 goto count;
5511 if (cmpxchg(&napi->state, val,
5512 val | NAPIF_STATE_IN_BUSY_POLL |
5513 NAPIF_STATE_SCHED) != val)
5514 goto count;
5515 have_poll_lock = netpoll_poll_lock(napi);
5516 napi_poll = napi->poll;
5517 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005518 work = napi_poll(napi, BUSY_POLL_BUDGET);
5519 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08005520count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005521 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005522 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005523 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005524 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005525
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005526 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08005527 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005528
Eric Dumazet217f6972016-11-15 10:15:11 -08005529 if (unlikely(need_resched())) {
5530 if (napi_poll)
5531 busy_poll_stop(napi, have_poll_lock);
5532 preempt_enable();
5533 rcu_read_unlock();
5534 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005535 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005536 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08005537 goto restart;
5538 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08005539 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08005540 }
5541 if (napi_poll)
5542 busy_poll_stop(napi, have_poll_lock);
5543 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005544out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005545 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005546}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005547EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08005548
5549#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005550
Eric Dumazet149d6ad2016-11-08 11:07:28 -08005551static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005552{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08005553 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
5554 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005555 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005556
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005557 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005558
Alexander Duyck545cd5e2017-03-24 10:07:53 -07005559 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005560 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07005561 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
5562 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005563 } while (napi_by_id(napi_gen_id));
5564 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005565
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005566 hlist_add_head_rcu(&napi->napi_hash_node,
5567 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005568
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005569 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005570}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005571
5572/* Warning : caller is responsible to make sure rcu grace period
5573 * is respected before freeing memory containing @napi
5574 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08005575bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005576{
Eric Dumazet34cbe272015-11-18 06:31:02 -08005577 bool rcu_sync_needed = false;
5578
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005579 spin_lock(&napi_hash_lock);
5580
Eric Dumazet34cbe272015-11-18 06:31:02 -08005581 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
5582 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005583 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08005584 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005585 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08005586 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005587}
5588EXPORT_SYMBOL_GPL(napi_hash_del);
5589
Eric Dumazet3b47d302014-11-06 21:09:44 -08005590static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
5591{
5592 struct napi_struct *napi;
5593
5594 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08005595
5596 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
5597 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
5598 */
5599 if (napi->gro_list && !napi_disable_pending(napi) &&
5600 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
5601 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005602
5603 return HRTIMER_NORESTART;
5604}
5605
Herbert Xud565b0a2008-12-15 23:38:52 -08005606void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
5607 int (*poll)(struct napi_struct *, int), int weight)
5608{
5609 INIT_LIST_HEAD(&napi->poll_list);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005610 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
5611 napi->timer.function = napi_watchdog;
Herbert Xu4ae55442009-02-08 18:00:36 +00005612 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005613 napi->gro_list = NULL;
Herbert Xu5d38a072009-01-04 16:13:40 -08005614 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08005615 napi->poll = poll;
Eric Dumazet82dc3c63c2013-03-05 15:57:22 +00005616 if (weight > NAPI_POLL_WEIGHT)
5617 pr_err_once("netif_napi_add() called with weight %d on device %s\n",
5618 weight, dev->name);
Herbert Xud565b0a2008-12-15 23:38:52 -08005619 napi->weight = weight;
5620 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08005621 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08005622#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08005623 napi->poll_owner = -1;
5624#endif
5625 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08005626 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08005627}
5628EXPORT_SYMBOL(netif_napi_add);
5629
Eric Dumazet3b47d302014-11-06 21:09:44 -08005630void napi_disable(struct napi_struct *n)
5631{
5632 might_sleep();
5633 set_bit(NAPI_STATE_DISABLE, &n->state);
5634
5635 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
5636 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04005637 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
5638 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005639
5640 hrtimer_cancel(&n->timer);
5641
5642 clear_bit(NAPI_STATE_DISABLE, &n->state);
5643}
5644EXPORT_SYMBOL(napi_disable);
5645
Eric Dumazet93d05d42015-11-18 06:31:03 -08005646/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08005647void netif_napi_del(struct napi_struct *napi)
5648{
Eric Dumazet93d05d42015-11-18 06:31:03 -08005649 might_sleep();
5650 if (napi_hash_del(napi))
5651 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08005652 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07005653 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08005654
Eric Dumazet289dccb2013-12-20 14:29:08 -08005655 kfree_skb_list(napi->gro_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08005656 napi->gro_list = NULL;
Herbert Xu4ae55442009-02-08 18:00:36 +00005657 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005658}
5659EXPORT_SYMBOL(netif_napi_del);
5660
Herbert Xu726ce702014-12-21 07:16:21 +11005661static int napi_poll(struct napi_struct *n, struct list_head *repoll)
5662{
5663 void *have;
5664 int work, weight;
5665
5666 list_del_init(&n->poll_list);
5667
5668 have = netpoll_poll_lock(n);
5669
5670 weight = n->weight;
5671
5672 /* This NAPI_STATE_SCHED test is for avoiding a race
5673 * with netpoll's poll_napi(). Only the entity which
5674 * obtains the lock and sees NAPI_STATE_SCHED set will
5675 * actually make the ->poll() call. Therefore we avoid
5676 * accidentally calling ->poll() when NAPI is not scheduled.
5677 */
5678 work = 0;
5679 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
5680 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02005681 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11005682 }
5683
5684 WARN_ON_ONCE(work > weight);
5685
5686 if (likely(work < weight))
5687 goto out_unlock;
5688
5689 /* Drivers must not modify the NAPI state if they
5690 * consume the entire weight. In such cases this code
5691 * still "owns" the NAPI instance and therefore can
5692 * move the instance around on the list at-will.
5693 */
5694 if (unlikely(napi_disable_pending(n))) {
5695 napi_complete(n);
5696 goto out_unlock;
5697 }
5698
5699 if (n->gro_list) {
5700 /* flush too old packets
5701 * If HZ < 1000, flush all packets.
5702 */
5703 napi_gro_flush(n, HZ >= 1000);
5704 }
5705
Herbert Xu001ce542014-12-21 07:16:22 +11005706 /* Some drivers may have called napi_schedule
5707 * prior to exhausting their budget.
5708 */
5709 if (unlikely(!list_empty(&n->poll_list))) {
5710 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
5711 n->dev ? n->dev->name : "backlog");
5712 goto out_unlock;
5713 }
5714
Herbert Xu726ce702014-12-21 07:16:21 +11005715 list_add_tail(&n->poll_list, repoll);
5716
5717out_unlock:
5718 netpoll_poll_unlock(have);
5719
5720 return work;
5721}
5722
Emese Revfy0766f782016-06-20 20:42:34 +02005723static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Christoph Lameter903ceff2014-08-17 12:30:35 -05005725 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04005726 unsigned long time_limit = jiffies +
5727 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07005728 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005729 LIST_HEAD(list);
5730 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07005731
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005733 list_splice_init(&sd->poll_list, &list);
5734 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005736 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005737 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005739 if (list_empty(&list)) {
5740 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08005741 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005742 break;
5743 }
5744
Herbert Xu6bd373e2014-12-21 07:16:24 +11005745 n = list_first_entry(&list, struct napi_struct, poll_list);
5746 budget -= napi_poll(n, &repoll);
5747
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005748 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08005749 * Allow this to run for 2 jiffies since which will allow
5750 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005751 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005752 if (unlikely(budget <= 0 ||
5753 time_after_eq(jiffies, time_limit))) {
5754 sd->time_squeeze++;
5755 break;
5756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005758
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005759 local_irq_disable();
5760
5761 list_splice_tail_init(&sd->poll_list, &list);
5762 list_splice_tail(&repoll, &list);
5763 list_splice(&list, &sd->poll_list);
5764 if (!list_empty(&sd->poll_list))
5765 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
5766
Eric Dumazete326bed2010-04-22 00:22:45 -07005767 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08005768out:
5769 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770}
5771
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005772struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005773 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005774
5775 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005776 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005777
Veaceslav Falico5d261912013-08-28 23:25:05 +02005778 /* counter for the number of times this device was added to us */
5779 u16 ref_nr;
5780
Veaceslav Falico402dae92013-09-25 09:20:09 +02005781 /* private field for the users */
5782 void *private;
5783
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005784 struct list_head list;
5785 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005786};
5787
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005788static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005789 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005790{
Veaceslav Falico5d261912013-08-28 23:25:05 +02005791 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005792
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005793 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02005794 if (adj->dev == adj_dev)
5795 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005796 }
5797 return NULL;
5798}
5799
David Ahernf1170fd2016-10-17 19:15:51 -07005800static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
5801{
5802 struct net_device *dev = data;
5803
5804 return upper_dev == dev;
5805}
5806
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005807/**
5808 * netdev_has_upper_dev - Check if device is linked to an upper device
5809 * @dev: device
5810 * @upper_dev: upper device to check
5811 *
5812 * Find out if a device is linked to specified upper device and return true
5813 * in case it is. Note that this checks only immediate upper device,
5814 * not through a complete stack of devices. The caller must hold the RTNL lock.
5815 */
5816bool netdev_has_upper_dev(struct net_device *dev,
5817 struct net_device *upper_dev)
5818{
5819 ASSERT_RTNL();
5820
David Ahernf1170fd2016-10-17 19:15:51 -07005821 return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
5822 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005823}
5824EXPORT_SYMBOL(netdev_has_upper_dev);
5825
5826/**
David Ahern1a3f0602016-10-17 19:15:44 -07005827 * netdev_has_upper_dev_all - Check if device is linked to an upper device
5828 * @dev: device
5829 * @upper_dev: upper device to check
5830 *
5831 * Find out if a device is linked to specified upper device and return true
5832 * in case it is. Note that this checks the entire upper device chain.
5833 * The caller must hold rcu lock.
5834 */
5835
David Ahern1a3f0602016-10-17 19:15:44 -07005836bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
5837 struct net_device *upper_dev)
5838{
5839 return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
5840 upper_dev);
5841}
5842EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
5843
5844/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005845 * netdev_has_any_upper_dev - Check if device is linked to some device
5846 * @dev: device
5847 *
5848 * Find out if a device is linked to an upper device and return true in case
5849 * it is. The caller must hold the RTNL lock.
5850 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02005851bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005852{
5853 ASSERT_RTNL();
5854
David Ahernf1170fd2016-10-17 19:15:51 -07005855 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005856}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02005857EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005858
5859/**
5860 * netdev_master_upper_dev_get - Get master upper device
5861 * @dev: device
5862 *
5863 * Find a master upper device and return pointer to it or NULL in case
5864 * it's not there. The caller must hold the RTNL lock.
5865 */
5866struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
5867{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005868 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005869
5870 ASSERT_RTNL();
5871
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005872 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005873 return NULL;
5874
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005875 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005876 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005877 if (likely(upper->master))
5878 return upper->dev;
5879 return NULL;
5880}
5881EXPORT_SYMBOL(netdev_master_upper_dev_get);
5882
David Ahern0f524a82016-10-17 19:15:52 -07005883/**
5884 * netdev_has_any_lower_dev - Check if device is linked to some device
5885 * @dev: device
5886 *
5887 * Find out if a device is linked to a lower device and return true in case
5888 * it is. The caller must hold the RTNL lock.
5889 */
5890static bool netdev_has_any_lower_dev(struct net_device *dev)
5891{
5892 ASSERT_RTNL();
5893
5894 return !list_empty(&dev->adj_list.lower);
5895}
5896
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02005897void *netdev_adjacent_get_private(struct list_head *adj_list)
5898{
5899 struct netdev_adjacent *adj;
5900
5901 adj = list_entry(adj_list, struct netdev_adjacent, list);
5902
5903 return adj->private;
5904}
5905EXPORT_SYMBOL(netdev_adjacent_get_private);
5906
Veaceslav Falico31088a12013-09-25 09:20:12 +02005907/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04005908 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
5909 * @dev: device
5910 * @iter: list_head ** of the current position
5911 *
5912 * Gets the next device from the dev's upper list, starting from iter
5913 * position. The caller must hold RCU read lock.
5914 */
5915struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
5916 struct list_head **iter)
5917{
5918 struct netdev_adjacent *upper;
5919
5920 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5921
5922 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5923
5924 if (&upper->list == &dev->adj_list.upper)
5925 return NULL;
5926
5927 *iter = &upper->list;
5928
5929 return upper->dev;
5930}
5931EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
5932
David Ahern1a3f0602016-10-17 19:15:44 -07005933static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
5934 struct list_head **iter)
5935{
5936 struct netdev_adjacent *upper;
5937
5938 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5939
5940 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5941
5942 if (&upper->list == &dev->adj_list.upper)
5943 return NULL;
5944
5945 *iter = &upper->list;
5946
5947 return upper->dev;
5948}
5949
5950int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
5951 int (*fn)(struct net_device *dev,
5952 void *data),
5953 void *data)
5954{
5955 struct net_device *udev;
5956 struct list_head *iter;
5957 int ret;
5958
5959 for (iter = &dev->adj_list.upper,
5960 udev = netdev_next_upper_dev_rcu(dev, &iter);
5961 udev;
5962 udev = netdev_next_upper_dev_rcu(dev, &iter)) {
5963 /* first is the upper device itself */
5964 ret = fn(udev, data);
5965 if (ret)
5966 return ret;
5967
5968 /* then look at all of its upper devices */
5969 ret = netdev_walk_all_upper_dev_rcu(udev, fn, data);
5970 if (ret)
5971 return ret;
5972 }
5973
5974 return 0;
5975}
5976EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
5977
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005978/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02005979 * netdev_lower_get_next_private - Get the next ->private from the
5980 * lower neighbour list
5981 * @dev: device
5982 * @iter: list_head ** of the current position
5983 *
5984 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5985 * list, starting from iter position. The caller must hold either hold the
5986 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00005987 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02005988 */
5989void *netdev_lower_get_next_private(struct net_device *dev,
5990 struct list_head **iter)
5991{
5992 struct netdev_adjacent *lower;
5993
5994 lower = list_entry(*iter, struct netdev_adjacent, list);
5995
5996 if (&lower->list == &dev->adj_list.lower)
5997 return NULL;
5998
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02005999 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006000
6001 return lower->private;
6002}
6003EXPORT_SYMBOL(netdev_lower_get_next_private);
6004
6005/**
6006 * netdev_lower_get_next_private_rcu - Get the next ->private from the
6007 * lower neighbour list, RCU
6008 * variant
6009 * @dev: device
6010 * @iter: list_head ** of the current position
6011 *
6012 * Gets the next netdev_adjacent->private from the dev's lower neighbour
6013 * list, starting from iter position. The caller must hold RCU read lock.
6014 */
6015void *netdev_lower_get_next_private_rcu(struct net_device *dev,
6016 struct list_head **iter)
6017{
6018 struct netdev_adjacent *lower;
6019
6020 WARN_ON_ONCE(!rcu_read_lock_held());
6021
6022 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6023
6024 if (&lower->list == &dev->adj_list.lower)
6025 return NULL;
6026
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02006027 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02006028
6029 return lower->private;
6030}
6031EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
6032
6033/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006034 * netdev_lower_get_next - Get the next device from the lower neighbour
6035 * list
6036 * @dev: device
6037 * @iter: list_head ** of the current position
6038 *
6039 * Gets the next netdev_adjacent from the dev's lower neighbour
6040 * list, starting from iter position. The caller must hold RTNL lock or
6041 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00006042 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006043 */
6044void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
6045{
6046 struct netdev_adjacent *lower;
6047
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006048 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006049
6050 if (&lower->list == &dev->adj_list.lower)
6051 return NULL;
6052
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01006053 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006054
6055 return lower->dev;
6056}
6057EXPORT_SYMBOL(netdev_lower_get_next);
6058
David Ahern1a3f0602016-10-17 19:15:44 -07006059static struct net_device *netdev_next_lower_dev(struct net_device *dev,
6060 struct list_head **iter)
6061{
6062 struct netdev_adjacent *lower;
6063
David Ahern46b5ab12016-10-26 13:21:33 -07006064 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07006065
6066 if (&lower->list == &dev->adj_list.lower)
6067 return NULL;
6068
David Ahern46b5ab12016-10-26 13:21:33 -07006069 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07006070
6071 return lower->dev;
6072}
6073
6074int netdev_walk_all_lower_dev(struct net_device *dev,
6075 int (*fn)(struct net_device *dev,
6076 void *data),
6077 void *data)
6078{
6079 struct net_device *ldev;
6080 struct list_head *iter;
6081 int ret;
6082
6083 for (iter = &dev->adj_list.lower,
6084 ldev = netdev_next_lower_dev(dev, &iter);
6085 ldev;
6086 ldev = netdev_next_lower_dev(dev, &iter)) {
6087 /* first is the lower device itself */
6088 ret = fn(ldev, data);
6089 if (ret)
6090 return ret;
6091
6092 /* then look at all of its lower devices */
6093 ret = netdev_walk_all_lower_dev(ldev, fn, data);
6094 if (ret)
6095 return ret;
6096 }
6097
6098 return 0;
6099}
6100EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
6101
David Ahern1a3f0602016-10-17 19:15:44 -07006102static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
6103 struct list_head **iter)
6104{
6105 struct netdev_adjacent *lower;
6106
6107 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6108 if (&lower->list == &dev->adj_list.lower)
6109 return NULL;
6110
6111 *iter = &lower->list;
6112
6113 return lower->dev;
6114}
6115
6116int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
6117 int (*fn)(struct net_device *dev,
6118 void *data),
6119 void *data)
6120{
6121 struct net_device *ldev;
6122 struct list_head *iter;
6123 int ret;
6124
6125 for (iter = &dev->adj_list.lower,
6126 ldev = netdev_next_lower_dev_rcu(dev, &iter);
6127 ldev;
6128 ldev = netdev_next_lower_dev_rcu(dev, &iter)) {
6129 /* first is the lower device itself */
6130 ret = fn(ldev, data);
6131 if (ret)
6132 return ret;
6133
6134 /* then look at all of its lower devices */
6135 ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data);
6136 if (ret)
6137 return ret;
6138 }
6139
6140 return 0;
6141}
6142EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
6143
Jiri Pirko7ce856a2016-07-04 08:23:12 +02006144/**
dingtianhonge001bfa2013-12-13 10:19:55 +08006145 * netdev_lower_get_first_private_rcu - Get the first ->private from the
6146 * lower neighbour list, RCU
6147 * variant
6148 * @dev: device
6149 *
6150 * Gets the first netdev_adjacent->private from the dev's lower neighbour
6151 * list. The caller must hold RCU read lock.
6152 */
6153void *netdev_lower_get_first_private_rcu(struct net_device *dev)
6154{
6155 struct netdev_adjacent *lower;
6156
6157 lower = list_first_or_null_rcu(&dev->adj_list.lower,
6158 struct netdev_adjacent, list);
6159 if (lower)
6160 return lower->private;
6161 return NULL;
6162}
6163EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
6164
6165/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006166 * netdev_master_upper_dev_get_rcu - Get master upper device
6167 * @dev: device
6168 *
6169 * Find a master upper device and return pointer to it or NULL in case
6170 * it's not there. The caller must hold the RCU read lock.
6171 */
6172struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
6173{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006174 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006175
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006176 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006177 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006178 if (upper && likely(upper->master))
6179 return upper->dev;
6180 return NULL;
6181}
6182EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
6183
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306184static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006185 struct net_device *adj_dev,
6186 struct list_head *dev_list)
6187{
6188 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006189
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006190 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6191 "upper_%s" : "lower_%s", adj_dev->name);
6192 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
6193 linkname);
6194}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306195static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006196 char *name,
6197 struct list_head *dev_list)
6198{
6199 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006200
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006201 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6202 "upper_%s" : "lower_%s", name);
6203 sysfs_remove_link(&(dev->dev.kobj), linkname);
6204}
6205
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006206static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
6207 struct net_device *adj_dev,
6208 struct list_head *dev_list)
6209{
6210 return (dev_list == &dev->adj_list.upper ||
6211 dev_list == &dev->adj_list.lower) &&
6212 net_eq(dev_net(dev), dev_net(adj_dev));
6213}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006214
Veaceslav Falico5d261912013-08-28 23:25:05 +02006215static int __netdev_adjacent_dev_insert(struct net_device *dev,
6216 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02006217 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006218 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006219{
6220 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006221 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006222
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006223 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006224
6225 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07006226 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07006227 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
6228 dev->name, adj_dev->name, adj->ref_nr);
6229
Veaceslav Falico5d261912013-08-28 23:25:05 +02006230 return 0;
6231 }
6232
6233 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
6234 if (!adj)
6235 return -ENOMEM;
6236
6237 adj->dev = adj_dev;
6238 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07006239 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006240 adj->private = private;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006241 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006242
David Ahern67b62f92016-10-17 19:15:53 -07006243 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
6244 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006245
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006246 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006247 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006248 if (ret)
6249 goto free_adj;
6250 }
6251
Veaceslav Falico7863c052013-09-25 09:20:06 +02006252 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006253 if (master) {
6254 ret = sysfs_create_link(&(dev->dev.kobj),
6255 &(adj_dev->dev.kobj), "master");
6256 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02006257 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006258
Veaceslav Falico7863c052013-09-25 09:20:06 +02006259 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006260 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02006261 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006262 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006263
6264 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006265
Veaceslav Falico5831d662013-09-25 09:20:32 +02006266remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006267 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006268 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006269free_adj:
6270 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02006271 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006272
6273 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006274}
6275
stephen hemminger1d143d92013-12-29 14:01:29 -08006276static void __netdev_adjacent_dev_remove(struct net_device *dev,
6277 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006278 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006279 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006280{
6281 struct netdev_adjacent *adj;
6282
David Ahern67b62f92016-10-17 19:15:53 -07006283 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
6284 dev->name, adj_dev->name, ref_nr);
6285
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006286 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006287
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006288 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07006289 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006290 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07006291 WARN_ON(1);
6292 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006293 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006294
Andrew Collins93409032016-10-03 13:43:02 -06006295 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07006296 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
6297 dev->name, adj_dev->name, ref_nr,
6298 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06006299 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006300 return;
6301 }
6302
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006303 if (adj->master)
6304 sysfs_remove_link(&(dev->dev.kobj), "master");
6305
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006306 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006307 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006308
Veaceslav Falico5d261912013-08-28 23:25:05 +02006309 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07006310 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006311 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006312 dev_put(adj_dev);
6313 kfree_rcu(adj, rcu);
6314}
6315
stephen hemminger1d143d92013-12-29 14:01:29 -08006316static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
6317 struct net_device *upper_dev,
6318 struct list_head *up_list,
6319 struct list_head *down_list,
6320 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006321{
6322 int ret;
6323
David Ahern790510d2016-10-17 19:15:43 -07006324 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06006325 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006326 if (ret)
6327 return ret;
6328
David Ahern790510d2016-10-17 19:15:43 -07006329 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06006330 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006331 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07006332 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006333 return ret;
6334 }
6335
6336 return 0;
6337}
6338
stephen hemminger1d143d92013-12-29 14:01:29 -08006339static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
6340 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006341 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006342 struct list_head *up_list,
6343 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006344{
Andrew Collins93409032016-10-03 13:43:02 -06006345 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
6346 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006347}
6348
stephen hemminger1d143d92013-12-29 14:01:29 -08006349static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
6350 struct net_device *upper_dev,
6351 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006352{
David Ahernf1170fd2016-10-17 19:15:51 -07006353 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
6354 &dev->adj_list.upper,
6355 &upper_dev->adj_list.lower,
6356 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006357}
6358
stephen hemminger1d143d92013-12-29 14:01:29 -08006359static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
6360 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006361{
Andrew Collins93409032016-10-03 13:43:02 -06006362 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006363 &dev->adj_list.upper,
6364 &upper_dev->adj_list.lower);
6365}
Veaceslav Falico5d261912013-08-28 23:25:05 +02006366
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006367static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006368 struct net_device *upper_dev, bool master,
David Ahern42ab19e2017-10-04 17:48:47 -07006369 void *upper_priv, void *upper_info,
6370 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006371{
David Ahern51d0c0472017-10-04 17:48:45 -07006372 struct netdev_notifier_changeupper_info changeupper_info = {
6373 .info = {
6374 .dev = dev,
David Ahern42ab19e2017-10-04 17:48:47 -07006375 .extack = extack,
David Ahern51d0c0472017-10-04 17:48:45 -07006376 },
6377 .upper_dev = upper_dev,
6378 .master = master,
6379 .linking = true,
6380 .upper_info = upper_info,
6381 };
Veaceslav Falico5d261912013-08-28 23:25:05 +02006382 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006383
6384 ASSERT_RTNL();
6385
6386 if (dev == upper_dev)
6387 return -EBUSY;
6388
6389 /* To prevent loops, check if dev is not upper device to upper_dev. */
David Ahernf1170fd2016-10-17 19:15:51 -07006390 if (netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006391 return -EBUSY;
6392
David Ahernf1170fd2016-10-17 19:15:51 -07006393 if (netdev_has_upper_dev(dev, upper_dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006394 return -EEXIST;
6395
6396 if (master && netdev_master_upper_dev_get(dev))
6397 return -EBUSY;
6398
David Ahern51d0c0472017-10-04 17:48:45 -07006399 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02006400 &changeupper_info.info);
6401 ret = notifier_to_errno(ret);
6402 if (ret)
6403 return ret;
6404
Jiri Pirko6dffb042015-12-03 12:12:10 +01006405 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006406 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006407 if (ret)
6408 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006409
David Ahern51d0c0472017-10-04 17:48:45 -07006410 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Ido Schimmelb03804e2015-12-03 12:12:03 +01006411 &changeupper_info.info);
6412 ret = notifier_to_errno(ret);
6413 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07006414 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01006415
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006416 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006417
David Ahernf1170fd2016-10-17 19:15:51 -07006418rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006419 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006420
6421 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006422}
6423
6424/**
6425 * netdev_upper_dev_link - Add a link to the upper device
6426 * @dev: device
6427 * @upper_dev: new upper device
6428 *
6429 * Adds a link to device which is upper to this one. The caller must hold
6430 * the RTNL lock. On a failure a negative errno code is returned.
6431 * On success the reference counts are adjusted and the function
6432 * returns zero.
6433 */
6434int netdev_upper_dev_link(struct net_device *dev,
David Ahern42ab19e2017-10-04 17:48:47 -07006435 struct net_device *upper_dev,
6436 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006437{
David Ahern42ab19e2017-10-04 17:48:47 -07006438 return __netdev_upper_dev_link(dev, upper_dev, false,
6439 NULL, NULL, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006440}
6441EXPORT_SYMBOL(netdev_upper_dev_link);
6442
6443/**
6444 * netdev_master_upper_dev_link - Add a master link to the upper device
6445 * @dev: device
6446 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01006447 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006448 * @upper_info: upper info to be passed down via notifier
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006449 *
6450 * Adds a link to device which is upper to this one. In this case, only
6451 * one master upper device can be linked, although other non-master devices
6452 * might be linked as well. The caller must hold the RTNL lock.
6453 * On a failure a negative errno code is returned. On success the reference
6454 * counts are adjusted and the function returns zero.
6455 */
6456int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01006457 struct net_device *upper_dev,
David Ahern42ab19e2017-10-04 17:48:47 -07006458 void *upper_priv, void *upper_info,
6459 struct netlink_ext_ack *extack)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006460{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006461 return __netdev_upper_dev_link(dev, upper_dev, true,
David Ahern42ab19e2017-10-04 17:48:47 -07006462 upper_priv, upper_info, extack);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006463}
6464EXPORT_SYMBOL(netdev_master_upper_dev_link);
6465
6466/**
6467 * netdev_upper_dev_unlink - Removes a link to upper device
6468 * @dev: device
6469 * @upper_dev: new upper device
6470 *
6471 * Removes a link to device which is upper to this one. The caller must hold
6472 * the RTNL lock.
6473 */
6474void netdev_upper_dev_unlink(struct net_device *dev,
6475 struct net_device *upper_dev)
6476{
David Ahern51d0c0472017-10-04 17:48:45 -07006477 struct netdev_notifier_changeupper_info changeupper_info = {
6478 .info = {
6479 .dev = dev,
6480 },
6481 .upper_dev = upper_dev,
6482 .linking = false,
6483 };
tchardingf4563a72017-02-09 17:56:07 +11006484
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006485 ASSERT_RTNL();
6486
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006487 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006488
David Ahern51d0c0472017-10-04 17:48:45 -07006489 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
Jiri Pirko573c7ba2015-10-16 14:01:22 +02006490 &changeupper_info.info);
6491
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006492 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006493
David Ahern51d0c0472017-10-04 17:48:45 -07006494 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006495 &changeupper_info.info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006496}
6497EXPORT_SYMBOL(netdev_upper_dev_unlink);
6498
Moni Shoua61bd3852015-02-03 16:48:29 +02006499/**
6500 * netdev_bonding_info_change - Dispatch event about slave change
6501 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09006502 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02006503 *
6504 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
6505 * The caller must hold the RTNL lock.
6506 */
6507void netdev_bonding_info_change(struct net_device *dev,
6508 struct netdev_bonding_info *bonding_info)
6509{
David Ahern51d0c0472017-10-04 17:48:45 -07006510 struct netdev_notifier_bonding_info info = {
6511 .info.dev = dev,
6512 };
Moni Shoua61bd3852015-02-03 16:48:29 +02006513
6514 memcpy(&info.bonding_info, bonding_info,
6515 sizeof(struct netdev_bonding_info));
David Ahern51d0c0472017-10-04 17:48:45 -07006516 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
Moni Shoua61bd3852015-02-03 16:48:29 +02006517 &info.info);
6518}
6519EXPORT_SYMBOL(netdev_bonding_info_change);
6520
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08006521static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006522{
6523 struct netdev_adjacent *iter;
6524
6525 struct net *net = dev_net(dev);
6526
6527 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006528 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006529 continue;
6530 netdev_adjacent_sysfs_add(iter->dev, dev,
6531 &iter->dev->adj_list.lower);
6532 netdev_adjacent_sysfs_add(dev, iter->dev,
6533 &dev->adj_list.upper);
6534 }
6535
6536 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006537 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006538 continue;
6539 netdev_adjacent_sysfs_add(iter->dev, dev,
6540 &iter->dev->adj_list.upper);
6541 netdev_adjacent_sysfs_add(dev, iter->dev,
6542 &dev->adj_list.lower);
6543 }
6544}
6545
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08006546static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006547{
6548 struct netdev_adjacent *iter;
6549
6550 struct net *net = dev_net(dev);
6551
6552 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006553 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006554 continue;
6555 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6556 &iter->dev->adj_list.lower);
6557 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6558 &dev->adj_list.upper);
6559 }
6560
6561 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006562 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006563 continue;
6564 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6565 &iter->dev->adj_list.upper);
6566 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6567 &dev->adj_list.lower);
6568 }
6569}
6570
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006571void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02006572{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006573 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006574
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006575 struct net *net = dev_net(dev);
6576
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006577 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006578 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006579 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006580 netdev_adjacent_sysfs_del(iter->dev, oldname,
6581 &iter->dev->adj_list.lower);
6582 netdev_adjacent_sysfs_add(iter->dev, dev,
6583 &iter->dev->adj_list.lower);
6584 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02006585
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006586 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006587 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006588 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006589 netdev_adjacent_sysfs_del(iter->dev, oldname,
6590 &iter->dev->adj_list.upper);
6591 netdev_adjacent_sysfs_add(iter->dev, dev,
6592 &iter->dev->adj_list.upper);
6593 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02006594}
Veaceslav Falico402dae92013-09-25 09:20:09 +02006595
6596void *netdev_lower_dev_get_private(struct net_device *dev,
6597 struct net_device *lower_dev)
6598{
6599 struct netdev_adjacent *lower;
6600
6601 if (!lower_dev)
6602 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006603 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02006604 if (!lower)
6605 return NULL;
6606
6607 return lower->private;
6608}
6609EXPORT_SYMBOL(netdev_lower_dev_get_private);
6610
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006611
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006612int dev_get_nest_level(struct net_device *dev)
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006613{
6614 struct net_device *lower = NULL;
6615 struct list_head *iter;
6616 int max_nest = -1;
6617 int nest;
6618
6619 ASSERT_RTNL();
6620
6621 netdev_for_each_lower_dev(dev, lower, iter) {
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006622 nest = dev_get_nest_level(lower);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006623 if (max_nest < nest)
6624 max_nest = nest;
6625 }
6626
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006627 return max_nest + 1;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006628}
6629EXPORT_SYMBOL(dev_get_nest_level);
6630
Jiri Pirko04d48262015-12-03 12:12:15 +01006631/**
6632 * netdev_lower_change - Dispatch event about lower device state change
6633 * @lower_dev: device
6634 * @lower_state_info: state to dispatch
6635 *
6636 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
6637 * The caller must hold the RTNL lock.
6638 */
6639void netdev_lower_state_changed(struct net_device *lower_dev,
6640 void *lower_state_info)
6641{
David Ahern51d0c0472017-10-04 17:48:45 -07006642 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
6643 .info.dev = lower_dev,
6644 };
Jiri Pirko04d48262015-12-03 12:12:15 +01006645
6646 ASSERT_RTNL();
6647 changelowerstate_info.lower_state_info = lower_state_info;
David Ahern51d0c0472017-10-04 17:48:45 -07006648 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
Jiri Pirko04d48262015-12-03 12:12:15 +01006649 &changelowerstate_info.info);
6650}
6651EXPORT_SYMBOL(netdev_lower_state_changed);
6652
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006653static void dev_change_rx_flags(struct net_device *dev, int flags)
6654{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006655 const struct net_device_ops *ops = dev->netdev_ops;
6656
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05006657 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006658 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006659}
6660
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006661static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07006662{
Eric Dumazetb536db92011-11-30 21:42:26 +00006663 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06006664 kuid_t uid;
6665 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07006666
Patrick McHardy24023452007-07-14 18:51:31 -07006667 ASSERT_RTNL();
6668
Wang Chendad9b332008-06-18 01:48:28 -07006669 dev->flags |= IFF_PROMISC;
6670 dev->promiscuity += inc;
6671 if (dev->promiscuity == 0) {
6672 /*
6673 * Avoid overflow.
6674 * If inc causes overflow, untouch promisc and return error.
6675 */
6676 if (inc < 0)
6677 dev->flags &= ~IFF_PROMISC;
6678 else {
6679 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006680 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
6681 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07006682 return -EOVERFLOW;
6683 }
6684 }
Patrick McHardy4417da62007-06-27 01:28:10 -07006685 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006686 pr_info("device %s %s promiscuous mode\n",
6687 dev->name,
6688 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11006689 if (audit_enabled) {
6690 current_uid_gid(&uid, &gid);
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05006691 audit_log(current->audit_context, GFP_ATOMIC,
6692 AUDIT_ANOM_PROMISCUOUS,
6693 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
6694 dev->name, (dev->flags & IFF_PROMISC),
6695 (old_flags & IFF_PROMISC),
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07006696 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06006697 from_kuid(&init_user_ns, uid),
6698 from_kgid(&init_user_ns, gid),
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05006699 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11006700 }
Patrick McHardy24023452007-07-14 18:51:31 -07006701
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006702 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07006703 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006704 if (notify)
6705 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07006706 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07006707}
6708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709/**
6710 * dev_set_promiscuity - update promiscuity count on a device
6711 * @dev: device
6712 * @inc: modifier
6713 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07006714 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 * remains above zero the interface remains promiscuous. Once it hits zero
6716 * the device reverts back to normal filtering operation. A negative inc
6717 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07006718 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 */
Wang Chendad9b332008-06-18 01:48:28 -07006720int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721{
Eric Dumazetb536db92011-11-30 21:42:26 +00006722 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07006723 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006725 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07006726 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07006727 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07006728 if (dev->flags != old_flags)
6729 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07006730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006732EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006734static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006736 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
Patrick McHardy24023452007-07-14 18:51:31 -07006738 ASSERT_RTNL();
6739
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07006741 dev->allmulti += inc;
6742 if (dev->allmulti == 0) {
6743 /*
6744 * Avoid overflow.
6745 * If inc causes overflow, untouch allmulti and return error.
6746 */
6747 if (inc < 0)
6748 dev->flags &= ~IFF_ALLMULTI;
6749 else {
6750 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006751 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
6752 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07006753 return -EOVERFLOW;
6754 }
6755 }
Patrick McHardy24023452007-07-14 18:51:31 -07006756 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006757 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07006758 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006759 if (notify)
6760 __dev_notify_flags(dev, old_flags,
6761 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07006762 }
Wang Chendad9b332008-06-18 01:48:28 -07006763 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07006764}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006765
6766/**
6767 * dev_set_allmulti - update allmulti count on a device
6768 * @dev: device
6769 * @inc: modifier
6770 *
6771 * Add or remove reception of all multicast frames to a device. While the
6772 * count in the device remains above zero the interface remains listening
6773 * to all interfaces. Once it hits zero the device reverts back to normal
6774 * filtering operation. A negative @inc value is used to drop the counter
6775 * when releasing a resource needing all multicasts.
6776 * Return 0 if successful or a negative errno code on error.
6777 */
6778
6779int dev_set_allmulti(struct net_device *dev, int inc)
6780{
6781 return __dev_set_allmulti(dev, inc, true);
6782}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006783EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07006784
6785/*
6786 * Upload unicast and multicast address lists to device and
6787 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08006788 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07006789 * are present.
6790 */
6791void __dev_set_rx_mode(struct net_device *dev)
6792{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006793 const struct net_device_ops *ops = dev->netdev_ops;
6794
Patrick McHardy4417da62007-06-27 01:28:10 -07006795 /* dev_open will call this function so the list will stay sane. */
6796 if (!(dev->flags&IFF_UP))
6797 return;
6798
6799 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09006800 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07006801
Jiri Pirko01789342011-08-16 06:29:00 +00006802 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07006803 /* Unicast addresses changes may only happen under the rtnl,
6804 * therefore calling __dev_set_promiscuity here is safe.
6805 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08006806 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006807 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07006808 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08006809 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006810 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07006811 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07006812 }
Patrick McHardy4417da62007-06-27 01:28:10 -07006813 }
Jiri Pirko01789342011-08-16 06:29:00 +00006814
6815 if (ops->ndo_set_rx_mode)
6816 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07006817}
6818
6819void dev_set_rx_mode(struct net_device *dev)
6820{
David S. Millerb9e40852008-07-15 00:15:08 -07006821 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07006822 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07006823 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824}
6825
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006826/**
6827 * dev_get_flags - get flags reported to userspace
6828 * @dev: device
6829 *
6830 * Get the combination of flag bits exported through APIs to userspace.
6831 */
Eric Dumazet95c96172012-04-15 05:58:06 +00006832unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833{
Eric Dumazet95c96172012-04-15 05:58:06 +00006834 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
6836 flags = (dev->flags & ~(IFF_PROMISC |
6837 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08006838 IFF_RUNNING |
6839 IFF_LOWER_UP |
6840 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 (dev->gflags & (IFF_PROMISC |
6842 IFF_ALLMULTI));
6843
Stefan Rompfb00055a2006-03-20 17:09:11 -08006844 if (netif_running(dev)) {
6845 if (netif_oper_up(dev))
6846 flags |= IFF_RUNNING;
6847 if (netif_carrier_ok(dev))
6848 flags |= IFF_LOWER_UP;
6849 if (netif_dormant(dev))
6850 flags |= IFF_DORMANT;
6851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 return flags;
6854}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006855EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
Patrick McHardybd380812010-02-26 06:34:53 +00006857int __dev_change_flags(struct net_device *dev, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858{
Eric Dumazetb536db92011-11-30 21:42:26 +00006859 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00006860 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Patrick McHardy24023452007-07-14 18:51:31 -07006862 ASSERT_RTNL();
6863
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 /*
6865 * Set the flags on our device.
6866 */
6867
6868 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
6869 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
6870 IFF_AUTOMEDIA)) |
6871 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
6872 IFF_ALLMULTI));
6873
6874 /*
6875 * Load in the correct multicast list now the flags have changed.
6876 */
6877
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006878 if ((old_flags ^ flags) & IFF_MULTICAST)
6879 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07006880
Patrick McHardy4417da62007-06-27 01:28:10 -07006881 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
6883 /*
6884 * Have we downed the interface. We handle IFF_UP ourselves
6885 * according to user attempts to set it, rather than blindly
6886 * setting it.
6887 */
6888
6889 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07006890 if ((old_flags ^ flags) & IFF_UP) {
6891 if (old_flags & IFF_UP)
6892 __dev_close(dev);
6893 else
6894 ret = __dev_open(dev);
6895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006898 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006899 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006900
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006902
6903 if (__dev_set_promiscuity(dev, inc, false) >= 0)
6904 if (dev->flags != old_flags)
6905 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 }
6907
6908 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11006909 * is important. Some (broken) drivers set IFF_PROMISC, when
6910 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 */
6912 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006913 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
6914
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006916 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 }
6918
Patrick McHardybd380812010-02-26 06:34:53 +00006919 return ret;
6920}
6921
Nicolas Dichtela528c212013-09-25 12:02:44 +02006922void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
6923 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00006924{
6925 unsigned int changes = dev->flags ^ old_flags;
6926
Nicolas Dichtela528c212013-09-25 12:02:44 +02006927 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07006928 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02006929
Patrick McHardybd380812010-02-26 06:34:53 +00006930 if (changes & IFF_UP) {
6931 if (dev->flags & IFF_UP)
6932 call_netdevice_notifiers(NETDEV_UP, dev);
6933 else
6934 call_netdevice_notifiers(NETDEV_DOWN, dev);
6935 }
6936
6937 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00006938 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
David Ahern51d0c0472017-10-04 17:48:45 -07006939 struct netdev_notifier_change_info change_info = {
6940 .info = {
6941 .dev = dev,
6942 },
6943 .flags_changed = changes,
6944 };
Jiri Pirkobe9efd32013-05-28 01:30:22 +00006945
David Ahern51d0c0472017-10-04 17:48:45 -07006946 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
Jiri Pirkobe9efd32013-05-28 01:30:22 +00006947 }
Patrick McHardybd380812010-02-26 06:34:53 +00006948}
6949
6950/**
6951 * dev_change_flags - change device settings
6952 * @dev: device
6953 * @flags: device state flags
6954 *
6955 * Change settings on device based state flags. The flags are
6956 * in the userspace exported format.
6957 */
Eric Dumazetb536db92011-11-30 21:42:26 +00006958int dev_change_flags(struct net_device *dev, unsigned int flags)
Patrick McHardybd380812010-02-26 06:34:53 +00006959{
Eric Dumazetb536db92011-11-30 21:42:26 +00006960 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006961 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00006962
6963 ret = __dev_change_flags(dev, flags);
6964 if (ret < 0)
6965 return ret;
6966
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006967 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02006968 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 return ret;
6970}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006971EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
WANG Congf51048c2017-07-06 15:01:57 -07006973int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006974{
6975 const struct net_device_ops *ops = dev->netdev_ops;
6976
6977 if (ops->ndo_change_mtu)
6978 return ops->ndo_change_mtu(dev, new_mtu);
6979
6980 dev->mtu = new_mtu;
6981 return 0;
6982}
WANG Congf51048c2017-07-06 15:01:57 -07006983EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006984
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006985/**
6986 * dev_set_mtu - Change maximum transfer unit
6987 * @dev: device
6988 * @new_mtu: new transfer unit
6989 *
6990 * Change the maximum transfer size of the network device.
6991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992int dev_set_mtu(struct net_device *dev, int new_mtu)
6993{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006994 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
6996 if (new_mtu == dev->mtu)
6997 return 0;
6998
Jarod Wilson61e84622016-10-07 22:04:33 -04006999 /* MTU must be positive, and in range */
7000 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
7001 net_err_ratelimited("%s: Invalid MTU %d requested, hw min %d\n",
7002 dev->name, new_mtu, dev->min_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 return -EINVAL;
Jarod Wilson61e84622016-10-07 22:04:33 -04007004 }
7005
7006 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
7007 net_err_ratelimited("%s: Invalid MTU %d requested, hw max %d\n",
Jakub Kicinskia0e65de2016-10-17 18:02:22 +01007008 dev->name, new_mtu, dev->max_mtu);
Jarod Wilson61e84622016-10-07 22:04:33 -04007009 return -EINVAL;
7010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011
7012 if (!netif_device_present(dev))
7013 return -ENODEV;
7014
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01007015 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
7016 err = notifier_to_errno(err);
7017 if (err)
7018 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007019
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007020 orig_mtu = dev->mtu;
7021 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
Veaceslav Falico2315dc92014-01-10 16:56:25 +01007023 if (!err) {
7024 err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
7025 err = notifier_to_errno(err);
7026 if (err) {
7027 /* setting mtu back and notifying everyone again,
7028 * so that they have a chance to revert changes.
7029 */
7030 __dev_set_mtu(dev, orig_mtu);
7031 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
7032 }
7033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 return err;
7035}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007036EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007038/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00007039 * dev_set_group - Change group this device belongs to
7040 * @dev: device
7041 * @new_group: group this device should belong to
7042 */
7043void dev_set_group(struct net_device *dev, int new_group)
7044{
7045 dev->group = new_group;
7046}
7047EXPORT_SYMBOL(dev_set_group);
7048
7049/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007050 * dev_set_mac_address - Change Media Access Control Address
7051 * @dev: device
7052 * @sa: new address
7053 *
7054 * Change the hardware (MAC) address of the device
7055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
7057{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007058 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 int err;
7060
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007061 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 return -EOPNOTSUPP;
7063 if (sa->sa_family != dev->type)
7064 return -EINVAL;
7065 if (!netif_device_present(dev))
7066 return -ENODEV;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007067 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00007068 if (err)
7069 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00007070 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00007071 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04007072 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00007073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007075EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
Jiri Pirko4bf84c32012-12-27 23:49:37 +00007077/**
7078 * dev_change_carrier - Change device carrier
7079 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00007080 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00007081 *
7082 * Change device carrier
7083 */
7084int dev_change_carrier(struct net_device *dev, bool new_carrier)
7085{
7086 const struct net_device_ops *ops = dev->netdev_ops;
7087
7088 if (!ops->ndo_change_carrier)
7089 return -EOPNOTSUPP;
7090 if (!netif_device_present(dev))
7091 return -ENODEV;
7092 return ops->ndo_change_carrier(dev, new_carrier);
7093}
7094EXPORT_SYMBOL(dev_change_carrier);
7095
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02007097 * dev_get_phys_port_id - Get device physical port ID
7098 * @dev: device
7099 * @ppid: port ID
7100 *
7101 * Get device physical port ID
7102 */
7103int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01007104 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02007105{
7106 const struct net_device_ops *ops = dev->netdev_ops;
7107
7108 if (!ops->ndo_get_phys_port_id)
7109 return -EOPNOTSUPP;
7110 return ops->ndo_get_phys_port_id(dev, ppid);
7111}
7112EXPORT_SYMBOL(dev_get_phys_port_id);
7113
7114/**
David Aherndb24a902015-03-17 20:23:15 -06007115 * dev_get_phys_port_name - Get device physical port name
7116 * @dev: device
7117 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00007118 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06007119 *
7120 * Get device physical port name
7121 */
7122int dev_get_phys_port_name(struct net_device *dev,
7123 char *name, size_t len)
7124{
7125 const struct net_device_ops *ops = dev->netdev_ops;
7126
7127 if (!ops->ndo_get_phys_port_name)
7128 return -EOPNOTSUPP;
7129 return ops->ndo_get_phys_port_name(dev, name, len);
7130}
7131EXPORT_SYMBOL(dev_get_phys_port_name);
7132
7133/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007134 * dev_change_proto_down - update protocol port state information
7135 * @dev: device
7136 * @proto_down: new value
7137 *
7138 * This info can be used by switch drivers to set the phys state of the
7139 * port.
7140 */
7141int dev_change_proto_down(struct net_device *dev, bool proto_down)
7142{
7143 const struct net_device_ops *ops = dev->netdev_ops;
7144
7145 if (!ops->ndo_change_proto_down)
7146 return -EOPNOTSUPP;
7147 if (!netif_device_present(dev))
7148 return -ENODEV;
7149 return ops->ndo_change_proto_down(dev, proto_down);
7150}
7151EXPORT_SYMBOL(dev_change_proto_down);
7152
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08007153void __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
7154 struct netdev_bpf *xdp)
7155{
7156 memset(xdp, 0, sizeof(*xdp));
7157 xdp->command = XDP_QUERY_PROG;
7158
7159 /* Query must always succeed. */
7160 WARN_ON(bpf_op(dev, xdp) < 0);
7161}
7162
7163static u8 __dev_xdp_attached(struct net_device *dev, bpf_op_t bpf_op)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007164{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007165 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007166
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08007167 __dev_xdp_query(dev, bpf_op, &xdp);
Martin KaFai Lau58038692017-06-15 17:29:09 -07007168
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007169 return xdp.prog_attached;
7170}
7171
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007172static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07007173 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007174 struct bpf_prog *prog)
7175{
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007176 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007177
7178 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07007179 if (flags & XDP_FLAGS_HW_MODE)
7180 xdp.command = XDP_SETUP_PROG_HW;
7181 else
7182 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007183 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07007184 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007185 xdp.prog = prog;
7186
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007187 return bpf_op(dev, &xdp);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007188}
7189
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007190static void dev_xdp_uninstall(struct net_device *dev)
7191{
7192 struct netdev_bpf xdp;
7193 bpf_op_t ndo_bpf;
7194
7195 /* Remove generic XDP */
7196 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
7197
7198 /* Remove from the driver */
7199 ndo_bpf = dev->netdev_ops->ndo_bpf;
7200 if (!ndo_bpf)
7201 return;
7202
7203 __dev_xdp_query(dev, ndo_bpf, &xdp);
7204 if (xdp.prog_attached == XDP_ATTACHED_NONE)
7205 return;
7206
7207 /* Program removal should always succeed */
7208 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, NULL));
7209}
7210
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007211/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07007212 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
7213 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07007214 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07007215 * @fd: new program fd or negative value to clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01007216 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07007217 *
7218 * Set or clear a bpf program for a device
7219 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07007220int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
7221 int fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07007222{
7223 const struct net_device_ops *ops = dev->netdev_ops;
7224 struct bpf_prog *prog = NULL;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007225 bpf_op_t bpf_op, bpf_chk;
Brenden Blancoa7862b42016-07-19 12:16:48 -07007226 int err;
7227
Daniel Borkmann85de8572016-11-28 23:16:54 +01007228 ASSERT_RTNL();
7229
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007230 bpf_op = bpf_chk = ops->ndo_bpf;
7231 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE)))
Daniel Borkmann0489df92017-05-12 01:04:45 +02007232 return -EOPNOTSUPP;
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007233 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
7234 bpf_op = generic_xdp_install;
7235 if (bpf_op == bpf_chk)
7236 bpf_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04007237
Brenden Blancoa7862b42016-07-19 12:16:48 -07007238 if (fd >= 0) {
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08007239 if (bpf_chk && __dev_xdp_attached(dev, bpf_chk))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007240 return -EEXIST;
7241 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08007242 __dev_xdp_attached(dev, bpf_op))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007243 return -EBUSY;
Daniel Borkmann85de8572016-11-28 23:16:54 +01007244
Jakub Kicinski288b3de2017-11-20 15:21:54 -08007245 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
7246 bpf_op == ops->ndo_bpf);
Brenden Blancoa7862b42016-07-19 12:16:48 -07007247 if (IS_ERR(prog))
7248 return PTR_ERR(prog);
Jakub Kicinski441a3302017-11-20 15:21:55 -08007249
7250 if (!(flags & XDP_FLAGS_HW_MODE) &&
7251 bpf_prog_is_dev_bound(prog->aux)) {
7252 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
7253 bpf_prog_put(prog);
7254 return -EINVAL;
7255 }
Brenden Blancoa7862b42016-07-19 12:16:48 -07007256 }
7257
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007258 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07007259 if (err < 0 && prog)
7260 bpf_prog_put(prog);
7261
7262 return err;
7263}
Brenden Blancoa7862b42016-07-19 12:16:48 -07007264
7265/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07007267 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 *
7269 * Returns a suitable unique value for a new device interface
7270 * number. The caller must hold the rtnl semaphore or the
7271 * dev_base_lock to be sure it remains unique.
7272 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07007273static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00007275 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11007276
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 for (;;) {
7278 if (++ifindex <= 0)
7279 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07007280 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00007281 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 }
7283}
7284
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08007286static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07007287DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
Stephen Hemminger6f05f622007-03-08 20:46:03 -08007289static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07007292 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293}
7294
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007295static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007296{
Krishna Kumare93737b2009-12-08 22:26:02 +00007297 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007298 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007299
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007300 BUG_ON(dev_boot_phase);
7301 ASSERT_RTNL();
7302
Krishna Kumare93737b2009-12-08 22:26:02 +00007303 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007304 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00007305 * for initialization unwind. Remove those
7306 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007307 */
7308 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007309 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
7310 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007311
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007312 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00007313 list_del(&dev->unreg_list);
7314 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007315 }
Eric Dumazet449f4542011-05-19 12:24:16 +00007316 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007317 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00007318 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007319
Octavian Purdila44345722010-12-13 12:44:07 +00007320 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007321 list_for_each_entry(dev, head, unreg_list)
7322 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04007323 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007324
Octavian Purdila44345722010-12-13 12:44:07 +00007325 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007326 /* And unlink it from device chain. */
7327 unlist_netdevice(dev);
7328
7329 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007330 }
Eric Dumazet41852492016-08-26 12:50:39 -07007331 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007332
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007333 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007334
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007335 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007336 struct sk_buff *skb = NULL;
7337
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007338 /* Shutdown queueing discipline. */
7339 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007340
Jakub Kicinskibd0b2e72017-12-01 15:08:57 -08007341 dev_xdp_uninstall(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007342
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007343 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11007344 * this device. They should clean all the things.
7345 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007346 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
7347
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007348 if (!dev->rtnl_link_ops ||
7349 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04007350 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02007351 GFP_KERNEL, NULL);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007352
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007353 /*
7354 * Flush the unicast and multicast chains
7355 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00007356 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00007357 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007358
7359 if (dev->netdev_ops->ndo_uninit)
7360 dev->netdev_ops->ndo_uninit(dev);
7361
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007362 if (skb)
7363 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07007364
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007365 /* Notifier chain MUST detach us all upper devices. */
7366 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07007367 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007368
7369 /* Remove entries from kobject tree */
7370 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00007371#ifdef CONFIG_XPS
7372 /* Remove XPS queueing entries */
7373 netif_reset_xps_queues_gt(dev, 0);
7374#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007375 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007376
Eric W. Biederman850a5452011-10-13 22:25:23 +00007377 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007378
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00007379 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007380 dev_put(dev);
7381}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007382
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007383static void rollback_registered(struct net_device *dev)
7384{
7385 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007386
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007387 list_add(&dev->unreg_list, &single);
7388 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00007389 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007390}
7391
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007392static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
7393 struct net_device *upper, netdev_features_t features)
7394{
7395 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
7396 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007397 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007398
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007399 for_each_netdev_feature(&upper_disables, feature_bit) {
7400 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007401 if (!(upper->wanted_features & feature)
7402 && (features & feature)) {
7403 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
7404 &feature, upper->name);
7405 features &= ~feature;
7406 }
7407 }
7408
7409 return features;
7410}
7411
7412static void netdev_sync_lower_features(struct net_device *upper,
7413 struct net_device *lower, netdev_features_t features)
7414{
7415 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
7416 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007417 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007418
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007419 for_each_netdev_feature(&upper_disables, feature_bit) {
7420 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007421 if (!(features & feature) && (lower->features & feature)) {
7422 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
7423 &feature, lower->name);
7424 lower->wanted_features &= ~feature;
7425 netdev_update_features(lower);
7426
7427 if (unlikely(lower->features & feature))
7428 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
7429 &feature, lower->name);
7430 }
7431 }
7432}
7433
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007434static netdev_features_t netdev_fix_features(struct net_device *dev,
7435 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07007436{
Michał Mirosław57422dc2011-01-22 12:14:12 +00007437 /* Fix illegal checksum combinations */
7438 if ((features & NETIF_F_HW_CSUM) &&
7439 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007440 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00007441 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
7442 }
7443
Herbert Xub63365a2008-10-23 01:11:29 -07007444 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00007445 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007446 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00007447 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07007448 }
7449
Pravin B Shelarec5f0612013-03-07 09:28:01 +00007450 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
7451 !(features & NETIF_F_IP_CSUM)) {
7452 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
7453 features &= ~NETIF_F_TSO;
7454 features &= ~NETIF_F_TSO_ECN;
7455 }
7456
7457 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
7458 !(features & NETIF_F_IPV6_CSUM)) {
7459 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
7460 features &= ~NETIF_F_TSO6;
7461 }
7462
Alexander Duyckb1dc4972016-05-02 09:38:24 -07007463 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
7464 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
7465 features &= ~NETIF_F_TSO_MANGLEID;
7466
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00007467 /* TSO ECN requires that TSO is present as well. */
7468 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
7469 features &= ~NETIF_F_TSO_ECN;
7470
Michał Mirosław212b5732011-02-15 16:59:16 +00007471 /* Software GSO depends on SG. */
7472 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007473 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00007474 features &= ~NETIF_F_GSO;
7475 }
7476
Alexander Duyck802ab552016-04-10 21:45:03 -04007477 /* GSO partial features require GSO partial be set */
7478 if ((features & dev->gso_partial_features) &&
7479 !(features & NETIF_F_GSO_PARTIAL)) {
7480 netdev_dbg(dev,
7481 "Dropping partially supported GSO features since no GSO partial.\n");
7482 features &= ~dev->gso_partial_features;
7483 }
7484
Michael Chanfb1f5f72017-12-16 03:09:40 -05007485 if (!(features & NETIF_F_RXCSUM)) {
7486 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
7487 * successfully merged by hardware must also have the
7488 * checksum verified by hardware. If the user does not
7489 * want to enable RXCSUM, logically, we should disable GRO_HW.
7490 */
7491 if (features & NETIF_F_GRO_HW) {
7492 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
7493 features &= ~NETIF_F_GRO_HW;
7494 }
7495 }
7496
Herbert Xub63365a2008-10-23 01:11:29 -07007497 return features;
7498}
Herbert Xub63365a2008-10-23 01:11:29 -07007499
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007500int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00007501{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007502 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007503 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007504 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05007505 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00007506
Michał Mirosław87267482011-04-12 09:56:38 +00007507 ASSERT_RTNL();
7508
Michał Mirosław5455c692011-02-15 16:59:17 +00007509 features = netdev_get_wanted_features(dev);
7510
7511 if (dev->netdev_ops->ndo_fix_features)
7512 features = dev->netdev_ops->ndo_fix_features(dev, features);
7513
7514 /* driver might be less strict about feature dependencies */
7515 features = netdev_fix_features(dev, features);
7516
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007517 /* some features can't be enabled if they're off an an upper device */
7518 netdev_for_each_upper_dev_rcu(dev, upper, iter)
7519 features = netdev_sync_upper_features(dev, upper, features);
7520
Michał Mirosław5455c692011-02-15 16:59:17 +00007521 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05007522 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00007523
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007524 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
7525 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00007526
7527 if (dev->netdev_ops->ndo_set_features)
7528 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01007529 else
7530 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00007531
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007532 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00007533 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007534 "set_features() failed (%d); wanted %pNF, left %pNF\n",
7535 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01007536 /* return non-0 since some features might have changed and
7537 * it's better to fire a spurious notification than miss it
7538 */
7539 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007540 }
7541
Jarod Wilsone7868a82015-11-03 23:09:32 -05007542sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007543 /* some features must be disabled on lower devices when disabled
7544 * on an upper device (think: bonding master or bridge)
7545 */
7546 netdev_for_each_lower_dev(dev, lower, iter)
7547 netdev_sync_lower_features(dev, lower, features);
7548
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02007549 if (!err) {
7550 netdev_features_t diff = features ^ dev->features;
7551
7552 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
7553 /* udp_tunnel_{get,drop}_rx_info both need
7554 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
7555 * device, or they won't do anything.
7556 * Thus we need to update dev->features
7557 * *before* calling udp_tunnel_get_rx_info,
7558 * but *after* calling udp_tunnel_drop_rx_info.
7559 */
7560 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
7561 dev->features = features;
7562 udp_tunnel_get_rx_info(dev);
7563 } else {
7564 udp_tunnel_drop_rx_info(dev);
7565 }
7566 }
7567
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007568 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02007569 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007570
Jarod Wilsone7868a82015-11-03 23:09:32 -05007571 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007572}
7573
Michał Mirosławafe12cc2011-05-07 03:22:17 +00007574/**
7575 * netdev_update_features - recalculate device features
7576 * @dev: the device to check
7577 *
7578 * Recalculate dev->features set and send notifications if it
7579 * has changed. Should be called after driver or hardware dependent
7580 * conditions might have changed that influence the features.
7581 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007582void netdev_update_features(struct net_device *dev)
7583{
7584 if (__netdev_update_features(dev))
7585 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00007586}
7587EXPORT_SYMBOL(netdev_update_features);
7588
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00007590 * netdev_change_features - recalculate device features
7591 * @dev: the device to check
7592 *
7593 * Recalculate dev->features set and send notifications even
7594 * if they have not changed. Should be called instead of
7595 * netdev_update_features() if also dev->vlan_features might
7596 * have changed to allow the changes to be propagated to stacked
7597 * VLAN devices.
7598 */
7599void netdev_change_features(struct net_device *dev)
7600{
7601 __netdev_update_features(dev);
7602 netdev_features_change(dev);
7603}
7604EXPORT_SYMBOL(netdev_change_features);
7605
7606/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007607 * netif_stacked_transfer_operstate - transfer operstate
7608 * @rootdev: the root or lower level device to transfer state from
7609 * @dev: the device to transfer operstate to
7610 *
7611 * Transfer operational state from root to device. This is normally
7612 * called when a stacking relationship exists between the root
7613 * device and the device(a leaf device).
7614 */
7615void netif_stacked_transfer_operstate(const struct net_device *rootdev,
7616 struct net_device *dev)
7617{
7618 if (rootdev->operstate == IF_OPER_DORMANT)
7619 netif_dormant_on(dev);
7620 else
7621 netif_dormant_off(dev);
7622
Zhang Shengju0575c862017-04-26 17:49:38 +08007623 if (netif_carrier_ok(rootdev))
7624 netif_carrier_on(dev);
7625 else
7626 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007627}
7628EXPORT_SYMBOL(netif_stacked_transfer_operstate);
7629
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007630static int netif_alloc_rx_queues(struct net_device *dev)
7631{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007632 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00007633 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05307634 size_t sz = count * sizeof(*rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007635 int err = 0;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007636
Tom Herbertbd25fa72010-10-18 18:00:16 +00007637 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007638
Michal Hockodcda9b02017-07-12 14:36:45 -07007639 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07007640 if (!rx)
7641 return -ENOMEM;
7642
Tom Herbertbd25fa72010-10-18 18:00:16 +00007643 dev->_rx = rx;
7644
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007645 for (i = 0; i < count; i++) {
Tom Herbertfe822242010-11-09 10:47:38 +00007646 rx[i].dev = dev;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007647
7648 /* XDP RX-queue setup */
7649 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
7650 if (err < 0)
7651 goto err_rxq_info;
7652 }
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007653 return 0;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007654
7655err_rxq_info:
7656 /* Rollback successful reg's and free other resources */
7657 while (i--)
7658 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
Jakub Kicinski141b52a2018-01-10 01:20:01 -08007659 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007660 dev->_rx = NULL;
7661 return err;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007662}
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007663
7664static void netif_free_rx_queues(struct net_device *dev)
7665{
7666 unsigned int i, count = dev->num_rx_queues;
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007667
7668 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
7669 if (!dev->_rx)
7670 return;
7671
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007672 for (i = 0; i < count; i++)
Jakub Kicinski82aaff22018-01-10 01:20:02 -08007673 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
7674
7675 kvfree(dev->_rx);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01007676}
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007677
Changli Gaoaa942102010-12-04 02:31:41 +00007678static void netdev_init_one_queue(struct net_device *dev,
7679 struct netdev_queue *queue, void *_unused)
7680{
7681 /* Initialize queue lock */
7682 spin_lock_init(&queue->_xmit_lock);
7683 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
7684 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00007685 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00007686 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00007687#ifdef CONFIG_BQL
7688 dql_init(&queue->dql, HZ);
7689#endif
Changli Gaoaa942102010-12-04 02:31:41 +00007690}
7691
Eric Dumazet60877a32013-06-20 01:15:51 -07007692static void netif_free_tx_queues(struct net_device *dev)
7693{
WANG Cong4cb28972014-06-02 15:55:22 -07007694 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07007695}
7696
Tom Herberte6484932010-10-18 18:04:39 +00007697static int netif_alloc_netdev_queues(struct net_device *dev)
7698{
7699 unsigned int count = dev->num_tx_queues;
7700 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07007701 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00007702
Eric Dumazetd3397272015-07-06 17:13:26 +02007703 if (count < 1 || count > 0xffff)
7704 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00007705
Michal Hockodcda9b02017-07-12 14:36:45 -07007706 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07007707 if (!tx)
7708 return -ENOMEM;
7709
Tom Herberte6484932010-10-18 18:04:39 +00007710 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00007711
Tom Herberte6484932010-10-18 18:04:39 +00007712 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
7713 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00007714
7715 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00007716}
7717
Denys Vlasenkoa2029242015-05-11 21:17:53 +02007718void netif_tx_stop_all_queues(struct net_device *dev)
7719{
7720 unsigned int i;
7721
7722 for (i = 0; i < dev->num_tx_queues; i++) {
7723 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11007724
Denys Vlasenkoa2029242015-05-11 21:17:53 +02007725 netif_tx_stop_queue(txq);
7726 }
7727}
7728EXPORT_SYMBOL(netif_tx_stop_all_queues);
7729
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007730/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731 * register_netdevice - register a network device
7732 * @dev: device to register
7733 *
7734 * Take a completed network device structure and add it to the kernel
7735 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7736 * chain. 0 is returned on success. A negative errno code is returned
7737 * on a failure to set up the device, or if the name is a duplicate.
7738 *
7739 * Callers must hold the rtnl semaphore. You may want
7740 * register_netdev() instead of this.
7741 *
7742 * BUGS:
7743 * The locking appears insufficient to guarantee two parallel registers
7744 * will not get the same name.
7745 */
7746
7747int register_netdevice(struct net_device *dev)
7748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007750 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751
7752 BUG_ON(dev_boot_phase);
7753 ASSERT_RTNL();
7754
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007755 might_sleep();
7756
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 /* When net_device's are persistent, this will be fatal. */
7758 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007759 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
David S. Millerf1f28aa2008-07-15 00:08:33 -07007761 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07007762 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763
Gao feng828de4f2012-09-13 20:58:27 +00007764 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00007765 if (ret < 0)
7766 goto out;
7767
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007769 if (dev->netdev_ops->ndo_init) {
7770 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 if (ret) {
7772 if (ret > 0)
7773 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08007774 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 }
7776 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007777
Patrick McHardyf6469682013-04-19 02:04:27 +00007778 if (((dev->hw_features | dev->features) &
7779 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00007780 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
7781 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
7782 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
7783 ret = -EINVAL;
7784 goto err_uninit;
7785 }
7786
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00007787 ret = -EBUSY;
7788 if (!dev->ifindex)
7789 dev->ifindex = dev_new_index(net);
7790 else if (__dev_get_by_index(net, dev->ifindex))
7791 goto err_uninit;
7792
Michał Mirosław5455c692011-02-15 16:59:17 +00007793 /* Transfer changeable features to wanted_features and enable
7794 * software offloads (GSO and GRO).
7795 */
7796 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00007797 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02007798
7799 if (dev->netdev_ops->ndo_udp_tunnel_add) {
7800 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
7801 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
7802 }
7803
Michał Mirosław14d12322011-02-22 16:52:28 +00007804 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007806 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00007807 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007808
Alexander Duyck7f348a62016-04-20 16:51:00 -04007809 /* If IPv4 TCP segmentation offload is supported we should also
7810 * allow the device to enable segmenting the frame with the option
7811 * of ignoring a static IP ID value. This doesn't enable the
7812 * feature itself but allows the user to enable it later.
7813 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007814 if (dev->hw_features & NETIF_F_TSO)
7815 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04007816 if (dev->vlan_features & NETIF_F_TSO)
7817 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
7818 if (dev->mpls_features & NETIF_F_TSO)
7819 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
7820 if (dev->hw_enc_features & NETIF_F_TSO)
7821 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07007822
Michał Mirosław1180e7d2011-07-14 14:41:11 -07007823 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00007824 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07007825 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00007826
Pravin B Shelaree579672013-03-07 09:28:08 +00007827 /* Make NETIF_F_SG inheritable to tunnel devices.
7828 */
Alexander Duyck802ab552016-04-10 21:45:03 -04007829 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00007830
Simon Horman0d89d202013-05-23 21:02:52 +00007831 /* Make NETIF_F_SG inheritable to MPLS.
7832 */
7833 dev->mpls_features |= NETIF_F_SG;
7834
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00007835 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
7836 ret = notifier_to_errno(ret);
7837 if (ret)
7838 goto err_uninit;
7839
Eric W. Biederman8b41d182007-09-26 22:02:53 -07007840 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007841 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007842 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007843 dev->reg_state = NETREG_REGISTERED;
7844
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007845 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00007846
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 /*
7848 * Default initial state at registry is that the
7849 * device is present.
7850 */
7851
7852 set_bit(__LINK_STATE_PRESENT, &dev->state);
7853
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01007854 linkwatch_init_dev(dev);
7855
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02007858 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04007859 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
Jiri Pirko948b3372013-01-08 01:38:25 +00007861 /* If the device has permanent device address, driver should
7862 * set dev_addr and also addr_assign_type should be set to
7863 * NET_ADDR_PERM (default value).
7864 */
7865 if (dev->addr_assign_type == NET_ADDR_PERM)
7866 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
7867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07007869 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07007870 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007871 if (ret) {
7872 rollback_registered(dev);
7873 dev->reg_state = NETREG_UNREGISTERED;
7874 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00007875 /*
7876 * Prevent userspace races by waiting until the network
7877 * device is fully setup before sending notifications.
7878 */
Patrick McHardya2835762010-02-26 06:34:51 +00007879 if (!dev->rtnl_link_ops ||
7880 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07007881 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
7883out:
7884 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007885
7886err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007887 if (dev->netdev_ops->ndo_uninit)
7888 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04007889 if (dev->priv_destructor)
7890 dev->priv_destructor(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007893EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
7895/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007896 * init_dummy_netdev - init a dummy network device for NAPI
7897 * @dev: device to init
7898 *
7899 * This takes a network device structure and initialize the minimum
7900 * amount of fields so it can be used to schedule NAPI polls without
7901 * registering a full blown interface. This is to be used by drivers
7902 * that need to tie several hardware interfaces to a single NAPI
7903 * poll scheduler due to HW limitations.
7904 */
7905int init_dummy_netdev(struct net_device *dev)
7906{
7907 /* Clear everything. Note we don't initialize spinlocks
7908 * are they aren't supposed to be taken by any of the
7909 * NAPI code and this dummy netdev is supposed to be
7910 * only ever used for NAPI polls
7911 */
7912 memset(dev, 0, sizeof(struct net_device));
7913
7914 /* make sure we BUG if trying to hit standard
7915 * register/unregister code path
7916 */
7917 dev->reg_state = NETREG_DUMMY;
7918
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007919 /* NAPI wants this */
7920 INIT_LIST_HEAD(&dev->napi_list);
7921
7922 /* a dummy interface is started by default */
7923 set_bit(__LINK_STATE_PRESENT, &dev->state);
7924 set_bit(__LINK_STATE_START, &dev->state);
7925
Eric Dumazet29b44332010-10-11 10:22:12 +00007926 /* Note : We dont allocate pcpu_refcnt for dummy devices,
7927 * because users of this 'device' dont need to change
7928 * its refcount.
7929 */
7930
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007931 return 0;
7932}
7933EXPORT_SYMBOL_GPL(init_dummy_netdev);
7934
7935
7936/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 * register_netdev - register a network device
7938 * @dev: device to register
7939 *
7940 * Take a completed network device structure and add it to the kernel
7941 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7942 * chain. 0 is returned on success. A negative errno code is returned
7943 * on a failure to set up the device, or if the name is a duplicate.
7944 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07007945 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 * and expands the device name if you passed a format string to
7947 * alloc_netdev.
7948 */
7949int register_netdev(struct net_device *dev)
7950{
7951 int err;
7952
7953 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 rtnl_unlock();
7956 return err;
7957}
7958EXPORT_SYMBOL(register_netdev);
7959
Eric Dumazet29b44332010-10-11 10:22:12 +00007960int netdev_refcnt_read(const struct net_device *dev)
7961{
7962 int i, refcnt = 0;
7963
7964 for_each_possible_cpu(i)
7965 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
7966 return refcnt;
7967}
7968EXPORT_SYMBOL(netdev_refcnt_read);
7969
Ben Hutchings2c530402012-07-10 10:55:09 +00007970/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00007972 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 *
7974 * This is called when unregistering network devices.
7975 *
7976 * Any protocol or device that holds a reference should register
7977 * for netdevice notification, and cleanup and put back the
7978 * reference if they receive an UNREGISTER event.
7979 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007980 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 */
7982static void netdev_wait_allrefs(struct net_device *dev)
7983{
7984 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00007985 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986
Eric Dumazete014deb2009-11-17 05:59:21 +00007987 linkwatch_forget_dev(dev);
7988
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00007990 refcnt = netdev_refcnt_read(dev);
7991
7992 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08007994 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
7996 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07007997 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998
Eric Dumazet748e2d92012-08-22 21:50:59 +00007999 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008000 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00008001 rtnl_lock();
8002
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008003 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
8005 &dev->state)) {
8006 /* We must not have linkwatch events
8007 * pending on unregister. If this
8008 * happens, we simply run the queue
8009 * unscheduled, resulting in a noop
8010 * for this device.
8011 */
8012 linkwatch_run_queue();
8013 }
8014
Stephen Hemminger6756ae42006-03-20 22:23:58 -08008015 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016
8017 rebroadcast_time = jiffies;
8018 }
8019
8020 msleep(250);
8021
Eric Dumazet29b44332010-10-11 10:22:12 +00008022 refcnt = netdev_refcnt_read(dev);
8023
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 if (time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008025 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
8026 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 warning_time = jiffies;
8028 }
8029 }
8030}
8031
8032/* The sequence is:
8033 *
8034 * rtnl_lock();
8035 * ...
8036 * register_netdevice(x1);
8037 * register_netdevice(x2);
8038 * ...
8039 * unregister_netdevice(y1);
8040 * unregister_netdevice(y2);
8041 * ...
8042 * rtnl_unlock();
8043 * free_netdev(y1);
8044 * free_netdev(y2);
8045 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07008046 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008048 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 * without deadlocking with linkwatch via keventd.
8050 * 2) Since we run with the RTNL semaphore not held, we can sleep
8051 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07008052 *
8053 * We must not return until all unregister events added during
8054 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056void netdev_run_todo(void)
8057{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008058 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008061 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07008062
8063 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07008064
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008065
8066 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00008067 if (!list_empty(&list))
8068 rcu_barrier();
8069
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 while (!list_empty(&list)) {
8071 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00008072 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 list_del(&dev->todo_list);
8074
Eric Dumazet748e2d92012-08-22 21:50:59 +00008075 rtnl_lock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008076 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Eric Dumazet748e2d92012-08-22 21:50:59 +00008077 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00008078
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008079 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008080 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008082 dump_stack();
8083 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008085
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008086 dev->reg_state = NETREG_UNREGISTERED;
8087
8088 netdev_wait_allrefs(dev);
8089
8090 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00008091 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08008092 BUG_ON(!list_empty(&dev->ptype_all));
8093 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00008094 WARN_ON(rcu_access_pointer(dev->ip_ptr));
8095 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
Ilpo Järvinen547b7922008-07-25 21:43:18 -07008096 WARN_ON(dev->dn_ptr);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07008097
David S. Millercf124db2017-05-08 12:52:56 -04008098 if (dev->priv_destructor)
8099 dev->priv_destructor(dev);
8100 if (dev->needs_free_netdev)
8101 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07008102
Eric W. Biederman50624c92013-09-23 21:19:49 -07008103 /* Report a network device has been unregistered */
8104 rtnl_lock();
8105 dev_net(dev)->dev_unreg_count--;
8106 __rtnl_unlock();
8107 wake_up(&netdev_unregistering_wq);
8108
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07008109 /* Free network device */
8110 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112}
8113
Jarod Wilson92566452016-02-01 18:51:04 -05008114/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
8115 * all the same fields in the same order as net_device_stats, with only
8116 * the type differing, but rtnl_link_stats64 may have additional fields
8117 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00008118 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00008119void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
8120 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00008121{
8122#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05008123 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02008124 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05008125 /* zero out counters that only exist in rtnl_link_stats64 */
8126 memset((char *)stats64 + sizeof(*netdev_stats), 0,
8127 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008128#else
Jarod Wilson92566452016-02-01 18:51:04 -05008129 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00008130 const unsigned long *src = (const unsigned long *)netdev_stats;
8131 u64 *dst = (u64 *)stats64;
8132
Jarod Wilson92566452016-02-01 18:51:04 -05008133 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008134 for (i = 0; i < n; i++)
8135 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05008136 /* zero out counters that only exist in rtnl_link_stats64 */
8137 memset((char *)stats64 + n * sizeof(u64), 0,
8138 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00008139#endif
8140}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00008141EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00008142
Eric Dumazetd83345a2009-11-16 03:36:51 +00008143/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008144 * dev_get_stats - get network device statistics
8145 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07008146 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008147 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00008148 * Get network statistics from device. Return @storage.
8149 * The device driver may provide its own method by setting
8150 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
8151 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008152 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00008153struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
8154 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00008155{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008156 const struct net_device_ops *ops = dev->netdev_ops;
8157
Eric Dumazet28172732010-07-07 14:58:56 -07008158 if (ops->ndo_get_stats64) {
8159 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00008160 ops->ndo_get_stats64(dev, storage);
8161 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00008162 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00008163 } else {
8164 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07008165 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07008166 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
8167 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
8168 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07008169 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07008170}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08008171EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07008172
Eric Dumazet24824a02010-10-02 06:11:55 +00008173struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07008174{
Eric Dumazet24824a02010-10-02 06:11:55 +00008175 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07008176
Eric Dumazet24824a02010-10-02 06:11:55 +00008177#ifdef CONFIG_NET_CLS_ACT
8178 if (queue)
8179 return queue;
8180 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
8181 if (!queue)
8182 return NULL;
8183 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008184 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00008185 queue->qdisc_sleeping = &noop_qdisc;
8186 rcu_assign_pointer(dev->ingress_queue, queue);
8187#endif
8188 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07008189}
8190
Eric Dumazet2c60db02012-09-16 09:17:26 +00008191static const struct ethtool_ops default_ethtool_ops;
8192
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00008193void netdev_set_default_ethtool_ops(struct net_device *dev,
8194 const struct ethtool_ops *ops)
8195{
8196 if (dev->ethtool_ops == &default_ethtool_ops)
8197 dev->ethtool_ops = ops;
8198}
8199EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
8200
Eric Dumazet74d332c2013-10-30 13:10:44 -07008201void netdev_freemem(struct net_device *dev)
8202{
8203 char *addr = (char *)dev - dev->padded;
8204
WANG Cong4cb28972014-06-02 15:55:22 -07008205 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07008206}
8207
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208/**
tcharding722c9a02017-02-09 17:56:04 +11008209 * alloc_netdev_mqs - allocate network device
8210 * @sizeof_priv: size of private data to allocate space for
8211 * @name: device name format string
8212 * @name_assign_type: origin of device name
8213 * @setup: callback to initialize device
8214 * @txqs: the number of TX subqueues to allocate
8215 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 *
tcharding722c9a02017-02-09 17:56:04 +11008217 * Allocates a struct net_device with private data area for driver use
8218 * and performs basic initialization. Also allocates subqueue structs
8219 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220 */
Tom Herbert36909ea2011-01-09 19:36:31 +00008221struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02008222 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00008223 void (*setup)(struct net_device *),
8224 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03008227 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008228 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07008230 BUG_ON(strlen(name) >= sizeof(dev->name));
8231
Tom Herbert36909ea2011-01-09 19:36:31 +00008232 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008233 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00008234 return NULL;
8235 }
8236
Tom Herbert36909ea2011-01-09 19:36:31 +00008237 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008238 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00008239 return NULL;
8240 }
Tom Herbert36909ea2011-01-09 19:36:31 +00008241
David S. Millerfd2ea0a2008-07-17 01:56:23 -07008242 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07008243 if (sizeof_priv) {
8244 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008245 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07008246 alloc_size += sizeof_priv;
8247 }
8248 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008249 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250
Michal Hockodcda9b02017-07-12 14:36:45 -07008251 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00008252 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008255 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008257
Eric Dumazet29b44332010-10-11 10:22:12 +00008258 dev->pcpu_refcnt = alloc_percpu(int);
8259 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07008260 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008261
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00008263 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
Jiri Pirko22bedad32010-04-01 21:22:57 +00008265 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008266 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00008267
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09008268 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07008270 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00008271 dev->gso_max_segs = GSO_MAX_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272
Herbert Xud565b0a2008-12-15 23:38:52 -08008273 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00008274 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008275 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00008276 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008277 INIT_LIST_HEAD(&dev->adj_list.upper);
8278 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08008279 INIT_LIST_HEAD(&dev->ptype_all);
8280 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02008281#ifdef CONFIG_NET_SCHED
8282 hash_init(dev->qdisc_hash);
8283#endif
Eric Dumazet02875872014-10-05 18:38:35 -07008284 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008286
Phil Suttera8131042016-02-17 15:37:43 +01008287 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02008288 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01008289 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01008290 }
Phil Sutter906470c2015-08-18 10:30:48 +02008291
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008292 dev->num_tx_queues = txqs;
8293 dev->real_num_tx_queues = txqs;
8294 if (netif_alloc_netdev_queues(dev))
8295 goto free_all;
8296
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008297 dev->num_rx_queues = rxqs;
8298 dev->real_num_rx_queues = rxqs;
8299 if (netif_alloc_rx_queues(dev))
8300 goto free_all;
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008301
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02008303 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008304 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00008305 if (!dev->ethtool_ops)
8306 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02008307
8308 nf_hook_ingress_init(dev);
8309
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008311
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008312free_all:
8313 free_netdev(dev);
8314 return NULL;
8315
Eric Dumazet29b44332010-10-11 10:22:12 +00008316free_pcpu:
8317 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07008318free_dev:
8319 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008320 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321}
Tom Herbert36909ea2011-01-09 19:36:31 +00008322EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323
8324/**
tcharding722c9a02017-02-09 17:56:04 +11008325 * free_netdev - free network device
8326 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 *
tcharding722c9a02017-02-09 17:56:04 +11008328 * This function does the last stage of destroying an allocated device
8329 * interface. The reference to the device object is released. If this
8330 * is the last reference then it will be freed.Must be called in process
8331 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332 */
8333void free_netdev(struct net_device *dev)
8334{
Herbert Xud565b0a2008-12-15 23:38:52 -08008335 struct napi_struct *p, *n;
8336
Eric Dumazet93d05d42015-11-18 06:31:03 -08008337 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07008338 netif_free_tx_queues(dev);
Jesper Dangaard Brouere817f852018-01-03 11:26:09 +01008339 netif_free_rx_queues(dev);
David S. Millere8a04642008-07-17 00:34:19 -07008340
Eric Dumazet33d480c2011-08-11 19:30:52 +00008341 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00008342
Jiri Pirkof001fde2009-05-05 02:48:28 +00008343 /* Flush device addresses */
8344 dev_addr_flush(dev);
8345
Herbert Xud565b0a2008-12-15 23:38:52 -08008346 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
8347 netif_napi_del(p);
8348
Eric Dumazet29b44332010-10-11 10:22:12 +00008349 free_percpu(dev->pcpu_refcnt);
8350 dev->pcpu_refcnt = NULL;
8351
Stephen Hemminger3041a062006-05-26 13:25:24 -07008352 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07008354 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 return;
8356 }
8357
8358 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
8359 dev->reg_state = NETREG_RELEASED;
8360
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07008361 /* will free via device release */
8362 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008364EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008365
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008366/**
8367 * synchronize_net - Synchronize with packet receive processing
8368 *
8369 * Wait for packets currently being received to be done.
8370 * Does not block later packets from starting.
8371 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008372void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373{
8374 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00008375 if (rtnl_is_locked())
8376 synchronize_rcu_expedited();
8377 else
8378 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008380EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381
8382/**
Eric Dumazet44a08732009-10-27 07:03:04 +00008383 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00008385 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08008386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08008388 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00008389 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 *
8391 * Callers must hold the rtnl semaphore. You may want
8392 * unregister_netdev() instead of this.
8393 */
8394
Eric Dumazet44a08732009-10-27 07:03:04 +00008395void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396{
Herbert Xua6620712007-12-12 19:21:56 -08008397 ASSERT_RTNL();
8398
Eric Dumazet44a08732009-10-27 07:03:04 +00008399 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00008400 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00008401 } else {
8402 rollback_registered(dev);
8403 /* Finish processing unregister after unlock */
8404 net_set_todo(dev);
8405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406}
Eric Dumazet44a08732009-10-27 07:03:04 +00008407EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408
8409/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008410 * unregister_netdevice_many - unregister many devices
8411 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07008412 *
8413 * Note: As most callers use a stack allocated list_head,
8414 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008415 */
8416void unregister_netdevice_many(struct list_head *head)
8417{
8418 struct net_device *dev;
8419
8420 if (!list_empty(head)) {
8421 rollback_registered_many(head);
8422 list_for_each_entry(dev, head, unreg_list)
8423 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07008424 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008425 }
8426}
Eric Dumazet63c80992009-10-27 07:06:49 +00008427EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008428
8429/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 * unregister_netdev - remove device from the kernel
8431 * @dev: device
8432 *
8433 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08008434 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 *
8436 * This is just a wrapper for unregister_netdevice that takes
8437 * the rtnl semaphore. In general you want to use this and not
8438 * unregister_netdevice.
8439 */
8440void unregister_netdev(struct net_device *dev)
8441{
8442 rtnl_lock();
8443 unregister_netdevice(dev);
8444 rtnl_unlock();
8445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446EXPORT_SYMBOL(unregister_netdev);
8447
Eric W. Biedermance286d32007-09-12 13:53:49 +02008448/**
8449 * dev_change_net_namespace - move device to different nethost namespace
8450 * @dev: device
8451 * @net: network namespace
8452 * @pat: If not NULL name pattern to try if the current device name
8453 * is already taken in the destination network namespace.
8454 *
8455 * This function shuts down a device interface and moves it
8456 * to a new network namespace. On success 0 is returned, on
8457 * a failure a netagive errno code is returned.
8458 *
8459 * Callers must hold the rtnl semaphore.
8460 */
8461
8462int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
8463{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02008464 int err, new_nsid;
Eric W. Biedermance286d32007-09-12 13:53:49 +02008465
8466 ASSERT_RTNL();
8467
8468 /* Don't allow namespace local devices to be moved. */
8469 err = -EINVAL;
8470 if (dev->features & NETIF_F_NETNS_LOCAL)
8471 goto out;
8472
8473 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02008474 if (dev->reg_state != NETREG_REGISTERED)
8475 goto out;
8476
8477 /* Get out if there is nothing todo */
8478 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09008479 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02008480 goto out;
8481
8482 /* Pick the destination device name, and ensure
8483 * we can use it in the destination network namespace.
8484 */
8485 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00008486 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008487 /* We get here if we can't use the current device name */
8488 if (!pat)
8489 goto out;
Gao feng828de4f2012-09-13 20:58:27 +00008490 if (dev_get_valid_name(net, dev, pat) < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02008491 goto out;
8492 }
8493
8494 /*
8495 * And now a mini version of register_netdevice unregister_netdevice.
8496 */
8497
8498 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07008499 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008500
8501 /* And unlink it from device chain */
8502 err = -ENODEV;
8503 unlist_netdevice(dev);
8504
8505 synchronize_net();
8506
8507 /* Shutdown queueing discipline. */
8508 dev_shutdown(dev);
8509
8510 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008511 * this device. They should clean all the things.
8512 *
8513 * Note that dev->reg_state stays at NETREG_REGISTERED.
8514 * This is wanted because this way 8021q and macvlan know
8515 * the device is just moving and can keep their slaves up.
8516 */
Eric W. Biedermance286d32007-09-12 13:53:49 +02008517 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +00008518 rcu_barrier();
8519 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02008520 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net)
8521 new_nsid = peernet2id_alloc(dev_net(dev), net);
8522 else
8523 new_nsid = peernet2id(dev_net(dev), net);
8524 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008525
8526 /*
8527 * Flush the unicast and multicast chains
8528 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008529 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008530 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008531
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008532 /* Send a netdev-removed uevent to the old namespace */
8533 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008534 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008535
Eric W. Biedermance286d32007-09-12 13:53:49 +02008536 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09008537 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008538
Eric W. Biedermance286d32007-09-12 13:53:49 +02008539 /* If there is an ifindex conflict assign a new one */
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +02008540 if (__dev_get_by_index(net, dev->ifindex))
Eric W. Biedermance286d32007-09-12 13:53:49 +02008541 dev->ifindex = dev_new_index(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008542
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008543 /* Send a netdev-add uevent to the new namespace */
8544 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008545 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008546
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008547 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07008548 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008549 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008550
8551 /* Add the device back in the hashes */
8552 list_netdevice(dev);
8553
8554 /* Notify protocols, that a new device appeared. */
8555 call_netdevice_notifiers(NETDEV_REGISTER, dev);
8556
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008557 /*
8558 * Prevent userspace races by waiting until the network
8559 * device is fully setup before sending notifications.
8560 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008561 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008562
Eric W. Biedermance286d32007-09-12 13:53:49 +02008563 synchronize_net();
8564 err = 0;
8565out:
8566 return err;
8567}
Johannes Berg463d0182009-07-14 00:33:35 +02008568EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008569
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008570static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571{
8572 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008574 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +05308575 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 local_irq_disable();
8578 cpu = smp_processor_id();
8579 sd = &per_cpu(softnet_data, cpu);
8580 oldsd = &per_cpu(softnet_data, oldcpu);
8581
8582 /* Find end of our completion_queue. */
8583 list_skb = &sd->completion_queue;
8584 while (*list_skb)
8585 list_skb = &(*list_skb)->next;
8586 /* Append completion queue from offline CPU. */
8587 *list_skb = oldsd->completion_queue;
8588 oldsd->completion_queue = NULL;
8589
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00008591 if (oldsd->output_queue) {
8592 *sd->output_queue_tailp = oldsd->output_queue;
8593 sd->output_queue_tailp = oldsd->output_queue_tailp;
8594 oldsd->output_queue = NULL;
8595 oldsd->output_queue_tailp = &oldsd->output_queue;
8596 }
Eric Dumazetac64da02015-01-15 17:04:22 -08008597 /* Append NAPI poll list from offline CPU, with one exception :
8598 * process_backlog() must be called by cpu owning percpu backlog.
8599 * We properly handle process_queue & input_pkt_queue later.
8600 */
8601 while (!list_empty(&oldsd->poll_list)) {
8602 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
8603 struct napi_struct,
8604 poll_list);
8605
8606 list_del_init(&napi->poll_list);
8607 if (napi->poll == process_backlog)
8608 napi->state = 0;
8609 else
8610 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +00008611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612
8613 raise_softirq_irqoff(NET_TX_SOFTIRQ);
8614 local_irq_enable();
8615
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05308616#ifdef CONFIG_RPS
8617 remsd = oldsd->rps_ipi_list;
8618 oldsd->rps_ipi_list = NULL;
8619#endif
8620 /* send out pending IPI's on offline CPU */
8621 net_rps_send_ipi(remsd);
8622
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00008624 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08008625 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00008626 input_queue_head_incr(oldsd);
8627 }
Eric Dumazetac64da02015-01-15 17:04:22 -08008628 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08008629 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00008630 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07008631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635
Herbert Xu7f353bf2007-08-10 15:47:58 -07008636/**
Herbert Xub63365a2008-10-23 01:11:29 -07008637 * netdev_increment_features - increment feature set by one
8638 * @all: current feature set
8639 * @one: new feature set
8640 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07008641 *
8642 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07008643 * @one to the master device with current feature set @all. Will not
8644 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07008645 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008646netdev_features_t netdev_increment_features(netdev_features_t all,
8647 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07008648{
Tom Herbertc8cd0982015-12-14 11:19:44 -08008649 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -08008650 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +00008651 mask |= NETIF_F_VLAN_CHALLENGED;
8652
Tom Herberta1882222015-12-14 11:19:43 -08008653 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +00008654 all &= one | ~NETIF_F_ALL_FOR_ALL;
8655
Michał Mirosław1742f182011-04-22 06:31:16 +00008656 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -08008657 if (all & NETIF_F_HW_CSUM)
8658 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -07008659
8660 return all;
8661}
Herbert Xub63365a2008-10-23 01:11:29 -07008662EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07008663
Baruch Siach430f03c2013-06-02 20:43:55 +00008664static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008665{
8666 int i;
8667 struct hlist_head *hash;
8668
8669 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
8670 if (hash != NULL)
8671 for (i = 0; i < NETDEV_HASHENTRIES; i++)
8672 INIT_HLIST_HEAD(&hash[i]);
8673
8674 return hash;
8675}
8676
Eric W. Biederman881d9662007-09-17 11:56:21 -07008677/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07008678static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07008679{
Rustad, Mark D734b6542012-07-18 09:06:07 +00008680 if (net != &init_net)
8681 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07008682
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008683 net->dev_name_head = netdev_create_hash();
8684 if (net->dev_name_head == NULL)
8685 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008686
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008687 net->dev_index_head = netdev_create_hash();
8688 if (net->dev_index_head == NULL)
8689 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008690
8691 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008692
8693err_idx:
8694 kfree(net->dev_name_head);
8695err_name:
8696 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008697}
8698
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008699/**
8700 * netdev_drivername - network driver for the device
8701 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008702 *
8703 * Determine network driver for device.
8704 */
David S. Miller3019de12011-06-06 16:41:33 -07008705const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -07008706{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07008707 const struct device_driver *driver;
8708 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -07008709 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -07008710
8711 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008712 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -07008713 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008714
8715 driver = parent->driver;
8716 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -07008717 return driver->name;
8718 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008719}
8720
Joe Perches6ea754e2014-09-22 11:10:50 -07008721static void __netdev_printk(const char *level, const struct net_device *dev,
8722 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +00008723{
Joe Perchesb004ff42012-09-12 20:12:19 -07008724 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -07008725 dev_printk_emit(level[1] - '0',
8726 dev->dev.parent,
8727 "%s %s %s%s: %pV",
8728 dev_driver_string(dev->dev.parent),
8729 dev_name(dev->dev.parent),
8730 netdev_name(dev), netdev_reg_state(dev),
8731 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008732 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -07008733 printk("%s%s%s: %pV",
8734 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008735 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -07008736 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008737 }
Joe Perches256df2f2010-06-27 01:02:35 +00008738}
8739
Joe Perches6ea754e2014-09-22 11:10:50 -07008740void netdev_printk(const char *level, const struct net_device *dev,
8741 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +00008742{
8743 struct va_format vaf;
8744 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +00008745
8746 va_start(args, format);
8747
8748 vaf.fmt = format;
8749 vaf.va = &args;
8750
Joe Perches6ea754e2014-09-22 11:10:50 -07008751 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008752
Joe Perches256df2f2010-06-27 01:02:35 +00008753 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +00008754}
8755EXPORT_SYMBOL(netdev_printk);
8756
8757#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -07008758void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +00008759{ \
Joe Perches256df2f2010-06-27 01:02:35 +00008760 struct va_format vaf; \
8761 va_list args; \
8762 \
8763 va_start(args, fmt); \
8764 \
8765 vaf.fmt = fmt; \
8766 vaf.va = &args; \
8767 \
Joe Perches6ea754e2014-09-22 11:10:50 -07008768 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -07008769 \
Joe Perches256df2f2010-06-27 01:02:35 +00008770 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +00008771} \
8772EXPORT_SYMBOL(func);
8773
8774define_netdev_printk_level(netdev_emerg, KERN_EMERG);
8775define_netdev_printk_level(netdev_alert, KERN_ALERT);
8776define_netdev_printk_level(netdev_crit, KERN_CRIT);
8777define_netdev_printk_level(netdev_err, KERN_ERR);
8778define_netdev_printk_level(netdev_warn, KERN_WARNING);
8779define_netdev_printk_level(netdev_notice, KERN_NOTICE);
8780define_netdev_printk_level(netdev_info, KERN_INFO);
8781
Pavel Emelyanov46650792007-10-08 20:38:39 -07008782static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07008783{
8784 kfree(net->dev_name_head);
8785 kfree(net->dev_index_head);
Vasily Averinee21b18b2017-11-12 22:28:46 +03008786 if (net != &init_net)
8787 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
Eric W. Biederman881d9662007-09-17 11:56:21 -07008788}
8789
Denis V. Lunev022cbae2007-11-13 03:23:50 -08008790static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07008791 .init = netdev_init,
8792 .exit = netdev_exit,
8793};
8794
Pavel Emelyanov46650792007-10-08 20:38:39 -07008795static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02008796{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008797 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02008798 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008799 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02008800 * initial network namespace
8801 */
8802 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008803 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008804 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008805 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02008806
8807 /* Ignore unmoveable devices (i.e. loopback) */
8808 if (dev->features & NETIF_F_NETNS_LOCAL)
8809 continue;
8810
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008811 /* Leave virtual devices for the generic cleanup */
8812 if (dev->rtnl_link_ops)
8813 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08008814
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008815 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008816 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
8817 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008818 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008819 pr_emerg("%s: failed to move %s to init_net: %d\n",
8820 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008821 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02008822 }
8823 }
8824 rtnl_unlock();
8825}
8826
Eric W. Biederman50624c92013-09-23 21:19:49 -07008827static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
8828{
8829 /* Return with the rtnl_lock held when there are no network
8830 * devices unregistering in any network namespace in net_list.
8831 */
8832 struct net *net;
8833 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +01008834 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008835
Peter Zijlstraff960a72014-10-29 17:04:56 +01008836 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008837 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -07008838 unregistering = false;
8839 rtnl_lock();
8840 list_for_each_entry(net, net_list, exit_list) {
8841 if (net->dev_unreg_count > 0) {
8842 unregistering = true;
8843 break;
8844 }
8845 }
8846 if (!unregistering)
8847 break;
8848 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +01008849
8850 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008851 }
Peter Zijlstraff960a72014-10-29 17:04:56 +01008852 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008853}
8854
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008855static void __net_exit default_device_exit_batch(struct list_head *net_list)
8856{
8857 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04008858 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008859 * Do this across as many network namespaces as possible to
8860 * improve batching efficiency.
8861 */
8862 struct net_device *dev;
8863 struct net *net;
8864 LIST_HEAD(dev_kill_list);
8865
Eric W. Biederman50624c92013-09-23 21:19:49 -07008866 /* To prevent network device cleanup code from dereferencing
8867 * loopback devices or network devices that have been freed
8868 * wait here for all pending unregistrations to complete,
8869 * before unregistring the loopback device and allowing the
8870 * network namespace be freed.
8871 *
8872 * The netdev todo list containing all network devices
8873 * unregistrations that happen in default_device_exit_batch
8874 * will run in the rtnl_unlock() at the end of
8875 * default_device_exit_batch.
8876 */
8877 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008878 list_for_each_entry(net, net_list, exit_list) {
8879 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02008880 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008881 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
8882 else
8883 unregister_netdevice_queue(dev, &dev_kill_list);
8884 }
8885 }
8886 unregister_netdevice_many(&dev_kill_list);
8887 rtnl_unlock();
8888}
8889
Denis V. Lunev022cbae2007-11-13 03:23:50 -08008890static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008891 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008892 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02008893};
8894
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895/*
8896 * Initialize the DEV module. At boot time this walks the device list and
8897 * unhooks any devices that fail to initialise (normally hardware not
8898 * present) and leaves us with a valid list of present and active devices.
8899 *
8900 */
8901
8902/*
8903 * This is called single threaded during boot, so no need
8904 * to take the rtnl semaphore.
8905 */
8906static int __init net_dev_init(void)
8907{
8908 int i, rc = -ENOMEM;
8909
8910 BUG_ON(!dev_boot_phase);
8911
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912 if (dev_proc_init())
8913 goto out;
8914
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008915 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 goto out;
8917
8918 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08008919 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920 INIT_LIST_HEAD(&ptype_base[i]);
8921
Vlad Yasevich62532da2012-11-15 08:49:10 +00008922 INIT_LIST_HEAD(&offload_base);
8923
Eric W. Biederman881d9662007-09-17 11:56:21 -07008924 if (register_pernet_subsys(&netdev_net_ops))
8925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926
8927 /*
8928 * Initialise the packet receive queues.
8929 */
8930
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07008931 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -07008932 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008933 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
Eric Dumazet41852492016-08-26 12:50:39 -07008935 INIT_WORK(flush, flush_backlog);
8936
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008937 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07008938 skb_queue_head_init(&sd->process_queue);
Steffen Klassertf53c7232017-12-20 10:41:36 +01008939#ifdef CONFIG_XFRM_OFFLOAD
8940 skb_queue_head_init(&sd->xfrm_backlog);
8941#endif
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008942 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00008943 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00008944#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008945 sd->csd.func = rps_trigger_softirq;
8946 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008947 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07008948#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00008949
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008950 sd->backlog.poll = process_backlog;
8951 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952 }
8953
Linus Torvalds1da177e2005-04-16 15:20:36 -07008954 dev_boot_phase = 0;
8955
Eric W. Biederman505d4f72008-11-07 22:54:20 -08008956 /* The loopback device is special if any other network devices
8957 * is present in a network namespace the loopback device must
8958 * be present. Since we now dynamically allocate and free the
8959 * loopback device ensure this invariant is maintained by
8960 * keeping the loopback device as the first device on the
8961 * list of network devices. Ensuring the loopback devices
8962 * is the first device that appears and the last network device
8963 * that disappears.
8964 */
8965 if (register_pernet_device(&loopback_net_ops))
8966 goto out;
8967
8968 if (register_pernet_device(&default_device_ops))
8969 goto out;
8970
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008971 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
8972 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008974 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
8975 NULL, dev_cpu_dead);
8976 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 rc = 0;
8978out:
8979 return rc;
8980}
8981
8982subsys_initcall(net_dev_init);