blob: bffc754291846310536cc8975120716787aebfc2 [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,
166 struct net_device *dev,
167 struct netdev_notifier_info *info);
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200168static struct napi_struct *napi_by_id(unsigned int napi_id);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700171 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * semaphore.
173 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800174 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700177 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * actual updates. This allows pure readers to access the list even
179 * while a writer is preparing to update it.
180 *
181 * To put it another way, dev_base_lock is held for writing only to
182 * protect against pure readers; the rtnl semaphore provides the
183 * protection against other writers.
184 *
185 * See, for example usages, register_netdevice() and
186 * unregister_netdevice(), which must be called with the rtnl
187 * semaphore held.
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190EXPORT_SYMBOL(dev_base_lock);
191
Florian Westphal6c557002017-10-02 23:50:05 +0200192static DEFINE_MUTEX(ifalias_mutex);
193
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300194/* protects napi_hash addition/deletion and napi_gen_id */
195static DEFINE_SPINLOCK(napi_hash_lock);
196
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800197static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800198static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300199
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200200static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000201
Thomas Graf4e985ad2011-06-21 03:11:20 +0000202static inline void dev_base_seq_inc(struct net *net)
203{
tcharding643aa9c2017-02-09 17:56:05 +1100204 while (++net->dev_base_seq == 0)
205 ;
Thomas Graf4e985ad2011-06-21 03:11:20 +0000206}
207
Eric W. Biederman881d9662007-09-17 11:56:21 -0700208static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700210 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000211
stephen hemminger08e98972009-11-10 07:20:34 +0000212 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Eric W. Biederman881d9662007-09-17 11:56:21 -0700215static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700217 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000220static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000221{
222#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000223 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000224#endif
225}
226
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000227static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000228{
229#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000230 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000231#endif
232}
233
Eric W. Biedermance286d32007-09-12 13:53:49 +0200234/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000235static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200236{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900237 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200238
239 ASSERT_RTNL();
240
241 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800242 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000243 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000244 hlist_add_head_rcu(&dev->index_hlist,
245 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200246 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000247
248 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200249}
250
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000251/* Device list removal
252 * caller must respect a RCU grace period before freeing/reusing dev
253 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200254static void unlist_netdevice(struct net_device *dev)
255{
256 ASSERT_RTNL();
257
258 /* Unlink dev from the device chain */
259 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800260 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000261 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000262 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200263 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000264
265 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/*
269 * Our notifier list
270 */
271
Alan Sternf07d5b92006-05-09 15:23:03 -0700272static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/*
275 * Device drivers call our routines to queue packets here. We empty the
276 * queue in the local softnet handler.
277 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700278
Eric Dumazet9958da02010-04-17 04:17:02 +0000279DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700280EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
David S. Millercf508b12008-07-22 14:16:42 -0700282#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700283/*
David S. Millerc773e842008-07-08 23:13:53 -0700284 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700285 * according to dev->type
286 */
tcharding643aa9c2017-02-09 17:56:05 +1100287static const unsigned short netdev_lock_type[] = {
288 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700289 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
290 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
291 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
292 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
293 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
294 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
295 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
296 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
297 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
298 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
299 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
Paul Gortmaker211ed862012-05-10 17:14:35 -0400300 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
301 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
302 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700303
tcharding643aa9c2017-02-09 17:56:05 +1100304static const char *const netdev_lock_name[] = {
305 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
306 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
307 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
308 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
309 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
310 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
311 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
312 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
313 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
314 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
315 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
316 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
317 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
318 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
319 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700320
321static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700322static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700323
324static inline unsigned short netdev_lock_pos(unsigned short dev_type)
325{
326 int i;
327
328 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
329 if (netdev_lock_type[i] == dev_type)
330 return i;
331 /* the last key is used by default */
332 return ARRAY_SIZE(netdev_lock_type) - 1;
333}
334
David S. Millercf508b12008-07-22 14:16:42 -0700335static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
336 unsigned short dev_type)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700337{
338 int i;
339
340 i = netdev_lock_pos(dev_type);
341 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
342 netdev_lock_name[i]);
343}
David S. Millercf508b12008-07-22 14:16:42 -0700344
345static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
346{
347 int i;
348
349 i = netdev_lock_pos(dev->type);
350 lockdep_set_class_and_name(&dev->addr_list_lock,
351 &netdev_addr_lock_key[i],
352 netdev_lock_name[i]);
353}
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700354#else
David S. Millercf508b12008-07-22 14:16:42 -0700355static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
356 unsigned short dev_type)
357{
358}
359static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700360{
361}
362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100365 *
366 * Protocol management and registration routines
367 *
368 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * Add a protocol ID to the list. Now that the input handler is
373 * smarter we can dispense with all the messy stuff that used to be
374 * here.
375 *
376 * BEWARE!!! Protocol handlers, mangling input packets,
377 * MUST BE last in hash buckets and checking protocol handlers
378 * MUST start from promiscuous ptype_all chain in net_bh.
379 * It is true now, do not change it.
380 * Explanation follows: if protocol handler, mangling packet, will
381 * be the first on list, it is not able to sense, that packet
382 * is cloned and should be copied-on-write, so that it will
383 * change it and subsequent readers will get broken packet.
384 * --ANK (980803)
385 */
386
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000387static inline struct list_head *ptype_head(const struct packet_type *pt)
388{
389 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800390 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000391 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800392 return pt->dev ? &pt->dev->ptype_specific :
393 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/**
397 * dev_add_pack - add packet handler
398 * @pt: packet type declaration
399 *
400 * Add a protocol handler to the networking stack. The passed &packet_type
401 * is linked into kernel lists and may not be freed until it has been
402 * removed from the kernel lists.
403 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900404 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * guarantee all CPU's that are in middle of receiving packets
406 * will see the new packet type (until the next received packet).
407 */
408
409void dev_add_pack(struct packet_type *pt)
410{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000411 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000413 spin_lock(&ptype_lock);
414 list_add_rcu(&pt->list, head);
415 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700417EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
420 * __dev_remove_pack - remove packet handler
421 * @pt: packet type declaration
422 *
423 * Remove a protocol handler that was previously added to the kernel
424 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
425 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900426 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
428 * The packet type might still be in use by receivers
429 * and must not be freed until after all the CPU's have gone
430 * through a quiescent state.
431 */
432void __dev_remove_pack(struct packet_type *pt)
433{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000434 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct packet_type *pt1;
436
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000437 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 list_for_each_entry(pt1, head, list) {
440 if (pt == pt1) {
441 list_del_rcu(&pt->list);
442 goto out;
443 }
444 }
445
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000446 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000448 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700450EXPORT_SYMBOL(__dev_remove_pack);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/**
453 * dev_remove_pack - remove packet handler
454 * @pt: packet type declaration
455 *
456 * Remove a protocol handler that was previously added to the kernel
457 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
458 * from the kernel lists and can be freed or reused once this function
459 * returns.
460 *
461 * This call sleeps to guarantee that no CPU is looking at the packet
462 * type after return.
463 */
464void dev_remove_pack(struct packet_type *pt)
465{
466 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 synchronize_net();
469}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700470EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Vlad Yasevich62532da2012-11-15 08:49:10 +0000472
473/**
474 * dev_add_offload - register offload handlers
475 * @po: protocol offload declaration
476 *
477 * Add protocol offload handlers to the networking stack. The passed
478 * &proto_offload is linked into kernel lists and may not be freed until
479 * it has been removed from the kernel lists.
480 *
481 * This call does not sleep therefore it can not
482 * guarantee all CPU's that are in middle of receiving packets
483 * will see the new offload handlers (until the next received packet).
484 */
485void dev_add_offload(struct packet_offload *po)
486{
David S. Millerbdef7de2015-06-01 14:56:09 -0700487 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000488
489 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700490 list_for_each_entry(elem, &offload_base, list) {
491 if (po->priority < elem->priority)
492 break;
493 }
494 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000495 spin_unlock(&offload_lock);
496}
497EXPORT_SYMBOL(dev_add_offload);
498
499/**
500 * __dev_remove_offload - remove offload handler
501 * @po: packet offload declaration
502 *
503 * Remove a protocol offload handler that was previously added to the
504 * kernel offload handlers by dev_add_offload(). The passed &offload_type
505 * is removed from the kernel lists and can be freed or reused once this
506 * function returns.
507 *
508 * The packet type might still be in use by receivers
509 * and must not be freed until after all the CPU's have gone
510 * through a quiescent state.
511 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800512static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000513{
514 struct list_head *head = &offload_base;
515 struct packet_offload *po1;
516
Eric Dumazetc53aa502012-11-16 08:08:23 +0000517 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000518
519 list_for_each_entry(po1, head, list) {
520 if (po == po1) {
521 list_del_rcu(&po->list);
522 goto out;
523 }
524 }
525
526 pr_warn("dev_remove_offload: %p not found\n", po);
527out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000528 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000529}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000530
531/**
532 * dev_remove_offload - remove packet offload handler
533 * @po: packet offload declaration
534 *
535 * Remove a packet offload handler that was previously added to the kernel
536 * offload handlers by dev_add_offload(). The passed &offload_type is
537 * removed from the kernel lists and can be freed or reused once this
538 * function returns.
539 *
540 * This call sleeps to guarantee that no CPU is looking at the packet
541 * type after return.
542 */
543void dev_remove_offload(struct packet_offload *po)
544{
545 __dev_remove_offload(po);
546
547 synchronize_net();
548}
549EXPORT_SYMBOL(dev_remove_offload);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100552 *
553 * Device Boot-time Settings Routines
554 *
555 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/* Boot time configuration table */
558static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
559
560/**
561 * netdev_boot_setup_add - add new setup entry
562 * @name: name of the device
563 * @map: configured settings for the device
564 *
565 * Adds new setup entry to the dev_boot_setup list. The function
566 * returns 0 on error and 1 on success. This is a generic routine to
567 * all netdevices.
568 */
569static int netdev_boot_setup_add(char *name, struct ifmap *map)
570{
571 struct netdev_boot_setup *s;
572 int i;
573
574 s = dev_boot_setup;
575 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
576 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
577 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700578 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 memcpy(&s[i].map, map, sizeof(s[i].map));
580 break;
581 }
582 }
583
584 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
585}
586
587/**
tcharding722c9a02017-02-09 17:56:04 +1100588 * netdev_boot_setup_check - check boot time settings
589 * @dev: the netdevice
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *
tcharding722c9a02017-02-09 17:56:04 +1100591 * Check boot time settings for the device.
592 * The found settings are set for the device to be used
593 * later in the device probing.
594 * Returns 0 if no settings found, 1 if they are.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
596int netdev_boot_setup_check(struct net_device *dev)
597{
598 struct netdev_boot_setup *s = dev_boot_setup;
599 int i;
600
601 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
602 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700603 !strcmp(dev->name, s[i].name)) {
tcharding722c9a02017-02-09 17:56:04 +1100604 dev->irq = s[i].map.irq;
605 dev->base_addr = s[i].map.base_addr;
606 dev->mem_start = s[i].map.mem_start;
607 dev->mem_end = s[i].map.mem_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 1;
609 }
610 }
611 return 0;
612}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700613EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615
616/**
tcharding722c9a02017-02-09 17:56:04 +1100617 * netdev_boot_base - get address from boot time settings
618 * @prefix: prefix for network device
619 * @unit: id for network device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 *
tcharding722c9a02017-02-09 17:56:04 +1100621 * Check boot time settings for the base address of device.
622 * The found settings are set for the device to be used
623 * later in the device probing.
624 * Returns 0 if no settings found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
626unsigned long netdev_boot_base(const char *prefix, int unit)
627{
628 const struct netdev_boot_setup *s = dev_boot_setup;
629 char name[IFNAMSIZ];
630 int i;
631
632 sprintf(name, "%s%d", prefix, unit);
633
634 /*
635 * If device already registered then return base of 1
636 * to indicate not to probe for this interface
637 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700638 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 1;
640
641 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
642 if (!strcmp(name, s[i].name))
643 return s[i].map.base_addr;
644 return 0;
645}
646
647/*
648 * Saves at boot time configured settings for any netdevice.
649 */
650int __init netdev_boot_setup(char *str)
651{
652 int ints[5];
653 struct ifmap map;
654
655 str = get_options(str, ARRAY_SIZE(ints), ints);
656 if (!str || !*str)
657 return 0;
658
659 /* Save settings */
660 memset(&map, 0, sizeof(map));
661 if (ints[0] > 0)
662 map.irq = ints[1];
663 if (ints[0] > 1)
664 map.base_addr = ints[2];
665 if (ints[0] > 2)
666 map.mem_start = ints[3];
667 if (ints[0] > 3)
668 map.mem_end = ints[4];
669
670 /* Add new entry to the list */
671 return netdev_boot_setup_add(str, &map);
672}
673
674__setup("netdev=", netdev_boot_setup);
675
676/*******************************************************************************
tchardingeb13da12017-02-09 17:56:06 +1100677 *
678 * Device Interface Subroutines
679 *
680 *******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200683 * dev_get_iflink - get 'iflink' value of a interface
684 * @dev: targeted interface
685 *
686 * Indicates the ifindex the interface is linked to.
687 * Physical interfaces have the same 'ifindex' and 'iflink' values.
688 */
689
690int dev_get_iflink(const struct net_device *dev)
691{
692 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
693 return dev->netdev_ops->ndo_get_iflink(dev);
694
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200695 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200696}
697EXPORT_SYMBOL(dev_get_iflink);
698
699/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700700 * dev_fill_metadata_dst - Retrieve tunnel egress information.
701 * @dev: targeted interface
702 * @skb: The packet.
703 *
704 * For better visibility of tunnel traffic OVS needs to retrieve
705 * egress tunnel information for a packet. Following API allows
706 * user to get this info.
707 */
708int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
709{
710 struct ip_tunnel_info *info;
711
712 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
713 return -EINVAL;
714
715 info = skb_tunnel_info_unclone(skb);
716 if (!info)
717 return -ENOMEM;
718 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
719 return -EINVAL;
720
721 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
722}
723EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
724
725/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700727 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * @name: name to find
729 *
730 * Find an interface by name. Must be called under RTNL semaphore
731 * or @dev_base_lock. If the name is found a pointer to the device
732 * is returned. If the name is not found then %NULL is returned. The
733 * reference counters are not incremented so the caller must be
734 * careful with locks.
735 */
736
Eric W. Biederman881d9662007-09-17 11:56:21 -0700737struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700739 struct net_device *dev;
740 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Sasha Levinb67bfe02013-02-27 17:06:00 -0800742 hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!strncmp(dev->name, name, IFNAMSIZ))
744 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return NULL;
747}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700748EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750/**
tcharding722c9a02017-02-09 17:56:04 +1100751 * dev_get_by_name_rcu - find a device by its name
752 * @net: the applicable net namespace
753 * @name: name to find
Eric Dumazet72c95282009-10-30 07:11:27 +0000754 *
tcharding722c9a02017-02-09 17:56:04 +1100755 * Find an interface by name.
756 * If the name is found a pointer to the device is returned.
757 * If the name is not found then %NULL is returned.
758 * The reference counters are not incremented so the caller must be
759 * careful with locks. The caller must hold RCU lock.
Eric Dumazet72c95282009-10-30 07:11:27 +0000760 */
761
762struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
763{
Eric Dumazet72c95282009-10-30 07:11:27 +0000764 struct net_device *dev;
765 struct hlist_head *head = dev_name_hash(net, name);
766
Sasha Levinb67bfe02013-02-27 17:06:00 -0800767 hlist_for_each_entry_rcu(dev, head, name_hlist)
Eric Dumazet72c95282009-10-30 07:11:27 +0000768 if (!strncmp(dev->name, name, IFNAMSIZ))
769 return dev;
770
771 return NULL;
772}
773EXPORT_SYMBOL(dev_get_by_name_rcu);
774
775/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700777 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * @name: name to find
779 *
780 * Find an interface by name. This can be called from any
781 * context and does its own locking. The returned handle has
782 * the usage count incremented and the caller must use dev_put() to
783 * release it when it is no longer needed. %NULL is returned if no
784 * matching device is found.
785 */
786
Eric W. Biederman881d9662007-09-17 11:56:21 -0700787struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct net_device *dev;
790
Eric Dumazet72c95282009-10-30 07:11:27 +0000791 rcu_read_lock();
792 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (dev)
794 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000795 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return dev;
797}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700798EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800/**
801 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700802 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * @ifindex: index of device
804 *
805 * Search for an interface by index. Returns %NULL if the device
806 * is not found or a pointer to the device. The device has not
807 * had its reference counter increased so the caller must be careful
808 * about locking. The caller must hold either the RTNL semaphore
809 * or @dev_base_lock.
810 */
811
Eric W. Biederman881d9662007-09-17 11:56:21 -0700812struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700814 struct net_device *dev;
815 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Sasha Levinb67bfe02013-02-27 17:06:00 -0800817 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (dev->ifindex == ifindex)
819 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return NULL;
822}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700823EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000825/**
826 * dev_get_by_index_rcu - find a device by its ifindex
827 * @net: the applicable net namespace
828 * @ifindex: index of device
829 *
830 * Search for an interface by index. Returns %NULL if the device
831 * is not found or a pointer to the device. The device has not
832 * had its reference counter increased so the caller must be careful
833 * about locking. The caller must hold RCU lock.
834 */
835
836struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
837{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000838 struct net_device *dev;
839 struct hlist_head *head = dev_index_hash(net, ifindex);
840
Sasha Levinb67bfe02013-02-27 17:06:00 -0800841 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000842 if (dev->ifindex == ifindex)
843 return dev;
844
845 return NULL;
846}
847EXPORT_SYMBOL(dev_get_by_index_rcu);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/**
851 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700852 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * @ifindex: index of device
854 *
855 * Search for an interface by index. Returns NULL if the device
856 * is not found or a pointer to the device. The device returned has
857 * had a reference added and the pointer is safe until the user calls
858 * dev_put to indicate they have finished with it.
859 */
860
Eric W. Biederman881d9662007-09-17 11:56:21 -0700861struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct net_device *dev;
864
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000865 rcu_read_lock();
866 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (dev)
868 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000869 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return dev;
871}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700872EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874/**
Miroslav Lichvar90b602f2017-05-19 17:52:37 +0200875 * dev_get_by_napi_id - find a device by napi_id
876 * @napi_id: ID of the NAPI struct
877 *
878 * Search for an interface by NAPI ID. Returns %NULL if the device
879 * is not found or a pointer to the device. The device has not had
880 * its reference counter increased so the caller must be careful
881 * about locking. The caller must hold RCU lock.
882 */
883
884struct net_device *dev_get_by_napi_id(unsigned int napi_id)
885{
886 struct napi_struct *napi;
887
888 WARN_ON_ONCE(!rcu_read_lock_held());
889
890 if (napi_id < MIN_NAPI_ID)
891 return NULL;
892
893 napi = napi_by_id(napi_id);
894
895 return napi ? napi->dev : NULL;
896}
897EXPORT_SYMBOL(dev_get_by_napi_id);
898
899/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200900 * netdev_get_name - get a netdevice name, knowing its ifindex.
901 * @net: network namespace
902 * @name: a pointer to the buffer where the name will be stored.
903 * @ifindex: the ifindex of the interface to get the name from.
904 *
905 * The use of raw_seqcount_begin() and cond_resched() before
906 * retrying is required as we want to give the writers a chance
907 * to complete when CONFIG_PREEMPT is not set.
908 */
909int netdev_get_name(struct net *net, char *name, int ifindex)
910{
911 struct net_device *dev;
912 unsigned int seq;
913
914retry:
915 seq = raw_seqcount_begin(&devnet_rename_seq);
916 rcu_read_lock();
917 dev = dev_get_by_index_rcu(net, ifindex);
918 if (!dev) {
919 rcu_read_unlock();
920 return -ENODEV;
921 }
922
923 strcpy(name, dev->name);
924 rcu_read_unlock();
925 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
926 cond_resched();
927 goto retry;
928 }
929
930 return 0;
931}
932
933/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000934 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700935 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * @type: media type of device
937 * @ha: hardware address
938 *
939 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800940 * is not found or a pointer to the device.
941 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000942 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * and the caller must therefore be careful about locking
944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946
Eric Dumazet941666c2010-12-05 01:23:53 +0000947struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
948 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct net_device *dev;
951
Eric Dumazet941666c2010-12-05 01:23:53 +0000952 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (dev->type == type &&
954 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700955 return dev;
956
957 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
Eric Dumazet941666c2010-12-05 01:23:53 +0000959EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300960
Eric W. Biederman881d9662007-09-17 11:56:21 -0700961struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700962{
963 struct net_device *dev;
964
965 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700966 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700967 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700968 return dev;
969
970 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700971}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700972EXPORT_SYMBOL(__dev_getfirstbyhwtype);
973
Eric W. Biederman881d9662007-09-17 11:56:21 -0700974struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000976 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000978 rcu_read_lock();
979 for_each_netdev_rcu(net, dev)
980 if (dev->type == type) {
981 dev_hold(dev);
982 ret = dev;
983 break;
984 }
985 rcu_read_unlock();
986 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988EXPORT_SYMBOL(dev_getfirstbyhwtype);
989
990/**
WANG Cong6c555492014-09-11 15:35:09 -0700991 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700992 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * @if_flags: IFF_* values
994 * @mask: bitmask of bits in if_flags to check
995 *
996 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000997 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -0700998 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
1000
WANG Cong6c555492014-09-11 15:35:09 -07001001struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1002 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001004 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
WANG Cong6c555492014-09-11 15:35:09 -07001006 ASSERT_RTNL();
1007
Pavel Emelianov7562f872007-05-03 15:13:45 -07001008 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -07001009 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001011 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013 }
1014 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001015 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
WANG Cong6c555492014-09-11 15:35:09 -07001017EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019/**
1020 * dev_valid_name - check if name is okay for network device
1021 * @name: name string
1022 *
1023 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -07001024 * to allow sysfs to work. We also disallow any kind of
1025 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
David S. Miller95f050b2012-03-06 16:12:15 -05001027bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
David S. Millerc7fa9d12006-08-15 16:34:13 -07001029 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -05001030 return false;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07001031 if (strlen(name) >= IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -05001032 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001033 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -05001034 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001035
1036 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001037 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001038 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001039 name++;
1040 }
David S. Miller95f050b2012-03-06 16:12:15 -05001041 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001043EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001046 * __dev_alloc_name - allocate a name for a device
1047 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001049 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *
1051 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001052 * id. It scans list of devices to build up a free map, then chooses
1053 * the first empty slot. The caller must hold the dev_base or rtnl lock
1054 * while allocating the name and adding the device in order to avoid
1055 * duplicates.
1056 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1057 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
1059
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001060static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 const char *p;
1064 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001065 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct net_device *d;
1067
1068 p = strnchr(name, IFNAMSIZ-1, '%');
1069 if (p) {
1070 /*
1071 * Verify the string as this thing may have come from
1072 * the user. There must be either one "%d" and no other "%"
1073 * characters.
1074 */
1075 if (p[1] != 'd' || strchr(p + 2, '%'))
1076 return -EINVAL;
1077
1078 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001079 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!inuse)
1081 return -ENOMEM;
1082
Eric W. Biederman881d9662007-09-17 11:56:21 -07001083 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!sscanf(d->name, name, &i))
1085 continue;
1086 if (i < 0 || i >= max_netdevices)
1087 continue;
1088
1089 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001090 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!strncmp(buf, d->name, IFNAMSIZ))
1092 set_bit(i, inuse);
1093 }
1094
1095 i = find_first_zero_bit(inuse, max_netdevices);
1096 free_page((unsigned long) inuse);
1097 }
1098
Octavian Purdilad9031022009-11-18 02:36:59 +00001099 if (buf != name)
1100 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001101 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* It is possible to run out of possible slots
1105 * when the name is long and there isn't enough space left
1106 * for the digits, or if all bits are used.
1107 */
1108 return -ENFILE;
1109}
1110
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001111/**
1112 * dev_alloc_name - allocate a name for a device
1113 * @dev: device
1114 * @name: name format string
1115 *
1116 * Passed a format string - eg "lt%d" it will try and find a suitable
1117 * id. It scans list of devices to build up a free map, then chooses
1118 * the first empty slot. The caller must hold the dev_base or rtnl lock
1119 * while allocating the name and adding the device in order to avoid
1120 * duplicates.
1121 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1122 * Returns the number of the unit assigned or a negative errno code.
1123 */
1124
1125int dev_alloc_name(struct net_device *dev, const char *name)
1126{
1127 char buf[IFNAMSIZ];
1128 struct net *net;
1129 int ret;
1130
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001131 BUG_ON(!dev_net(dev));
1132 net = dev_net(dev);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001133 ret = __dev_alloc_name(net, name, buf);
1134 if (ret >= 0)
1135 strlcpy(dev->name, buf, IFNAMSIZ);
1136 return ret;
1137}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001138EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001139
Gao feng828de4f2012-09-13 20:58:27 +00001140static int dev_alloc_name_ns(struct net *net,
1141 struct net_device *dev,
1142 const char *name)
Octavian Purdilad9031022009-11-18 02:36:59 +00001143{
Gao feng828de4f2012-09-13 20:58:27 +00001144 char buf[IFNAMSIZ];
1145 int ret;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001146
Gao feng828de4f2012-09-13 20:58:27 +00001147 ret = __dev_alloc_name(net, name, buf);
1148 if (ret >= 0)
1149 strlcpy(dev->name, buf, IFNAMSIZ);
1150 return ret;
1151}
1152
1153static int dev_get_valid_name(struct net *net,
1154 struct net_device *dev,
1155 const char *name)
1156{
1157 BUG_ON(!net);
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001158
Octavian Purdilad9031022009-11-18 02:36:59 +00001159 if (!dev_valid_name(name))
1160 return -EINVAL;
1161
Jiri Pirko1c5cae82011-04-30 01:21:32 +00001162 if (strchr(name, '%'))
Gao feng828de4f2012-09-13 20:58:27 +00001163 return dev_alloc_name_ns(net, dev, name);
Octavian Purdilad9031022009-11-18 02:36:59 +00001164 else if (__dev_get_by_name(net, name))
1165 return -EEXIST;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001166 else if (dev->name != name)
1167 strlcpy(dev->name, name, IFNAMSIZ);
Octavian Purdilad9031022009-11-18 02:36:59 +00001168
1169 return 0;
1170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172/**
1173 * dev_change_name - change name of a device
1174 * @dev: device
1175 * @newname: name (or format string) must be at least IFNAMSIZ
1176 *
1177 * Change name of a device, can pass format strings "eth%d".
1178 * for wildcarding.
1179 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001180int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Tom Gundersen238fa362014-07-14 16:37:23 +02001182 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001183 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001185 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001186 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001189 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001191 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (dev->flags & IFF_UP)
1193 return -EBUSY;
1194
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001195 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001196
1197 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001198 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001199 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001200 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001201
Herbert Xufcc5a032007-07-30 17:03:38 -07001202 memcpy(oldname, dev->name, IFNAMSIZ);
1203
Gao feng828de4f2012-09-13 20:58:27 +00001204 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001205 if (err < 0) {
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001206 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001207 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001210 if (oldname[0] && !strchr(oldname, '%'))
1211 netdev_info(dev, "renamed from %s\n", oldname);
1212
Tom Gundersen238fa362014-07-14 16:37:23 +02001213 old_assign_type = dev->name_assign_type;
1214 dev->name_assign_type = NET_NAME_RENAMED;
1215
Herbert Xufcc5a032007-07-30 17:03:38 -07001216rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001217 ret = device_rename(&dev->dev, dev->name);
1218 if (ret) {
1219 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001220 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001221 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001222 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001223 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001224
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001225 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001226
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001227 netdev_adjacent_rename_links(dev, oldname);
1228
Herbert Xu7f988ea2007-07-30 16:35:46 -07001229 write_lock_bh(&dev_base_lock);
Eric Dumazet372b2312011-05-17 13:56:59 -04001230 hlist_del_rcu(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001231 write_unlock_bh(&dev_base_lock);
1232
1233 synchronize_rcu();
1234
1235 write_lock_bh(&dev_base_lock);
1236 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001237 write_unlock_bh(&dev_base_lock);
1238
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001239 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001240 ret = notifier_to_errno(ret);
1241
1242 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001243 /* err >= 0 after dev_alloc_name() or stores the first errno */
1244 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001245 err = ret;
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001246 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001247 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001248 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001249 dev->name_assign_type = old_assign_type;
1250 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001251 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001252 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001253 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001254 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001255 }
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 return err;
1259}
1260
1261/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001262 * dev_set_alias - change ifalias of a device
1263 * @dev: device
1264 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001265 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001266 *
1267 * Set ifalias for a device,
1268 */
1269int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1270{
Florian Westphal6c557002017-10-02 23:50:05 +02001271 struct dev_ifalias *new_alias = NULL;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001272
1273 if (len >= IFALIASZ)
1274 return -EINVAL;
1275
Florian Westphal6c557002017-10-02 23:50:05 +02001276 if (len) {
1277 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1278 if (!new_alias)
1279 return -ENOMEM;
1280
1281 memcpy(new_alias->ifalias, alias, len);
1282 new_alias->ifalias[len] = 0;
Oliver Hartkopp96ca4a22008-09-23 21:23:19 -07001283 }
1284
Florian Westphal6c557002017-10-02 23:50:05 +02001285 mutex_lock(&ifalias_mutex);
1286 rcu_swap_protected(dev->ifalias, new_alias,
1287 mutex_is_locked(&ifalias_mutex));
1288 mutex_unlock(&ifalias_mutex);
1289
1290 if (new_alias)
1291 kfree_rcu(new_alias, rcuhead);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001292
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001293 return len;
1294}
1295
Florian Westphal6c557002017-10-02 23:50:05 +02001296/**
1297 * dev_get_alias - get ifalias of a device
1298 * @dev: device
Florian Westphal20e88322017-10-04 13:56:50 +02001299 * @name: buffer to store name of ifalias
Florian Westphal6c557002017-10-02 23:50:05 +02001300 * @len: size of buffer
1301 *
1302 * get ifalias for a device. Caller must make sure dev cannot go
1303 * away, e.g. rcu read lock or own a reference count to device.
1304 */
1305int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1306{
1307 const struct dev_ifalias *alias;
1308 int ret = 0;
1309
1310 rcu_read_lock();
1311 alias = rcu_dereference(dev->ifalias);
1312 if (alias)
1313 ret = snprintf(name, len, "%s", alias->ifalias);
1314 rcu_read_unlock();
1315
1316 return ret;
1317}
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001318
1319/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001320 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001321 * @dev: device to cause notification
1322 *
1323 * Called to indicate a device has changed features.
1324 */
1325void netdev_features_change(struct net_device *dev)
1326{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001327 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001328}
1329EXPORT_SYMBOL(netdev_features_change);
1330
1331/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * netdev_state_change - device changes state
1333 * @dev: device to cause notification
1334 *
1335 * Called to indicate a device has changed state. This function calls
1336 * the notifier chains for netdev_chain and sends a NEWLINK message
1337 * to the routing socket.
1338 */
1339void netdev_state_change(struct net_device *dev)
1340{
1341 if (dev->flags & IFF_UP) {
Loic Prylli54951192014-07-01 21:39:43 -07001342 struct netdev_notifier_change_info change_info;
1343
1344 change_info.flags_changed = 0;
1345 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
1346 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001347 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001350EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Amerigo Wangee89bab2012-08-09 22:14:56 +00001352/**
tcharding722c9a02017-02-09 17:56:04 +11001353 * netdev_notify_peers - notify network peers about existence of @dev
1354 * @dev: network device
Amerigo Wangee89bab2012-08-09 22:14:56 +00001355 *
1356 * Generate traffic such that interested network peers are aware of
1357 * @dev, such as by generating a gratuitous ARP. This may be used when
1358 * a device wants to inform the rest of the network about some sort of
1359 * reconfiguration such as a failover event or virtual machine
1360 * migration.
1361 */
1362void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001363{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001364 rtnl_lock();
1365 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich37c343b2017-03-14 08:58:08 -04001366 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001367 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001368}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001369EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001370
Patrick McHardybd380812010-02-26 06:34:53 +00001371static int __dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001373 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001374 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001376 ASSERT_RTNL();
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!netif_device_present(dev))
1379 return -ENODEV;
1380
Neil Hormanca99ca12013-02-05 08:05:43 +00001381 /* Block netpoll from trying to do any rx path servicing.
1382 * If we don't do this there is a chance ndo_poll_controller
1383 * or ndo_poll may be running while we open the device
1384 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001385 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001386
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001387 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1388 ret = notifier_to_errno(ret);
1389 if (ret)
1390 return ret;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001393
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001394 if (ops->ndo_validate_addr)
1395 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001396
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001397 if (!ret && ops->ndo_open)
1398 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Eric W. Biederman66b55522014-03-27 15:39:03 -07001400 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001401
Jeff Garzikbada3392007-10-23 20:19:37 -07001402 if (ret)
1403 clear_bit(__LINK_STATE_START, &dev->state);
1404 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001406 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001408 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return ret;
1412}
Patrick McHardybd380812010-02-26 06:34:53 +00001413
1414/**
1415 * dev_open - prepare an interface for use.
1416 * @dev: device to open
1417 *
1418 * Takes a device from down to up state. The device's private open
1419 * function is invoked and then the multicast lists are loaded. Finally
1420 * the device is moved into the up state and a %NETDEV_UP message is
1421 * sent to the netdev notifier chain.
1422 *
1423 * Calling this function on an active interface is a nop. On a failure
1424 * a negative errno code is returned.
1425 */
1426int dev_open(struct net_device *dev)
1427{
1428 int ret;
1429
Patrick McHardybd380812010-02-26 06:34:53 +00001430 if (dev->flags & IFF_UP)
1431 return 0;
1432
Patrick McHardybd380812010-02-26 06:34:53 +00001433 ret = __dev_open(dev);
1434 if (ret < 0)
1435 return ret;
1436
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001437 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001438 call_netdevice_notifiers(NETDEV_UP, dev);
1439
1440 return ret;
1441}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001442EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
stephen hemminger7051b882017-07-18 15:59:27 -07001444static void __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Octavian Purdila44345722010-12-13 12:44:07 +00001446 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001447
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001448 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001449 might_sleep();
1450
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001451 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001452 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001453 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001454
Octavian Purdila44345722010-12-13 12:44:07 +00001455 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Octavian Purdila44345722010-12-13 12:44:07 +00001457 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Octavian Purdila44345722010-12-13 12:44:07 +00001459 /* Synchronize to scheduled poll. We cannot touch poll list, it
1460 * can be even on different cpu. So just clear netif_running().
1461 *
1462 * dev->stop() will invoke napi_disable() on all of it's
1463 * napi_struct instances on this device.
1464 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001465 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Octavian Purdila44345722010-12-13 12:44:07 +00001468 dev_deactivate_many(head);
1469
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001470 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001471 const struct net_device_ops *ops = dev->netdev_ops;
1472
1473 /*
1474 * Call the device specific close. This cannot fail.
1475 * Only if device is UP
1476 *
1477 * We allow it to be called even after a DETACH hot-plug
1478 * event.
1479 */
1480 if (ops->ndo_stop)
1481 ops->ndo_stop(dev);
1482
Octavian Purdila44345722010-12-13 12:44:07 +00001483 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001484 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001485 }
Octavian Purdila44345722010-12-13 12:44:07 +00001486}
1487
stephen hemminger7051b882017-07-18 15:59:27 -07001488static void __dev_close(struct net_device *dev)
Octavian Purdila44345722010-12-13 12:44:07 +00001489{
1490 LIST_HEAD(single);
1491
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001492 list_add(&dev->close_list, &single);
stephen hemminger7051b882017-07-18 15:59:27 -07001493 __dev_close_many(&single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001494 list_del(&single);
Octavian Purdila44345722010-12-13 12:44:07 +00001495}
1496
stephen hemminger7051b882017-07-18 15:59:27 -07001497void dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001498{
1499 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001500
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001501 /* Remove the devices that don't need to be closed */
1502 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001503 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001504 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001505
1506 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001507
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001508 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001509 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001510 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001511 if (unlink)
1512 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
David S. Miller99c4a262015-03-18 22:52:33 -04001515EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001516
1517/**
1518 * dev_close - shutdown an interface.
1519 * @dev: device to shutdown
1520 *
1521 * This function moves an active device into down state. A
1522 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1523 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1524 * chain.
1525 */
stephen hemminger7051b882017-07-18 15:59:27 -07001526void dev_close(struct net_device *dev)
Patrick McHardybd380812010-02-26 06:34:53 +00001527{
Eric Dumazete14a5992011-05-10 12:26:06 -07001528 if (dev->flags & IFF_UP) {
1529 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001530
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001531 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001532 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001533 list_del(&single);
1534 }
Patrick McHardybd380812010-02-26 06:34:53 +00001535}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001536EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001539/**
1540 * dev_disable_lro - disable Large Receive Offload on a device
1541 * @dev: device
1542 *
1543 * Disable Large Receive Offload (LRO) on a net device. Must be
1544 * called under RTNL. This is needed if received packets may be
1545 * forwarded to another interface.
1546 */
1547void dev_disable_lro(struct net_device *dev)
1548{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001549 struct net_device *lower_dev;
1550 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001551
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001552 dev->wanted_features &= ~NETIF_F_LRO;
1553 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001554
Michał Mirosław22d59692011-04-21 12:42:15 +00001555 if (unlikely(dev->features & NETIF_F_LRO))
1556 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001557
1558 netdev_for_each_lower_dev(dev, lower_dev, iter)
1559 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001560}
1561EXPORT_SYMBOL(dev_disable_lro);
1562
Jiri Pirko351638e2013-05-28 01:30:21 +00001563static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1564 struct net_device *dev)
1565{
1566 struct netdev_notifier_info info;
1567
1568 netdev_notifier_info_init(&info, dev);
1569 return nb->notifier_call(nb, val, &info);
1570}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001571
Eric W. Biederman881d9662007-09-17 11:56:21 -07001572static int dev_boot_phase = 1;
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/**
tcharding722c9a02017-02-09 17:56:04 +11001575 * register_netdevice_notifier - register a network notifier block
1576 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 *
tcharding722c9a02017-02-09 17:56:04 +11001578 * Register a notifier to be called when network device events occur.
1579 * The notifier passed is linked into the kernel structures and must
1580 * not be reused until it has been unregistered. A negative errno code
1581 * is returned on a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 *
tcharding722c9a02017-02-09 17:56:04 +11001583 * When registered all registration and up events are replayed
1584 * to the new notifier to allow device to have a race free
1585 * view of the network device list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 */
1587
1588int register_netdevice_notifier(struct notifier_block *nb)
1589{
1590 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001591 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001592 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int err;
1594
1595 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001596 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001597 if (err)
1598 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001599 if (dev_boot_phase)
1600 goto unlock;
1601 for_each_net(net) {
1602 for_each_netdev(net, dev) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001603 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001604 err = notifier_to_errno(err);
1605 if (err)
1606 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Eric W. Biederman881d9662007-09-17 11:56:21 -07001608 if (!(dev->flags & IFF_UP))
1609 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001610
Jiri Pirko351638e2013-05-28 01:30:21 +00001611 call_netdevice_notifier(nb, NETDEV_UP, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001614
1615unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 rtnl_unlock();
1617 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001618
1619rollback:
1620 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001621 for_each_net(net) {
1622 for_each_netdev(net, dev) {
1623 if (dev == last)
RongQing.Li8f891482011-11-30 23:43:07 -05001624 goto outroll;
Herbert Xufcc5a032007-07-30 17:03:38 -07001625
Eric W. Biederman881d9662007-09-17 11:56:21 -07001626 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001627 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1628 dev);
1629 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001630 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001631 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001632 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001633 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001634
RongQing.Li8f891482011-11-30 23:43:07 -05001635outroll:
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001636 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001637 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001639EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641/**
tcharding722c9a02017-02-09 17:56:04 +11001642 * unregister_netdevice_notifier - unregister a network notifier block
1643 * @nb: notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
tcharding722c9a02017-02-09 17:56:04 +11001645 * Unregister a notifier previously registered by
1646 * register_netdevice_notifier(). The notifier is unlinked into the
1647 * kernel structures and may then be reused. A negative errno code
1648 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001649 *
tcharding722c9a02017-02-09 17:56:04 +11001650 * After unregistering unregister and down device events are synthesized
1651 * for all devices on the device list to the removed notifier to remove
1652 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
1654
1655int unregister_netdevice_notifier(struct notifier_block *nb)
1656{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001657 struct net_device *dev;
1658 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001659 int err;
1660
1661 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001662 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001663 if (err)
1664 goto unlock;
1665
1666 for_each_net(net) {
1667 for_each_netdev(net, dev) {
1668 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001669 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1670 dev);
1671 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001672 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001673 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001674 }
1675 }
1676unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001677 rtnl_unlock();
1678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001680EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001683 * call_netdevice_notifiers_info - call all network notifier blocks
1684 * @val: value passed unmodified to notifier function
1685 * @dev: net_device pointer passed unmodified to notifier function
1686 * @info: notifier information data
1687 *
1688 * Call all network notifier blocks. Parameters and return value
1689 * are as for raw_notifier_call_chain().
1690 */
1691
stephen hemminger1d143d92013-12-29 14:01:29 -08001692static int call_netdevice_notifiers_info(unsigned long val,
1693 struct net_device *dev,
1694 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001695{
1696 ASSERT_RTNL();
1697 netdev_notifier_info_init(info, dev);
1698 return raw_notifier_call_chain(&netdev_chain, val, info);
1699}
Jiri Pirko351638e2013-05-28 01:30:21 +00001700
1701/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 * call_netdevice_notifiers - call all network notifier blocks
1703 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001704 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *
1706 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001707 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
1709
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001710int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Jiri Pirko351638e2013-05-28 01:30:21 +00001712 struct netdev_notifier_info info;
1713
1714 return call_netdevice_notifiers_info(val, dev, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001716EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Pablo Neira1cf519002015-05-13 18:19:37 +02001718#ifdef CONFIG_NET_INGRESS
Daniel Borkmann45771392015-04-10 23:07:54 +02001719static struct static_key ingress_needed __read_mostly;
1720
1721void net_inc_ingress_queue(void)
1722{
1723 static_key_slow_inc(&ingress_needed);
1724}
1725EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1726
1727void net_dec_ingress_queue(void)
1728{
1729 static_key_slow_dec(&ingress_needed);
1730}
1731EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1732#endif
1733
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001734#ifdef CONFIG_NET_EGRESS
1735static struct static_key egress_needed __read_mostly;
1736
1737void net_inc_egress_queue(void)
1738{
1739 static_key_slow_inc(&egress_needed);
1740}
1741EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1742
1743void net_dec_egress_queue(void)
1744{
1745 static_key_slow_dec(&egress_needed);
1746}
1747EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1748#endif
1749
Ingo Molnarc5905af2012-02-24 08:31:31 +01001750static struct static_key netstamp_needed __read_mostly;
Eric Dumazetb90e5792011-11-28 11:16:50 +00001751#ifdef HAVE_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00001752static atomic_t netstamp_needed_deferred;
Eric Dumazet13baa002017-03-01 14:28:39 -08001753static atomic_t netstamp_wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001754static void netstamp_clear(struct work_struct *work)
1755{
1756 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet13baa002017-03-01 14:28:39 -08001757 int wanted;
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001758
Eric Dumazet13baa002017-03-01 14:28:39 -08001759 wanted = atomic_add_return(deferred, &netstamp_wanted);
1760 if (wanted > 0)
1761 static_key_enable(&netstamp_needed);
1762 else
1763 static_key_disable(&netstamp_needed);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001764}
1765static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001766#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768void net_enable_timestamp(void)
1769{
Eric Dumazet13baa002017-03-01 14:28:39 -08001770#ifdef HAVE_JUMP_LABEL
1771 int wanted;
1772
1773 while (1) {
1774 wanted = atomic_read(&netstamp_wanted);
1775 if (wanted <= 0)
1776 break;
1777 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1778 return;
1779 }
1780 atomic_inc(&netstamp_needed_deferred);
1781 schedule_work(&netstamp_work);
1782#else
Ingo Molnarc5905af2012-02-24 08:31:31 +01001783 static_key_slow_inc(&netstamp_needed);
Eric Dumazet13baa002017-03-01 14:28:39 -08001784#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001786EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788void net_disable_timestamp(void)
1789{
Eric Dumazetb90e5792011-11-28 11:16:50 +00001790#ifdef HAVE_JUMP_LABEL
Eric Dumazet13baa002017-03-01 14:28:39 -08001791 int wanted;
1792
1793 while (1) {
1794 wanted = atomic_read(&netstamp_wanted);
1795 if (wanted <= 1)
1796 break;
1797 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1798 return;
1799 }
1800 atomic_dec(&netstamp_needed_deferred);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001801 schedule_work(&netstamp_work);
1802#else
Ingo Molnarc5905af2012-02-24 08:31:31 +01001803 static_key_slow_dec(&netstamp_needed);
Eric Dumazet5fa8bbd2017-02-02 10:31:35 -08001804#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001806EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Eric Dumazet3b098e22010-05-15 23:57:10 -07001808static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Thomas Gleixner2456e852016-12-25 11:38:40 +01001810 skb->tstamp = 0;
Ingo Molnarc5905af2012-02-24 08:31:31 +01001811 if (static_key_false(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001812 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Eric Dumazet588f0332011-11-15 04:12:55 +00001815#define net_timestamp_check(COND, SKB) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001816 if (static_key_false(&netstamp_needed)) { \
Thomas Gleixner2456e852016-12-25 11:38:40 +01001817 if ((COND) && !(SKB)->tstamp) \
Eric Dumazet588f0332011-11-15 04:12:55 +00001818 __net_timestamp(SKB); \
1819 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07001820
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02001821bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001822{
1823 unsigned int len;
1824
1825 if (!(dev->flags & IFF_UP))
1826 return false;
1827
1828 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1829 if (skb->len <= len)
1830 return true;
1831
1832 /* if TSO is enabled, we don't care about the length as the packet
1833 * could be forwarded without being segmented before
1834 */
1835 if (skb_is_gso(skb))
1836 return true;
1837
1838 return false;
1839}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04001840EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001841
Herbert Xua0265d22014-04-17 13:45:03 +08001842int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1843{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001844 int ret = ____dev_forward_skb(dev, skb);
1845
1846 if (likely(!ret)) {
1847 skb->protocol = eth_type_trans(skb, dev);
1848 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08001849 }
1850
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001851 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08001852}
1853EXPORT_SYMBOL_GPL(__dev_forward_skb);
1854
Arnd Bergmann44540962009-11-26 06:07:08 +00001855/**
1856 * dev_forward_skb - loopback an skb to another netif
1857 *
1858 * @dev: destination network device
1859 * @skb: buffer to forward
1860 *
1861 * return values:
1862 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001863 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001864 *
1865 * dev_forward_skb can be used for injecting an skb from the
1866 * start_xmit function of one device into the receive queue
1867 * of another device.
1868 *
1869 * The receiving device may be in another namespace, so
1870 * we have to clear all information in the skb that could
1871 * impact namespace isolation.
1872 */
1873int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1874{
Herbert Xua0265d22014-04-17 13:45:03 +08001875 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001876}
1877EXPORT_SYMBOL_GPL(dev_forward_skb);
1878
Changli Gao71d9dec2010-12-15 19:57:25 +00001879static inline int deliver_skb(struct sk_buff *skb,
1880 struct packet_type *pt_prev,
1881 struct net_device *orig_dev)
1882{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001883 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00001884 return -ENOMEM;
Reshetova, Elena63354792017-06-30 13:07:58 +03001885 refcount_inc(&skb->users);
Changli Gao71d9dec2010-12-15 19:57:25 +00001886 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1887}
1888
Salam Noureddine7866a622015-01-27 11:35:48 -08001889static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1890 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001891 struct net_device *orig_dev,
1892 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08001893 struct list_head *ptype_list)
1894{
1895 struct packet_type *ptype, *pt_prev = *pt;
1896
1897 list_for_each_entry_rcu(ptype, ptype_list, list) {
1898 if (ptype->type != type)
1899 continue;
1900 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001901 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08001902 pt_prev = ptype;
1903 }
1904 *pt = pt_prev;
1905}
1906
Eric Leblondc0de08d2012-08-16 22:02:58 +00001907static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1908{
Eric Leblonda3d744e2012-11-06 02:10:10 +00001909 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001910 return false;
1911
1912 if (ptype->id_match)
1913 return ptype->id_match(ptype, skb->sk);
1914 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1915 return true;
1916
1917 return false;
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920/*
1921 * Support routine. Sends outgoing frames to any network
1922 * taps currently in use.
1923 */
1924
David Ahern74b20582016-05-10 11:19:50 -07001925void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00001928 struct sk_buff *skb2 = NULL;
1929 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08001930 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08001933again:
1934 list_for_each_entry_rcu(ptype, ptype_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /* Never send packets back to the socket
1936 * they originated from - MvS (miquels@drinkel.ow.org)
1937 */
Salam Noureddine7866a622015-01-27 11:35:48 -08001938 if (skb_loop_sk(ptype, skb))
1939 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00001940
Salam Noureddine7866a622015-01-27 11:35:48 -08001941 if (pt_prev) {
1942 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00001943 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08001944 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Salam Noureddine7866a622015-01-27 11:35:48 -08001946
1947 /* need to clone skb, done only once */
1948 skb2 = skb_clone(skb, GFP_ATOMIC);
1949 if (!skb2)
1950 goto out_unlock;
1951
1952 net_timestamp_set(skb2);
1953
1954 /* skb->nh should be correctly
1955 * set by sender, so that the second statement is
1956 * just protection against buggy protocols.
1957 */
1958 skb_reset_mac_header(skb2);
1959
1960 if (skb_network_header(skb2) < skb2->data ||
1961 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
1962 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1963 ntohs(skb2->protocol),
1964 dev->name);
1965 skb_reset_network_header(skb2);
1966 }
1967
1968 skb2->transport_header = skb2->network_header;
1969 skb2->pkt_type = PACKET_OUTGOING;
1970 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Salam Noureddine7866a622015-01-27 11:35:48 -08001972
1973 if (ptype_list == &ptype_all) {
1974 ptype_list = &dev->ptype_all;
1975 goto again;
1976 }
1977out_unlock:
Willem de Bruijn581fe0e2017-09-22 19:42:37 -04001978 if (pt_prev) {
1979 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
1980 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1981 else
1982 kfree_skb(skb2);
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 rcu_read_unlock();
1985}
David Ahern74b20582016-05-10 11:19:50 -07001986EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Ben Hutchings2c530402012-07-10 10:55:09 +00001988/**
1989 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00001990 * @dev: Network device
1991 * @txq: number of queues available
1992 *
1993 * If real_num_tx_queues is changed the tc mappings may no longer be
1994 * valid. To resolve this verify the tc mapping remains valid and if
1995 * not NULL the mapping. With no priorities mapping to this
1996 * offset/count pair it will no longer be used. In the worst case TC0
1997 * is invalid nothing can be done so disable priority mappings. If is
1998 * expected that drivers will fix this mapping if they can before
1999 * calling netif_set_real_num_tx_queues.
2000 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00002001static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00002002{
2003 int i;
2004 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2005
2006 /* If TC0 is invalidated disable TC mapping */
2007 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002008 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00002009 dev->num_tc = 0;
2010 return;
2011 }
2012
2013 /* Invalidated prio to tc mappings set to TC0 */
2014 for (i = 1; i < TC_BITMASK + 1; i++) {
2015 int q = netdev_get_prio_tc_map(dev, i);
2016
2017 tc = &dev->tc_to_txq[q];
2018 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002019 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2020 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00002021 netdev_set_prio_tc_map(dev, i, 0);
2022 }
2023 }
2024}
2025
Alexander Duyck8d059b02016-10-28 11:43:49 -04002026int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2027{
2028 if (dev->num_tc) {
2029 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2030 int i;
2031
2032 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2033 if ((txq - tc->offset) < tc->count)
2034 return i;
2035 }
2036
2037 return -1;
2038 }
2039
2040 return 0;
2041}
2042
Alexander Duyck537c00d2013-01-10 08:57:02 +00002043#ifdef CONFIG_XPS
2044static DEFINE_MUTEX(xps_map_mutex);
2045#define xmap_dereference(P) \
2046 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2047
Alexander Duyck6234f872016-10-28 11:46:49 -04002048static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2049 int tci, u16 index)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002050{
2051 struct xps_map *map = NULL;
2052 int pos;
2053
2054 if (dev_maps)
Alexander Duyck6234f872016-10-28 11:46:49 -04002055 map = xmap_dereference(dev_maps->cpu_map[tci]);
2056 if (!map)
2057 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002058
Alexander Duyck6234f872016-10-28 11:46:49 -04002059 for (pos = map->len; pos--;) {
2060 if (map->queues[pos] != index)
2061 continue;
2062
2063 if (map->len > 1) {
2064 map->queues[pos] = map->queues[--map->len];
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002065 break;
2066 }
Alexander Duyck6234f872016-10-28 11:46:49 -04002067
2068 RCU_INIT_POINTER(dev_maps->cpu_map[tci], NULL);
2069 kfree_rcu(map, rcu);
2070 return false;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002071 }
2072
Alexander Duyck6234f872016-10-28 11:46:49 -04002073 return true;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002074}
2075
Alexander Duyck6234f872016-10-28 11:46:49 -04002076static bool remove_xps_queue_cpu(struct net_device *dev,
2077 struct xps_dev_maps *dev_maps,
2078 int cpu, u16 offset, u16 count)
2079{
Alexander Duyck184c4492016-10-28 11:50:13 -04002080 int num_tc = dev->num_tc ? : 1;
2081 bool active = false;
2082 int tci;
Alexander Duyck6234f872016-10-28 11:46:49 -04002083
Alexander Duyck184c4492016-10-28 11:50:13 -04002084 for (tci = cpu * num_tc; num_tc--; tci++) {
2085 int i, j;
2086
2087 for (i = count, j = offset; i--; j++) {
2088 if (!remove_xps_queue(dev_maps, cpu, j))
2089 break;
2090 }
2091
2092 active |= i < 0;
Alexander Duyck6234f872016-10-28 11:46:49 -04002093 }
2094
Alexander Duyck184c4492016-10-28 11:50:13 -04002095 return active;
Alexander Duyck6234f872016-10-28 11:46:49 -04002096}
2097
2098static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2099 u16 count)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002100{
2101 struct xps_dev_maps *dev_maps;
Alexander Duyck024e9672013-01-10 08:57:46 +00002102 int cpu, i;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002103 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002104
2105 mutex_lock(&xps_map_mutex);
2106 dev_maps = xmap_dereference(dev->xps_maps);
2107
2108 if (!dev_maps)
2109 goto out_no_maps;
2110
Alexander Duyck6234f872016-10-28 11:46:49 -04002111 for_each_possible_cpu(cpu)
2112 active |= remove_xps_queue_cpu(dev, dev_maps, cpu,
2113 offset, count);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002114
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002115 if (!active) {
Alexander Duyck537c00d2013-01-10 08:57:02 +00002116 RCU_INIT_POINTER(dev->xps_maps, NULL);
2117 kfree_rcu(dev_maps, rcu);
2118 }
2119
Alexander Duyck6234f872016-10-28 11:46:49 -04002120 for (i = offset + (count - 1); count--; i--)
Alexander Duyck024e9672013-01-10 08:57:46 +00002121 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
2122 NUMA_NO_NODE);
2123
Alexander Duyck537c00d2013-01-10 08:57:02 +00002124out_no_maps:
2125 mutex_unlock(&xps_map_mutex);
2126}
2127
Alexander Duyck6234f872016-10-28 11:46:49 -04002128static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2129{
2130 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2131}
2132
Alexander Duyck01c5f862013-01-10 08:57:35 +00002133static struct xps_map *expand_xps_map(struct xps_map *map,
2134 int cpu, u16 index)
2135{
2136 struct xps_map *new_map;
2137 int alloc_len = XPS_MIN_MAP_ALLOC;
2138 int i, pos;
2139
2140 for (pos = 0; map && pos < map->len; pos++) {
2141 if (map->queues[pos] != index)
2142 continue;
2143 return map;
2144 }
2145
2146 /* Need to add queue to this CPU's existing map */
2147 if (map) {
2148 if (pos < map->alloc_len)
2149 return map;
2150
2151 alloc_len = map->alloc_len * 2;
2152 }
2153
2154 /* Need to allocate new map to store queue on this CPU's map */
2155 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2156 cpu_to_node(cpu));
2157 if (!new_map)
2158 return NULL;
2159
2160 for (i = 0; i < pos; i++)
2161 new_map->queues[i] = map->queues[i];
2162 new_map->alloc_len = alloc_len;
2163 new_map->len = pos;
2164
2165 return new_map;
2166}
2167
Michael S. Tsirkin35735402013-10-02 09:14:06 +03002168int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2169 u16 index)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002170{
Alexander Duyck01c5f862013-01-10 08:57:35 +00002171 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Alexander Duyck184c4492016-10-28 11:50:13 -04002172 int i, cpu, tci, numa_node_id = -2;
2173 int maps_sz, num_tc = 1, tc = 0;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002174 struct xps_map *map, *new_map;
Alexander Duyck01c5f862013-01-10 08:57:35 +00002175 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002176
Alexander Duyck184c4492016-10-28 11:50:13 -04002177 if (dev->num_tc) {
2178 num_tc = dev->num_tc;
2179 tc = netdev_txq_to_tc(dev, index);
2180 if (tc < 0)
2181 return -EINVAL;
2182 }
2183
2184 maps_sz = XPS_DEV_MAPS_SIZE(num_tc);
2185 if (maps_sz < L1_CACHE_BYTES)
2186 maps_sz = L1_CACHE_BYTES;
2187
Alexander Duyck537c00d2013-01-10 08:57:02 +00002188 mutex_lock(&xps_map_mutex);
2189
2190 dev_maps = xmap_dereference(dev->xps_maps);
2191
Alexander Duyck01c5f862013-01-10 08:57:35 +00002192 /* allocate memory for queue storage */
Alexander Duyck184c4492016-10-28 11:50:13 -04002193 for_each_cpu_and(cpu, cpu_online_mask, mask) {
Alexander Duyck01c5f862013-01-10 08:57:35 +00002194 if (!new_dev_maps)
2195 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002196 if (!new_dev_maps) {
2197 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002198 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002199 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002200
Alexander Duyck184c4492016-10-28 11:50:13 -04002201 tci = cpu * num_tc + tc;
2202 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[tci]) :
Alexander Duyck01c5f862013-01-10 08:57:35 +00002203 NULL;
2204
2205 map = expand_xps_map(map, cpu, index);
2206 if (!map)
2207 goto error;
2208
Alexander Duyck184c4492016-10-28 11:50:13 -04002209 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002210 }
2211
2212 if (!new_dev_maps)
2213 goto out_no_new_maps;
2214
2215 for_each_possible_cpu(cpu) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002216 /* copy maps belonging to foreign traffic classes */
2217 for (i = tc, tci = cpu * num_tc; dev_maps && i--; tci++) {
2218 /* fill in the new device map from the old device map */
2219 map = xmap_dereference(dev_maps->cpu_map[tci]);
2220 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2221 }
2222
2223 /* We need to explicitly update tci as prevous loop
2224 * could break out early if dev_maps is NULL.
2225 */
2226 tci = cpu * num_tc + tc;
2227
Alexander Duyck01c5f862013-01-10 08:57:35 +00002228 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
2229 /* add queue to CPU maps */
2230 int pos = 0;
2231
Alexander Duyck184c4492016-10-28 11:50:13 -04002232 map = xmap_dereference(new_dev_maps->cpu_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002233 while ((pos < map->len) && (map->queues[pos] != index))
2234 pos++;
2235
2236 if (pos == map->len)
2237 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002238#ifdef CONFIG_NUMA
Alexander Duyck537c00d2013-01-10 08:57:02 +00002239 if (numa_node_id == -2)
2240 numa_node_id = cpu_to_node(cpu);
2241 else if (numa_node_id != cpu_to_node(cpu))
2242 numa_node_id = -1;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002243#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002244 } else if (dev_maps) {
2245 /* fill in the new device map from the old device map */
Alexander Duyck184c4492016-10-28 11:50:13 -04002246 map = xmap_dereference(dev_maps->cpu_map[tci]);
2247 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002248 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002249
Alexander Duyck184c4492016-10-28 11:50:13 -04002250 /* copy maps belonging to foreign traffic classes */
2251 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2252 /* fill in the new device map from the old device map */
2253 map = xmap_dereference(dev_maps->cpu_map[tci]);
2254 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2255 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002256 }
2257
Alexander Duyck01c5f862013-01-10 08:57:35 +00002258 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2259
Alexander Duyck537c00d2013-01-10 08:57:02 +00002260 /* Cleanup old maps */
Alexander Duyck184c4492016-10-28 11:50:13 -04002261 if (!dev_maps)
2262 goto out_no_old_maps;
2263
2264 for_each_possible_cpu(cpu) {
2265 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2266 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2267 map = xmap_dereference(dev_maps->cpu_map[tci]);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002268 if (map && map != new_map)
2269 kfree_rcu(map, rcu);
2270 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002271 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002272
Alexander Duyck184c4492016-10-28 11:50:13 -04002273 kfree_rcu(dev_maps, rcu);
2274
2275out_no_old_maps:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002276 dev_maps = new_dev_maps;
2277 active = true;
2278
2279out_no_new_maps:
2280 /* update Tx queue numa node */
Alexander Duyck537c00d2013-01-10 08:57:02 +00002281 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2282 (numa_node_id >= 0) ? numa_node_id :
2283 NUMA_NO_NODE);
2284
Alexander Duyck01c5f862013-01-10 08:57:35 +00002285 if (!dev_maps)
2286 goto out_no_maps;
2287
2288 /* removes queue from unused CPUs */
2289 for_each_possible_cpu(cpu) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002290 for (i = tc, tci = cpu * num_tc; i--; tci++)
2291 active |= remove_xps_queue(dev_maps, tci, index);
2292 if (!cpumask_test_cpu(cpu, mask) || !cpu_online(cpu))
2293 active |= remove_xps_queue(dev_maps, tci, index);
2294 for (i = num_tc - tc, tci++; --i; tci++)
2295 active |= remove_xps_queue(dev_maps, tci, index);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002296 }
2297
2298 /* free map if not active */
2299 if (!active) {
2300 RCU_INIT_POINTER(dev->xps_maps, NULL);
2301 kfree_rcu(dev_maps, rcu);
2302 }
2303
2304out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002305 mutex_unlock(&xps_map_mutex);
2306
2307 return 0;
2308error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002309 /* remove any maps that we added */
2310 for_each_possible_cpu(cpu) {
Alexander Duyck184c4492016-10-28 11:50:13 -04002311 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2312 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2313 map = dev_maps ?
2314 xmap_dereference(dev_maps->cpu_map[tci]) :
2315 NULL;
2316 if (new_map && new_map != map)
2317 kfree(new_map);
2318 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002319 }
2320
Alexander Duyck537c00d2013-01-10 08:57:02 +00002321 mutex_unlock(&xps_map_mutex);
2322
Alexander Duyck537c00d2013-01-10 08:57:02 +00002323 kfree(new_dev_maps);
2324 return -ENOMEM;
2325}
2326EXPORT_SYMBOL(netif_set_xps_queue);
2327
2328#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002329void netdev_reset_tc(struct net_device *dev)
2330{
Alexander Duyck6234f872016-10-28 11:46:49 -04002331#ifdef CONFIG_XPS
2332 netif_reset_xps_queues_gt(dev, 0);
2333#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002334 dev->num_tc = 0;
2335 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2336 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2337}
2338EXPORT_SYMBOL(netdev_reset_tc);
2339
2340int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2341{
2342 if (tc >= dev->num_tc)
2343 return -EINVAL;
2344
Alexander Duyck6234f872016-10-28 11:46:49 -04002345#ifdef CONFIG_XPS
2346 netif_reset_xps_queues(dev, offset, count);
2347#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002348 dev->tc_to_txq[tc].count = count;
2349 dev->tc_to_txq[tc].offset = offset;
2350 return 0;
2351}
2352EXPORT_SYMBOL(netdev_set_tc_queue);
2353
2354int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2355{
2356 if (num_tc > TC_MAX_QUEUE)
2357 return -EINVAL;
2358
Alexander Duyck6234f872016-10-28 11:46:49 -04002359#ifdef CONFIG_XPS
2360 netif_reset_xps_queues_gt(dev, 0);
2361#endif
Alexander Duyck9cf1f6a2016-10-28 11:43:20 -04002362 dev->num_tc = num_tc;
2363 return 0;
2364}
2365EXPORT_SYMBOL(netdev_set_num_tc);
2366
John Fastabendf0796d52010-07-01 13:21:57 +00002367/*
2368 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2369 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2370 */
Tom Herberte6484932010-10-18 18:04:39 +00002371int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002372{
Tom Herbert1d24eb42010-11-21 13:17:27 +00002373 int rc;
2374
Tom Herberte6484932010-10-18 18:04:39 +00002375 if (txq < 1 || txq > dev->num_tx_queues)
2376 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002377
Ben Hutchings5c565802011-02-15 19:39:21 +00002378 if (dev->reg_state == NETREG_REGISTERED ||
2379 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002380 ASSERT_RTNL();
2381
Tom Herbert1d24eb42010-11-21 13:17:27 +00002382 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2383 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002384 if (rc)
2385 return rc;
2386
John Fastabend4f57c082011-01-17 08:06:04 +00002387 if (dev->num_tc)
2388 netif_setup_tc(dev, txq);
2389
Alexander Duyck024e9672013-01-10 08:57:46 +00002390 if (txq < dev->real_num_tx_queues) {
Tom Herberte6484932010-10-18 18:04:39 +00002391 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002392#ifdef CONFIG_XPS
2393 netif_reset_xps_queues_gt(dev, txq);
2394#endif
2395 }
John Fastabendf0796d52010-07-01 13:21:57 +00002396 }
Tom Herberte6484932010-10-18 18:04:39 +00002397
2398 dev->real_num_tx_queues = txq;
2399 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002400}
2401EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002402
Michael Daltona953be52014-01-16 22:23:28 -08002403#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002404/**
2405 * netif_set_real_num_rx_queues - set actual number of RX queues used
2406 * @dev: Network device
2407 * @rxq: Actual number of RX queues
2408 *
2409 * This must be called either with the rtnl_lock held or before
2410 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002411 * negative error code. If called before registration, it always
2412 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002413 */
2414int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2415{
2416 int rc;
2417
Tom Herbertbd25fa72010-10-18 18:00:16 +00002418 if (rxq < 1 || rxq > dev->num_rx_queues)
2419 return -EINVAL;
2420
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002421 if (dev->reg_state == NETREG_REGISTERED) {
2422 ASSERT_RTNL();
2423
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002424 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2425 rxq);
2426 if (rc)
2427 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002428 }
2429
2430 dev->real_num_rx_queues = rxq;
2431 return 0;
2432}
2433EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2434#endif
2435
Ben Hutchings2c530402012-07-10 10:55:09 +00002436/**
2437 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002438 *
2439 * This routine should set an upper limit on the number of RSS queues
2440 * used by default by multiqueue devices.
2441 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002442int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002443{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302444 return is_kdump_kernel() ?
2445 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002446}
2447EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2448
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002449static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002450{
2451 struct softnet_data *sd;
2452 unsigned long flags;
2453
2454 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002455 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002456 q->next_sched = NULL;
2457 *sd->output_queue_tailp = q;
2458 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002459 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2460 local_irq_restore(flags);
2461}
2462
David S. Miller37437bb2008-07-16 02:15:04 -07002463void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002464{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002465 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2466 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002467}
2468EXPORT_SYMBOL(__netif_schedule);
2469
Eric Dumazete6247022013-12-05 04:45:08 -08002470struct dev_kfree_skb_cb {
2471 enum skb_free_reason reason;
2472};
2473
2474static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002475{
Eric Dumazete6247022013-12-05 04:45:08 -08002476 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002477}
Denis Vlasenko56079432006-03-29 15:57:29 -08002478
John Fastabend46e5da42014-09-12 20:04:52 -07002479void netif_schedule_queue(struct netdev_queue *txq)
2480{
2481 rcu_read_lock();
2482 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2483 struct Qdisc *q = rcu_dereference(txq->qdisc);
2484
2485 __netif_schedule(q);
2486 }
2487 rcu_read_unlock();
2488}
2489EXPORT_SYMBOL(netif_schedule_queue);
2490
John Fastabend46e5da42014-09-12 20:04:52 -07002491void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2492{
2493 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2494 struct Qdisc *q;
2495
2496 rcu_read_lock();
2497 q = rcu_dereference(dev_queue->qdisc);
2498 __netif_schedule(q);
2499 rcu_read_unlock();
2500 }
2501}
2502EXPORT_SYMBOL(netif_tx_wake_queue);
2503
Eric Dumazete6247022013-12-05 04:45:08 -08002504void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2505{
2506 unsigned long flags;
2507
Myungho Jung98998862017-04-25 11:58:15 -07002508 if (unlikely(!skb))
2509 return;
2510
Reshetova, Elena63354792017-06-30 13:07:58 +03002511 if (likely(refcount_read(&skb->users) == 1)) {
Eric Dumazete6247022013-12-05 04:45:08 -08002512 smp_rmb();
Reshetova, Elena63354792017-06-30 13:07:58 +03002513 refcount_set(&skb->users, 0);
2514 } else if (likely(!refcount_dec_and_test(&skb->users))) {
Eric Dumazete6247022013-12-05 04:45:08 -08002515 return;
2516 }
2517 get_kfree_skb_cb(skb)->reason = reason;
2518 local_irq_save(flags);
2519 skb->next = __this_cpu_read(softnet_data.completion_queue);
2520 __this_cpu_write(softnet_data.completion_queue, skb);
2521 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2522 local_irq_restore(flags);
2523}
2524EXPORT_SYMBOL(__dev_kfree_skb_irq);
2525
2526void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08002527{
2528 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08002529 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08002530 else
2531 dev_kfree_skb(skb);
2532}
Eric Dumazete6247022013-12-05 04:45:08 -08002533EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08002534
2535
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002536/**
2537 * netif_device_detach - mark device as removed
2538 * @dev: network device
2539 *
2540 * Mark device as removed from system and therefore no longer available.
2541 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002542void netif_device_detach(struct net_device *dev)
2543{
2544 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2545 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002546 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002547 }
2548}
2549EXPORT_SYMBOL(netif_device_detach);
2550
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002551/**
2552 * netif_device_attach - mark device as attached
2553 * @dev: network device
2554 *
2555 * Mark device as attached from system and restart if needed.
2556 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002557void netif_device_attach(struct net_device *dev)
2558{
2559 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2560 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002561 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002562 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002563 }
2564}
2565EXPORT_SYMBOL(netif_device_attach);
2566
Jiri Pirko5605c762015-05-12 14:56:12 +02002567/*
2568 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2569 * to be used as a distribution range.
2570 */
2571u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
2572 unsigned int num_tx_queues)
2573{
2574 u32 hash;
2575 u16 qoffset = 0;
2576 u16 qcount = num_tx_queues;
2577
2578 if (skb_rx_queue_recorded(skb)) {
2579 hash = skb_get_rx_queue(skb);
2580 while (unlikely(hash >= num_tx_queues))
2581 hash -= num_tx_queues;
2582 return hash;
2583 }
2584
2585 if (dev->num_tc) {
2586 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
tchardingf4563a72017-02-09 17:56:07 +11002587
Jiri Pirko5605c762015-05-12 14:56:12 +02002588 qoffset = dev->tc_to_txq[tc].offset;
2589 qcount = dev->tc_to_txq[tc].count;
2590 }
2591
2592 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2593}
2594EXPORT_SYMBOL(__skb_tx_hash);
2595
Ben Hutchings36c92472012-01-17 07:57:56 +00002596static void skb_warn_bad_offload(const struct sk_buff *skb)
2597{
Wei Tang84d15ae2016-06-16 21:17:49 +08002598 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00002599 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01002600 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00002601
Ben Greearc846ad92013-04-19 10:45:52 +00002602 if (!net_ratelimit())
2603 return;
2604
Bjørn Mork88ad4172015-11-16 19:16:40 +01002605 if (dev) {
2606 if (dev->dev.parent)
2607 name = dev_driver_string(dev->dev.parent);
2608 else
2609 name = netdev_name(dev);
2610 }
Ben Hutchings36c92472012-01-17 07:57:56 +00002611 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2612 "gso_type=%d ip_summed=%d\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01002613 name, dev ? &dev->features : &null_features,
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002614 skb->sk ? &skb->sk->sk_route_caps : &null_features,
Ben Hutchings36c92472012-01-17 07:57:56 +00002615 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2616 skb_shinfo(skb)->gso_type, skb->ip_summed);
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/*
2620 * Invalidate hardware checksum when packet is to be mangled, and
2621 * complete checksum manually on outgoing path.
2622 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002623int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Al Virod3bc23e2006-11-14 21:24:49 -08002625 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07002626 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Patrick McHardy84fa7932006-08-29 16:44:56 -07002628 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07002629 goto out_set_summed;
2630
2631 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00002632 skb_warn_bad_offload(skb);
2633 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
2635
Eric Dumazetcef401d2013-01-25 20:34:37 +00002636 /* Before computing a checksum, we should make sure no frag could
2637 * be modified by an external entity : checksum could be wrong.
2638 */
2639 if (skb_has_shared_frag(skb)) {
2640 ret = __skb_linearize(skb);
2641 if (ret)
2642 goto out;
2643 }
2644
Michał Mirosław55508d62010-12-14 15:24:08 +00002645 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07002646 BUG_ON(offset >= skb_headlen(skb));
2647 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2648
2649 offset += skb->csum_offset;
2650 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2651
2652 if (skb_cloned(skb) &&
2653 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2655 if (ret)
2656 goto out;
2657 }
2658
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07002659 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07002660out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002662out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return ret;
2664}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002665EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Davide Carattib72b5bf2017-05-18 15:44:38 +02002667int skb_crc32c_csum_help(struct sk_buff *skb)
2668{
2669 __le32 crc32c_csum;
2670 int ret = 0, offset, start;
2671
2672 if (skb->ip_summed != CHECKSUM_PARTIAL)
2673 goto out;
2674
2675 if (unlikely(skb_is_gso(skb)))
2676 goto out;
2677
2678 /* Before computing a checksum, we should make sure no frag could
2679 * be modified by an external entity : checksum could be wrong.
2680 */
2681 if (unlikely(skb_has_shared_frag(skb))) {
2682 ret = __skb_linearize(skb);
2683 if (ret)
2684 goto out;
2685 }
2686 start = skb_checksum_start_offset(skb);
2687 offset = start + offsetof(struct sctphdr, checksum);
2688 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
2689 ret = -EINVAL;
2690 goto out;
2691 }
2692 if (skb_cloned(skb) &&
2693 !skb_clone_writable(skb, offset + sizeof(__le32))) {
2694 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2695 if (ret)
2696 goto out;
2697 }
2698 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
2699 skb->len - start, ~(__u32)0,
2700 crc32c_csum_stub));
2701 *(__le32 *)(skb->data + offset) = crc32c_csum;
2702 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +02002703 skb->csum_not_inet = 0;
Davide Carattib72b5bf2017-05-18 15:44:38 +02002704out:
2705 return ret;
2706}
2707
Vlad Yasevich53d64712014-03-27 17:26:18 -04002708__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002709{
2710 __be16 type = skb->protocol;
2711
Pravin B Shelar19acc322013-05-07 20:41:07 +00002712 /* Tunnel gso handlers can set protocol to ethernet. */
2713 if (type == htons(ETH_P_TEB)) {
2714 struct ethhdr *eth;
2715
2716 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2717 return 0;
2718
2719 eth = (struct ethhdr *)skb_mac_header(skb);
2720 type = eth->h_proto;
2721 }
2722
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09002723 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002724}
2725
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002726/**
2727 * skb_mac_gso_segment - mac layer segmentation handler.
2728 * @skb: buffer to segment
2729 * @features: features for the output path (see dev->features)
2730 */
2731struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2732 netdev_features_t features)
2733{
2734 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2735 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002736 int vlan_depth = skb->mac_len;
2737 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002738
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002739 if (unlikely(!type))
2740 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002741
Vlad Yasevich53d64712014-03-27 17:26:18 -04002742 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002743
2744 rcu_read_lock();
2745 list_for_each_entry_rcu(ptype, &offload_base, list) {
2746 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002747 segs = ptype->callbacks.gso_segment(skb, features);
2748 break;
2749 }
2750 }
2751 rcu_read_unlock();
2752
2753 __skb_push(skb, skb->data - skb_mac_header(skb));
2754
2755 return segs;
2756}
2757EXPORT_SYMBOL(skb_mac_gso_segment);
2758
2759
Cong Wang12b00042013-02-05 16:36:38 +00002760/* openvswitch calls this on rx path, so we need a different check.
2761 */
2762static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
2763{
2764 if (tx_path)
Tonghao Zhang93991222017-08-10 20:16:29 -07002765 return skb->ip_summed != CHECKSUM_PARTIAL;
Eric Dumazet6e7bc472017-02-03 14:29:42 -08002766
2767 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00002768}
2769
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002770/**
Cong Wang12b00042013-02-05 16:36:38 +00002771 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002772 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002773 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00002774 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002775 *
2776 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07002777 *
2778 * It may return NULL if the skb requires no segmentation. This is
2779 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03002780 *
2781 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002782 */
Cong Wang12b00042013-02-05 16:36:38 +00002783struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2784 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002785{
Eric Dumazetb2504a52017-01-31 10:20:32 -08002786 struct sk_buff *segs;
2787
Cong Wang12b00042013-02-05 16:36:38 +00002788 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002789 int err;
2790
Eric Dumazetb2504a52017-01-31 10:20:32 -08002791 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02002792 err = skb_cow_head(skb, 0);
2793 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07002794 return ERR_PTR(err);
2795 }
2796
Alexander Duyck802ab552016-04-10 21:45:03 -04002797 /* Only report GSO partial support if it will enable us to
2798 * support segmentation on this frame without needing additional
2799 * work.
2800 */
2801 if (features & NETIF_F_GSO_PARTIAL) {
2802 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
2803 struct net_device *dev = skb->dev;
2804
2805 partial_features |= dev->features & dev->gso_partial_features;
2806 if (!skb_gso_ok(skb, features | partial_features))
2807 features &= ~NETIF_F_GSO_PARTIAL;
2808 }
2809
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03002810 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
2811 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
2812
Pravin B Shelar68c33162013-02-14 14:02:41 +00002813 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07002814 SKB_GSO_CB(skb)->encap_level = 0;
2815
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002816 skb_reset_mac_header(skb);
2817 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002818
Eric Dumazetb2504a52017-01-31 10:20:32 -08002819 segs = skb_mac_gso_segment(skb, features);
2820
2821 if (unlikely(skb_needs_check(skb, tx_path)))
2822 skb_warn_bad_offload(skb);
2823
2824 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002825}
Cong Wang12b00042013-02-05 16:36:38 +00002826EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002827
Herbert Xufb286bb2005-11-10 13:01:24 -08002828/* Take action when hardware reception checksum errors are detected. */
2829#ifdef CONFIG_BUG
2830void netdev_rx_csum_fault(struct net_device *dev)
2831{
2832 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002833 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08002834 dump_stack();
2835 }
2836}
2837EXPORT_SYMBOL(netdev_rx_csum_fault);
2838#endif
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840/* Actually, we should eliminate this check as soon as we know, that:
2841 * 1. IOMMU is present and allows to map all the memory.
2842 * 2. No high memory really exists on this machine.
2843 */
2844
Florian Westphalc1e756b2014-05-05 15:00:44 +02002845static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Herbert Xu3d3a8532006-06-27 13:33:10 -07002847#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 int i;
tchardingf4563a72017-02-09 17:56:07 +11002849
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002850 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002851 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2852 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
tchardingf4563a72017-02-09 17:56:07 +11002853
Ian Campbellea2ab692011-08-22 23:44:58 +00002854 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002855 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00002856 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002859 if (PCI_DMA_BUS_IS_PHYS) {
2860 struct device *pdev = dev->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Eric Dumazet9092c652010-04-02 13:34:49 -07002862 if (!pdev)
2863 return 0;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002864 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002865 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2866 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
tchardingf4563a72017-02-09 17:56:07 +11002867
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002868 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2869 return 1;
2870 }
2871 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07002872#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return 0;
2874}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Simon Horman3b392dd2014-06-04 08:53:17 +09002876/* If MPLS offload request, verify we are testing hardware MPLS features
2877 * instead of standard features for the netdev.
2878 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08002879#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09002880static netdev_features_t net_mpls_features(struct sk_buff *skb,
2881 netdev_features_t features,
2882 __be16 type)
2883{
Simon Horman25cd9ba2014-10-06 05:05:13 -07002884 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09002885 features &= skb->dev->mpls_features;
2886
2887 return features;
2888}
2889#else
2890static netdev_features_t net_mpls_features(struct sk_buff *skb,
2891 netdev_features_t features,
2892 __be16 type)
2893{
2894 return features;
2895}
2896#endif
2897
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002898static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02002899 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00002900{
Vlad Yasevich53d64712014-03-27 17:26:18 -04002901 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09002902 __be16 type;
2903
2904 type = skb_network_protocol(skb, &tmp);
2905 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002906
Ed Cashinc0d680e2012-09-19 15:49:00 +00002907 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09002908 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07002909 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00002910 }
Eric Dumazet7be2c822017-01-18 12:12:17 -08002911 if (illegal_highdma(skb->dev, skb))
2912 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00002913
2914 return features;
2915}
2916
Toshiaki Makitae38f3022015-03-27 14:31:13 +09002917netdev_features_t passthru_features_check(struct sk_buff *skb,
2918 struct net_device *dev,
2919 netdev_features_t features)
2920{
2921 return features;
2922}
2923EXPORT_SYMBOL(passthru_features_check);
2924
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09002925static netdev_features_t dflt_features_check(const struct sk_buff *skb,
2926 struct net_device *dev,
2927 netdev_features_t features)
2928{
2929 return vlan_features_check(skb, features);
2930}
2931
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002932static netdev_features_t gso_features_check(const struct sk_buff *skb,
2933 struct net_device *dev,
2934 netdev_features_t features)
2935{
2936 u16 gso_segs = skb_shinfo(skb)->gso_segs;
2937
2938 if (gso_segs > dev->gso_max_segs)
2939 return features & ~NETIF_F_GSO_MASK;
2940
Alexander Duyck802ab552016-04-10 21:45:03 -04002941 /* Support for GSO partial features requires software
2942 * intervention before we can actually process the packets
2943 * so we need to strip support for any partial features now
2944 * and we can pull them back in after we have partially
2945 * segmented the frame.
2946 */
2947 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
2948 features &= ~dev->gso_partial_features;
2949
2950 /* Make sure to clear the IPv4 ID mangling feature if the
2951 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002952 */
2953 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2954 struct iphdr *iph = skb->encapsulation ?
2955 inner_ip_hdr(skb) : ip_hdr(skb);
2956
2957 if (!(iph->frag_off & htons(IP_DF)))
2958 features &= ~NETIF_F_TSO_MANGLEID;
2959 }
2960
2961 return features;
2962}
2963
Florian Westphalc1e756b2014-05-05 15:00:44 +02002964netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00002965{
Jesse Gross5f352272014-12-23 22:37:26 -08002966 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07002967 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00002968
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002969 if (skb_is_gso(skb))
2970 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00002971
Jesse Gross5f352272014-12-23 22:37:26 -08002972 /* If encapsulation offload request, verify we are testing
2973 * hardware encapsulation features instead of standard
2974 * features for the netdev
2975 */
2976 if (skb->encapsulation)
2977 features &= dev->hw_enc_features;
2978
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09002979 if (skb_vlan_tagged(skb))
2980 features = netdev_intersect_features(features,
2981 dev->vlan_features |
2982 NETIF_F_HW_VLAN_CTAG_TX |
2983 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00002984
Jesse Gross5f352272014-12-23 22:37:26 -08002985 if (dev->netdev_ops->ndo_features_check)
2986 features &= dev->netdev_ops->ndo_features_check(skb, dev,
2987 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09002988 else
2989 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08002990
Florian Westphalc1e756b2014-05-05 15:00:44 +02002991 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00002992}
Florian Westphalc1e756b2014-05-05 15:00:44 +02002993EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00002994
David S. Miller2ea25512014-08-29 21:10:01 -07002995static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07002996 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002997{
David S. Miller2ea25512014-08-29 21:10:01 -07002998 unsigned int len;
2999 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08003000
Salam Noureddine7866a622015-01-27 11:35:48 -08003001 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
David S. Miller2ea25512014-08-29 21:10:01 -07003002 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00003003
David S. Miller2ea25512014-08-29 21:10:01 -07003004 len = skb->len;
3005 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07003006 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07003007 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00003008
Patrick McHardy572a9d72009-11-10 06:14:14 +00003009 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003010}
David S. Miller2ea25512014-08-29 21:10:01 -07003011
David S. Miller8dcda222014-09-01 15:06:40 -07003012struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3013 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07003014{
3015 struct sk_buff *skb = first;
3016 int rc = NETDEV_TX_OK;
3017
3018 while (skb) {
3019 struct sk_buff *next = skb->next;
3020
3021 skb->next = NULL;
David S. Miller95f6b3d2014-08-29 21:57:30 -07003022 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07003023 if (unlikely(!dev_xmit_complete(rc))) {
3024 skb->next = next;
3025 goto out;
3026 }
3027
3028 skb = next;
3029 if (netif_xmit_stopped(txq) && skb) {
3030 rc = NETDEV_TX_BUSY;
3031 break;
3032 }
3033 }
3034
3035out:
3036 *ret = rc;
3037 return skb;
3038}
3039
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003040static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3041 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003042{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003043 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003044 !vlan_hw_offload_capable(features, skb->vlan_proto))
3045 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003046 return skb;
3047}
3048
Davide Caratti43c26a12017-05-18 15:44:41 +02003049int skb_csum_hwoffload_help(struct sk_buff *skb,
3050 const netdev_features_t features)
3051{
3052 if (unlikely(skb->csum_not_inet))
3053 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3054 skb_crc32c_csum_help(skb);
3055
3056 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3057}
3058EXPORT_SYMBOL(skb_csum_hwoffload_help);
3059
Eric Dumazet55a93b32014-10-03 15:31:07 -07003060static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev)
David S. Millereae3f882014-08-30 15:17:13 -07003061{
3062 netdev_features_t features;
3063
David S. Millereae3f882014-08-30 15:17:13 -07003064 features = netif_skb_features(skb);
3065 skb = validate_xmit_vlan(skb, features);
3066 if (unlikely(!skb))
3067 goto out_null;
3068
Johannes Berg8b86a612015-04-17 15:45:04 +02003069 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003070 struct sk_buff *segs;
3071
3072 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003073 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003074 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003075 } else if (segs) {
3076 consume_skb(skb);
3077 skb = segs;
3078 }
David S. Millereae3f882014-08-30 15:17:13 -07003079 } else {
3080 if (skb_needs_linearize(skb, features) &&
3081 __skb_linearize(skb))
3082 goto out_kfree_skb;
3083
Steffen Klassertf6e27112017-04-14 10:07:28 +02003084 if (validate_xmit_xfrm(skb, features))
3085 goto out_kfree_skb;
3086
David S. Millereae3f882014-08-30 15:17:13 -07003087 /* If packet is not checksummed and device does not
3088 * support checksumming for this protocol, complete
3089 * checksumming here.
3090 */
3091 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3092 if (skb->encapsulation)
3093 skb_set_inner_transport_header(skb,
3094 skb_checksum_start_offset(skb));
3095 else
3096 skb_set_transport_header(skb,
3097 skb_checksum_start_offset(skb));
Davide Caratti43c26a12017-05-18 15:44:41 +02003098 if (skb_csum_hwoffload_help(skb, features))
David S. Millereae3f882014-08-30 15:17:13 -07003099 goto out_kfree_skb;
3100 }
3101 }
3102
3103 return skb;
3104
3105out_kfree_skb:
3106 kfree_skb(skb);
3107out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003108 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003109 return NULL;
3110}
3111
Eric Dumazet55a93b32014-10-03 15:31:07 -07003112struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev)
3113{
3114 struct sk_buff *next, *head = NULL, *tail;
3115
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003116 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003117 next = skb->next;
3118 skb->next = NULL;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003119
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003120 /* in case skb wont be segmented, point to itself */
3121 skb->prev = skb;
3122
3123 skb = validate_xmit_skb(skb, dev);
3124 if (!skb)
3125 continue;
3126
3127 if (!head)
3128 head = skb;
3129 else
3130 tail->next = skb;
3131 /* If skb was segmented, skb->prev points to
3132 * the last segment. If not, it still contains skb.
3133 */
3134 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003135 }
3136 return head;
3137}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003138EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003139
Eric Dumazet1def9232013-01-10 12:36:42 +00003140static void qdisc_pkt_len_init(struct sk_buff *skb)
3141{
3142 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3143
3144 qdisc_skb_cb(skb)->pkt_len = skb->len;
3145
3146 /* To get more precise estimation of bytes sent on wire,
3147 * we add to pkt_len the headers size of all segments
3148 */
3149 if (shinfo->gso_size) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003150 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003151 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003152
Eric Dumazet757b8b12013-01-15 21:14:21 -08003153 /* mac layer + network layer */
3154 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3155
3156 /* + transport layer */
Eric Dumazet1def9232013-01-10 12:36:42 +00003157 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3158 hdr_len += tcp_hdrlen(skb);
3159 else
3160 hdr_len += sizeof(struct udphdr);
Jason Wang15e5a032013-03-25 20:19:59 +00003161
3162 if (shinfo->gso_type & SKB_GSO_DODGY)
3163 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3164 shinfo->gso_size);
3165
3166 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003167 }
3168}
3169
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003170static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3171 struct net_device *dev,
3172 struct netdev_queue *txq)
3173{
3174 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003175 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003176 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003177 int rc;
3178
Eric Dumazeta2da5702011-01-20 03:48:19 +00003179 qdisc_calculate_pkt_len(skb, q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003180 /*
3181 * Heuristic to force contended enqueues to serialize on a
3182 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003183 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003184 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003185 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003186 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003187 if (unlikely(contended))
3188 spin_lock(&q->busylock);
3189
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003190 spin_lock(root_lock);
3191 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003192 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003193 rc = NET_XMIT_DROP;
3194 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003195 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003196 /*
3197 * This is a work-conserving queue; there are no old skbs
3198 * waiting to be sent out; and the qdisc is not running -
3199 * xmit the skb directly.
3200 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003201
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003202 qdisc_bstats_update(q, skb);
3203
Eric Dumazet55a93b32014-10-03 15:31:07 -07003204 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003205 if (unlikely(contended)) {
3206 spin_unlock(&q->busylock);
3207 contended = false;
3208 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003209 __qdisc_run(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003210 } else
Eric Dumazetbc135b22010-06-02 03:23:51 -07003211 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003212
3213 rc = NET_XMIT_SUCCESS;
3214 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003215 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003216 if (qdisc_run_begin(q)) {
3217 if (unlikely(contended)) {
3218 spin_unlock(&q->busylock);
3219 contended = false;
3220 }
3221 __qdisc_run(q);
3222 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003223 }
3224 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003225 if (unlikely(to_free))
3226 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003227 if (unlikely(contended))
3228 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003229 return rc;
3230}
3231
Daniel Borkmann86f85152013-12-29 17:27:11 +01003232#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003233static void skb_update_prio(struct sk_buff *skb)
3234{
Igor Maravic6977a792011-11-25 07:44:54 +00003235 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
Neil Horman5bc14212011-11-22 05:10:51 +00003236
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003237 if (!skb->priority && skb->sk && map) {
Tejun Heo2a56a1f2015-12-07 17:38:52 -05003238 unsigned int prioidx =
3239 sock_cgroup_prioidx(&skb->sk->sk_cgrp_data);
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003240
3241 if (prioidx < map->priomap_len)
3242 skb->priority = map->priomap[prioidx];
3243 }
Neil Horman5bc14212011-11-22 05:10:51 +00003244}
3245#else
3246#define skb_update_prio(skb)
3247#endif
3248
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +02003249DEFINE_PER_CPU(int, xmit_recursion);
3250EXPORT_SYMBOL(xmit_recursion);
3251
Dave Jonesd29f7492008-07-22 14:09:06 -07003252/**
Michel Machado95603e22012-06-12 10:16:35 +00003253 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003254 * @net: network namespace this loopback is happening in
3255 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003256 * @skb: buffer to transmit
3257 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003258int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003259{
3260 skb_reset_mac_header(skb);
3261 __skb_pull(skb, skb_network_offset(skb));
3262 skb->pkt_type = PACKET_LOOPBACK;
3263 skb->ip_summed = CHECKSUM_UNNECESSARY;
3264 WARN_ON(!skb_dst(skb));
3265 skb_dst_force(skb);
3266 netif_rx_ni(skb);
3267 return 0;
3268}
3269EXPORT_SYMBOL(dev_loopback_xmit);
3270
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003271#ifdef CONFIG_NET_EGRESS
3272static struct sk_buff *
3273sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3274{
3275 struct tcf_proto *cl = rcu_dereference_bh(dev->egress_cl_list);
3276 struct tcf_result cl_res;
3277
3278 if (!cl)
3279 return skb;
3280
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003281 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003282 qdisc_bstats_cpu_update(cl->q, skb);
3283
Jiri Pirko87d83092017-05-17 11:07:54 +02003284 switch (tcf_classify(skb, cl, &cl_res, false)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003285 case TC_ACT_OK:
3286 case TC_ACT_RECLASSIFY:
3287 skb->tc_index = TC_H_MIN(cl_res.classid);
3288 break;
3289 case TC_ACT_SHOT:
3290 qdisc_qstats_cpu_drop(cl->q);
3291 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003292 kfree_skb(skb);
3293 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003294 case TC_ACT_STOLEN:
3295 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02003296 case TC_ACT_TRAP:
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003297 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003298 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003299 return NULL;
3300 case TC_ACT_REDIRECT:
3301 /* No need to push/pop skb's mac_header here on egress! */
3302 skb_do_redirect(skb);
3303 *ret = NET_XMIT_SUCCESS;
3304 return NULL;
3305 default:
3306 break;
3307 }
3308
3309 return skb;
3310}
3311#endif /* CONFIG_NET_EGRESS */
3312
Jiri Pirko638b2a62015-05-12 14:56:13 +02003313static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
3314{
3315#ifdef CONFIG_XPS
3316 struct xps_dev_maps *dev_maps;
3317 struct xps_map *map;
3318 int queue_index = -1;
3319
3320 rcu_read_lock();
3321 dev_maps = rcu_dereference(dev->xps_maps);
3322 if (dev_maps) {
Alexander Duyck184c4492016-10-28 11:50:13 -04003323 unsigned int tci = skb->sender_cpu - 1;
3324
3325 if (dev->num_tc) {
3326 tci *= dev->num_tc;
3327 tci += netdev_get_prio_tc_map(dev, skb->priority);
3328 }
3329
3330 map = rcu_dereference(dev_maps->cpu_map[tci]);
Jiri Pirko638b2a62015-05-12 14:56:13 +02003331 if (map) {
3332 if (map->len == 1)
3333 queue_index = map->queues[0];
3334 else
3335 queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
3336 map->len)];
3337 if (unlikely(queue_index >= dev->real_num_tx_queues))
3338 queue_index = -1;
3339 }
3340 }
3341 rcu_read_unlock();
3342
3343 return queue_index;
3344#else
3345 return -1;
3346#endif
3347}
3348
3349static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
3350{
3351 struct sock *sk = skb->sk;
3352 int queue_index = sk_tx_queue_get(sk);
3353
3354 if (queue_index < 0 || skb->ooo_okay ||
3355 queue_index >= dev->real_num_tx_queues) {
3356 int new_index = get_xps_queue(dev, skb);
tchardingf4563a72017-02-09 17:56:07 +11003357
Jiri Pirko638b2a62015-05-12 14:56:13 +02003358 if (new_index < 0)
3359 new_index = skb_tx_hash(dev, skb);
3360
3361 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003362 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003363 rcu_access_pointer(sk->sk_dst_cache))
3364 sk_tx_queue_set(sk, new_index);
3365
3366 queue_index = new_index;
3367 }
3368
3369 return queue_index;
3370}
3371
3372struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3373 struct sk_buff *skb,
3374 void *accel_priv)
3375{
3376 int queue_index = 0;
3377
3378#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003379 u32 sender_cpu = skb->sender_cpu - 1;
3380
3381 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003382 skb->sender_cpu = raw_smp_processor_id() + 1;
3383#endif
3384
3385 if (dev->real_num_tx_queues != 1) {
3386 const struct net_device_ops *ops = dev->netdev_ops;
tchardingf4563a72017-02-09 17:56:07 +11003387
Jiri Pirko638b2a62015-05-12 14:56:13 +02003388 if (ops->ndo_select_queue)
3389 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
3390 __netdev_pick_tx);
3391 else
3392 queue_index = __netdev_pick_tx(dev, skb);
3393
3394 if (!accel_priv)
3395 queue_index = netdev_cap_txqueue(dev, queue_index);
3396 }
3397
3398 skb_set_queue_mapping(skb, queue_index);
3399 return netdev_get_tx_queue(dev, queue_index);
3400}
3401
Michel Machado95603e22012-06-12 10:16:35 +00003402/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003403 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003404 * @skb: buffer to transmit
Jason Wang9d08dd32014-01-20 11:25:13 +08003405 * @accel_priv: private data used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003406 *
3407 * Queue a buffer for transmission to a network device. The caller must
3408 * have set the device and priority and built the buffer before calling
3409 * this function. The function can be called from an interrupt.
3410 *
3411 * A negative errno code is returned on a failure. A success does not
3412 * guarantee the frame will be transmitted as it may be dropped due
3413 * to congestion or traffic shaping.
3414 *
3415 * -----------------------------------------------------------------------------------
3416 * I notice this method can also return errors from the queue disciplines,
3417 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3418 * be positive.
3419 *
3420 * Regardless of the return value, the skb is consumed, so it is currently
3421 * difficult to retry a send to this method. (You can bump the ref count
3422 * before sending to hold a reference for retry if you are careful.)
3423 *
3424 * When calling this method, interrupts MUST be enabled. This is because
3425 * the BH enable code must have IRQs enabled so that it will not deadlock.
3426 * --BLG
3427 */
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05303428static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003431 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 struct Qdisc *q;
3433 int rc = -ENOMEM;
3434
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003435 skb_reset_mac_header(skb);
3436
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003437 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3438 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3439
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003440 /* Disable soft irqs for various locks below. Also
3441 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003443 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Neil Horman5bc14212011-11-22 05:10:51 +00003445 skb_update_prio(skb);
3446
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003447 qdisc_pkt_len_init(skb);
3448#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05003449 skb->tc_at_ingress = 0;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003450# ifdef CONFIG_NET_EGRESS
3451 if (static_key_false(&egress_needed)) {
3452 skb = sch_handle_egress(skb, &rc, dev);
3453 if (!skb)
3454 goto out;
3455 }
3456# endif
3457#endif
Eric Dumazet02875872014-10-05 18:38:35 -07003458 /* If device/qdisc don't need skb->dst, release it right now while
3459 * its hot in this cpu cache.
3460 */
3461 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3462 skb_dst_drop(skb);
3463 else
3464 skb_dst_force(skb);
3465
Jason Wangf663dd92014-01-10 16:18:26 +08003466 txq = netdev_pick_tx(dev, skb, accel_priv);
Paul E. McKenneya898def2010-02-22 17:04:49 -08003467 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07003468
Koki Sanagicf66ba52010-08-23 18:45:02 +09003469 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003471 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07003472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
3474
3475 /* The device has no queue. Common case for software devices:
tchardingeb13da12017-02-09 17:56:06 +11003476 * loopback, all the sorts of tunnels...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
tchardingeb13da12017-02-09 17:56:06 +11003478 * Really, it is unlikely that netif_tx_lock protection is necessary
3479 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3480 * counters.)
3481 * However, it is possible, that they rely on protection
3482 * made by us here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
tchardingeb13da12017-02-09 17:56:06 +11003484 * Check this and shot the lock. It is not prone from deadlocks.
3485 *Either shot noqueue qdisc, it is even simpler 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 */
3487 if (dev->flags & IFF_UP) {
3488 int cpu = smp_processor_id(); /* ok because BHs are off */
3489
David S. Millerc773e842008-07-08 23:13:53 -07003490 if (txq->xmit_lock_owner != cpu) {
Daniel Borkmanna70b5062016-06-10 21:19:06 +02003491 if (unlikely(__this_cpu_read(xmit_recursion) >
3492 XMIT_RECURSION_LIMIT))
Eric Dumazet745e20f2010-09-29 13:23:09 -07003493 goto recursion_alert;
3494
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003495 skb = validate_xmit_skb(skb, dev);
3496 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07003497 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003498
David S. Millerc773e842008-07-08 23:13:53 -07003499 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Tom Herbert734664982011-11-28 16:32:44 +00003501 if (!netif_xmit_stopped(txq)) {
Eric Dumazet745e20f2010-09-29 13:23:09 -07003502 __this_cpu_inc(xmit_recursion);
David S. Millerce937182014-08-30 19:22:20 -07003503 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Eric Dumazet745e20f2010-09-29 13:23:09 -07003504 __this_cpu_dec(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00003505 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07003506 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 goto out;
3508 }
3509 }
David S. Millerc773e842008-07-08 23:13:53 -07003510 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00003511 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3512 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 } else {
3514 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07003515 * unfortunately
3516 */
3517recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00003518 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3519 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
3521 }
3522
3523 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07003524 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Eric Dumazet015f0682014-03-27 08:45:56 -07003526 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003527 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return rc;
3529out:
Herbert Xud4828d82006-06-22 02:28:18 -07003530 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return rc;
3532}
Jason Wangf663dd92014-01-10 16:18:26 +08003533
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003534int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08003535{
3536 return __dev_queue_xmit(skb, NULL);
3537}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003538EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Jason Wangf663dd92014-01-10 16:18:26 +08003540int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
3541{
3542 return __dev_queue_xmit(skb, accel_priv);
3543}
3544EXPORT_SYMBOL(dev_queue_xmit_accel);
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
tchardingeb13da12017-02-09 17:56:06 +11003547/*************************************************************************
3548 * Receiver routines
3549 *************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003551int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00003552EXPORT_SYMBOL(netdev_max_backlog);
3553
Eric Dumazet3b098e22010-05-15 23:57:10 -07003554int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003555int netdev_budget __read_mostly = 300;
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04003556unsigned int __read_mostly netdev_budget_usecs = 2000;
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01003557int weight_p __read_mostly = 64; /* old backlog weight */
3558int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
3559int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
3560int dev_rx_weight __read_mostly = 64;
3561int dev_tx_weight __read_mostly = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003563/* Called with irq disabled */
3564static inline void ____napi_schedule(struct softnet_data *sd,
3565 struct napi_struct *napi)
3566{
3567 list_add_tail(&napi->poll_list, &sd->poll_list);
3568 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3569}
3570
Eric Dumazetdf334542010-03-24 19:13:54 +00003571#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07003572
3573/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00003574struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07003575EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003576u32 rps_cpu_mask __read_mostly;
3577EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07003578
Ingo Molnarc5905af2012-02-24 08:31:31 +01003579struct static_key rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04003580EXPORT_SYMBOL(rps_needed);
Eric Dumazet13bfff22016-12-07 08:29:10 -08003581struct static_key rfs_needed __read_mostly;
3582EXPORT_SYMBOL(rfs_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00003583
Ben Hutchingsc4454772011-01-19 11:03:53 +00003584static struct rps_dev_flow *
3585set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3586 struct rps_dev_flow *rflow, u16 next_cpu)
3587{
Eric Dumazeta31196b2015-04-25 09:35:24 -07003588 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00003589#ifdef CONFIG_RFS_ACCEL
3590 struct netdev_rx_queue *rxqueue;
3591 struct rps_dev_flow_table *flow_table;
3592 struct rps_dev_flow *old_rflow;
3593 u32 flow_id;
3594 u16 rxq_index;
3595 int rc;
3596
3597 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00003598 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3599 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00003600 goto out;
3601 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3602 if (rxq_index == skb_get_rx_queue(skb))
3603 goto out;
3604
3605 rxqueue = dev->_rx + rxq_index;
3606 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3607 if (!flow_table)
3608 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07003609 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003610 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3611 rxq_index, flow_id);
3612 if (rc < 0)
3613 goto out;
3614 old_rflow = rflow;
3615 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00003616 rflow->filter = rc;
3617 if (old_rflow->filter == rflow->filter)
3618 old_rflow->filter = RPS_NO_FILTER;
3619 out:
3620#endif
3621 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00003622 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003623 }
3624
Ben Hutchings09994d12011-10-03 04:42:46 +00003625 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003626 return rflow;
3627}
3628
Tom Herbert0a9627f2010-03-16 08:03:29 +00003629/*
3630 * get_rps_cpu is called from netif_receive_skb and returns the target
3631 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003632 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00003633 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003634static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3635 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003636{
Eric Dumazet567e4b72015-02-06 12:59:01 -08003637 const struct rps_sock_flow_table *sock_flow_table;
3638 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07003639 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003640 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003641 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003642 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07003643 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003644
Tom Herbert0a9627f2010-03-16 08:03:29 +00003645 if (skb_rx_queue_recorded(skb)) {
3646 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003647
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003648 if (unlikely(index >= dev->real_num_rx_queues)) {
3649 WARN_ONCE(dev->real_num_rx_queues > 1,
3650 "%s received packet on queue %u, but number "
3651 "of RX queues is %u\n",
3652 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003653 goto done;
3654 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08003655 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003656 }
3657
Eric Dumazet567e4b72015-02-06 12:59:01 -08003658 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
3659
3660 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3661 map = rcu_dereference(rxqueue->rps_map);
3662 if (!flow_table && !map)
3663 goto done;
3664
Changli Gao2d47b452010-08-17 19:00:56 +00003665 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07003666 hash = skb_get_hash(skb);
3667 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003668 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003669
Tom Herbertfec5e652010-04-16 16:01:27 -07003670 sock_flow_table = rcu_dereference(rps_sock_flow_table);
3671 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003672 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003673 u32 next_cpu;
3674 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07003675
Eric Dumazet567e4b72015-02-06 12:59:01 -08003676 /* First check into global flow table if there is a match */
3677 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
3678 if ((ident ^ hash) & ~rps_cpu_mask)
3679 goto try_rps;
3680
3681 next_cpu = ident & rps_cpu_mask;
3682
3683 /* OK, now we know there is a match,
3684 * we can look at the local (per receive queue) flow table
3685 */
Tom Herbert61b905d2014-03-24 15:34:47 -07003686 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07003687 tcpu = rflow->cpu;
3688
Tom Herbertfec5e652010-04-16 16:01:27 -07003689 /*
3690 * If the desired CPU (where last recvmsg was done) is
3691 * different from current CPU (one in the rx-queue flow
3692 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07003693 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07003694 * - Current CPU is offline.
3695 * - The current CPU's queue tail has advanced beyond the
3696 * last packet that was enqueued using this table entry.
3697 * This guarantees that all previous packets for the flow
3698 * have been dequeued, thus preserving in order delivery.
3699 */
3700 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07003701 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07003702 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00003703 rflow->last_qtail)) >= 0)) {
3704 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003705 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00003706 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00003707
Eric Dumazeta31196b2015-04-25 09:35:24 -07003708 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003709 *rflowp = rflow;
3710 cpu = tcpu;
3711 goto done;
3712 }
3713 }
3714
Eric Dumazet567e4b72015-02-06 12:59:01 -08003715try_rps:
3716
Tom Herbert0a9627f2010-03-16 08:03:29 +00003717 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02003718 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00003719 if (cpu_online(tcpu)) {
3720 cpu = tcpu;
3721 goto done;
3722 }
3723 }
3724
3725done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00003726 return cpu;
3727}
3728
Ben Hutchingsc4454772011-01-19 11:03:53 +00003729#ifdef CONFIG_RFS_ACCEL
3730
3731/**
3732 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3733 * @dev: Device on which the filter was set
3734 * @rxq_index: RX queue index
3735 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3736 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3737 *
3738 * Drivers that implement ndo_rx_flow_steer() should periodically call
3739 * this function for each installed filter and remove the filters for
3740 * which it returns %true.
3741 */
3742bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3743 u32 flow_id, u16 filter_id)
3744{
3745 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3746 struct rps_dev_flow_table *flow_table;
3747 struct rps_dev_flow *rflow;
3748 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07003749 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003750
3751 rcu_read_lock();
3752 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3753 if (flow_table && flow_id <= flow_table->mask) {
3754 rflow = &flow_table->flows[flow_id];
3755 cpu = ACCESS_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07003756 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00003757 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3758 rflow->last_qtail) <
3759 (int)(10 * flow_table->mask)))
3760 expire = false;
3761 }
3762 rcu_read_unlock();
3763 return expire;
3764}
3765EXPORT_SYMBOL(rps_may_expire_flow);
3766
3767#endif /* CONFIG_RFS_ACCEL */
3768
Tom Herbert0a9627f2010-03-16 08:03:29 +00003769/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003770static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003771{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003772 struct softnet_data *sd = data;
3773
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003774 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00003775 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003776}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003777
Tom Herbertfec5e652010-04-16 16:01:27 -07003778#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00003779
3780/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003781 * Check if this softnet_data structure is another cpu one
3782 * If yes, queue it to our IPI list and return 1
3783 * If no, return 0
3784 */
3785static int rps_ipi_queued(struct softnet_data *sd)
3786{
3787#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05003788 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003789
3790 if (sd != mysd) {
3791 sd->rps_ipi_next = mysd->rps_ipi_list;
3792 mysd->rps_ipi_list = sd;
3793
3794 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3795 return 1;
3796 }
3797#endif /* CONFIG_RPS */
3798 return 0;
3799}
3800
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003801#ifdef CONFIG_NET_FLOW_LIMIT
3802int netdev_flow_limit_table_len __read_mostly = (1 << 12);
3803#endif
3804
3805static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
3806{
3807#ifdef CONFIG_NET_FLOW_LIMIT
3808 struct sd_flow_limit *fl;
3809 struct softnet_data *sd;
3810 unsigned int old_flow, new_flow;
3811
3812 if (qlen < (netdev_max_backlog >> 1))
3813 return false;
3814
Christoph Lameter903ceff2014-08-17 12:30:35 -05003815 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003816
3817 rcu_read_lock();
3818 fl = rcu_dereference(sd->flow_limit);
3819 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08003820 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003821 old_flow = fl->history[fl->history_head];
3822 fl->history[fl->history_head] = new_flow;
3823
3824 fl->history_head++;
3825 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
3826
3827 if (likely(fl->buckets[old_flow]))
3828 fl->buckets[old_flow]--;
3829
3830 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
3831 fl->count++;
3832 rcu_read_unlock();
3833 return true;
3834 }
3835 }
3836 rcu_read_unlock();
3837#endif
3838 return false;
3839}
3840
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003841/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00003842 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3843 * queue (may be a remote CPU queue).
3844 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003845static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3846 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003847{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003848 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003849 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003850 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003851
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003852 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003853
3854 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003855
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003856 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03003857 if (!netif_running(skb->dev))
3858 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003859 qlen = skb_queue_len(&sd->input_pkt_queue);
3860 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08003861 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00003862enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003863 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003864 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003865 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00003866 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003867 return NET_RX_SUCCESS;
3868 }
3869
Eric Dumazetebda37c22010-05-06 23:51:21 +00003870 /* Schedule NAPI for backlog device
3871 * We can use non atomic operation since we own the queue lock
3872 */
3873 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003874 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003875 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003876 }
3877 goto enqueue;
3878 }
3879
Julian Anastasove9e4dd32015-07-09 09:59:09 +03003880drop:
Changli Gaodee42872010-05-02 05:42:16 +00003881 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003882 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003883
Tom Herbert0a9627f2010-03-16 08:03:29 +00003884 local_irq_restore(flags);
3885
Eric Dumazetcaf586e2010-09-30 21:06:55 +00003886 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003887 kfree_skb(skb);
3888 return NET_RX_DROP;
3889}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
John Fastabendd4455162017-07-17 09:26:45 -07003891static u32 netif_receive_generic_xdp(struct sk_buff *skb,
3892 struct bpf_prog *xdp_prog)
3893{
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003894 u32 metalen, act = XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07003895 struct xdp_buff xdp;
John Fastabendd4455162017-07-17 09:26:45 -07003896 void *orig_data;
3897 int hlen, off;
3898 u32 mac_len;
3899
3900 /* Reinjected packets coming from act_mirred or similar should
3901 * not get XDP generic processing.
3902 */
3903 if (skb_cloned(skb))
3904 return XDP_PASS;
3905
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003906 /* XDP packets must be linear and must have sufficient headroom
3907 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
3908 * native XDP provides, thus we need to do it here as well.
3909 */
3910 if (skb_is_nonlinear(skb) ||
3911 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
3912 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
3913 int troom = skb->tail + skb->data_len - skb->end;
3914
3915 /* In case we have to go down the path and also linearize,
3916 * then lets do the pskb_expand_head() work just once here.
3917 */
3918 if (pskb_expand_head(skb,
3919 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
3920 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
3921 goto do_drop;
3922 if (troom > 0 && __skb_linearize(skb))
3923 goto do_drop;
3924 }
John Fastabendd4455162017-07-17 09:26:45 -07003925
3926 /* The XDP program wants to see the packet starting at the MAC
3927 * header.
3928 */
3929 mac_len = skb->data - skb_mac_header(skb);
3930 hlen = skb_headlen(skb) + mac_len;
3931 xdp.data = skb->data - mac_len;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003932 xdp.data_meta = xdp.data;
John Fastabendd4455162017-07-17 09:26:45 -07003933 xdp.data_end = xdp.data + hlen;
3934 xdp.data_hard_start = skb->data - skb_headroom(skb);
3935 orig_data = xdp.data;
3936
3937 act = bpf_prog_run_xdp(xdp_prog, &xdp);
3938
3939 off = xdp.data - orig_data;
3940 if (off > 0)
3941 __skb_pull(skb, off);
3942 else if (off < 0)
3943 __skb_push(skb, -off);
Edward Cree92dd5452017-09-19 18:45:56 +01003944 skb->mac_header += off;
John Fastabendd4455162017-07-17 09:26:45 -07003945
3946 switch (act) {
John Fastabend6103aa92017-07-17 09:27:50 -07003947 case XDP_REDIRECT:
John Fastabendd4455162017-07-17 09:26:45 -07003948 case XDP_TX:
3949 __skb_push(skb, mac_len);
John Fastabendd4455162017-07-17 09:26:45 -07003950 break;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003951 case XDP_PASS:
3952 metalen = xdp.data - xdp.data_meta;
3953 if (metalen)
3954 skb_metadata_set(skb, metalen);
3955 break;
John Fastabendd4455162017-07-17 09:26:45 -07003956 default:
3957 bpf_warn_invalid_xdp_action(act);
3958 /* fall through */
3959 case XDP_ABORTED:
3960 trace_xdp_exception(skb->dev, xdp_prog, act);
3961 /* fall through */
3962 case XDP_DROP:
3963 do_drop:
3964 kfree_skb(skb);
3965 break;
3966 }
3967
3968 return act;
3969}
3970
3971/* When doing generic XDP we have to bypass the qdisc layer and the
3972 * network taps in order to match in-driver-XDP behavior.
3973 */
Jason Wang7c497472017-08-11 19:41:17 +08003974void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
John Fastabendd4455162017-07-17 09:26:45 -07003975{
3976 struct net_device *dev = skb->dev;
3977 struct netdev_queue *txq;
3978 bool free_skb = true;
3979 int cpu, rc;
3980
3981 txq = netdev_pick_tx(dev, skb, NULL);
3982 cpu = smp_processor_id();
3983 HARD_TX_LOCK(dev, txq, cpu);
3984 if (!netif_xmit_stopped(txq)) {
3985 rc = netdev_start_xmit(skb, dev, txq, 0);
3986 if (dev_xmit_complete(rc))
3987 free_skb = false;
3988 }
3989 HARD_TX_UNLOCK(dev, txq);
3990 if (free_skb) {
3991 trace_xdp_exception(dev, xdp_prog, XDP_TX);
3992 kfree_skb(skb);
3993 }
3994}
Jason Wang7c497472017-08-11 19:41:17 +08003995EXPORT_SYMBOL_GPL(generic_xdp_tx);
John Fastabendd4455162017-07-17 09:26:45 -07003996
3997static struct static_key generic_xdp_needed __read_mostly;
3998
Jason Wang7c497472017-08-11 19:41:17 +08003999int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
John Fastabendd4455162017-07-17 09:26:45 -07004000{
John Fastabendd4455162017-07-17 09:26:45 -07004001 if (xdp_prog) {
4002 u32 act = netif_receive_generic_xdp(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004003 int err;
John Fastabendd4455162017-07-17 09:26:45 -07004004
4005 if (act != XDP_PASS) {
John Fastabend6103aa92017-07-17 09:27:50 -07004006 switch (act) {
4007 case XDP_REDIRECT:
Jesper Dangaard Brouer2facaad2017-08-24 12:33:08 +02004008 err = xdp_do_generic_redirect(skb->dev, skb,
4009 xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004010 if (err)
4011 goto out_redir;
4012 /* fallthru to submit skb */
4013 case XDP_TX:
John Fastabendd4455162017-07-17 09:26:45 -07004014 generic_xdp_tx(skb, xdp_prog);
John Fastabend6103aa92017-07-17 09:27:50 -07004015 break;
4016 }
John Fastabendd4455162017-07-17 09:26:45 -07004017 return XDP_DROP;
4018 }
4019 }
4020 return XDP_PASS;
John Fastabend6103aa92017-07-17 09:27:50 -07004021out_redir:
John Fastabend6103aa92017-07-17 09:27:50 -07004022 kfree_skb(skb);
4023 return XDP_DROP;
John Fastabendd4455162017-07-17 09:26:45 -07004024}
Jason Wang7c497472017-08-11 19:41:17 +08004025EXPORT_SYMBOL_GPL(do_xdp_generic);
John Fastabendd4455162017-07-17 09:26:45 -07004026
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004027static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004029 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Eric Dumazet588f0332011-11-15 04:12:55 +00004031 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Koki Sanagicf66ba52010-08-23 18:45:02 +09004033 trace_netif_rx(skb);
John Fastabendd4455162017-07-17 09:26:45 -07004034
4035 if (static_key_false(&generic_xdp_needed)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07004036 int ret;
4037
4038 preempt_disable();
4039 rcu_read_lock();
4040 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4041 rcu_read_unlock();
4042 preempt_enable();
John Fastabendd4455162017-07-17 09:26:45 -07004043
John Fastabend6103aa92017-07-17 09:27:50 -07004044 /* Consider XDP consuming the packet a success from
4045 * the netdev point of view we do not want to count
4046 * this as an error.
4047 */
John Fastabendd4455162017-07-17 09:26:45 -07004048 if (ret != XDP_PASS)
John Fastabend6103aa92017-07-17 09:27:50 -07004049 return NET_RX_SUCCESS;
John Fastabendd4455162017-07-17 09:26:45 -07004050 }
4051
Eric Dumazetdf334542010-03-24 19:13:54 +00004052#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004053 if (static_key_false(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07004054 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004055 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Changli Gaocece1942010-08-07 20:35:43 -07004057 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004058 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07004059
4060 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004061 if (cpu < 0)
4062 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07004063
4064 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4065
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004066 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07004067 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00004068 } else
4069#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07004070 {
4071 unsigned int qtail;
tchardingf4563a72017-02-09 17:56:07 +11004072
Tom Herbertfec5e652010-04-16 16:01:27 -07004073 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4074 put_cpu();
4075 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07004076 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004078
4079/**
4080 * netif_rx - post buffer to the network code
4081 * @skb: buffer to post
4082 *
4083 * This function receives a packet from a device driver and queues it for
4084 * the upper (protocol) levels to process. It always succeeds. The buffer
4085 * may be dropped during processing for congestion control or by the
4086 * protocol layers.
4087 *
4088 * return values:
4089 * NET_RX_SUCCESS (no congestion)
4090 * NET_RX_DROP (packet was dropped)
4091 *
4092 */
4093
4094int netif_rx(struct sk_buff *skb)
4095{
4096 trace_netif_rx_entry(skb);
4097
4098 return netif_rx_internal(skb);
4099}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004100EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102int netif_rx_ni(struct sk_buff *skb)
4103{
4104 int err;
4105
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004106 trace_netif_rx_ni_entry(skb);
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004109 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 if (local_softirq_pending())
4111 do_softirq();
4112 preempt_enable();
4113
4114 return err;
4115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116EXPORT_SYMBOL(netif_rx_ni);
4117
Emese Revfy0766f782016-06-20 20:42:34 +02004118static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Christoph Lameter903ceff2014-08-17 12:30:35 -05004120 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 if (sd->completion_queue) {
4123 struct sk_buff *clist;
4124
4125 local_irq_disable();
4126 clist = sd->completion_queue;
4127 sd->completion_queue = NULL;
4128 local_irq_enable();
4129
4130 while (clist) {
4131 struct sk_buff *skb = clist;
tchardingf4563a72017-02-09 17:56:07 +11004132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 clist = clist->next;
4134
Reshetova, Elena63354792017-06-30 13:07:58 +03004135 WARN_ON(refcount_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08004136 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4137 trace_consume_skb(skb);
4138 else
4139 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004140
4141 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4142 __kfree_skb(skb);
4143 else
4144 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01004146
4147 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
4149
4150 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07004151 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 local_irq_disable();
4154 head = sd->output_queue;
4155 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00004156 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 local_irq_enable();
4158
4159 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07004160 struct Qdisc *q = head;
4161 spinlock_t *root_lock;
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 head = head->next_sched;
4164
David S. Miller5fb66222008-08-02 20:02:43 -07004165 root_lock = qdisc_lock(q);
Eric Dumazet3bcb8462016-06-04 20:02:28 -07004166 spin_lock(root_lock);
4167 /* We need to make sure head->next_sched is read
4168 * before clearing __QDISC_STATE_SCHED
4169 */
4170 smp_mb__before_atomic();
4171 clear_bit(__QDISC_STATE_SCHED, &q->state);
4172 qdisc_run(q);
4173 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 }
4175 }
4176}
4177
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04004178#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00004179/* This hook is defined here for ATM LANE */
4180int (*br_fdb_test_addr_hook)(struct net_device *dev,
4181 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07004182EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00004183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004185static inline struct sk_buff *
4186sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4187 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07004188{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004189#ifdef CONFIG_NET_CLS_ACT
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004190 struct tcf_proto *cl = rcu_dereference_bh(skb->dev->ingress_cl_list);
4191 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00004192
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004193 /* If there's at least one ingress present somewhere (so
4194 * we get here via enabled static key), remaining devices
4195 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004196 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004197 */
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004198 if (!cl)
Daniel Borkmann45771392015-04-10 23:07:54 +02004199 return skb;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004200 if (*pt_prev) {
4201 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4202 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004203 }
4204
Florian Westphal33654952015-05-14 00:36:28 +02004205 qdisc_skb_cb(skb)->pkt_len = skb->len;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -05004206 skb->tc_at_ingress = 1;
Eric Dumazet24ea5912015-07-06 05:18:03 -07004207 qdisc_bstats_cpu_update(cl->q, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004208
Jiri Pirko87d83092017-05-17 11:07:54 +02004209 switch (tcf_classify(skb, cl, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004210 case TC_ACT_OK:
4211 case TC_ACT_RECLASSIFY:
4212 skb->tc_index = TC_H_MIN(cl_res.classid);
4213 break;
4214 case TC_ACT_SHOT:
Eric Dumazet24ea5912015-07-06 05:18:03 -07004215 qdisc_qstats_cpu_drop(cl->q);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004216 kfree_skb(skb);
4217 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004218 case TC_ACT_STOLEN:
4219 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +02004220 case TC_ACT_TRAP:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004221 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004222 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004223 case TC_ACT_REDIRECT:
4224 /* skb_mac_header check was done by cls/act_bpf, so
4225 * we can safely push the L2 header back before
4226 * redirecting to another netdev
4227 */
4228 __skb_push(skb, skb->mac_len);
4229 skb_do_redirect(skb);
4230 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004231 default:
4232 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004233 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004234#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004235 return skb;
4236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004238/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004239 * netdev_is_rx_handler_busy - check if receive handler is registered
4240 * @dev: device to check
4241 *
4242 * Check if a receive handler is already registered for a given device.
4243 * Return true if there one.
4244 *
4245 * The caller must hold the rtnl_mutex.
4246 */
4247bool netdev_is_rx_handler_busy(struct net_device *dev)
4248{
4249 ASSERT_RTNL();
4250 return dev && rtnl_dereference(dev->rx_handler);
4251}
4252EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4253
4254/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004255 * netdev_rx_handler_register - register receive handler
4256 * @dev: device to register a handler for
4257 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004258 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004259 *
Masanari Iidae2278672014-02-18 22:54:36 +09004260 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004261 * called from __netif_receive_skb. A negative errno code is returned
4262 * on a failure.
4263 *
4264 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004265 *
4266 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004267 */
4268int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004269 rx_handler_func_t *rx_handler,
4270 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004271{
Mahesh Bandewar1b7cd002017-01-18 15:02:49 -08004272 if (netdev_is_rx_handler_busy(dev))
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004273 return -EBUSY;
4274
Eric Dumazet00cfec32013-03-29 03:01:22 +00004275 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004276 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004277 rcu_assign_pointer(dev->rx_handler, rx_handler);
4278
4279 return 0;
4280}
4281EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4282
4283/**
4284 * netdev_rx_handler_unregister - unregister receive handler
4285 * @dev: device to unregister a handler from
4286 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004287 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004288 *
4289 * The caller must hold the rtnl_mutex.
4290 */
4291void netdev_rx_handler_unregister(struct net_device *dev)
4292{
4293
4294 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004295 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004296 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4297 * section has a guarantee to see a non NULL rx_handler_data
4298 * as well.
4299 */
4300 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004301 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004302}
4303EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4304
Mel Gormanb4b9e352012-07-31 16:44:26 -07004305/*
4306 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4307 * the special handling of PFMEMALLOC skbs.
4308 */
4309static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4310{
4311 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004312 case htons(ETH_P_ARP):
4313 case htons(ETH_P_IP):
4314 case htons(ETH_P_IPV6):
4315 case htons(ETH_P_8021Q):
4316 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004317 return true;
4318 default:
4319 return false;
4320 }
4321}
4322
Pablo Neirae687ad62015-05-13 18:19:38 +02004323static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4324 int *ret, struct net_device *orig_dev)
4325{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004326#ifdef CONFIG_NETFILTER_INGRESS
Pablo Neirae687ad62015-05-13 18:19:38 +02004327 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004328 int ingress_retval;
4329
Pablo Neirae687ad62015-05-13 18:19:38 +02004330 if (*pt_prev) {
4331 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4332 *pt_prev = NULL;
4333 }
4334
Aaron Conole2c1e2702016-09-21 11:35:03 -04004335 rcu_read_lock();
4336 ingress_retval = nf_hook_ingress(skb);
4337 rcu_read_unlock();
4338 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004339 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004340#endif /* CONFIG_NETFILTER_INGRESS */
Pablo Neirae687ad62015-05-13 18:19:38 +02004341 return 0;
4342}
Pablo Neirae687ad62015-05-13 18:19:38 +02004343
David S. Miller9754e292013-02-14 15:57:38 -05004344static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004347 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004348 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004349 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08004351 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Eric Dumazet588f0332011-11-15 04:12:55 +00004353 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07004354
Koki Sanagicf66ba52010-08-23 18:45:02 +09004355 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08004356
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07004357 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00004358
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07004359 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00004360 if (!skb_transport_header_was_set(skb))
4361 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00004362 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 pt_prev = NULL;
4365
David S. Miller63d8ea72011-02-28 10:48:59 -08004366another_round:
David S. Millerb6858172012-07-23 16:27:54 -07004367 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08004368
4369 __this_cpu_inc(softnet_data.processed);
4370
Patrick McHardy8ad227f2013-04-19 02:04:31 +00004371 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4372 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004373 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004374 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004375 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004376 }
4377
Willem de Bruijne7246e12017-01-07 17:06:35 -05004378 if (skb_skip_tc_classify(skb))
4379 goto skip_classify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
David S. Miller9754e292013-02-14 15:57:38 -05004381 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07004382 goto skip_taps;
4383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08004385 if (pt_prev)
4386 ret = deliver_skb(skb, pt_prev, orig_dev);
4387 pt_prev = ptype;
4388 }
4389
4390 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
4391 if (pt_prev)
4392 ret = deliver_skb(skb, pt_prev, orig_dev);
4393 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 }
4395
Mel Gormanb4b9e352012-07-31 16:44:26 -07004396skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02004397#ifdef CONFIG_NET_INGRESS
Daniel Borkmann45771392015-04-10 23:07:54 +02004398 if (static_key_false(&ingress_needed)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004399 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02004400 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004401 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02004402
4403 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004404 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02004405 }
Pablo Neira1cf519002015-05-13 18:19:37 +02004406#endif
Willem de Bruijna5135bc2017-01-07 17:06:36 -05004407 skb_reset_tc(skb);
Willem de Bruijne7246e12017-01-07 17:06:35 -05004408skip_classify:
David S. Miller9754e292013-02-14 15:57:38 -05004409 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07004410 goto drop;
4411
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004412 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00004413 if (pt_prev) {
4414 ret = deliver_skb(skb, pt_prev, orig_dev);
4415 pt_prev = NULL;
4416 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004417 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00004418 goto another_round;
4419 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004420 goto out;
John Fastabend24257172011-10-10 09:16:41 +00004421 }
4422
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004423 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004424 if (rx_handler) {
4425 if (pt_prev) {
4426 ret = deliver_skb(skb, pt_prev, orig_dev);
4427 pt_prev = NULL;
4428 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004429 switch (rx_handler(&skb)) {
4430 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00004431 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004432 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004433 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08004434 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004435 case RX_HANDLER_EXACT:
4436 deliver_exact = true;
4437 case RX_HANDLER_PASS:
4438 break;
4439 default:
4440 BUG();
4441 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004444 if (unlikely(skb_vlan_tag_present(skb))) {
4445 if (skb_vlan_tag_get_id(skb))
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004446 skb->pkt_type = PACKET_OTHERHOST;
4447 /* Note: we might in the future use prio bits
4448 * and set skb->priority like in vlan_do_receive()
4449 * For the time being, just ignore Priority Code Point
4450 */
4451 skb->vlan_tci = 0;
4452 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08004455
4456 /* deliver only exact match when indicated */
4457 if (likely(!deliver_exact)) {
4458 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4459 &ptype_base[ntohs(type) &
4460 PTYPE_HASH_MASK]);
4461 }
4462
4463 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4464 &orig_dev->ptype_specific);
4465
4466 if (unlikely(skb->dev != orig_dev)) {
4467 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4468 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470
4471 if (pt_prev) {
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004472 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00004473 goto drop;
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00004474 else
4475 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07004477drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05004478 if (!deliver_exact)
4479 atomic_long_inc(&skb->dev->rx_dropped);
4480 else
4481 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 kfree_skb(skb);
4483 /* Jamal, now you will not able to escape explaining
4484 * me how you were going to use this. :-)
4485 */
4486 ret = NET_RX_DROP;
4487 }
4488
Julian Anastasov2c17d272015-07-09 09:59:10 +03004489out:
David S. Miller9754e292013-02-14 15:57:38 -05004490 return ret;
4491}
4492
4493static int __netif_receive_skb(struct sk_buff *skb)
4494{
4495 int ret;
4496
4497 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
Vlastimil Babkaf1083042017-05-08 15:59:53 -07004498 unsigned int noreclaim_flag;
David S. Miller9754e292013-02-14 15:57:38 -05004499
4500 /*
4501 * PFMEMALLOC skbs are special, they should
4502 * - be delivered to SOCK_MEMALLOC sockets only
4503 * - stay away from userspace
4504 * - have bounded memory usage
4505 *
4506 * Use PF_MEMALLOC as this saves us from propagating the allocation
4507 * context down to all allocation sites.
4508 */
Vlastimil Babkaf1083042017-05-08 15:59:53 -07004509 noreclaim_flag = memalloc_noreclaim_save();
David S. Miller9754e292013-02-14 15:57:38 -05004510 ret = __netif_receive_skb_core(skb, true);
Vlastimil Babkaf1083042017-05-08 15:59:53 -07004511 memalloc_noreclaim_restore(noreclaim_flag);
David S. Miller9754e292013-02-14 15:57:38 -05004512 } else
4513 ret = __netif_receive_skb_core(skb, false);
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return ret;
4516}
Tom Herbert0a9627f2010-03-16 08:03:29 +00004517
David S. Millerb5cdae32017-04-18 15:36:58 -04004518static int generic_xdp_install(struct net_device *dev, struct netdev_xdp *xdp)
4519{
Martin KaFai Lau58038692017-06-15 17:29:09 -07004520 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
David S. Millerb5cdae32017-04-18 15:36:58 -04004521 struct bpf_prog *new = xdp->prog;
4522 int ret = 0;
4523
4524 switch (xdp->command) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07004525 case XDP_SETUP_PROG:
David S. Millerb5cdae32017-04-18 15:36:58 -04004526 rcu_assign_pointer(dev->xdp_prog, new);
4527 if (old)
4528 bpf_prog_put(old);
4529
4530 if (old && !new) {
4531 static_key_slow_dec(&generic_xdp_needed);
4532 } else if (new && !old) {
4533 static_key_slow_inc(&generic_xdp_needed);
4534 dev_disable_lro(dev);
4535 }
4536 break;
David S. Millerb5cdae32017-04-18 15:36:58 -04004537
4538 case XDP_QUERY_PROG:
Martin KaFai Lau58038692017-06-15 17:29:09 -07004539 xdp->prog_attached = !!old;
4540 xdp->prog_id = old ? old->aux->id : 0;
David S. Millerb5cdae32017-04-18 15:36:58 -04004541 break;
4542
4543 default:
4544 ret = -EINVAL;
4545 break;
4546 }
4547
4548 return ret;
4549}
4550
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004551static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004552{
Julian Anastasov2c17d272015-07-09 09:59:10 +03004553 int ret;
4554
Eric Dumazet588f0332011-11-15 04:12:55 +00004555 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07004556
Richard Cochranc1f19b52010-07-17 08:49:36 +00004557 if (skb_defer_rx_timestamp(skb))
4558 return NET_RX_SUCCESS;
4559
David S. Millerb5cdae32017-04-18 15:36:58 -04004560 if (static_key_false(&generic_xdp_needed)) {
John Fastabendbbbe2112017-09-08 14:00:30 -07004561 int ret;
David S. Millerb5cdae32017-04-18 15:36:58 -04004562
John Fastabendbbbe2112017-09-08 14:00:30 -07004563 preempt_disable();
4564 rcu_read_lock();
4565 ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
4566 rcu_read_unlock();
4567 preempt_enable();
4568
4569 if (ret != XDP_PASS)
John Fastabendd4455162017-07-17 09:26:45 -07004570 return NET_RX_DROP;
David S. Millerb5cdae32017-04-18 15:36:58 -04004571 }
4572
John Fastabendbbbe2112017-09-08 14:00:30 -07004573 rcu_read_lock();
Eric Dumazetdf334542010-03-24 19:13:54 +00004574#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004575 if (static_key_false(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07004576 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004577 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07004578
Eric Dumazet3b098e22010-05-15 23:57:10 -07004579 if (cpu >= 0) {
4580 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4581 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00004582 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07004583 }
Tom Herbertfec5e652010-04-16 16:01:27 -07004584 }
Tom Herbert1e94d722010-03-18 17:45:44 -07004585#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03004586 ret = __netif_receive_skb(skb);
4587 rcu_read_unlock();
4588 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004589}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004590
4591/**
4592 * netif_receive_skb - process receive buffer from network
4593 * @skb: buffer to process
4594 *
4595 * netif_receive_skb() is the main receive data processing function.
4596 * It always succeeds. The buffer may be dropped during processing
4597 * for congestion control or by the protocol layers.
4598 *
4599 * This function may only be called from softirq context and interrupts
4600 * should be enabled.
4601 *
4602 * Return values (usually ignored):
4603 * NET_RX_SUCCESS: no congestion
4604 * NET_RX_DROP: packet was dropped
4605 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05004606int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004607{
4608 trace_netif_receive_skb_entry(skb);
4609
4610 return netif_receive_skb_internal(skb);
4611}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05004612EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Eric Dumazet41852492016-08-26 12:50:39 -07004614DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004615
4616/* Network device is going away, flush any packets still pending */
4617static void flush_backlog(struct work_struct *work)
4618{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004619 struct sk_buff *skb, *tmp;
4620 struct softnet_data *sd;
4621
4622 local_bh_disable();
4623 sd = this_cpu_ptr(&softnet_data);
4624
4625 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004626 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07004627 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07004628 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004629 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004630 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004631 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004632 }
Changli Gao6e7676c2010-04-27 15:07:33 -07004633 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004634 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004635 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07004636
4637 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07004638 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07004639 __skb_unlink(skb, &sd->process_queue);
4640 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004641 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07004642 }
4643 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004644 local_bh_enable();
4645}
4646
Eric Dumazet41852492016-08-26 12:50:39 -07004647static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004648{
4649 unsigned int cpu;
4650
4651 get_online_cpus();
4652
Eric Dumazet41852492016-08-26 12:50:39 -07004653 for_each_online_cpu(cpu)
4654 queue_work_on(cpu, system_highpri_wq,
4655 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004656
4657 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07004658 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004659
4660 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004661}
4662
Herbert Xud565b0a2008-12-15 23:38:52 -08004663static int napi_gro_complete(struct sk_buff *skb)
4664{
Vlad Yasevich22061d82012-11-15 08:49:11 +00004665 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08004666 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004667 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08004668 int err = -ENOENT;
4669
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004670 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
4671
Herbert Xufc59f9a2009-04-14 15:11:06 -07004672 if (NAPI_GRO_CB(skb)->count == 1) {
4673 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004674 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07004675 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004676
4677 rcu_read_lock();
4678 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004679 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08004680 continue;
4681
Jerry Chu299603e82013-12-11 20:53:45 -08004682 err = ptype->callbacks.gro_complete(skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08004683 break;
4684 }
4685 rcu_read_unlock();
4686
4687 if (err) {
4688 WARN_ON(&ptype->list == head);
4689 kfree_skb(skb);
4690 return NET_RX_SUCCESS;
4691 }
4692
4693out:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004694 return netif_receive_skb_internal(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004695}
4696
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004697/* napi->gro_list contains packets ordered by age.
4698 * youngest packets at the head of it.
4699 * Complete skbs in reverse order to reduce latencies.
4700 */
4701void napi_gro_flush(struct napi_struct *napi, bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08004702{
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004703 struct sk_buff *skb, *prev = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08004704
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004705 /* scan list and build reverse chain */
4706 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
4707 skb->prev = prev;
4708 prev = skb;
Herbert Xud565b0a2008-12-15 23:38:52 -08004709 }
4710
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004711 for (skb = prev; skb; skb = prev) {
4712 skb->next = NULL;
4713
4714 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
4715 return;
4716
4717 prev = skb->prev;
4718 napi_gro_complete(skb);
4719 napi->gro_count--;
4720 }
4721
Herbert Xud565b0a2008-12-15 23:38:52 -08004722 napi->gro_list = NULL;
4723}
Eric Dumazet86cac582010-08-31 18:25:32 +00004724EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08004725
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004726static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
4727{
4728 struct sk_buff *p;
4729 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08004730 u32 hash = skb_get_hash_raw(skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004731
4732 for (p = napi->gro_list; p; p = p->next) {
4733 unsigned long diffs;
4734
Tom Herbert0b4cec82014-01-15 08:58:06 -08004735 NAPI_GRO_CB(p)->flush = 0;
4736
4737 if (hash != skb_get_hash_raw(p)) {
4738 NAPI_GRO_CB(p)->same_flow = 0;
4739 continue;
4740 }
4741
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004742 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
4743 diffs |= p->vlan_tci ^ skb->vlan_tci;
Jesse Grossce87fc62016-01-20 17:59:49 -08004744 diffs |= skb_metadata_dst_cmp(p, skb);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004745 diffs |= skb_metadata_differs(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004746 if (maclen == ETH_HLEN)
4747 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07004748 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004749 else if (!diffs)
4750 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07004751 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004752 maclen);
4753 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004754 }
4755}
4756
Jerry Chu299603e82013-12-11 20:53:45 -08004757static void skb_gro_reset_offset(struct sk_buff *skb)
4758{
4759 const struct skb_shared_info *pinfo = skb_shinfo(skb);
4760 const skb_frag_t *frag0 = &pinfo->frags[0];
4761
4762 NAPI_GRO_CB(skb)->data_offset = 0;
4763 NAPI_GRO_CB(skb)->frag0 = NULL;
4764 NAPI_GRO_CB(skb)->frag0_len = 0;
4765
4766 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
4767 pinfo->nr_frags &&
4768 !PageHighMem(skb_frag_page(frag0))) {
4769 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet7cfd5fd2017-01-10 19:52:43 -08004770 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
4771 skb_frag_size(frag0),
4772 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08004773 }
4774}
4775
Eric Dumazeta50e2332014-03-29 21:28:21 -07004776static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
4777{
4778 struct skb_shared_info *pinfo = skb_shinfo(skb);
4779
4780 BUG_ON(skb->end - skb->tail < grow);
4781
4782 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
4783
4784 skb->data_len -= grow;
4785 skb->tail += grow;
4786
4787 pinfo->frags[0].page_offset += grow;
4788 skb_frag_size_sub(&pinfo->frags[0], grow);
4789
4790 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
4791 skb_frag_unref(skb, 0);
4792 memmove(pinfo->frags, pinfo->frags + 1,
4793 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
4794 }
4795}
4796
Rami Rosenbb728822012-11-28 21:55:25 +00004797static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08004798{
4799 struct sk_buff **pp = NULL;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004800 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08004801 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004802 struct list_head *head = &offload_base;
Herbert Xu0da2afd52008-12-26 14:57:42 -08004803 int same_flow;
Ben Hutchings5b252f02009-10-29 07:17:09 +00004804 enum gro_result ret;
Eric Dumazeta50e2332014-03-29 21:28:21 -07004805 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08004806
David S. Millerb5cdae32017-04-18 15:36:58 -04004807 if (netif_elide_gro(skb->dev))
Herbert Xud565b0a2008-12-15 23:38:52 -08004808 goto normal;
4809
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004810 gro_list_prepare(napi, skb);
4811
Herbert Xud565b0a2008-12-15 23:38:52 -08004812 rcu_read_lock();
4813 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004814 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08004815 continue;
4816
Herbert Xu86911732009-01-29 14:19:50 +00004817 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00004818 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004819 NAPI_GRO_CB(skb)->same_flow = 0;
Eric Dumazetd61d0722016-11-07 11:12:27 -08004820 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
Herbert Xu5d38a072009-01-04 16:13:40 -08004821 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07004822 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02004823 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07004824 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04004825 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08004826 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004827
Tom Herbert662880f2014-08-27 21:26:56 -07004828 /* Setup for GRO checksum validation */
4829 switch (skb->ip_summed) {
4830 case CHECKSUM_COMPLETE:
4831 NAPI_GRO_CB(skb)->csum = skb->csum;
4832 NAPI_GRO_CB(skb)->csum_valid = 1;
4833 NAPI_GRO_CB(skb)->csum_cnt = 0;
4834 break;
4835 case CHECKSUM_UNNECESSARY:
4836 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
4837 NAPI_GRO_CB(skb)->csum_valid = 0;
4838 break;
4839 default:
4840 NAPI_GRO_CB(skb)->csum_cnt = 0;
4841 NAPI_GRO_CB(skb)->csum_valid = 0;
4842 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004843
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004844 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004845 break;
4846 }
4847 rcu_read_unlock();
4848
4849 if (&ptype->list == head)
4850 goto normal;
4851
Steffen Klassert25393d32017-02-15 09:39:44 +01004852 if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
4853 ret = GRO_CONSUMED;
4854 goto ok;
4855 }
4856
Herbert Xu0da2afd52008-12-26 14:57:42 -08004857 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004858 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08004859
Herbert Xud565b0a2008-12-15 23:38:52 -08004860 if (pp) {
4861 struct sk_buff *nskb = *pp;
4862
4863 *pp = nskb->next;
4864 nskb->next = NULL;
4865 napi_gro_complete(nskb);
Herbert Xu4ae55442009-02-08 18:00:36 +00004866 napi->gro_count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08004867 }
4868
Herbert Xu0da2afd52008-12-26 14:57:42 -08004869 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08004870 goto ok;
4871
Eric Dumazet600adc12014-01-09 14:12:19 -08004872 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08004873 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08004874
Eric Dumazet600adc12014-01-09 14:12:19 -08004875 if (unlikely(napi->gro_count >= MAX_GRO_SKBS)) {
4876 struct sk_buff *nskb = napi->gro_list;
4877
4878 /* locate the end of the list to select the 'oldest' flow */
4879 while (nskb->next) {
4880 pp = &nskb->next;
4881 nskb = *pp;
4882 }
4883 *pp = NULL;
4884 nskb->next = NULL;
4885 napi_gro_complete(nskb);
4886 } else {
4887 napi->gro_count++;
4888 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004889 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004890 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07004891 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00004892 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004893 skb->next = napi->gro_list;
4894 napi->gro_list = skb;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004895 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08004896
Herbert Xuad0f9902009-02-01 01:24:55 -08004897pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07004898 grow = skb_gro_offset(skb) - skb_headlen(skb);
4899 if (grow > 0)
4900 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08004901ok:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004902 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08004903
4904normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08004905 ret = GRO_NORMAL;
4906 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08004907}
Herbert Xu96e93ea2009-01-06 10:49:34 -08004908
Jerry Chubf5a7552014-01-07 10:23:19 -08004909struct packet_offload *gro_find_receive_by_type(__be16 type)
4910{
4911 struct list_head *offload_head = &offload_base;
4912 struct packet_offload *ptype;
4913
4914 list_for_each_entry_rcu(ptype, offload_head, list) {
4915 if (ptype->type != type || !ptype->callbacks.gro_receive)
4916 continue;
4917 return ptype;
4918 }
4919 return NULL;
4920}
Or Gerlitze27a2f82014-01-20 13:59:20 +02004921EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08004922
4923struct packet_offload *gro_find_complete_by_type(__be16 type)
4924{
4925 struct list_head *offload_head = &offload_base;
4926 struct packet_offload *ptype;
4927
4928 list_for_each_entry_rcu(ptype, offload_head, list) {
4929 if (ptype->type != type || !ptype->callbacks.gro_complete)
4930 continue;
4931 return ptype;
4932 }
4933 return NULL;
4934}
Or Gerlitze27a2f82014-01-20 13:59:20 +02004935EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08004936
Michal Kubečeke44699d2017-06-29 11:13:36 +02004937static void napi_skb_free_stolen_head(struct sk_buff *skb)
4938{
4939 skb_dst_drop(skb);
4940 secpath_reset(skb);
4941 kmem_cache_free(skbuff_head_cache, skb);
4942}
4943
Rami Rosenbb728822012-11-28 21:55:25 +00004944static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08004945{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004946 switch (ret) {
4947 case GRO_NORMAL:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004948 if (netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004949 ret = GRO_DROP;
4950 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08004951
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004952 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08004953 kfree_skb(skb);
4954 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00004955
Eric Dumazetdaa86542012-04-19 07:07:40 +00004956 case GRO_MERGED_FREE:
Michal Kubečeke44699d2017-06-29 11:13:36 +02004957 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
4958 napi_skb_free_stolen_head(skb);
4959 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00004960 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00004961 break;
4962
Ben Hutchings5b252f02009-10-29 07:17:09 +00004963 case GRO_HELD:
4964 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01004965 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00004966 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08004967 }
4968
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004969 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004970}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004971
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004972gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004973{
Eric Dumazet93f93a42015-11-18 06:30:59 -08004974 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004975 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00004976
Eric Dumazeta50e2332014-03-29 21:28:21 -07004977 skb_gro_reset_offset(skb);
4978
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004979 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004980}
4981EXPORT_SYMBOL(napi_gro_receive);
4982
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00004983static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08004984{
Eric Dumazet93a35f52014-10-23 06:30:30 -07004985 if (unlikely(skb->pfmemalloc)) {
4986 consume_skb(skb);
4987 return;
4988 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08004989 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00004990 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
4991 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Jesse Gross3701e512010-10-20 13:56:06 +00004992 skb->vlan_tci = 0;
Herbert Xu66c46d72011-01-29 20:44:54 -08004993 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08004994 skb->skb_iif = 0;
Jerry Chuc3caf112014-07-14 15:54:46 -07004995 skb->encapsulation = 0;
4996 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07004997 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Steffen Klassertf991bb92017-01-30 06:45:38 +01004998 secpath_reset(skb);
Herbert Xu96e93ea2009-01-06 10:49:34 -08004999
5000 napi->skb = skb;
5001}
Herbert Xu96e93ea2009-01-06 10:49:34 -08005002
Herbert Xu76620aa2009-04-16 02:02:07 -07005003struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08005004{
Herbert Xu5d38a072009-01-04 16:13:40 -08005005 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08005006
5007 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08005008 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08005009 if (skb) {
5010 napi->skb = skb;
5011 skb_mark_napi_id(skb, napi);
5012 }
Herbert Xu5d38a072009-01-04 16:13:40 -08005013 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08005014 return skb;
5015}
Herbert Xu76620aa2009-04-16 02:02:07 -07005016EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005017
Eric Dumazeta50e2332014-03-29 21:28:21 -07005018static gro_result_t napi_frags_finish(struct napi_struct *napi,
5019 struct sk_buff *skb,
5020 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005021{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005022 switch (ret) {
5023 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07005024 case GRO_HELD:
5025 __skb_push(skb, ETH_HLEN);
5026 skb->protocol = eth_type_trans(skb, skb->dev);
5027 if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005028 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00005029 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005030
5031 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005032 napi_reuse_skb(napi, skb);
5033 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00005034
Michal Kubečeke44699d2017-06-29 11:13:36 +02005035 case GRO_MERGED_FREE:
5036 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5037 napi_skb_free_stolen_head(skb);
5038 else
5039 napi_reuse_skb(napi, skb);
5040 break;
5041
Ben Hutchings5b252f02009-10-29 07:17:09 +00005042 case GRO_MERGED:
Steffen Klassert25393d32017-02-15 09:39:44 +01005043 case GRO_CONSUMED:
Ben Hutchings5b252f02009-10-29 07:17:09 +00005044 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005045 }
5046
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005047 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005048}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00005049
Eric Dumazeta50e2332014-03-29 21:28:21 -07005050/* Upper GRO stack assumes network header starts at gro_offset=0
5051 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5052 * We copy ethernet header into skb->data to have a common layout.
5053 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00005054static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08005055{
Herbert Xu76620aa2009-04-16 02:02:07 -07005056 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07005057 const struct ethhdr *eth;
5058 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07005059
5060 napi->skb = NULL;
5061
Eric Dumazeta50e2332014-03-29 21:28:21 -07005062 skb_reset_mac_header(skb);
5063 skb_gro_reset_offset(skb);
5064
5065 eth = skb_gro_header_fast(skb, 0);
5066 if (unlikely(skb_gro_header_hard(skb, hlen))) {
5067 eth = skb_gro_header_slow(skb, hlen, 0);
5068 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04005069 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
5070 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07005071 napi_reuse_skb(napi, skb);
5072 return NULL;
5073 }
5074 } else {
5075 gro_pull_from_frag0(skb, hlen);
5076 NAPI_GRO_CB(skb)->frag0 += hlen;
5077 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07005078 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07005079 __skb_pull(skb, hlen);
5080
5081 /*
5082 * This works because the only protocols we care about don't require
5083 * special handling.
5084 * We'll fix it up properly in napi_frags_finish()
5085 */
5086 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07005087
Herbert Xu76620aa2009-04-16 02:02:07 -07005088 return skb;
5089}
Herbert Xu76620aa2009-04-16 02:02:07 -07005090
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005091gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07005092{
5093 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08005094
5095 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07005096 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08005097
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00005098 trace_napi_gro_frags_entry(skb);
5099
Eric Dumazet89c5fa32012-12-10 13:28:16 +00005100 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
Herbert Xu5d38a072009-01-04 16:13:40 -08005101}
5102EXPORT_SYMBOL(napi_gro_frags);
5103
Tom Herbert573e8fc2014-08-22 13:33:47 -07005104/* Compute the checksum from gro_offset and return the folded value
5105 * after adding in any pseudo checksum.
5106 */
5107__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
5108{
5109 __wsum wsum;
5110 __sum16 sum;
5111
5112 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
5113
5114 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
5115 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
5116 if (likely(!sum)) {
5117 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
5118 !skb->csum_complete_sw)
5119 netdev_rx_csum_fault(skb->dev);
5120 }
5121
5122 NAPI_GRO_CB(skb)->csum = wsum;
5123 NAPI_GRO_CB(skb)->csum_valid = 1;
5124
5125 return sum;
5126}
5127EXPORT_SYMBOL(__skb_gro_checksum_complete);
5128
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305129static void net_rps_send_ipi(struct softnet_data *remsd)
5130{
5131#ifdef CONFIG_RPS
5132 while (remsd) {
5133 struct softnet_data *next = remsd->rps_ipi_next;
5134
5135 if (cpu_online(remsd->cpu))
5136 smp_call_function_single_async(remsd->cpu, &remsd->csd);
5137 remsd = next;
5138 }
5139#endif
5140}
5141
Eric Dumazete326bed2010-04-22 00:22:45 -07005142/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08005143 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07005144 * Note: called with local irq disabled, but exits with local irq enabled.
5145 */
5146static void net_rps_action_and_irq_enable(struct softnet_data *sd)
5147{
5148#ifdef CONFIG_RPS
5149 struct softnet_data *remsd = sd->rps_ipi_list;
5150
5151 if (remsd) {
5152 sd->rps_ipi_list = NULL;
5153
5154 local_irq_enable();
5155
5156 /* Send pending IPI's to kick RPS processing on remote cpus. */
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05305157 net_rps_send_ipi(remsd);
Eric Dumazete326bed2010-04-22 00:22:45 -07005158 } else
5159#endif
5160 local_irq_enable();
5161}
5162
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005163static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5164{
5165#ifdef CONFIG_RPS
5166 return sd->rps_ipi_list != NULL;
5167#else
5168 return false;
5169#endif
5170}
5171
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005172static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005174 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005175 bool again = true;
5176 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Eric Dumazete326bed2010-04-22 00:22:45 -07005178 /* Check if we have pending ipi, its better to send them now,
5179 * not waiting net_rx_action() end.
5180 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005181 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07005182 local_irq_disable();
5183 net_rps_action_and_irq_enable(sd);
5184 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005185
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +01005186 napi->weight = dev_rx_weight;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005187 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
Changli Gao6e7676c2010-04-27 15:07:33 -07005190 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03005191 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07005192 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03005193 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00005194 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005195 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00005196 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005197
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005200 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005201 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07005202 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005203 /*
5204 * Inline a custom version of __napi_complete().
5205 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07005206 * and NAPI_STATE_SCHED is the only possible flag set
5207 * on backlog.
5208 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005209 * and we dont need an smp_mb() memory barrier.
5210 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07005211 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005212 again = false;
5213 } else {
5214 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5215 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07005216 }
5217 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02005218 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07005219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005221 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222}
5223
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005224/**
5225 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07005226 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005227 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005228 * The entry's receive function will be scheduled to run.
5229 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005230 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08005231void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005232{
5233 unsigned long flags;
5234
5235 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05005236 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005237 local_irq_restore(flags);
5238}
5239EXPORT_SYMBOL(__napi_schedule);
5240
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005241/**
Eric Dumazet39e6c822017-02-28 10:34:50 -08005242 * napi_schedule_prep - check if napi can be scheduled
5243 * @n: napi context
5244 *
5245 * Test if NAPI routine is already running, and if not mark
5246 * it as running. This is used as a condition variable
5247 * insure only one NAPI poll instance runs. We also make
5248 * sure there is no pending NAPI disable.
5249 */
5250bool napi_schedule_prep(struct napi_struct *n)
5251{
5252 unsigned long val, new;
5253
5254 do {
5255 val = READ_ONCE(n->state);
5256 if (unlikely(val & NAPIF_STATE_DISABLE))
5257 return false;
5258 new = val | NAPIF_STATE_SCHED;
5259
5260 /* Sets STATE_MISSED bit if STATE_SCHED was already set
5261 * This was suggested by Alexander Duyck, as compiler
5262 * emits better code than :
5263 * if (val & NAPIF_STATE_SCHED)
5264 * new |= NAPIF_STATE_MISSED;
5265 */
5266 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5267 NAPIF_STATE_MISSED;
5268 } while (cmpxchg(&n->state, val, new) != val);
5269
5270 return !(val & NAPIF_STATE_SCHED);
5271}
5272EXPORT_SYMBOL(napi_schedule_prep);
5273
5274/**
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005275 * __napi_schedule_irqoff - schedule for receive
5276 * @n: entry to schedule
5277 *
5278 * Variant of __napi_schedule() assuming hard irqs are masked
5279 */
5280void __napi_schedule_irqoff(struct napi_struct *n)
5281{
5282 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5283}
5284EXPORT_SYMBOL(__napi_schedule_irqoff);
5285
Eric Dumazet364b6052016-11-15 10:15:13 -08005286bool napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08005287{
Eric Dumazet39e6c822017-02-28 10:34:50 -08005288 unsigned long flags, val, new;
Herbert Xud565b0a2008-12-15 23:38:52 -08005289
5290 /*
Eric Dumazet217f6972016-11-15 10:15:11 -08005291 * 1) Don't let napi dequeue from the cpu poll list
5292 * just in case its running on a different cpu.
5293 * 2) If we are busy polling, do nothing here, we have
5294 * the guarantee we will be called later.
Herbert Xud565b0a2008-12-15 23:38:52 -08005295 */
Eric Dumazet217f6972016-11-15 10:15:11 -08005296 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
5297 NAPIF_STATE_IN_BUSY_POLL)))
Eric Dumazet364b6052016-11-15 10:15:13 -08005298 return false;
Herbert Xud565b0a2008-12-15 23:38:52 -08005299
Eric Dumazet3b47d302014-11-06 21:09:44 -08005300 if (n->gro_list) {
5301 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005302
Eric Dumazet3b47d302014-11-06 21:09:44 -08005303 if (work_done)
5304 timeout = n->dev->gro_flush_timeout;
5305
5306 if (timeout)
5307 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5308 HRTIMER_MODE_REL_PINNED);
5309 else
5310 napi_gro_flush(n, false);
5311 }
Eric Dumazet02c16022017-02-04 15:25:02 -08005312 if (unlikely(!list_empty(&n->poll_list))) {
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005313 /* If n->poll_list is not empty, we need to mask irqs */
5314 local_irq_save(flags);
Eric Dumazet02c16022017-02-04 15:25:02 -08005315 list_del_init(&n->poll_list);
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005316 local_irq_restore(flags);
5317 }
Eric Dumazet39e6c822017-02-28 10:34:50 -08005318
5319 do {
5320 val = READ_ONCE(n->state);
5321
5322 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
5323
5324 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
5325
5326 /* If STATE_MISSED was set, leave STATE_SCHED set,
5327 * because we will call napi->poll() one more time.
5328 * This C code was suggested by Alexander Duyck to help gcc.
5329 */
5330 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
5331 NAPIF_STATE_SCHED;
5332 } while (cmpxchg(&n->state, val, new) != val);
5333
5334 if (unlikely(val & NAPIF_STATE_MISSED)) {
5335 __napi_schedule(n);
5336 return false;
5337 }
5338
Eric Dumazet364b6052016-11-15 10:15:13 -08005339 return true;
Herbert Xud565b0a2008-12-15 23:38:52 -08005340}
Eric Dumazet3b47d302014-11-06 21:09:44 -08005341EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08005342
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005343/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08005344static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005345{
5346 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
5347 struct napi_struct *napi;
5348
5349 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
5350 if (napi->napi_id == napi_id)
5351 return napi;
5352
5353 return NULL;
5354}
Eric Dumazet02d62e82015-11-18 06:30:52 -08005355
5356#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazet217f6972016-11-15 10:15:11 -08005357
Eric Dumazetce6aea92015-11-18 06:30:54 -08005358#define BUSY_POLL_BUDGET 8
Eric Dumazet217f6972016-11-15 10:15:11 -08005359
5360static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
5361{
5362 int rc;
5363
Eric Dumazet39e6c822017-02-28 10:34:50 -08005364 /* Busy polling means there is a high chance device driver hard irq
5365 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
5366 * set in napi_schedule_prep().
5367 * Since we are about to call napi->poll() once more, we can safely
5368 * clear NAPI_STATE_MISSED.
5369 *
5370 * Note: x86 could use a single "lock and ..." instruction
5371 * to perform these two clear_bit()
5372 */
5373 clear_bit(NAPI_STATE_MISSED, &napi->state);
Eric Dumazet217f6972016-11-15 10:15:11 -08005374 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
5375
5376 local_bh_disable();
5377
5378 /* All we really want here is to re-enable device interrupts.
5379 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
5380 */
5381 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Jesper Dangaard Brouer1e223912017-08-25 15:04:32 +02005382 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08005383 netpoll_poll_unlock(have_poll_lock);
5384 if (rc == BUSY_POLL_BUDGET)
5385 __napi_schedule(napi);
5386 local_bh_enable();
Eric Dumazet217f6972016-11-15 10:15:11 -08005387}
5388
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005389void napi_busy_loop(unsigned int napi_id,
5390 bool (*loop_end)(void *, unsigned long),
5391 void *loop_end_arg)
Eric Dumazet02d62e82015-11-18 06:30:52 -08005392{
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005393 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
Eric Dumazet217f6972016-11-15 10:15:11 -08005394 int (*napi_poll)(struct napi_struct *napi, int budget);
Eric Dumazet217f6972016-11-15 10:15:11 -08005395 void *have_poll_lock = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005396 struct napi_struct *napi;
Eric Dumazet217f6972016-11-15 10:15:11 -08005397
5398restart:
Eric Dumazet217f6972016-11-15 10:15:11 -08005399 napi_poll = NULL;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005400
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005401 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005402
Alexander Duyck545cd5e2017-03-24 10:07:53 -07005403 napi = napi_by_id(napi_id);
Eric Dumazet02d62e82015-11-18 06:30:52 -08005404 if (!napi)
5405 goto out;
5406
Eric Dumazet217f6972016-11-15 10:15:11 -08005407 preempt_disable();
5408 for (;;) {
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005409 int work = 0;
5410
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005411 local_bh_disable();
Eric Dumazet217f6972016-11-15 10:15:11 -08005412 if (!napi_poll) {
5413 unsigned long val = READ_ONCE(napi->state);
5414
5415 /* If multiple threads are competing for this napi,
5416 * we avoid dirtying napi->state as much as we can.
5417 */
5418 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
5419 NAPIF_STATE_IN_BUSY_POLL))
5420 goto count;
5421 if (cmpxchg(&napi->state, val,
5422 val | NAPIF_STATE_IN_BUSY_POLL |
5423 NAPIF_STATE_SCHED) != val)
5424 goto count;
5425 have_poll_lock = netpoll_poll_lock(napi);
5426 napi_poll = napi->poll;
5427 }
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005428 work = napi_poll(napi, BUSY_POLL_BUDGET);
5429 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
Eric Dumazet217f6972016-11-15 10:15:11 -08005430count:
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005431 if (work > 0)
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005432 __NET_ADD_STATS(dev_net(napi->dev),
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005433 LINUX_MIB_BUSYPOLLRXPACKETS, work);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005434 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005435
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005436 if (!loop_end || loop_end(loop_end_arg, start_time))
Eric Dumazet217f6972016-11-15 10:15:11 -08005437 break;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005438
Eric Dumazet217f6972016-11-15 10:15:11 -08005439 if (unlikely(need_resched())) {
5440 if (napi_poll)
5441 busy_poll_stop(napi, have_poll_lock);
5442 preempt_enable();
5443 rcu_read_unlock();
5444 cond_resched();
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005445 if (loop_end(loop_end_arg, start_time))
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07005446 return;
Eric Dumazet217f6972016-11-15 10:15:11 -08005447 goto restart;
5448 }
Linus Torvalds6cdf89b2016-12-12 10:48:02 -08005449 cpu_relax();
Eric Dumazet217f6972016-11-15 10:15:11 -08005450 }
5451 if (napi_poll)
5452 busy_poll_stop(napi, have_poll_lock);
5453 preempt_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005454out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005455 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005456}
Sridhar Samudrala7db6b042017-03-24 10:08:24 -07005457EXPORT_SYMBOL(napi_busy_loop);
Eric Dumazet02d62e82015-11-18 06:30:52 -08005458
5459#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005460
Eric Dumazet149d6ad2016-11-08 11:07:28 -08005461static void napi_hash_add(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005462{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08005463 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
5464 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005465 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005466
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005467 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005468
Alexander Duyck545cd5e2017-03-24 10:07:53 -07005469 /* 0..NR_CPUS range is reserved for sender_cpu use */
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005470 do {
Alexander Duyck545cd5e2017-03-24 10:07:53 -07005471 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
5472 napi_gen_id = MIN_NAPI_ID;
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005473 } while (napi_by_id(napi_gen_id));
5474 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005475
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005476 hlist_add_head_rcu(&napi->napi_hash_node,
5477 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005478
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005479 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005480}
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005481
5482/* Warning : caller is responsible to make sure rcu grace period
5483 * is respected before freeing memory containing @napi
5484 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08005485bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005486{
Eric Dumazet34cbe272015-11-18 06:31:02 -08005487 bool rcu_sync_needed = false;
5488
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005489 spin_lock(&napi_hash_lock);
5490
Eric Dumazet34cbe272015-11-18 06:31:02 -08005491 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
5492 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005493 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08005494 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005495 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08005496 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005497}
5498EXPORT_SYMBOL_GPL(napi_hash_del);
5499
Eric Dumazet3b47d302014-11-06 21:09:44 -08005500static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
5501{
5502 struct napi_struct *napi;
5503
5504 napi = container_of(timer, struct napi_struct, timer);
Eric Dumazet39e6c822017-02-28 10:34:50 -08005505
5506 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
5507 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
5508 */
5509 if (napi->gro_list && !napi_disable_pending(napi) &&
5510 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
5511 __napi_schedule_irqoff(napi);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005512
5513 return HRTIMER_NORESTART;
5514}
5515
Herbert Xud565b0a2008-12-15 23:38:52 -08005516void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
5517 int (*poll)(struct napi_struct *, int), int weight)
5518{
5519 INIT_LIST_HEAD(&napi->poll_list);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005520 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
5521 napi->timer.function = napi_watchdog;
Herbert Xu4ae55442009-02-08 18:00:36 +00005522 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005523 napi->gro_list = NULL;
Herbert Xu5d38a072009-01-04 16:13:40 -08005524 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08005525 napi->poll = poll;
Eric Dumazet82dc3c62013-03-05 15:57:22 +00005526 if (weight > NAPI_POLL_WEIGHT)
5527 pr_err_once("netif_napi_add() called with weight %d on device %s\n",
5528 weight, dev->name);
Herbert Xud565b0a2008-12-15 23:38:52 -08005529 napi->weight = weight;
5530 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08005531 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08005532#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08005533 napi->poll_owner = -1;
5534#endif
5535 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08005536 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08005537}
5538EXPORT_SYMBOL(netif_napi_add);
5539
Eric Dumazet3b47d302014-11-06 21:09:44 -08005540void napi_disable(struct napi_struct *n)
5541{
5542 might_sleep();
5543 set_bit(NAPI_STATE_DISABLE, &n->state);
5544
5545 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
5546 msleep(1);
Neil Horman2d8bff1262015-09-23 14:57:58 -04005547 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
5548 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005549
5550 hrtimer_cancel(&n->timer);
5551
5552 clear_bit(NAPI_STATE_DISABLE, &n->state);
5553}
5554EXPORT_SYMBOL(napi_disable);
5555
Eric Dumazet93d05d42015-11-18 06:31:03 -08005556/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08005557void netif_napi_del(struct napi_struct *napi)
5558{
Eric Dumazet93d05d42015-11-18 06:31:03 -08005559 might_sleep();
5560 if (napi_hash_del(napi))
5561 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08005562 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07005563 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08005564
Eric Dumazet289dccb2013-12-20 14:29:08 -08005565 kfree_skb_list(napi->gro_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08005566 napi->gro_list = NULL;
Herbert Xu4ae55442009-02-08 18:00:36 +00005567 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005568}
5569EXPORT_SYMBOL(netif_napi_del);
5570
Herbert Xu726ce702014-12-21 07:16:21 +11005571static int napi_poll(struct napi_struct *n, struct list_head *repoll)
5572{
5573 void *have;
5574 int work, weight;
5575
5576 list_del_init(&n->poll_list);
5577
5578 have = netpoll_poll_lock(n);
5579
5580 weight = n->weight;
5581
5582 /* This NAPI_STATE_SCHED test is for avoiding a race
5583 * with netpoll's poll_napi(). Only the entity which
5584 * obtains the lock and sees NAPI_STATE_SCHED set will
5585 * actually make the ->poll() call. Therefore we avoid
5586 * accidentally calling ->poll() when NAPI is not scheduled.
5587 */
5588 work = 0;
5589 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
5590 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02005591 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11005592 }
5593
5594 WARN_ON_ONCE(work > weight);
5595
5596 if (likely(work < weight))
5597 goto out_unlock;
5598
5599 /* Drivers must not modify the NAPI state if they
5600 * consume the entire weight. In such cases this code
5601 * still "owns" the NAPI instance and therefore can
5602 * move the instance around on the list at-will.
5603 */
5604 if (unlikely(napi_disable_pending(n))) {
5605 napi_complete(n);
5606 goto out_unlock;
5607 }
5608
5609 if (n->gro_list) {
5610 /* flush too old packets
5611 * If HZ < 1000, flush all packets.
5612 */
5613 napi_gro_flush(n, HZ >= 1000);
5614 }
5615
Herbert Xu001ce542014-12-21 07:16:22 +11005616 /* Some drivers may have called napi_schedule
5617 * prior to exhausting their budget.
5618 */
5619 if (unlikely(!list_empty(&n->poll_list))) {
5620 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
5621 n->dev ? n->dev->name : "backlog");
5622 goto out_unlock;
5623 }
5624
Herbert Xu726ce702014-12-21 07:16:21 +11005625 list_add_tail(&n->poll_list, repoll);
5626
5627out_unlock:
5628 netpoll_poll_unlock(have);
5629
5630 return work;
5631}
5632
Emese Revfy0766f782016-06-20 20:42:34 +02005633static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634{
Christoph Lameter903ceff2014-08-17 12:30:35 -05005635 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Matthew Whitehead7acf8a12017-04-19 12:37:10 -04005636 unsigned long time_limit = jiffies +
5637 usecs_to_jiffies(netdev_budget_usecs);
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07005638 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005639 LIST_HEAD(list);
5640 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07005641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005643 list_splice_init(&sd->poll_list, &list);
5644 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005646 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005647 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005649 if (list_empty(&list)) {
5650 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
Eric Dumazetf52dffe2016-11-23 08:44:56 -08005651 goto out;
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005652 break;
5653 }
5654
Herbert Xu6bd373e2014-12-21 07:16:24 +11005655 n = list_first_entry(&list, struct napi_struct, poll_list);
5656 budget -= napi_poll(n, &repoll);
5657
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005658 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08005659 * Allow this to run for 2 jiffies since which will allow
5660 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005661 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005662 if (unlikely(budget <= 0 ||
5663 time_after_eq(jiffies, time_limit))) {
5664 sd->time_squeeze++;
5665 break;
5666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005668
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005669 local_irq_disable();
5670
5671 list_splice_tail_init(&sd->poll_list, &list);
5672 list_splice_tail(&repoll, &list);
5673 list_splice(&list, &sd->poll_list);
5674 if (!list_empty(&sd->poll_list))
5675 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
5676
Eric Dumazete326bed2010-04-22 00:22:45 -07005677 net_rps_action_and_irq_enable(sd);
Eric Dumazetf52dffe2016-11-23 08:44:56 -08005678out:
5679 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680}
5681
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005682struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005683 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005684
5685 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005686 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005687
Veaceslav Falico5d261912013-08-28 23:25:05 +02005688 /* counter for the number of times this device was added to us */
5689 u16 ref_nr;
5690
Veaceslav Falico402dae92013-09-25 09:20:09 +02005691 /* private field for the users */
5692 void *private;
5693
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005694 struct list_head list;
5695 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005696};
5697
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005698static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005699 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005700{
Veaceslav Falico5d261912013-08-28 23:25:05 +02005701 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005702
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005703 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02005704 if (adj->dev == adj_dev)
5705 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005706 }
5707 return NULL;
5708}
5709
David Ahernf1170fd2016-10-17 19:15:51 -07005710static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
5711{
5712 struct net_device *dev = data;
5713
5714 return upper_dev == dev;
5715}
5716
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005717/**
5718 * netdev_has_upper_dev - Check if device is linked to an upper device
5719 * @dev: device
5720 * @upper_dev: upper device to check
5721 *
5722 * Find out if a device is linked to specified upper device and return true
5723 * in case it is. Note that this checks only immediate upper device,
5724 * not through a complete stack of devices. The caller must hold the RTNL lock.
5725 */
5726bool netdev_has_upper_dev(struct net_device *dev,
5727 struct net_device *upper_dev)
5728{
5729 ASSERT_RTNL();
5730
David Ahernf1170fd2016-10-17 19:15:51 -07005731 return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
5732 upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005733}
5734EXPORT_SYMBOL(netdev_has_upper_dev);
5735
5736/**
David Ahern1a3f0602016-10-17 19:15:44 -07005737 * netdev_has_upper_dev_all - Check if device is linked to an upper device
5738 * @dev: device
5739 * @upper_dev: upper device to check
5740 *
5741 * Find out if a device is linked to specified upper device and return true
5742 * in case it is. Note that this checks the entire upper device chain.
5743 * The caller must hold rcu lock.
5744 */
5745
David Ahern1a3f0602016-10-17 19:15:44 -07005746bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
5747 struct net_device *upper_dev)
5748{
5749 return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
5750 upper_dev);
5751}
5752EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
5753
5754/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005755 * netdev_has_any_upper_dev - Check if device is linked to some device
5756 * @dev: device
5757 *
5758 * Find out if a device is linked to an upper device and return true in case
5759 * it is. The caller must hold the RTNL lock.
5760 */
Ido Schimmel25cc72a2017-09-01 10:52:31 +02005761bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005762{
5763 ASSERT_RTNL();
5764
David Ahernf1170fd2016-10-17 19:15:51 -07005765 return !list_empty(&dev->adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005766}
Ido Schimmel25cc72a2017-09-01 10:52:31 +02005767EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005768
5769/**
5770 * netdev_master_upper_dev_get - Get master upper device
5771 * @dev: device
5772 *
5773 * Find a master upper device and return pointer to it or NULL in case
5774 * it's not there. The caller must hold the RTNL lock.
5775 */
5776struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
5777{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005778 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005779
5780 ASSERT_RTNL();
5781
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005782 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005783 return NULL;
5784
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005785 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005786 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005787 if (likely(upper->master))
5788 return upper->dev;
5789 return NULL;
5790}
5791EXPORT_SYMBOL(netdev_master_upper_dev_get);
5792
David Ahern0f524a82016-10-17 19:15:52 -07005793/**
5794 * netdev_has_any_lower_dev - Check if device is linked to some device
5795 * @dev: device
5796 *
5797 * Find out if a device is linked to a lower device and return true in case
5798 * it is. The caller must hold the RTNL lock.
5799 */
5800static bool netdev_has_any_lower_dev(struct net_device *dev)
5801{
5802 ASSERT_RTNL();
5803
5804 return !list_empty(&dev->adj_list.lower);
5805}
5806
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02005807void *netdev_adjacent_get_private(struct list_head *adj_list)
5808{
5809 struct netdev_adjacent *adj;
5810
5811 adj = list_entry(adj_list, struct netdev_adjacent, list);
5812
5813 return adj->private;
5814}
5815EXPORT_SYMBOL(netdev_adjacent_get_private);
5816
Veaceslav Falico31088a12013-09-25 09:20:12 +02005817/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04005818 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
5819 * @dev: device
5820 * @iter: list_head ** of the current position
5821 *
5822 * Gets the next device from the dev's upper list, starting from iter
5823 * position. The caller must hold RCU read lock.
5824 */
5825struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
5826 struct list_head **iter)
5827{
5828 struct netdev_adjacent *upper;
5829
5830 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5831
5832 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5833
5834 if (&upper->list == &dev->adj_list.upper)
5835 return NULL;
5836
5837 *iter = &upper->list;
5838
5839 return upper->dev;
5840}
5841EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
5842
David Ahern1a3f0602016-10-17 19:15:44 -07005843static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
5844 struct list_head **iter)
5845{
5846 struct netdev_adjacent *upper;
5847
5848 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5849
5850 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5851
5852 if (&upper->list == &dev->adj_list.upper)
5853 return NULL;
5854
5855 *iter = &upper->list;
5856
5857 return upper->dev;
5858}
5859
5860int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
5861 int (*fn)(struct net_device *dev,
5862 void *data),
5863 void *data)
5864{
5865 struct net_device *udev;
5866 struct list_head *iter;
5867 int ret;
5868
5869 for (iter = &dev->adj_list.upper,
5870 udev = netdev_next_upper_dev_rcu(dev, &iter);
5871 udev;
5872 udev = netdev_next_upper_dev_rcu(dev, &iter)) {
5873 /* first is the upper device itself */
5874 ret = fn(udev, data);
5875 if (ret)
5876 return ret;
5877
5878 /* then look at all of its upper devices */
5879 ret = netdev_walk_all_upper_dev_rcu(udev, fn, data);
5880 if (ret)
5881 return ret;
5882 }
5883
5884 return 0;
5885}
5886EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
5887
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005888/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02005889 * netdev_lower_get_next_private - Get the next ->private from the
5890 * lower neighbour list
5891 * @dev: device
5892 * @iter: list_head ** of the current position
5893 *
5894 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5895 * list, starting from iter position. The caller must hold either hold the
5896 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00005897 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02005898 */
5899void *netdev_lower_get_next_private(struct net_device *dev,
5900 struct list_head **iter)
5901{
5902 struct netdev_adjacent *lower;
5903
5904 lower = list_entry(*iter, struct netdev_adjacent, list);
5905
5906 if (&lower->list == &dev->adj_list.lower)
5907 return NULL;
5908
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02005909 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02005910
5911 return lower->private;
5912}
5913EXPORT_SYMBOL(netdev_lower_get_next_private);
5914
5915/**
5916 * netdev_lower_get_next_private_rcu - Get the next ->private from the
5917 * lower neighbour list, RCU
5918 * variant
5919 * @dev: device
5920 * @iter: list_head ** of the current position
5921 *
5922 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5923 * list, starting from iter position. The caller must hold RCU read lock.
5924 */
5925void *netdev_lower_get_next_private_rcu(struct net_device *dev,
5926 struct list_head **iter)
5927{
5928 struct netdev_adjacent *lower;
5929
5930 WARN_ON_ONCE(!rcu_read_lock_held());
5931
5932 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5933
5934 if (&lower->list == &dev->adj_list.lower)
5935 return NULL;
5936
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02005937 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02005938
5939 return lower->private;
5940}
5941EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
5942
5943/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005944 * netdev_lower_get_next - Get the next device from the lower neighbour
5945 * list
5946 * @dev: device
5947 * @iter: list_head ** of the current position
5948 *
5949 * Gets the next netdev_adjacent from the dev's lower neighbour
5950 * list, starting from iter position. The caller must hold RTNL lock or
5951 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00005952 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005953 */
5954void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
5955{
5956 struct netdev_adjacent *lower;
5957
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01005958 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005959
5960 if (&lower->list == &dev->adj_list.lower)
5961 return NULL;
5962
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01005963 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005964
5965 return lower->dev;
5966}
5967EXPORT_SYMBOL(netdev_lower_get_next);
5968
David Ahern1a3f0602016-10-17 19:15:44 -07005969static struct net_device *netdev_next_lower_dev(struct net_device *dev,
5970 struct list_head **iter)
5971{
5972 struct netdev_adjacent *lower;
5973
David Ahern46b5ab12016-10-26 13:21:33 -07005974 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
David Ahern1a3f0602016-10-17 19:15:44 -07005975
5976 if (&lower->list == &dev->adj_list.lower)
5977 return NULL;
5978
David Ahern46b5ab12016-10-26 13:21:33 -07005979 *iter = &lower->list;
David Ahern1a3f0602016-10-17 19:15:44 -07005980
5981 return lower->dev;
5982}
5983
5984int netdev_walk_all_lower_dev(struct net_device *dev,
5985 int (*fn)(struct net_device *dev,
5986 void *data),
5987 void *data)
5988{
5989 struct net_device *ldev;
5990 struct list_head *iter;
5991 int ret;
5992
5993 for (iter = &dev->adj_list.lower,
5994 ldev = netdev_next_lower_dev(dev, &iter);
5995 ldev;
5996 ldev = netdev_next_lower_dev(dev, &iter)) {
5997 /* first is the lower device itself */
5998 ret = fn(ldev, data);
5999 if (ret)
6000 return ret;
6001
6002 /* then look at all of its lower devices */
6003 ret = netdev_walk_all_lower_dev(ldev, fn, data);
6004 if (ret)
6005 return ret;
6006 }
6007
6008 return 0;
6009}
6010EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
6011
David Ahern1a3f0602016-10-17 19:15:44 -07006012static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
6013 struct list_head **iter)
6014{
6015 struct netdev_adjacent *lower;
6016
6017 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6018 if (&lower->list == &dev->adj_list.lower)
6019 return NULL;
6020
6021 *iter = &lower->list;
6022
6023 return lower->dev;
6024}
6025
6026int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
6027 int (*fn)(struct net_device *dev,
6028 void *data),
6029 void *data)
6030{
6031 struct net_device *ldev;
6032 struct list_head *iter;
6033 int ret;
6034
6035 for (iter = &dev->adj_list.lower,
6036 ldev = netdev_next_lower_dev_rcu(dev, &iter);
6037 ldev;
6038 ldev = netdev_next_lower_dev_rcu(dev, &iter)) {
6039 /* first is the lower device itself */
6040 ret = fn(ldev, data);
6041 if (ret)
6042 return ret;
6043
6044 /* then look at all of its lower devices */
6045 ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data);
6046 if (ret)
6047 return ret;
6048 }
6049
6050 return 0;
6051}
6052EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
6053
Jiri Pirko7ce856a2016-07-04 08:23:12 +02006054/**
dingtianhonge001bfa2013-12-13 10:19:55 +08006055 * netdev_lower_get_first_private_rcu - Get the first ->private from the
6056 * lower neighbour list, RCU
6057 * variant
6058 * @dev: device
6059 *
6060 * Gets the first netdev_adjacent->private from the dev's lower neighbour
6061 * list. The caller must hold RCU read lock.
6062 */
6063void *netdev_lower_get_first_private_rcu(struct net_device *dev)
6064{
6065 struct netdev_adjacent *lower;
6066
6067 lower = list_first_or_null_rcu(&dev->adj_list.lower,
6068 struct netdev_adjacent, list);
6069 if (lower)
6070 return lower->private;
6071 return NULL;
6072}
6073EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
6074
6075/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006076 * netdev_master_upper_dev_get_rcu - Get master upper device
6077 * @dev: device
6078 *
6079 * Find a master upper device and return pointer to it or NULL in case
6080 * it's not there. The caller must hold the RCU read lock.
6081 */
6082struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
6083{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006084 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006085
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006086 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02006087 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006088 if (upper && likely(upper->master))
6089 return upper->dev;
6090 return NULL;
6091}
6092EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
6093
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306094static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006095 struct net_device *adj_dev,
6096 struct list_head *dev_list)
6097{
6098 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006099
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006100 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6101 "upper_%s" : "lower_%s", adj_dev->name);
6102 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
6103 linkname);
6104}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05306105static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006106 char *name,
6107 struct list_head *dev_list)
6108{
6109 char linkname[IFNAMSIZ+7];
tchardingf4563a72017-02-09 17:56:07 +11006110
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006111 sprintf(linkname, dev_list == &dev->adj_list.upper ?
6112 "upper_%s" : "lower_%s", name);
6113 sysfs_remove_link(&(dev->dev.kobj), linkname);
6114}
6115
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006116static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
6117 struct net_device *adj_dev,
6118 struct list_head *dev_list)
6119{
6120 return (dev_list == &dev->adj_list.upper ||
6121 dev_list == &dev->adj_list.lower) &&
6122 net_eq(dev_net(dev), dev_net(adj_dev));
6123}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006124
Veaceslav Falico5d261912013-08-28 23:25:05 +02006125static int __netdev_adjacent_dev_insert(struct net_device *dev,
6126 struct net_device *adj_dev,
Veaceslav Falico7863c052013-09-25 09:20:06 +02006127 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006128 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006129{
6130 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006131 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006132
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006133 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006134
6135 if (adj) {
David Ahern790510d2016-10-17 19:15:43 -07006136 adj->ref_nr += 1;
David Ahern67b62f92016-10-17 19:15:53 -07006137 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
6138 dev->name, adj_dev->name, adj->ref_nr);
6139
Veaceslav Falico5d261912013-08-28 23:25:05 +02006140 return 0;
6141 }
6142
6143 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
6144 if (!adj)
6145 return -ENOMEM;
6146
6147 adj->dev = adj_dev;
6148 adj->master = master;
David Ahern790510d2016-10-17 19:15:43 -07006149 adj->ref_nr = 1;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006150 adj->private = private;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006151 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006152
David Ahern67b62f92016-10-17 19:15:53 -07006153 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
6154 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006155
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006156 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006157 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006158 if (ret)
6159 goto free_adj;
6160 }
6161
Veaceslav Falico7863c052013-09-25 09:20:06 +02006162 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006163 if (master) {
6164 ret = sysfs_create_link(&(dev->dev.kobj),
6165 &(adj_dev->dev.kobj), "master");
6166 if (ret)
Veaceslav Falico5831d662013-09-25 09:20:32 +02006167 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006168
Veaceslav Falico7863c052013-09-25 09:20:06 +02006169 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006170 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02006171 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006172 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006173
6174 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006175
Veaceslav Falico5831d662013-09-25 09:20:32 +02006176remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006177 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006178 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006179free_adj:
6180 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02006181 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006182
6183 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006184}
6185
stephen hemminger1d143d92013-12-29 14:01:29 -08006186static void __netdev_adjacent_dev_remove(struct net_device *dev,
6187 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006188 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006189 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006190{
6191 struct netdev_adjacent *adj;
6192
David Ahern67b62f92016-10-17 19:15:53 -07006193 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
6194 dev->name, adj_dev->name, ref_nr);
6195
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006196 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006197
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006198 if (!adj) {
David Ahern67b62f92016-10-17 19:15:53 -07006199 pr_err("Adjacency does not exist for device %s from %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006200 dev->name, adj_dev->name);
David Ahern67b62f92016-10-17 19:15:53 -07006201 WARN_ON(1);
6202 return;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006203 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02006204
Andrew Collins93409032016-10-03 13:43:02 -06006205 if (adj->ref_nr > ref_nr) {
David Ahern67b62f92016-10-17 19:15:53 -07006206 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
6207 dev->name, adj_dev->name, ref_nr,
6208 adj->ref_nr - ref_nr);
Andrew Collins93409032016-10-03 13:43:02 -06006209 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006210 return;
6211 }
6212
Veaceslav Falico842d67a2013-09-25 09:20:31 +02006213 if (adj->master)
6214 sysfs_remove_link(&(dev->dev.kobj), "master");
6215
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04006216 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01006217 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d662013-09-25 09:20:32 +02006218
Veaceslav Falico5d261912013-08-28 23:25:05 +02006219 list_del_rcu(&adj->list);
David Ahern67b62f92016-10-17 19:15:53 -07006220 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006221 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006222 dev_put(adj_dev);
6223 kfree_rcu(adj, rcu);
6224}
6225
stephen hemminger1d143d92013-12-29 14:01:29 -08006226static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
6227 struct net_device *upper_dev,
6228 struct list_head *up_list,
6229 struct list_head *down_list,
6230 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006231{
6232 int ret;
6233
David Ahern790510d2016-10-17 19:15:43 -07006234 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
Andrew Collins93409032016-10-03 13:43:02 -06006235 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006236 if (ret)
6237 return ret;
6238
David Ahern790510d2016-10-17 19:15:43 -07006239 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
Andrew Collins93409032016-10-03 13:43:02 -06006240 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006241 if (ret) {
David Ahern790510d2016-10-17 19:15:43 -07006242 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006243 return ret;
6244 }
6245
6246 return 0;
6247}
6248
stephen hemminger1d143d92013-12-29 14:01:29 -08006249static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
6250 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06006251 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08006252 struct list_head *up_list,
6253 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02006254{
Andrew Collins93409032016-10-03 13:43:02 -06006255 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
6256 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006257}
6258
stephen hemminger1d143d92013-12-29 14:01:29 -08006259static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
6260 struct net_device *upper_dev,
6261 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006262{
David Ahernf1170fd2016-10-17 19:15:51 -07006263 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
6264 &dev->adj_list.upper,
6265 &upper_dev->adj_list.lower,
6266 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006267}
6268
stephen hemminger1d143d92013-12-29 14:01:29 -08006269static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
6270 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006271{
Andrew Collins93409032016-10-03 13:43:02 -06006272 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006273 &dev->adj_list.upper,
6274 &upper_dev->adj_list.lower);
6275}
Veaceslav Falico5d261912013-08-28 23:25:05 +02006276
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006277static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006278 struct net_device *upper_dev, bool master,
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006279 void *upper_priv, void *upper_info)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006280{
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006281 struct netdev_notifier_changeupper_info changeupper_info;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006282 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006283
6284 ASSERT_RTNL();
6285
6286 if (dev == upper_dev)
6287 return -EBUSY;
6288
6289 /* To prevent loops, check if dev is not upper device to upper_dev. */
David Ahernf1170fd2016-10-17 19:15:51 -07006290 if (netdev_has_upper_dev(upper_dev, dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006291 return -EBUSY;
6292
David Ahernf1170fd2016-10-17 19:15:51 -07006293 if (netdev_has_upper_dev(dev, upper_dev))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006294 return -EEXIST;
6295
6296 if (master && netdev_master_upper_dev_get(dev))
6297 return -EBUSY;
6298
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006299 changeupper_info.upper_dev = upper_dev;
6300 changeupper_info.master = master;
6301 changeupper_info.linking = true;
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006302 changeupper_info.upper_info = upper_info;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006303
Jiri Pirko573c7ba2015-10-16 14:01:22 +02006304 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, dev,
6305 &changeupper_info.info);
6306 ret = notifier_to_errno(ret);
6307 if (ret)
6308 return ret;
6309
Jiri Pirko6dffb042015-12-03 12:12:10 +01006310 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02006311 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006312 if (ret)
6313 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006314
Ido Schimmelb03804e2015-12-03 12:12:03 +01006315 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
6316 &changeupper_info.info);
6317 ret = notifier_to_errno(ret);
6318 if (ret)
David Ahernf1170fd2016-10-17 19:15:51 -07006319 goto rollback;
Ido Schimmelb03804e2015-12-03 12:12:03 +01006320
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006321 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006322
David Ahernf1170fd2016-10-17 19:15:51 -07006323rollback:
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006324 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006325
6326 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006327}
6328
6329/**
6330 * netdev_upper_dev_link - Add a link to the upper device
6331 * @dev: device
6332 * @upper_dev: new upper device
6333 *
6334 * Adds a link to device which is upper to this one. The caller must hold
6335 * the RTNL lock. On a failure a negative errno code is returned.
6336 * On success the reference counts are adjusted and the function
6337 * returns zero.
6338 */
6339int netdev_upper_dev_link(struct net_device *dev,
6340 struct net_device *upper_dev)
6341{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006342 return __netdev_upper_dev_link(dev, upper_dev, false, NULL, NULL);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006343}
6344EXPORT_SYMBOL(netdev_upper_dev_link);
6345
6346/**
6347 * netdev_master_upper_dev_link - Add a master link to the upper device
6348 * @dev: device
6349 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01006350 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006351 * @upper_info: upper info to be passed down via notifier
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006352 *
6353 * Adds a link to device which is upper to this one. In this case, only
6354 * one master upper device can be linked, although other non-master devices
6355 * might be linked as well. The caller must hold the RTNL lock.
6356 * On a failure a negative errno code is returned. On success the reference
6357 * counts are adjusted and the function returns zero.
6358 */
6359int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01006360 struct net_device *upper_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006361 void *upper_priv, void *upper_info)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006362{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006363 return __netdev_upper_dev_link(dev, upper_dev, true,
6364 upper_priv, upper_info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006365}
6366EXPORT_SYMBOL(netdev_master_upper_dev_link);
6367
6368/**
6369 * netdev_upper_dev_unlink - Removes a link to upper device
6370 * @dev: device
6371 * @upper_dev: new upper device
6372 *
6373 * Removes a link to device which is upper to this one. The caller must hold
6374 * the RTNL lock.
6375 */
6376void netdev_upper_dev_unlink(struct net_device *dev,
6377 struct net_device *upper_dev)
6378{
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006379 struct netdev_notifier_changeupper_info changeupper_info;
tchardingf4563a72017-02-09 17:56:07 +11006380
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006381 ASSERT_RTNL();
6382
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006383 changeupper_info.upper_dev = upper_dev;
6384 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
6385 changeupper_info.linking = false;
6386
Jiri Pirko573c7ba2015-10-16 14:01:22 +02006387 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, dev,
6388 &changeupper_info.info);
6389
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006390 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006391
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006392 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
6393 &changeupper_info.info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006394}
6395EXPORT_SYMBOL(netdev_upper_dev_unlink);
6396
Moni Shoua61bd3852015-02-03 16:48:29 +02006397/**
6398 * netdev_bonding_info_change - Dispatch event about slave change
6399 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09006400 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02006401 *
6402 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
6403 * The caller must hold the RTNL lock.
6404 */
6405void netdev_bonding_info_change(struct net_device *dev,
6406 struct netdev_bonding_info *bonding_info)
6407{
6408 struct netdev_notifier_bonding_info info;
6409
6410 memcpy(&info.bonding_info, bonding_info,
6411 sizeof(struct netdev_bonding_info));
6412 call_netdevice_notifiers_info(NETDEV_BONDING_INFO, dev,
6413 &info.info);
6414}
6415EXPORT_SYMBOL(netdev_bonding_info_change);
6416
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08006417static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006418{
6419 struct netdev_adjacent *iter;
6420
6421 struct net *net = dev_net(dev);
6422
6423 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006424 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006425 continue;
6426 netdev_adjacent_sysfs_add(iter->dev, dev,
6427 &iter->dev->adj_list.lower);
6428 netdev_adjacent_sysfs_add(dev, iter->dev,
6429 &dev->adj_list.upper);
6430 }
6431
6432 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006433 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006434 continue;
6435 netdev_adjacent_sysfs_add(iter->dev, dev,
6436 &iter->dev->adj_list.upper);
6437 netdev_adjacent_sysfs_add(dev, iter->dev,
6438 &dev->adj_list.lower);
6439 }
6440}
6441
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08006442static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006443{
6444 struct netdev_adjacent *iter;
6445
6446 struct net *net = dev_net(dev);
6447
6448 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006449 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006450 continue;
6451 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6452 &iter->dev->adj_list.lower);
6453 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6454 &dev->adj_list.upper);
6455 }
6456
6457 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006458 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006459 continue;
6460 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6461 &iter->dev->adj_list.upper);
6462 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6463 &dev->adj_list.lower);
6464 }
6465}
6466
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006467void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02006468{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006469 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006470
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006471 struct net *net = dev_net(dev);
6472
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006473 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006474 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006475 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006476 netdev_adjacent_sysfs_del(iter->dev, oldname,
6477 &iter->dev->adj_list.lower);
6478 netdev_adjacent_sysfs_add(iter->dev, dev,
6479 &iter->dev->adj_list.lower);
6480 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02006481
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006482 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006483 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006484 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006485 netdev_adjacent_sysfs_del(iter->dev, oldname,
6486 &iter->dev->adj_list.upper);
6487 netdev_adjacent_sysfs_add(iter->dev, dev,
6488 &iter->dev->adj_list.upper);
6489 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02006490}
Veaceslav Falico402dae92013-09-25 09:20:09 +02006491
6492void *netdev_lower_dev_get_private(struct net_device *dev,
6493 struct net_device *lower_dev)
6494{
6495 struct netdev_adjacent *lower;
6496
6497 if (!lower_dev)
6498 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006499 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02006500 if (!lower)
6501 return NULL;
6502
6503 return lower->private;
6504}
6505EXPORT_SYMBOL(netdev_lower_dev_get_private);
6506
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006507
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006508int dev_get_nest_level(struct net_device *dev)
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006509{
6510 struct net_device *lower = NULL;
6511 struct list_head *iter;
6512 int max_nest = -1;
6513 int nest;
6514
6515 ASSERT_RTNL();
6516
6517 netdev_for_each_lower_dev(dev, lower, iter) {
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006518 nest = dev_get_nest_level(lower);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006519 if (max_nest < nest)
6520 max_nest = nest;
6521 }
6522
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006523 return max_nest + 1;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006524}
6525EXPORT_SYMBOL(dev_get_nest_level);
6526
Jiri Pirko04d48262015-12-03 12:12:15 +01006527/**
6528 * netdev_lower_change - Dispatch event about lower device state change
6529 * @lower_dev: device
6530 * @lower_state_info: state to dispatch
6531 *
6532 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
6533 * The caller must hold the RTNL lock.
6534 */
6535void netdev_lower_state_changed(struct net_device *lower_dev,
6536 void *lower_state_info)
6537{
6538 struct netdev_notifier_changelowerstate_info changelowerstate_info;
6539
6540 ASSERT_RTNL();
6541 changelowerstate_info.lower_state_info = lower_state_info;
6542 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, lower_dev,
6543 &changelowerstate_info.info);
6544}
6545EXPORT_SYMBOL(netdev_lower_state_changed);
6546
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006547static void dev_change_rx_flags(struct net_device *dev, int flags)
6548{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006549 const struct net_device_ops *ops = dev->netdev_ops;
6550
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05006551 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006552 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006553}
6554
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006555static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07006556{
Eric Dumazetb536db92011-11-30 21:42:26 +00006557 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06006558 kuid_t uid;
6559 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07006560
Patrick McHardy24023452007-07-14 18:51:31 -07006561 ASSERT_RTNL();
6562
Wang Chendad9b332008-06-18 01:48:28 -07006563 dev->flags |= IFF_PROMISC;
6564 dev->promiscuity += inc;
6565 if (dev->promiscuity == 0) {
6566 /*
6567 * Avoid overflow.
6568 * If inc causes overflow, untouch promisc and return error.
6569 */
6570 if (inc < 0)
6571 dev->flags &= ~IFF_PROMISC;
6572 else {
6573 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006574 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
6575 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07006576 return -EOVERFLOW;
6577 }
6578 }
Patrick McHardy4417da62007-06-27 01:28:10 -07006579 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006580 pr_info("device %s %s promiscuous mode\n",
6581 dev->name,
6582 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11006583 if (audit_enabled) {
6584 current_uid_gid(&uid, &gid);
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05006585 audit_log(current->audit_context, GFP_ATOMIC,
6586 AUDIT_ANOM_PROMISCUOUS,
6587 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
6588 dev->name, (dev->flags & IFF_PROMISC),
6589 (old_flags & IFF_PROMISC),
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07006590 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06006591 from_kuid(&init_user_ns, uid),
6592 from_kgid(&init_user_ns, gid),
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05006593 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11006594 }
Patrick McHardy24023452007-07-14 18:51:31 -07006595
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006596 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07006597 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006598 if (notify)
6599 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07006600 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07006601}
6602
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603/**
6604 * dev_set_promiscuity - update promiscuity count on a device
6605 * @dev: device
6606 * @inc: modifier
6607 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07006608 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 * remains above zero the interface remains promiscuous. Once it hits zero
6610 * the device reverts back to normal filtering operation. A negative inc
6611 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07006612 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 */
Wang Chendad9b332008-06-18 01:48:28 -07006614int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615{
Eric Dumazetb536db92011-11-30 21:42:26 +00006616 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07006617 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006619 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07006620 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07006621 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07006622 if (dev->flags != old_flags)
6623 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07006624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006626EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006628static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006630 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
Patrick McHardy24023452007-07-14 18:51:31 -07006632 ASSERT_RTNL();
6633
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07006635 dev->allmulti += inc;
6636 if (dev->allmulti == 0) {
6637 /*
6638 * Avoid overflow.
6639 * If inc causes overflow, untouch allmulti and return error.
6640 */
6641 if (inc < 0)
6642 dev->flags &= ~IFF_ALLMULTI;
6643 else {
6644 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006645 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
6646 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07006647 return -EOVERFLOW;
6648 }
6649 }
Patrick McHardy24023452007-07-14 18:51:31 -07006650 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006651 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07006652 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006653 if (notify)
6654 __dev_notify_flags(dev, old_flags,
6655 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07006656 }
Wang Chendad9b332008-06-18 01:48:28 -07006657 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07006658}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006659
6660/**
6661 * dev_set_allmulti - update allmulti count on a device
6662 * @dev: device
6663 * @inc: modifier
6664 *
6665 * Add or remove reception of all multicast frames to a device. While the
6666 * count in the device remains above zero the interface remains listening
6667 * to all interfaces. Once it hits zero the device reverts back to normal
6668 * filtering operation. A negative @inc value is used to drop the counter
6669 * when releasing a resource needing all multicasts.
6670 * Return 0 if successful or a negative errno code on error.
6671 */
6672
6673int dev_set_allmulti(struct net_device *dev, int inc)
6674{
6675 return __dev_set_allmulti(dev, inc, true);
6676}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006677EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07006678
6679/*
6680 * Upload unicast and multicast address lists to device and
6681 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08006682 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07006683 * are present.
6684 */
6685void __dev_set_rx_mode(struct net_device *dev)
6686{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006687 const struct net_device_ops *ops = dev->netdev_ops;
6688
Patrick McHardy4417da62007-06-27 01:28:10 -07006689 /* dev_open will call this function so the list will stay sane. */
6690 if (!(dev->flags&IFF_UP))
6691 return;
6692
6693 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09006694 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07006695
Jiri Pirko01789342011-08-16 06:29:00 +00006696 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07006697 /* Unicast addresses changes may only happen under the rtnl,
6698 * therefore calling __dev_set_promiscuity here is safe.
6699 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08006700 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006701 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07006702 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08006703 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006704 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07006705 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07006706 }
Patrick McHardy4417da62007-06-27 01:28:10 -07006707 }
Jiri Pirko01789342011-08-16 06:29:00 +00006708
6709 if (ops->ndo_set_rx_mode)
6710 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07006711}
6712
6713void dev_set_rx_mode(struct net_device *dev)
6714{
David S. Millerb9e40852008-07-15 00:15:08 -07006715 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07006716 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07006717 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718}
6719
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006720/**
6721 * dev_get_flags - get flags reported to userspace
6722 * @dev: device
6723 *
6724 * Get the combination of flag bits exported through APIs to userspace.
6725 */
Eric Dumazet95c96172012-04-15 05:58:06 +00006726unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727{
Eric Dumazet95c96172012-04-15 05:58:06 +00006728 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
6730 flags = (dev->flags & ~(IFF_PROMISC |
6731 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08006732 IFF_RUNNING |
6733 IFF_LOWER_UP |
6734 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 (dev->gflags & (IFF_PROMISC |
6736 IFF_ALLMULTI));
6737
Stefan Rompfb00055a2006-03-20 17:09:11 -08006738 if (netif_running(dev)) {
6739 if (netif_oper_up(dev))
6740 flags |= IFF_RUNNING;
6741 if (netif_carrier_ok(dev))
6742 flags |= IFF_LOWER_UP;
6743 if (netif_dormant(dev))
6744 flags |= IFF_DORMANT;
6745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746
6747 return flags;
6748}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006749EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
Patrick McHardybd380812010-02-26 06:34:53 +00006751int __dev_change_flags(struct net_device *dev, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752{
Eric Dumazetb536db92011-11-30 21:42:26 +00006753 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00006754 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
Patrick McHardy24023452007-07-14 18:51:31 -07006756 ASSERT_RTNL();
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 /*
6759 * Set the flags on our device.
6760 */
6761
6762 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
6763 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
6764 IFF_AUTOMEDIA)) |
6765 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
6766 IFF_ALLMULTI));
6767
6768 /*
6769 * Load in the correct multicast list now the flags have changed.
6770 */
6771
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006772 if ((old_flags ^ flags) & IFF_MULTICAST)
6773 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07006774
Patrick McHardy4417da62007-06-27 01:28:10 -07006775 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
6777 /*
6778 * Have we downed the interface. We handle IFF_UP ourselves
6779 * according to user attempts to set it, rather than blindly
6780 * setting it.
6781 */
6782
6783 ret = 0;
stephen hemminger7051b882017-07-18 15:59:27 -07006784 if ((old_flags ^ flags) & IFF_UP) {
6785 if (old_flags & IFF_UP)
6786 __dev_close(dev);
6787 else
6788 ret = __dev_open(dev);
6789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006792 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006793 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006794
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006796
6797 if (__dev_set_promiscuity(dev, inc, false) >= 0)
6798 if (dev->flags != old_flags)
6799 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 }
6801
6802 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
tchardingeb13da12017-02-09 17:56:06 +11006803 * is important. Some (broken) drivers set IFF_PROMISC, when
6804 * IFF_ALLMULTI is requested not asking us and not reporting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 */
6806 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006807 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
6808
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006810 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 }
6812
Patrick McHardybd380812010-02-26 06:34:53 +00006813 return ret;
6814}
6815
Nicolas Dichtela528c212013-09-25 12:02:44 +02006816void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
6817 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00006818{
6819 unsigned int changes = dev->flags ^ old_flags;
6820
Nicolas Dichtela528c212013-09-25 12:02:44 +02006821 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07006822 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02006823
Patrick McHardybd380812010-02-26 06:34:53 +00006824 if (changes & IFF_UP) {
6825 if (dev->flags & IFF_UP)
6826 call_netdevice_notifiers(NETDEV_UP, dev);
6827 else
6828 call_netdevice_notifiers(NETDEV_DOWN, dev);
6829 }
6830
6831 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00006832 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
6833 struct netdev_notifier_change_info change_info;
6834
6835 change_info.flags_changed = changes;
6836 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
6837 &change_info.info);
6838 }
Patrick McHardybd380812010-02-26 06:34:53 +00006839}
6840
6841/**
6842 * dev_change_flags - change device settings
6843 * @dev: device
6844 * @flags: device state flags
6845 *
6846 * Change settings on device based state flags. The flags are
6847 * in the userspace exported format.
6848 */
Eric Dumazetb536db92011-11-30 21:42:26 +00006849int dev_change_flags(struct net_device *dev, unsigned int flags)
Patrick McHardybd380812010-02-26 06:34:53 +00006850{
Eric Dumazetb536db92011-11-30 21:42:26 +00006851 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006852 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00006853
6854 ret = __dev_change_flags(dev, flags);
6855 if (ret < 0)
6856 return ret;
6857
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006858 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02006859 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 return ret;
6861}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006862EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
WANG Congf51048c2017-07-06 15:01:57 -07006864int __dev_set_mtu(struct net_device *dev, int new_mtu)
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006865{
6866 const struct net_device_ops *ops = dev->netdev_ops;
6867
6868 if (ops->ndo_change_mtu)
6869 return ops->ndo_change_mtu(dev, new_mtu);
6870
6871 dev->mtu = new_mtu;
6872 return 0;
6873}
WANG Congf51048c2017-07-06 15:01:57 -07006874EXPORT_SYMBOL(__dev_set_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006875
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006876/**
6877 * dev_set_mtu - Change maximum transfer unit
6878 * @dev: device
6879 * @new_mtu: new transfer unit
6880 *
6881 * Change the maximum transfer size of the network device.
6882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883int dev_set_mtu(struct net_device *dev, int new_mtu)
6884{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006885 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
6887 if (new_mtu == dev->mtu)
6888 return 0;
6889
Jarod Wilson61e84622016-10-07 22:04:33 -04006890 /* MTU must be positive, and in range */
6891 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
6892 net_err_ratelimited("%s: Invalid MTU %d requested, hw min %d\n",
6893 dev->name, new_mtu, dev->min_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 return -EINVAL;
Jarod Wilson61e84622016-10-07 22:04:33 -04006895 }
6896
6897 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
6898 net_err_ratelimited("%s: Invalid MTU %d requested, hw max %d\n",
Jakub Kicinskia0e65de2016-10-17 18:02:22 +01006899 dev->name, new_mtu, dev->max_mtu);
Jarod Wilson61e84622016-10-07 22:04:33 -04006900 return -EINVAL;
6901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903 if (!netif_device_present(dev))
6904 return -ENODEV;
6905
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01006906 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
6907 err = notifier_to_errno(err);
6908 if (err)
6909 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006910
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006911 orig_mtu = dev->mtu;
6912 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006914 if (!err) {
6915 err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
6916 err = notifier_to_errno(err);
6917 if (err) {
6918 /* setting mtu back and notifying everyone again,
6919 * so that they have a chance to revert changes.
6920 */
6921 __dev_set_mtu(dev, orig_mtu);
6922 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
6923 }
6924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 return err;
6926}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006927EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006929/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00006930 * dev_set_group - Change group this device belongs to
6931 * @dev: device
6932 * @new_group: group this device should belong to
6933 */
6934void dev_set_group(struct net_device *dev, int new_group)
6935{
6936 dev->group = new_group;
6937}
6938EXPORT_SYMBOL(dev_set_group);
6939
6940/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006941 * dev_set_mac_address - Change Media Access Control Address
6942 * @dev: device
6943 * @sa: new address
6944 *
6945 * Change the hardware (MAC) address of the device
6946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
6948{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006949 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 int err;
6951
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006952 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 return -EOPNOTSUPP;
6954 if (sa->sa_family != dev->type)
6955 return -EINVAL;
6956 if (!netif_device_present(dev))
6957 return -ENODEV;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006958 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00006959 if (err)
6960 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00006961 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00006962 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04006963 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00006964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006966EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
Jiri Pirko4bf84c32012-12-27 23:49:37 +00006968/**
6969 * dev_change_carrier - Change device carrier
6970 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00006971 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00006972 *
6973 * Change device carrier
6974 */
6975int dev_change_carrier(struct net_device *dev, bool new_carrier)
6976{
6977 const struct net_device_ops *ops = dev->netdev_ops;
6978
6979 if (!ops->ndo_change_carrier)
6980 return -EOPNOTSUPP;
6981 if (!netif_device_present(dev))
6982 return -ENODEV;
6983 return ops->ndo_change_carrier(dev, new_carrier);
6984}
6985EXPORT_SYMBOL(dev_change_carrier);
6986
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02006988 * dev_get_phys_port_id - Get device physical port ID
6989 * @dev: device
6990 * @ppid: port ID
6991 *
6992 * Get device physical port ID
6993 */
6994int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01006995 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02006996{
6997 const struct net_device_ops *ops = dev->netdev_ops;
6998
6999 if (!ops->ndo_get_phys_port_id)
7000 return -EOPNOTSUPP;
7001 return ops->ndo_get_phys_port_id(dev, ppid);
7002}
7003EXPORT_SYMBOL(dev_get_phys_port_id);
7004
7005/**
David Aherndb24a902015-03-17 20:23:15 -06007006 * dev_get_phys_port_name - Get device physical port name
7007 * @dev: device
7008 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00007009 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06007010 *
7011 * Get device physical port name
7012 */
7013int dev_get_phys_port_name(struct net_device *dev,
7014 char *name, size_t len)
7015{
7016 const struct net_device_ops *ops = dev->netdev_ops;
7017
7018 if (!ops->ndo_get_phys_port_name)
7019 return -EOPNOTSUPP;
7020 return ops->ndo_get_phys_port_name(dev, name, len);
7021}
7022EXPORT_SYMBOL(dev_get_phys_port_name);
7023
7024/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007025 * dev_change_proto_down - update protocol port state information
7026 * @dev: device
7027 * @proto_down: new value
7028 *
7029 * This info can be used by switch drivers to set the phys state of the
7030 * port.
7031 */
7032int dev_change_proto_down(struct net_device *dev, bool proto_down)
7033{
7034 const struct net_device_ops *ops = dev->netdev_ops;
7035
7036 if (!ops->ndo_change_proto_down)
7037 return -EOPNOTSUPP;
7038 if (!netif_device_present(dev))
7039 return -ENODEV;
7040 return ops->ndo_change_proto_down(dev, proto_down);
7041}
7042EXPORT_SYMBOL(dev_change_proto_down);
7043
Jakub Kicinskice158e52017-06-21 18:25:09 -07007044u8 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007045{
7046 struct netdev_xdp xdp;
7047
7048 memset(&xdp, 0, sizeof(xdp));
7049 xdp.command = XDP_QUERY_PROG;
7050
7051 /* Query must always succeed. */
7052 WARN_ON(xdp_op(dev, &xdp) < 0);
Martin KaFai Lau58038692017-06-15 17:29:09 -07007053 if (prog_id)
7054 *prog_id = xdp.prog_id;
7055
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007056 return xdp.prog_attached;
7057}
7058
7059static int dev_xdp_install(struct net_device *dev, xdp_op_t xdp_op,
Jakub Kicinski32d60272017-06-21 18:25:03 -07007060 struct netlink_ext_ack *extack, u32 flags,
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007061 struct bpf_prog *prog)
7062{
7063 struct netdev_xdp xdp;
7064
7065 memset(&xdp, 0, sizeof(xdp));
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07007066 if (flags & XDP_FLAGS_HW_MODE)
7067 xdp.command = XDP_SETUP_PROG_HW;
7068 else
7069 xdp.command = XDP_SETUP_PROG;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007070 xdp.extack = extack;
Jakub Kicinski32d60272017-06-21 18:25:03 -07007071 xdp.flags = flags;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007072 xdp.prog = prog;
7073
7074 return xdp_op(dev, &xdp);
7075}
7076
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07007077/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07007078 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
7079 * @dev: device
Jakub Kicinskib5d60982017-05-01 15:53:43 -07007080 * @extack: netlink extended ack
Brenden Blancoa7862b42016-07-19 12:16:48 -07007081 * @fd: new program fd or negative value to clear
Daniel Borkmann85de8572016-11-28 23:16:54 +01007082 * @flags: xdp-related flags
Brenden Blancoa7862b42016-07-19 12:16:48 -07007083 *
7084 * Set or clear a bpf program for a device
7085 */
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07007086int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
7087 int fd, u32 flags)
Brenden Blancoa7862b42016-07-19 12:16:48 -07007088{
7089 const struct net_device_ops *ops = dev->netdev_ops;
7090 struct bpf_prog *prog = NULL;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007091 xdp_op_t xdp_op, xdp_chk;
Brenden Blancoa7862b42016-07-19 12:16:48 -07007092 int err;
7093
Daniel Borkmann85de8572016-11-28 23:16:54 +01007094 ASSERT_RTNL();
7095
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007096 xdp_op = xdp_chk = ops->ndo_xdp;
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07007097 if (!xdp_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE)))
Daniel Borkmann0489df92017-05-12 01:04:45 +02007098 return -EOPNOTSUPP;
David S. Millerb5cdae32017-04-18 15:36:58 -04007099 if (!xdp_op || (flags & XDP_FLAGS_SKB_MODE))
7100 xdp_op = generic_xdp_install;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007101 if (xdp_op == xdp_chk)
7102 xdp_chk = generic_xdp_install;
David S. Millerb5cdae32017-04-18 15:36:58 -04007103
Brenden Blancoa7862b42016-07-19 12:16:48 -07007104 if (fd >= 0) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07007105 if (xdp_chk && __dev_xdp_attached(dev, xdp_chk, NULL))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007106 return -EEXIST;
7107 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
Martin KaFai Lau58038692017-06-15 17:29:09 -07007108 __dev_xdp_attached(dev, xdp_op, NULL))
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02007109 return -EBUSY;
Daniel Borkmann85de8572016-11-28 23:16:54 +01007110
Brenden Blancoa7862b42016-07-19 12:16:48 -07007111 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
7112 if (IS_ERR(prog))
7113 return PTR_ERR(prog);
7114 }
7115
Jakub Kicinski32d60272017-06-21 18:25:03 -07007116 err = dev_xdp_install(dev, xdp_op, extack, flags, prog);
Brenden Blancoa7862b42016-07-19 12:16:48 -07007117 if (err < 0 && prog)
7118 bpf_prog_put(prog);
7119
7120 return err;
7121}
Brenden Blancoa7862b42016-07-19 12:16:48 -07007122
7123/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07007125 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 *
7127 * Returns a suitable unique value for a new device interface
7128 * number. The caller must hold the rtnl semaphore or the
7129 * dev_base_lock to be sure it remains unique.
7130 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07007131static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00007133 int ifindex = net->ifindex;
tchardingf4563a72017-02-09 17:56:07 +11007134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 for (;;) {
7136 if (++ifindex <= 0)
7137 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07007138 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00007139 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 }
7141}
7142
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08007144static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07007145DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
Stephen Hemminger6f05f622007-03-08 20:46:03 -08007147static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07007150 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151}
7152
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007153static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007154{
Krishna Kumare93737b2009-12-08 22:26:02 +00007155 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007156 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007157
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007158 BUG_ON(dev_boot_phase);
7159 ASSERT_RTNL();
7160
Krishna Kumare93737b2009-12-08 22:26:02 +00007161 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007162 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00007163 * for initialization unwind. Remove those
7164 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007165 */
7166 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007167 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
7168 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007169
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007170 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00007171 list_del(&dev->unreg_list);
7172 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007173 }
Eric Dumazet449f4542011-05-19 12:24:16 +00007174 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007175 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00007176 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007177
Octavian Purdila44345722010-12-13 12:44:07 +00007178 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007179 list_for_each_entry(dev, head, unreg_list)
7180 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04007181 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007182
Octavian Purdila44345722010-12-13 12:44:07 +00007183 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007184 /* And unlink it from device chain. */
7185 unlist_netdevice(dev);
7186
7187 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007188 }
Eric Dumazet41852492016-08-26 12:50:39 -07007189 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007190
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007191 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007192
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007193 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007194 struct sk_buff *skb = NULL;
7195
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007196 /* Shutdown queueing discipline. */
7197 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007198
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007199
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007200 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11007201 * this device. They should clean all the things.
7202 */
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007203 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
7204
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007205 if (!dev->rtnl_link_ops ||
7206 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04007207 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02007208 GFP_KERNEL, NULL);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007209
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007210 /*
7211 * Flush the unicast and multicast chains
7212 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00007213 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00007214 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007215
7216 if (dev->netdev_ops->ndo_uninit)
7217 dev->netdev_ops->ndo_uninit(dev);
7218
Mahesh Bandewar395eea62014-12-03 13:46:24 -08007219 if (skb)
7220 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07007221
Jiri Pirko9ff162a2013-01-03 22:48:49 +00007222 /* Notifier chain MUST detach us all upper devices. */
7223 WARN_ON(netdev_has_any_upper_dev(dev));
David Ahern0f524a82016-10-17 19:15:52 -07007224 WARN_ON(netdev_has_any_lower_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007225
7226 /* Remove entries from kobject tree */
7227 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00007228#ifdef CONFIG_XPS
7229 /* Remove XPS queueing entries */
7230 netif_reset_xps_queues_gt(dev, 0);
7231#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007232 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007233
Eric W. Biederman850a5452011-10-13 22:25:23 +00007234 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007235
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00007236 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007237 dev_put(dev);
7238}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007239
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007240static void rollback_registered(struct net_device *dev)
7241{
7242 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007243
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007244 list_add(&dev->unreg_list, &single);
7245 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00007246 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007247}
7248
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007249static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
7250 struct net_device *upper, netdev_features_t features)
7251{
7252 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
7253 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007254 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007255
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007256 for_each_netdev_feature(&upper_disables, feature_bit) {
7257 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007258 if (!(upper->wanted_features & feature)
7259 && (features & feature)) {
7260 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
7261 &feature, upper->name);
7262 features &= ~feature;
7263 }
7264 }
7265
7266 return features;
7267}
7268
7269static void netdev_sync_lower_features(struct net_device *upper,
7270 struct net_device *lower, netdev_features_t features)
7271{
7272 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
7273 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007274 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007275
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05007276 for_each_netdev_feature(&upper_disables, feature_bit) {
7277 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007278 if (!(features & feature) && (lower->features & feature)) {
7279 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
7280 &feature, lower->name);
7281 lower->wanted_features &= ~feature;
7282 netdev_update_features(lower);
7283
7284 if (unlikely(lower->features & feature))
7285 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
7286 &feature, lower->name);
7287 }
7288 }
7289}
7290
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007291static netdev_features_t netdev_fix_features(struct net_device *dev,
7292 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07007293{
Michał Mirosław57422dc2011-01-22 12:14:12 +00007294 /* Fix illegal checksum combinations */
7295 if ((features & NETIF_F_HW_CSUM) &&
7296 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007297 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00007298 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
7299 }
7300
Herbert Xub63365a2008-10-23 01:11:29 -07007301 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00007302 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007303 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00007304 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07007305 }
7306
Pravin B Shelarec5f0612013-03-07 09:28:01 +00007307 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
7308 !(features & NETIF_F_IP_CSUM)) {
7309 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
7310 features &= ~NETIF_F_TSO;
7311 features &= ~NETIF_F_TSO_ECN;
7312 }
7313
7314 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
7315 !(features & NETIF_F_IPV6_CSUM)) {
7316 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
7317 features &= ~NETIF_F_TSO6;
7318 }
7319
Alexander Duyckb1dc4972016-05-02 09:38:24 -07007320 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
7321 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
7322 features &= ~NETIF_F_TSO_MANGLEID;
7323
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00007324 /* TSO ECN requires that TSO is present as well. */
7325 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
7326 features &= ~NETIF_F_TSO_ECN;
7327
Michał Mirosław212b5732011-02-15 16:59:16 +00007328 /* Software GSO depends on SG. */
7329 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007330 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00007331 features &= ~NETIF_F_GSO;
7332 }
7333
Alexander Duyck802ab552016-04-10 21:45:03 -04007334 /* GSO partial features require GSO partial be set */
7335 if ((features & dev->gso_partial_features) &&
7336 !(features & NETIF_F_GSO_PARTIAL)) {
7337 netdev_dbg(dev,
7338 "Dropping partially supported GSO features since no GSO partial.\n");
7339 features &= ~dev->gso_partial_features;
7340 }
7341
Herbert Xub63365a2008-10-23 01:11:29 -07007342 return features;
7343}
Herbert Xub63365a2008-10-23 01:11:29 -07007344
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007345int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00007346{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007347 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007348 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007349 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05007350 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00007351
Michał Mirosław87267482011-04-12 09:56:38 +00007352 ASSERT_RTNL();
7353
Michał Mirosław5455c692011-02-15 16:59:17 +00007354 features = netdev_get_wanted_features(dev);
7355
7356 if (dev->netdev_ops->ndo_fix_features)
7357 features = dev->netdev_ops->ndo_fix_features(dev, features);
7358
7359 /* driver might be less strict about feature dependencies */
7360 features = netdev_fix_features(dev, features);
7361
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007362 /* some features can't be enabled if they're off an an upper device */
7363 netdev_for_each_upper_dev_rcu(dev, upper, iter)
7364 features = netdev_sync_upper_features(dev, upper, features);
7365
Michał Mirosław5455c692011-02-15 16:59:17 +00007366 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05007367 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00007368
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007369 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
7370 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00007371
7372 if (dev->netdev_ops->ndo_set_features)
7373 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01007374 else
7375 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00007376
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007377 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00007378 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007379 "set_features() failed (%d); wanted %pNF, left %pNF\n",
7380 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01007381 /* return non-0 since some features might have changed and
7382 * it's better to fire a spurious notification than miss it
7383 */
7384 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007385 }
7386
Jarod Wilsone7868a82015-11-03 23:09:32 -05007387sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007388 /* some features must be disabled on lower devices when disabled
7389 * on an upper device (think: bonding master or bridge)
7390 */
7391 netdev_for_each_lower_dev(dev, lower, iter)
7392 netdev_sync_lower_features(dev, lower, features);
7393
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02007394 if (!err) {
7395 netdev_features_t diff = features ^ dev->features;
7396
7397 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
7398 /* udp_tunnel_{get,drop}_rx_info both need
7399 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
7400 * device, or they won't do anything.
7401 * Thus we need to update dev->features
7402 * *before* calling udp_tunnel_get_rx_info,
7403 * but *after* calling udp_tunnel_drop_rx_info.
7404 */
7405 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
7406 dev->features = features;
7407 udp_tunnel_get_rx_info(dev);
7408 } else {
7409 udp_tunnel_drop_rx_info(dev);
7410 }
7411 }
7412
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007413 dev->features = features;
Sabrina Dubrocaae847f42017-07-21 12:49:31 +02007414 }
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007415
Jarod Wilsone7868a82015-11-03 23:09:32 -05007416 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007417}
7418
Michał Mirosławafe12cc2011-05-07 03:22:17 +00007419/**
7420 * netdev_update_features - recalculate device features
7421 * @dev: the device to check
7422 *
7423 * Recalculate dev->features set and send notifications if it
7424 * has changed. Should be called after driver or hardware dependent
7425 * conditions might have changed that influence the features.
7426 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007427void netdev_update_features(struct net_device *dev)
7428{
7429 if (__netdev_update_features(dev))
7430 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00007431}
7432EXPORT_SYMBOL(netdev_update_features);
7433
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00007435 * netdev_change_features - recalculate device features
7436 * @dev: the device to check
7437 *
7438 * Recalculate dev->features set and send notifications even
7439 * if they have not changed. Should be called instead of
7440 * netdev_update_features() if also dev->vlan_features might
7441 * have changed to allow the changes to be propagated to stacked
7442 * VLAN devices.
7443 */
7444void netdev_change_features(struct net_device *dev)
7445{
7446 __netdev_update_features(dev);
7447 netdev_features_change(dev);
7448}
7449EXPORT_SYMBOL(netdev_change_features);
7450
7451/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007452 * netif_stacked_transfer_operstate - transfer operstate
7453 * @rootdev: the root or lower level device to transfer state from
7454 * @dev: the device to transfer operstate to
7455 *
7456 * Transfer operational state from root to device. This is normally
7457 * called when a stacking relationship exists between the root
7458 * device and the device(a leaf device).
7459 */
7460void netif_stacked_transfer_operstate(const struct net_device *rootdev,
7461 struct net_device *dev)
7462{
7463 if (rootdev->operstate == IF_OPER_DORMANT)
7464 netif_dormant_on(dev);
7465 else
7466 netif_dormant_off(dev);
7467
Zhang Shengju0575c862017-04-26 17:49:38 +08007468 if (netif_carrier_ok(rootdev))
7469 netif_carrier_on(dev);
7470 else
7471 netif_carrier_off(dev);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007472}
7473EXPORT_SYMBOL(netif_stacked_transfer_operstate);
7474
Michael Daltona953be52014-01-16 22:23:28 -08007475#ifdef CONFIG_SYSFS
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007476static int netif_alloc_rx_queues(struct net_device *dev)
7477{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007478 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00007479 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05307480 size_t sz = count * sizeof(*rx);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007481
Tom Herbertbd25fa72010-10-18 18:00:16 +00007482 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007483
Michal Hockodcda9b02017-07-12 14:36:45 -07007484 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07007485 if (!rx)
7486 return -ENOMEM;
7487
Tom Herbertbd25fa72010-10-18 18:00:16 +00007488 dev->_rx = rx;
7489
Tom Herbertbd25fa72010-10-18 18:00:16 +00007490 for (i = 0; i < count; i++)
Tom Herbertfe822242010-11-09 10:47:38 +00007491 rx[i].dev = dev;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007492 return 0;
7493}
Tom Herbertbf264142010-11-26 08:36:09 +00007494#endif
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007495
Changli Gaoaa942102010-12-04 02:31:41 +00007496static void netdev_init_one_queue(struct net_device *dev,
7497 struct netdev_queue *queue, void *_unused)
7498{
7499 /* Initialize queue lock */
7500 spin_lock_init(&queue->_xmit_lock);
7501 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
7502 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00007503 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00007504 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00007505#ifdef CONFIG_BQL
7506 dql_init(&queue->dql, HZ);
7507#endif
Changli Gaoaa942102010-12-04 02:31:41 +00007508}
7509
Eric Dumazet60877a32013-06-20 01:15:51 -07007510static void netif_free_tx_queues(struct net_device *dev)
7511{
WANG Cong4cb28972014-06-02 15:55:22 -07007512 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07007513}
7514
Tom Herberte6484932010-10-18 18:04:39 +00007515static int netif_alloc_netdev_queues(struct net_device *dev)
7516{
7517 unsigned int count = dev->num_tx_queues;
7518 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07007519 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00007520
Eric Dumazetd3397272015-07-06 17:13:26 +02007521 if (count < 1 || count > 0xffff)
7522 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00007523
Michal Hockodcda9b02017-07-12 14:36:45 -07007524 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hockoda6bc572017-05-08 15:57:31 -07007525 if (!tx)
7526 return -ENOMEM;
7527
Tom Herberte6484932010-10-18 18:04:39 +00007528 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00007529
Tom Herberte6484932010-10-18 18:04:39 +00007530 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
7531 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00007532
7533 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00007534}
7535
Denys Vlasenkoa2029242015-05-11 21:17:53 +02007536void netif_tx_stop_all_queues(struct net_device *dev)
7537{
7538 unsigned int i;
7539
7540 for (i = 0; i < dev->num_tx_queues; i++) {
7541 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
tchardingf4563a72017-02-09 17:56:07 +11007542
Denys Vlasenkoa2029242015-05-11 21:17:53 +02007543 netif_tx_stop_queue(txq);
7544 }
7545}
7546EXPORT_SYMBOL(netif_tx_stop_all_queues);
7547
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007548/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 * register_netdevice - register a network device
7550 * @dev: device to register
7551 *
7552 * Take a completed network device structure and add it to the kernel
7553 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7554 * chain. 0 is returned on success. A negative errno code is returned
7555 * on a failure to set up the device, or if the name is a duplicate.
7556 *
7557 * Callers must hold the rtnl semaphore. You may want
7558 * register_netdev() instead of this.
7559 *
7560 * BUGS:
7561 * The locking appears insufficient to guarantee two parallel registers
7562 * will not get the same name.
7563 */
7564
7565int register_netdevice(struct net_device *dev)
7566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007568 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
7570 BUG_ON(dev_boot_phase);
7571 ASSERT_RTNL();
7572
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007573 might_sleep();
7574
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 /* When net_device's are persistent, this will be fatal. */
7576 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007577 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
David S. Millerf1f28aa2008-07-15 00:08:33 -07007579 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07007580 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
Gao feng828de4f2012-09-13 20:58:27 +00007582 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00007583 if (ret < 0)
7584 goto out;
7585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007587 if (dev->netdev_ops->ndo_init) {
7588 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 if (ret) {
7590 if (ret > 0)
7591 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08007592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 }
7594 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007595
Patrick McHardyf6469682013-04-19 02:04:27 +00007596 if (((dev->hw_features | dev->features) &
7597 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00007598 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
7599 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
7600 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
7601 ret = -EINVAL;
7602 goto err_uninit;
7603 }
7604
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00007605 ret = -EBUSY;
7606 if (!dev->ifindex)
7607 dev->ifindex = dev_new_index(net);
7608 else if (__dev_get_by_index(net, dev->ifindex))
7609 goto err_uninit;
7610
Michał Mirosław5455c692011-02-15 16:59:17 +00007611 /* Transfer changeable features to wanted_features and enable
7612 * software offloads (GSO and GRO).
7613 */
7614 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00007615 dev->features |= NETIF_F_SOFT_FEATURES;
Sabrina Dubrocad764a122017-07-21 12:49:28 +02007616
7617 if (dev->netdev_ops->ndo_udp_tunnel_add) {
7618 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
7619 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
7620 }
7621
Michał Mirosław14d12322011-02-22 16:52:28 +00007622 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007624 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00007625 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007626
Alexander Duyck7f348a62016-04-20 16:51:00 -04007627 /* If IPv4 TCP segmentation offload is supported we should also
7628 * allow the device to enable segmenting the frame with the option
7629 * of ignoring a static IP ID value. This doesn't enable the
7630 * feature itself but allows the user to enable it later.
7631 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007632 if (dev->hw_features & NETIF_F_TSO)
7633 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04007634 if (dev->vlan_features & NETIF_F_TSO)
7635 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
7636 if (dev->mpls_features & NETIF_F_TSO)
7637 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
7638 if (dev->hw_enc_features & NETIF_F_TSO)
7639 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07007640
Michał Mirosław1180e7d2011-07-14 14:41:11 -07007641 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00007642 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07007643 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00007644
Pravin B Shelaree579672013-03-07 09:28:08 +00007645 /* Make NETIF_F_SG inheritable to tunnel devices.
7646 */
Alexander Duyck802ab552016-04-10 21:45:03 -04007647 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00007648
Simon Horman0d89d202013-05-23 21:02:52 +00007649 /* Make NETIF_F_SG inheritable to MPLS.
7650 */
7651 dev->mpls_features |= NETIF_F_SG;
7652
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00007653 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
7654 ret = notifier_to_errno(ret);
7655 if (ret)
7656 goto err_uninit;
7657
Eric W. Biederman8b41d182007-09-26 22:02:53 -07007658 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007659 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007660 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007661 dev->reg_state = NETREG_REGISTERED;
7662
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007663 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00007664
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 /*
7666 * Default initial state at registry is that the
7667 * device is present.
7668 */
7669
7670 set_bit(__LINK_STATE_PRESENT, &dev->state);
7671
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01007672 linkwatch_init_dev(dev);
7673
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02007676 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04007677 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678
Jiri Pirko948b3372013-01-08 01:38:25 +00007679 /* If the device has permanent device address, driver should
7680 * set dev_addr and also addr_assign_type should be set to
7681 * NET_ADDR_PERM (default value).
7682 */
7683 if (dev->addr_assign_type == NET_ADDR_PERM)
7684 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
7685
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07007687 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07007688 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007689 if (ret) {
7690 rollback_registered(dev);
7691 dev->reg_state = NETREG_UNREGISTERED;
7692 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00007693 /*
7694 * Prevent userspace races by waiting until the network
7695 * device is fully setup before sending notifications.
7696 */
Patrick McHardya2835762010-02-26 06:34:51 +00007697 if (!dev->rtnl_link_ops ||
7698 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07007699 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700
7701out:
7702 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007703
7704err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007705 if (dev->netdev_ops->ndo_uninit)
7706 dev->netdev_ops->ndo_uninit(dev);
David S. Millercf124db2017-05-08 12:52:56 -04007707 if (dev->priv_destructor)
7708 dev->priv_destructor(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007709 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007711EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
7713/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007714 * init_dummy_netdev - init a dummy network device for NAPI
7715 * @dev: device to init
7716 *
7717 * This takes a network device structure and initialize the minimum
7718 * amount of fields so it can be used to schedule NAPI polls without
7719 * registering a full blown interface. This is to be used by drivers
7720 * that need to tie several hardware interfaces to a single NAPI
7721 * poll scheduler due to HW limitations.
7722 */
7723int init_dummy_netdev(struct net_device *dev)
7724{
7725 /* Clear everything. Note we don't initialize spinlocks
7726 * are they aren't supposed to be taken by any of the
7727 * NAPI code and this dummy netdev is supposed to be
7728 * only ever used for NAPI polls
7729 */
7730 memset(dev, 0, sizeof(struct net_device));
7731
7732 /* make sure we BUG if trying to hit standard
7733 * register/unregister code path
7734 */
7735 dev->reg_state = NETREG_DUMMY;
7736
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007737 /* NAPI wants this */
7738 INIT_LIST_HEAD(&dev->napi_list);
7739
7740 /* a dummy interface is started by default */
7741 set_bit(__LINK_STATE_PRESENT, &dev->state);
7742 set_bit(__LINK_STATE_START, &dev->state);
7743
Eric Dumazet29b44332010-10-11 10:22:12 +00007744 /* Note : We dont allocate pcpu_refcnt for dummy devices,
7745 * because users of this 'device' dont need to change
7746 * its refcount.
7747 */
7748
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007749 return 0;
7750}
7751EXPORT_SYMBOL_GPL(init_dummy_netdev);
7752
7753
7754/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 * register_netdev - register a network device
7756 * @dev: device to register
7757 *
7758 * Take a completed network device structure and add it to the kernel
7759 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7760 * chain. 0 is returned on success. A negative errno code is returned
7761 * on a failure to set up the device, or if the name is a duplicate.
7762 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07007763 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 * and expands the device name if you passed a format string to
7765 * alloc_netdev.
7766 */
7767int register_netdev(struct net_device *dev)
7768{
7769 int err;
7770
7771 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 rtnl_unlock();
7774 return err;
7775}
7776EXPORT_SYMBOL(register_netdev);
7777
Eric Dumazet29b44332010-10-11 10:22:12 +00007778int netdev_refcnt_read(const struct net_device *dev)
7779{
7780 int i, refcnt = 0;
7781
7782 for_each_possible_cpu(i)
7783 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
7784 return refcnt;
7785}
7786EXPORT_SYMBOL(netdev_refcnt_read);
7787
Ben Hutchings2c530402012-07-10 10:55:09 +00007788/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00007790 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 *
7792 * This is called when unregistering network devices.
7793 *
7794 * Any protocol or device that holds a reference should register
7795 * for netdevice notification, and cleanup and put back the
7796 * reference if they receive an UNREGISTER event.
7797 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007798 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 */
7800static void netdev_wait_allrefs(struct net_device *dev)
7801{
7802 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00007803 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
Eric Dumazete014deb2009-11-17 05:59:21 +00007805 linkwatch_forget_dev(dev);
7806
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00007808 refcnt = netdev_refcnt_read(dev);
7809
7810 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08007812 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813
7814 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07007815 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816
Eric Dumazet748e2d92012-08-22 21:50:59 +00007817 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007818 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00007819 rtnl_lock();
7820
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007821 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
7823 &dev->state)) {
7824 /* We must not have linkwatch events
7825 * pending on unregister. If this
7826 * happens, we simply run the queue
7827 * unscheduled, resulting in a noop
7828 * for this device.
7829 */
7830 linkwatch_run_queue();
7831 }
7832
Stephen Hemminger6756ae42006-03-20 22:23:58 -08007833 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834
7835 rebroadcast_time = jiffies;
7836 }
7837
7838 msleep(250);
7839
Eric Dumazet29b44332010-10-11 10:22:12 +00007840 refcnt = netdev_refcnt_read(dev);
7841
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 if (time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007843 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
7844 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 warning_time = jiffies;
7846 }
7847 }
7848}
7849
7850/* The sequence is:
7851 *
7852 * rtnl_lock();
7853 * ...
7854 * register_netdevice(x1);
7855 * register_netdevice(x2);
7856 * ...
7857 * unregister_netdevice(y1);
7858 * unregister_netdevice(y2);
7859 * ...
7860 * rtnl_unlock();
7861 * free_netdev(y1);
7862 * free_netdev(y2);
7863 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07007864 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007866 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 * without deadlocking with linkwatch via keventd.
7868 * 2) Since we run with the RTNL semaphore not held, we can sleep
7869 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07007870 *
7871 * We must not return until all unregister events added during
7872 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874void netdev_run_todo(void)
7875{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07007876 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07007879 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07007880
7881 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07007882
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007883
7884 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00007885 if (!list_empty(&list))
7886 rcu_barrier();
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 while (!list_empty(&list)) {
7889 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00007890 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 list_del(&dev->todo_list);
7892
Eric Dumazet748e2d92012-08-22 21:50:59 +00007893 rtnl_lock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007894 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Eric Dumazet748e2d92012-08-22 21:50:59 +00007895 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007896
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007897 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007898 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007900 dump_stack();
7901 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007903
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007904 dev->reg_state = NETREG_UNREGISTERED;
7905
7906 netdev_wait_allrefs(dev);
7907
7908 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00007909 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08007910 BUG_ON(!list_empty(&dev->ptype_all));
7911 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00007912 WARN_ON(rcu_access_pointer(dev->ip_ptr));
7913 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
Ilpo Järvinen547b7922008-07-25 21:43:18 -07007914 WARN_ON(dev->dn_ptr);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007915
David S. Millercf124db2017-05-08 12:52:56 -04007916 if (dev->priv_destructor)
7917 dev->priv_destructor(dev);
7918 if (dev->needs_free_netdev)
7919 free_netdev(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07007920
Eric W. Biederman50624c92013-09-23 21:19:49 -07007921 /* Report a network device has been unregistered */
7922 rtnl_lock();
7923 dev_net(dev)->dev_unreg_count--;
7924 __rtnl_unlock();
7925 wake_up(&netdev_unregistering_wq);
7926
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07007927 /* Free network device */
7928 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930}
7931
Jarod Wilson92566452016-02-01 18:51:04 -05007932/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
7933 * all the same fields in the same order as net_device_stats, with only
7934 * the type differing, but rtnl_link_stats64 may have additional fields
7935 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00007936 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00007937void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
7938 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00007939{
7940#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05007941 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys9af99592017-07-03 03:20:13 +02007942 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05007943 /* zero out counters that only exist in rtnl_link_stats64 */
7944 memset((char *)stats64 + sizeof(*netdev_stats), 0,
7945 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00007946#else
Jarod Wilson92566452016-02-01 18:51:04 -05007947 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00007948 const unsigned long *src = (const unsigned long *)netdev_stats;
7949 u64 *dst = (u64 *)stats64;
7950
Jarod Wilson92566452016-02-01 18:51:04 -05007951 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00007952 for (i = 0; i < n; i++)
7953 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05007954 /* zero out counters that only exist in rtnl_link_stats64 */
7955 memset((char *)stats64 + n * sizeof(u64), 0,
7956 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00007957#endif
7958}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00007959EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00007960
Eric Dumazetd83345a2009-11-16 03:36:51 +00007961/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007962 * dev_get_stats - get network device statistics
7963 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07007964 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007965 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00007966 * Get network statistics from device. Return @storage.
7967 * The device driver may provide its own method by setting
7968 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
7969 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007970 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00007971struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
7972 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00007973{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007974 const struct net_device_ops *ops = dev->netdev_ops;
7975
Eric Dumazet28172732010-07-07 14:58:56 -07007976 if (ops->ndo_get_stats64) {
7977 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00007978 ops->ndo_get_stats64(dev, storage);
7979 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00007980 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00007981 } else {
7982 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07007983 }
Eric Dumazet6f64ec72017-06-27 07:02:20 -07007984 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
7985 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
7986 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07007987 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07007988}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007989EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07007990
Eric Dumazet24824a02010-10-02 06:11:55 +00007991struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07007992{
Eric Dumazet24824a02010-10-02 06:11:55 +00007993 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07007994
Eric Dumazet24824a02010-10-02 06:11:55 +00007995#ifdef CONFIG_NET_CLS_ACT
7996 if (queue)
7997 return queue;
7998 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
7999 if (!queue)
8000 return NULL;
8001 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08008002 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00008003 queue->qdisc_sleeping = &noop_qdisc;
8004 rcu_assign_pointer(dev->ingress_queue, queue);
8005#endif
8006 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07008007}
8008
Eric Dumazet2c60db02012-09-16 09:17:26 +00008009static const struct ethtool_ops default_ethtool_ops;
8010
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00008011void netdev_set_default_ethtool_ops(struct net_device *dev,
8012 const struct ethtool_ops *ops)
8013{
8014 if (dev->ethtool_ops == &default_ethtool_ops)
8015 dev->ethtool_ops = ops;
8016}
8017EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
8018
Eric Dumazet74d332c2013-10-30 13:10:44 -07008019void netdev_freemem(struct net_device *dev)
8020{
8021 char *addr = (char *)dev - dev->padded;
8022
WANG Cong4cb28972014-06-02 15:55:22 -07008023 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07008024}
8025
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026/**
tcharding722c9a02017-02-09 17:56:04 +11008027 * alloc_netdev_mqs - allocate network device
8028 * @sizeof_priv: size of private data to allocate space for
8029 * @name: device name format string
8030 * @name_assign_type: origin of device name
8031 * @setup: callback to initialize device
8032 * @txqs: the number of TX subqueues to allocate
8033 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 *
tcharding722c9a02017-02-09 17:56:04 +11008035 * Allocates a struct net_device with private data area for driver use
8036 * and performs basic initialization. Also allocates subqueue structs
8037 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038 */
Tom Herbert36909ea2011-01-09 19:36:31 +00008039struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02008040 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00008041 void (*setup)(struct net_device *),
8042 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 struct net_device *dev;
Alexey Dobriyan52a59bd2017-09-21 23:33:29 +03008045 unsigned int alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008046 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07008048 BUG_ON(strlen(name) >= sizeof(dev->name));
8049
Tom Herbert36909ea2011-01-09 19:36:31 +00008050 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008051 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00008052 return NULL;
8053 }
8054
Michael Daltona953be52014-01-16 22:23:28 -08008055#ifdef CONFIG_SYSFS
Tom Herbert36909ea2011-01-09 19:36:31 +00008056 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008057 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00008058 return NULL;
8059 }
8060#endif
8061
David S. Millerfd2ea0a2008-07-17 01:56:23 -07008062 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07008063 if (sizeof_priv) {
8064 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008065 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07008066 alloc_size += sizeof_priv;
8067 }
8068 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008069 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070
Michal Hockodcda9b02017-07-12 14:36:45 -07008071 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Joe Perches62b59422013-02-04 16:48:16 +00008072 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00008075 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008077
Eric Dumazet29b44332010-10-11 10:22:12 +00008078 dev->pcpu_refcnt = alloc_percpu(int);
8079 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07008080 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008081
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00008083 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084
Jiri Pirko22bedad32010-04-01 21:22:57 +00008085 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008086 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00008087
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09008088 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07008090 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00008091 dev->gso_max_segs = GSO_MAX_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092
Herbert Xud565b0a2008-12-15 23:38:52 -08008093 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00008094 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07008095 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00008096 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02008097 INIT_LIST_HEAD(&dev->adj_list.upper);
8098 INIT_LIST_HEAD(&dev->adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08008099 INIT_LIST_HEAD(&dev->ptype_all);
8100 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02008101#ifdef CONFIG_NET_SCHED
8102 hash_init(dev->qdisc_hash);
8103#endif
Eric Dumazet02875872014-10-05 18:38:35 -07008104 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008106
Phil Suttera8131042016-02-17 15:37:43 +01008107 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02008108 dev->priv_flags |= IFF_NO_QUEUE;
Jesper Dangaard Brouer11597082016-11-03 14:56:06 +01008109 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Phil Suttera8131042016-02-17 15:37:43 +01008110 }
Phil Sutter906470c2015-08-18 10:30:48 +02008111
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008112 dev->num_tx_queues = txqs;
8113 dev->real_num_tx_queues = txqs;
8114 if (netif_alloc_netdev_queues(dev))
8115 goto free_all;
8116
Michael Daltona953be52014-01-16 22:23:28 -08008117#ifdef CONFIG_SYSFS
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008118 dev->num_rx_queues = rxqs;
8119 dev->real_num_rx_queues = rxqs;
8120 if (netif_alloc_rx_queues(dev))
8121 goto free_all;
8122#endif
8123
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02008125 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00008126 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00008127 if (!dev->ethtool_ops)
8128 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02008129
8130 nf_hook_ingress_init(dev);
8131
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008133
David S. Miller8d3bdbd2011-02-08 15:02:50 -08008134free_all:
8135 free_netdev(dev);
8136 return NULL;
8137
Eric Dumazet29b44332010-10-11 10:22:12 +00008138free_pcpu:
8139 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07008140free_dev:
8141 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00008142 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143}
Tom Herbert36909ea2011-01-09 19:36:31 +00008144EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
8146/**
tcharding722c9a02017-02-09 17:56:04 +11008147 * free_netdev - free network device
8148 * @dev: device
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 *
tcharding722c9a02017-02-09 17:56:04 +11008150 * This function does the last stage of destroying an allocated device
8151 * interface. The reference to the device object is released. If this
8152 * is the last reference then it will be freed.Must be called in process
8153 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 */
8155void free_netdev(struct net_device *dev)
8156{
Herbert Xud565b0a2008-12-15 23:38:52 -08008157 struct napi_struct *p, *n;
David S. Millerb5cdae32017-04-18 15:36:58 -04008158 struct bpf_prog *prog;
Herbert Xud565b0a2008-12-15 23:38:52 -08008159
Eric Dumazet93d05d42015-11-18 06:31:03 -08008160 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07008161 netif_free_tx_queues(dev);
Michael Daltona953be52014-01-16 22:23:28 -08008162#ifdef CONFIG_SYSFS
Pankaj Gupta10595902015-01-12 11:41:28 +05308163 kvfree(dev->_rx);
Tom Herbertfe822242010-11-09 10:47:38 +00008164#endif
David S. Millere8a04642008-07-17 00:34:19 -07008165
Eric Dumazet33d480c2011-08-11 19:30:52 +00008166 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00008167
Jiri Pirkof001fde2009-05-05 02:48:28 +00008168 /* Flush device addresses */
8169 dev_addr_flush(dev);
8170
Herbert Xud565b0a2008-12-15 23:38:52 -08008171 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
8172 netif_napi_del(p);
8173
Eric Dumazet29b44332010-10-11 10:22:12 +00008174 free_percpu(dev->pcpu_refcnt);
8175 dev->pcpu_refcnt = NULL;
8176
David S. Millerb5cdae32017-04-18 15:36:58 -04008177 prog = rcu_dereference_protected(dev->xdp_prog, 1);
8178 if (prog) {
8179 bpf_prog_put(prog);
8180 static_key_slow_dec(&generic_xdp_needed);
8181 }
8182
Stephen Hemminger3041a062006-05-26 13:25:24 -07008183 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07008185 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186 return;
8187 }
8188
8189 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
8190 dev->reg_state = NETREG_RELEASED;
8191
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07008192 /* will free via device release */
8193 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008195EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008196
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008197/**
8198 * synchronize_net - Synchronize with packet receive processing
8199 *
8200 * Wait for packets currently being received to be done.
8201 * Does not block later packets from starting.
8202 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09008203void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204{
8205 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00008206 if (rtnl_is_locked())
8207 synchronize_rcu_expedited();
8208 else
8209 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07008211EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212
8213/**
Eric Dumazet44a08732009-10-27 07:03:04 +00008214 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00008216 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08008217 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08008219 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00008220 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 *
8222 * Callers must hold the rtnl semaphore. You may want
8223 * unregister_netdev() instead of this.
8224 */
8225
Eric Dumazet44a08732009-10-27 07:03:04 +00008226void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227{
Herbert Xua6620712007-12-12 19:21:56 -08008228 ASSERT_RTNL();
8229
Eric Dumazet44a08732009-10-27 07:03:04 +00008230 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00008231 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00008232 } else {
8233 rollback_registered(dev);
8234 /* Finish processing unregister after unlock */
8235 net_set_todo(dev);
8236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237}
Eric Dumazet44a08732009-10-27 07:03:04 +00008238EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
8240/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008241 * unregister_netdevice_many - unregister many devices
8242 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07008243 *
8244 * Note: As most callers use a stack allocated list_head,
8245 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008246 */
8247void unregister_netdevice_many(struct list_head *head)
8248{
8249 struct net_device *dev;
8250
8251 if (!list_empty(head)) {
8252 rollback_registered_many(head);
8253 list_for_each_entry(dev, head, unreg_list)
8254 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07008255 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008256 }
8257}
Eric Dumazet63c80992009-10-27 07:06:49 +00008258EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00008259
8260/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 * unregister_netdev - remove device from the kernel
8262 * @dev: device
8263 *
8264 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08008265 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266 *
8267 * This is just a wrapper for unregister_netdevice that takes
8268 * the rtnl semaphore. In general you want to use this and not
8269 * unregister_netdevice.
8270 */
8271void unregister_netdev(struct net_device *dev)
8272{
8273 rtnl_lock();
8274 unregister_netdevice(dev);
8275 rtnl_unlock();
8276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277EXPORT_SYMBOL(unregister_netdev);
8278
Eric W. Biedermance286d32007-09-12 13:53:49 +02008279/**
8280 * dev_change_net_namespace - move device to different nethost namespace
8281 * @dev: device
8282 * @net: network namespace
8283 * @pat: If not NULL name pattern to try if the current device name
8284 * is already taken in the destination network namespace.
8285 *
8286 * This function shuts down a device interface and moves it
8287 * to a new network namespace. On success 0 is returned, on
8288 * a failure a netagive errno code is returned.
8289 *
8290 * Callers must hold the rtnl semaphore.
8291 */
8292
8293int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
8294{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02008295 int err, new_nsid;
Eric W. Biedermance286d32007-09-12 13:53:49 +02008296
8297 ASSERT_RTNL();
8298
8299 /* Don't allow namespace local devices to be moved. */
8300 err = -EINVAL;
8301 if (dev->features & NETIF_F_NETNS_LOCAL)
8302 goto out;
8303
8304 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02008305 if (dev->reg_state != NETREG_REGISTERED)
8306 goto out;
8307
8308 /* Get out if there is nothing todo */
8309 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09008310 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02008311 goto out;
8312
8313 /* Pick the destination device name, and ensure
8314 * we can use it in the destination network namespace.
8315 */
8316 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00008317 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008318 /* We get here if we can't use the current device name */
8319 if (!pat)
8320 goto out;
Gao feng828de4f2012-09-13 20:58:27 +00008321 if (dev_get_valid_name(net, dev, pat) < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02008322 goto out;
8323 }
8324
8325 /*
8326 * And now a mini version of register_netdevice unregister_netdevice.
8327 */
8328
8329 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07008330 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008331
8332 /* And unlink it from device chain */
8333 err = -ENODEV;
8334 unlist_netdevice(dev);
8335
8336 synchronize_net();
8337
8338 /* Shutdown queueing discipline. */
8339 dev_shutdown(dev);
8340
8341 /* Notify protocols, that we are about to destroy
tchardingeb13da12017-02-09 17:56:06 +11008342 * this device. They should clean all the things.
8343 *
8344 * Note that dev->reg_state stays at NETREG_REGISTERED.
8345 * This is wanted because this way 8021q and macvlan know
8346 * the device is just moving and can keep their slaves up.
8347 */
Eric W. Biedermance286d32007-09-12 13:53:49 +02008348 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +00008349 rcu_barrier();
8350 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02008351 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net)
8352 new_nsid = peernet2id_alloc(dev_net(dev), net);
8353 else
8354 new_nsid = peernet2id(dev_net(dev), net);
8355 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008356
8357 /*
8358 * Flush the unicast and multicast chains
8359 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008360 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008361 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008362
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008363 /* Send a netdev-removed uevent to the old namespace */
8364 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008365 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008366
Eric W. Biedermance286d32007-09-12 13:53:49 +02008367 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09008368 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008369
Eric W. Biedermance286d32007-09-12 13:53:49 +02008370 /* If there is an ifindex conflict assign a new one */
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +02008371 if (__dev_get_by_index(net, dev->ifindex))
Eric W. Biedermance286d32007-09-12 13:53:49 +02008372 dev->ifindex = dev_new_index(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008373
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008374 /* Send a netdev-add uevent to the new namespace */
8375 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008376 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008377
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008378 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07008379 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008380 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008381
8382 /* Add the device back in the hashes */
8383 list_netdevice(dev);
8384
8385 /* Notify protocols, that a new device appeared. */
8386 call_netdevice_notifiers(NETDEV_REGISTER, dev);
8387
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008388 /*
8389 * Prevent userspace races by waiting until the network
8390 * device is fully setup before sending notifications.
8391 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008392 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008393
Eric W. Biedermance286d32007-09-12 13:53:49 +02008394 synchronize_net();
8395 err = 0;
8396out:
8397 return err;
8398}
Johannes Berg463d0182009-07-14 00:33:35 +02008399EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008400
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008401static int dev_cpu_dead(unsigned int oldcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402{
8403 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 struct sk_buff *skb;
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008405 unsigned int cpu;
Ashwanth Goli97d8b6e2017-06-13 16:54:55 +05308406 struct softnet_data *sd, *oldsd, *remsd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 local_irq_disable();
8409 cpu = smp_processor_id();
8410 sd = &per_cpu(softnet_data, cpu);
8411 oldsd = &per_cpu(softnet_data, oldcpu);
8412
8413 /* Find end of our completion_queue. */
8414 list_skb = &sd->completion_queue;
8415 while (*list_skb)
8416 list_skb = &(*list_skb)->next;
8417 /* Append completion queue from offline CPU. */
8418 *list_skb = oldsd->completion_queue;
8419 oldsd->completion_queue = NULL;
8420
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00008422 if (oldsd->output_queue) {
8423 *sd->output_queue_tailp = oldsd->output_queue;
8424 sd->output_queue_tailp = oldsd->output_queue_tailp;
8425 oldsd->output_queue = NULL;
8426 oldsd->output_queue_tailp = &oldsd->output_queue;
8427 }
Eric Dumazetac64da02015-01-15 17:04:22 -08008428 /* Append NAPI poll list from offline CPU, with one exception :
8429 * process_backlog() must be called by cpu owning percpu backlog.
8430 * We properly handle process_queue & input_pkt_queue later.
8431 */
8432 while (!list_empty(&oldsd->poll_list)) {
8433 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
8434 struct napi_struct,
8435 poll_list);
8436
8437 list_del_init(&napi->poll_list);
8438 if (napi->poll == process_backlog)
8439 napi->state = 0;
8440 else
8441 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +00008442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
8444 raise_softirq_irqoff(NET_TX_SOFTIRQ);
8445 local_irq_enable();
8446
ashwanth@codeaurora.org773fc8f2017-06-09 14:24:58 +05308447#ifdef CONFIG_RPS
8448 remsd = oldsd->rps_ipi_list;
8449 oldsd->rps_ipi_list = NULL;
8450#endif
8451 /* send out pending IPI's on offline CPU */
8452 net_rps_send_ipi(remsd);
8453
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00008455 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08008456 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00008457 input_queue_head_incr(oldsd);
8458 }
Eric Dumazetac64da02015-01-15 17:04:22 -08008459 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08008460 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00008461 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07008462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466
Herbert Xu7f353bf2007-08-10 15:47:58 -07008467/**
Herbert Xub63365a2008-10-23 01:11:29 -07008468 * netdev_increment_features - increment feature set by one
8469 * @all: current feature set
8470 * @one: new feature set
8471 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07008472 *
8473 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07008474 * @one to the master device with current feature set @all. Will not
8475 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07008476 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008477netdev_features_t netdev_increment_features(netdev_features_t all,
8478 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07008479{
Tom Herbertc8cd0982015-12-14 11:19:44 -08008480 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -08008481 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +00008482 mask |= NETIF_F_VLAN_CHALLENGED;
8483
Tom Herberta1882222015-12-14 11:19:43 -08008484 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +00008485 all &= one | ~NETIF_F_ALL_FOR_ALL;
8486
Michał Mirosław1742f182011-04-22 06:31:16 +00008487 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -08008488 if (all & NETIF_F_HW_CSUM)
8489 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -07008490
8491 return all;
8492}
Herbert Xub63365a2008-10-23 01:11:29 -07008493EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07008494
Baruch Siach430f03c2013-06-02 20:43:55 +00008495static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008496{
8497 int i;
8498 struct hlist_head *hash;
8499
8500 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
8501 if (hash != NULL)
8502 for (i = 0; i < NETDEV_HASHENTRIES; i++)
8503 INIT_HLIST_HEAD(&hash[i]);
8504
8505 return hash;
8506}
8507
Eric W. Biederman881d9662007-09-17 11:56:21 -07008508/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07008509static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07008510{
Rustad, Mark D734b6542012-07-18 09:06:07 +00008511 if (net != &init_net)
8512 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07008513
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008514 net->dev_name_head = netdev_create_hash();
8515 if (net->dev_name_head == NULL)
8516 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008517
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008518 net->dev_index_head = netdev_create_hash();
8519 if (net->dev_index_head == NULL)
8520 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008521
8522 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008523
8524err_idx:
8525 kfree(net->dev_name_head);
8526err_name:
8527 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008528}
8529
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008530/**
8531 * netdev_drivername - network driver for the device
8532 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008533 *
8534 * Determine network driver for device.
8535 */
David S. Miller3019de12011-06-06 16:41:33 -07008536const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -07008537{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07008538 const struct device_driver *driver;
8539 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -07008540 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -07008541
8542 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008543 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -07008544 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008545
8546 driver = parent->driver;
8547 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -07008548 return driver->name;
8549 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008550}
8551
Joe Perches6ea754e2014-09-22 11:10:50 -07008552static void __netdev_printk(const char *level, const struct net_device *dev,
8553 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +00008554{
Joe Perchesb004ff42012-09-12 20:12:19 -07008555 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -07008556 dev_printk_emit(level[1] - '0',
8557 dev->dev.parent,
8558 "%s %s %s%s: %pV",
8559 dev_driver_string(dev->dev.parent),
8560 dev_name(dev->dev.parent),
8561 netdev_name(dev), netdev_reg_state(dev),
8562 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008563 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -07008564 printk("%s%s%s: %pV",
8565 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008566 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -07008567 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008568 }
Joe Perches256df2f2010-06-27 01:02:35 +00008569}
8570
Joe Perches6ea754e2014-09-22 11:10:50 -07008571void netdev_printk(const char *level, const struct net_device *dev,
8572 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +00008573{
8574 struct va_format vaf;
8575 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +00008576
8577 va_start(args, format);
8578
8579 vaf.fmt = format;
8580 vaf.va = &args;
8581
Joe Perches6ea754e2014-09-22 11:10:50 -07008582 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008583
Joe Perches256df2f2010-06-27 01:02:35 +00008584 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +00008585}
8586EXPORT_SYMBOL(netdev_printk);
8587
8588#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -07008589void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +00008590{ \
Joe Perches256df2f2010-06-27 01:02:35 +00008591 struct va_format vaf; \
8592 va_list args; \
8593 \
8594 va_start(args, fmt); \
8595 \
8596 vaf.fmt = fmt; \
8597 vaf.va = &args; \
8598 \
Joe Perches6ea754e2014-09-22 11:10:50 -07008599 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -07008600 \
Joe Perches256df2f2010-06-27 01:02:35 +00008601 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +00008602} \
8603EXPORT_SYMBOL(func);
8604
8605define_netdev_printk_level(netdev_emerg, KERN_EMERG);
8606define_netdev_printk_level(netdev_alert, KERN_ALERT);
8607define_netdev_printk_level(netdev_crit, KERN_CRIT);
8608define_netdev_printk_level(netdev_err, KERN_ERR);
8609define_netdev_printk_level(netdev_warn, KERN_WARNING);
8610define_netdev_printk_level(netdev_notice, KERN_NOTICE);
8611define_netdev_printk_level(netdev_info, KERN_INFO);
8612
Pavel Emelyanov46650792007-10-08 20:38:39 -07008613static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07008614{
8615 kfree(net->dev_name_head);
8616 kfree(net->dev_index_head);
8617}
8618
Denis V. Lunev022cbae2007-11-13 03:23:50 -08008619static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07008620 .init = netdev_init,
8621 .exit = netdev_exit,
8622};
8623
Pavel Emelyanov46650792007-10-08 20:38:39 -07008624static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02008625{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008626 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02008627 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008628 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02008629 * initial network namespace
8630 */
8631 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008632 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008633 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008634 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02008635
8636 /* Ignore unmoveable devices (i.e. loopback) */
8637 if (dev->features & NETIF_F_NETNS_LOCAL)
8638 continue;
8639
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008640 /* Leave virtual devices for the generic cleanup */
8641 if (dev->rtnl_link_ops)
8642 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08008643
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008644 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008645 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
8646 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008647 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008648 pr_emerg("%s: failed to move %s to init_net: %d\n",
8649 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008650 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02008651 }
8652 }
8653 rtnl_unlock();
8654}
8655
Eric W. Biederman50624c92013-09-23 21:19:49 -07008656static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
8657{
8658 /* Return with the rtnl_lock held when there are no network
8659 * devices unregistering in any network namespace in net_list.
8660 */
8661 struct net *net;
8662 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +01008663 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008664
Peter Zijlstraff960a72014-10-29 17:04:56 +01008665 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008666 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -07008667 unregistering = false;
8668 rtnl_lock();
8669 list_for_each_entry(net, net_list, exit_list) {
8670 if (net->dev_unreg_count > 0) {
8671 unregistering = true;
8672 break;
8673 }
8674 }
8675 if (!unregistering)
8676 break;
8677 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +01008678
8679 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008680 }
Peter Zijlstraff960a72014-10-29 17:04:56 +01008681 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008682}
8683
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008684static void __net_exit default_device_exit_batch(struct list_head *net_list)
8685{
8686 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04008687 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008688 * Do this across as many network namespaces as possible to
8689 * improve batching efficiency.
8690 */
8691 struct net_device *dev;
8692 struct net *net;
8693 LIST_HEAD(dev_kill_list);
8694
Eric W. Biederman50624c92013-09-23 21:19:49 -07008695 /* To prevent network device cleanup code from dereferencing
8696 * loopback devices or network devices that have been freed
8697 * wait here for all pending unregistrations to complete,
8698 * before unregistring the loopback device and allowing the
8699 * network namespace be freed.
8700 *
8701 * The netdev todo list containing all network devices
8702 * unregistrations that happen in default_device_exit_batch
8703 * will run in the rtnl_unlock() at the end of
8704 * default_device_exit_batch.
8705 */
8706 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008707 list_for_each_entry(net, net_list, exit_list) {
8708 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02008709 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008710 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
8711 else
8712 unregister_netdevice_queue(dev, &dev_kill_list);
8713 }
8714 }
8715 unregister_netdevice_many(&dev_kill_list);
8716 rtnl_unlock();
8717}
8718
Denis V. Lunev022cbae2007-11-13 03:23:50 -08008719static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008720 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008721 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02008722};
8723
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724/*
8725 * Initialize the DEV module. At boot time this walks the device list and
8726 * unhooks any devices that fail to initialise (normally hardware not
8727 * present) and leaves us with a valid list of present and active devices.
8728 *
8729 */
8730
8731/*
8732 * This is called single threaded during boot, so no need
8733 * to take the rtnl semaphore.
8734 */
8735static int __init net_dev_init(void)
8736{
8737 int i, rc = -ENOMEM;
8738
8739 BUG_ON(!dev_boot_phase);
8740
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 if (dev_proc_init())
8742 goto out;
8743
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008744 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 goto out;
8746
8747 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08008748 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749 INIT_LIST_HEAD(&ptype_base[i]);
8750
Vlad Yasevich62532da2012-11-15 08:49:10 +00008751 INIT_LIST_HEAD(&offload_base);
8752
Eric W. Biederman881d9662007-09-17 11:56:21 -07008753 if (register_pernet_subsys(&netdev_net_ops))
8754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755
8756 /*
8757 * Initialise the packet receive queues.
8758 */
8759
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07008760 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -07008761 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008762 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763
Eric Dumazet41852492016-08-26 12:50:39 -07008764 INIT_WORK(flush, flush_backlog);
8765
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008766 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07008767 skb_queue_head_init(&sd->process_queue);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008768 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00008769 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00008770#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008771 sd->csd.func = rps_trigger_softirq;
8772 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008773 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07008774#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00008775
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008776 sd->backlog.poll = process_backlog;
8777 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778 }
8779
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780 dev_boot_phase = 0;
8781
Eric W. Biederman505d4f72008-11-07 22:54:20 -08008782 /* The loopback device is special if any other network devices
8783 * is present in a network namespace the loopback device must
8784 * be present. Since we now dynamically allocate and free the
8785 * loopback device ensure this invariant is maintained by
8786 * keeping the loopback device as the first device on the
8787 * list of network devices. Ensuring the loopback devices
8788 * is the first device that appears and the last network device
8789 * that disappears.
8790 */
8791 if (register_pernet_device(&loopback_net_ops))
8792 goto out;
8793
8794 if (register_pernet_device(&default_device_ops))
8795 goto out;
8796
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008797 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
8798 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799
Sebastian Andrzej Siewiorf0bf90d2016-11-03 15:50:04 +01008800 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
8801 NULL, dev_cpu_dead);
8802 WARN_ON(rc < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 rc = 0;
8804out:
8805 return rc;
8806}
8807
8808subsys_initcall(net_dev_init);