blob: 4e10bae5e3dada53fb6d5da74038ccad0a3b4d29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET3 Protocol independent device support routines.
3 *
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
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * 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
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * 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.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * 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.
49 * Alan Cox : Fixed nasty side effect of device close
50 * 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
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
73 */
74
75#include <asm/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>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080084#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/string.h>
86#include <linux/mm.h>
87#include <linux/socket.h>
88#include <linux/sockios.h>
89#include <linux/errno.h>
90#include <linux/interrupt.h>
91#include <linux/if_ether.h>
92#include <linux/netdevice.h>
93#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070094#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/notifier.h>
96#include <linux/skbuff.h>
Brenden Blancoa7862b42016-07-19 12:16:48 -070097#include <linux/bpf.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020098#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <net/sock.h>
Eric Dumazet02d62e82015-11-18 06:30:52 -0800100#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <net/dst.h>
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700104#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <net/pkt_sched.h>
106#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000107#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/highmem.h>
109#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <linux/netpoll.h>
112#include <linux/rcupdate.h>
113#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500116#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700117#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700118#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700119#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700120#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700121#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700122#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700123#include <net/ip.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -0700124#include <net/mpls.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700125#include <linux/ipv6.h>
126#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700127#include <linux/jhash.h>
128#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700129#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900130#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900131#include <trace/events/skb.h>
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +0000132#include <linux/pci.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700133#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000134#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100135#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300136#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700137#include <linux/vmalloc.h>
Michal Kubeček529d0482013-11-15 06:18:50 +0100138#include <linux/if_macvlan.h>
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400139#include <linux/errqueue.h>
Eric Dumazet3b47d302014-11-06 21:09:44 -0800140#include <linux/hrtimer.h>
Pablo Neirae687ad62015-05-13 18:19:38 +0200141#include <linux/netfilter_ingress.h>
Tom Herbert6ae23ad2015-12-14 11:19:46 -0800142#include <linux/sctp.h>
Hariprasad Shenai40e4e712016-06-08 18:09:08 +0530143#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700145#include "net-sysfs.h"
146
Herbert Xud565b0a2008-12-15 23:38:52 -0800147/* Instead of increasing this, you should create a hash table. */
148#define MAX_GRO_SKBS 8
149
Herbert Xu5d38a072009-01-04 16:13:40 -0800150/* This should be increased if a protocol with a bigger head is added. */
151#define GRO_MAX_HEAD (MAX_HEADER + 128)
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000154static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000155struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
156struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000157static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000159static int netif_rx_internal(struct sk_buff *skb);
Loic Prylli5495119462014-07-01 21:39:43 -0700160static int call_netdevice_notifiers_info(unsigned long val,
161 struct net_device *dev,
162 struct netdev_notifier_info *info);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +0000163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700165 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * semaphore.
167 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800168 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700171 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * actual updates. This allows pure readers to access the list even
173 * while a writer is preparing to update it.
174 *
175 * To put it another way, dev_base_lock is held for writing only to
176 * protect against pure readers; the rtnl semaphore provides the
177 * protection against other writers.
178 *
179 * See, for example usages, register_netdevice() and
180 * unregister_netdevice(), which must be called with the rtnl
181 * semaphore held.
182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184EXPORT_SYMBOL(dev_base_lock);
185
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300186/* protects napi_hash addition/deletion and napi_gen_id */
187static DEFINE_SPINLOCK(napi_hash_lock);
188
Eric Dumazet52bd2d62015-11-18 06:30:50 -0800189static unsigned int napi_gen_id = NR_CPUS;
Eric Dumazet6180d9d2015-11-18 06:31:01 -0800190static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300191
Thomas Gleixner18afa4b2013-07-23 16:13:17 +0200192static seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000193
Thomas Graf4e985ad2011-06-21 03:11:20 +0000194static inline void dev_base_seq_inc(struct net *net)
195{
196 while (++net->dev_base_seq == 0);
197}
198
Eric W. Biederman881d9662007-09-17 11:56:21 -0700199static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700201 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
Eric Dumazet95c96172012-04-15 05:58:06 +0000202
stephen hemminger08e98972009-11-10 07:20:34 +0000203 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Eric W. Biederman881d9662007-09-17 11:56:21 -0700206static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700208 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000211static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000212{
213#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000214 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000215#endif
216}
217
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000218static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000219{
220#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000221 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000222#endif
223}
224
Eric W. Biedermance286d32007-09-12 13:53:49 +0200225/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000226static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200227{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900228 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200229
230 ASSERT_RTNL();
231
232 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800233 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000234 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000235 hlist_add_head_rcu(&dev->index_hlist,
236 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200237 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000238
239 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200240}
241
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000242/* Device list removal
243 * caller must respect a RCU grace period before freeing/reusing dev
244 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200245static void unlist_netdevice(struct net_device *dev)
246{
247 ASSERT_RTNL();
248
249 /* Unlink dev from the device chain */
250 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800251 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000252 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000253 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200254 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000255
256 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * Our notifier list
261 */
262
Alan Sternf07d5b92006-05-09 15:23:03 -0700263static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265/*
266 * Device drivers call our routines to queue packets here. We empty the
267 * queue in the local softnet handler.
268 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700269
Eric Dumazet9958da02010-04-17 04:17:02 +0000270DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700271EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David S. Millercf508b12008-07-22 14:16:42 -0700273#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700274/*
David S. Millerc773e842008-07-08 23:13:53 -0700275 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700276 * according to dev->type
277 */
278static const unsigned short netdev_lock_type[] =
279 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
280 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
281 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
282 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
283 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
284 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
285 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
286 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
287 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
288 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
289 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
290 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
Paul Gortmaker211ed862012-05-10 17:14:35 -0400291 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
292 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
293 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700294
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700295static const char *const netdev_lock_name[] =
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700296 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
297 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
298 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
299 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
300 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
301 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
302 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
303 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
304 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
305 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
306 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
307 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
Paul Gortmaker211ed862012-05-10 17:14:35 -0400308 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
309 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
310 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700311
312static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700313static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700314
315static inline unsigned short netdev_lock_pos(unsigned short dev_type)
316{
317 int i;
318
319 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
320 if (netdev_lock_type[i] == dev_type)
321 return i;
322 /* the last key is used by default */
323 return ARRAY_SIZE(netdev_lock_type) - 1;
324}
325
David S. Millercf508b12008-07-22 14:16:42 -0700326static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
327 unsigned short dev_type)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700328{
329 int i;
330
331 i = netdev_lock_pos(dev_type);
332 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
333 netdev_lock_name[i]);
334}
David S. Millercf508b12008-07-22 14:16:42 -0700335
336static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
337{
338 int i;
339
340 i = netdev_lock_pos(dev->type);
341 lockdep_set_class_and_name(&dev->addr_list_lock,
342 &netdev_addr_lock_key[i],
343 netdev_lock_name[i]);
344}
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700345#else
David S. Millercf508b12008-07-22 14:16:42 -0700346static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
347 unsigned short dev_type)
348{
349}
350static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700351{
352}
353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355/*******************************************************************************
356
357 Protocol management and registration routines
358
359*******************************************************************************/
360
361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * Add a protocol ID to the list. Now that the input handler is
363 * smarter we can dispense with all the messy stuff that used to be
364 * here.
365 *
366 * BEWARE!!! Protocol handlers, mangling input packets,
367 * MUST BE last in hash buckets and checking protocol handlers
368 * MUST start from promiscuous ptype_all chain in net_bh.
369 * It is true now, do not change it.
370 * Explanation follows: if protocol handler, mangling packet, will
371 * be the first on list, it is not able to sense, that packet
372 * is cloned and should be copied-on-write, so that it will
373 * change it and subsequent readers will get broken packet.
374 * --ANK (980803)
375 */
376
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000377static inline struct list_head *ptype_head(const struct packet_type *pt)
378{
379 if (pt->type == htons(ETH_P_ALL))
Salam Noureddine7866a622015-01-27 11:35:48 -0800380 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000381 else
Salam Noureddine7866a622015-01-27 11:35:48 -0800382 return pt->dev ? &pt->dev->ptype_specific :
383 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
387 * dev_add_pack - add packet handler
388 * @pt: packet type declaration
389 *
390 * Add a protocol handler to the networking stack. The passed &packet_type
391 * is linked into kernel lists and may not be freed until it has been
392 * removed from the kernel lists.
393 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900394 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * guarantee all CPU's that are in middle of receiving packets
396 * will see the new packet type (until the next received packet).
397 */
398
399void dev_add_pack(struct packet_type *pt)
400{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000401 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000403 spin_lock(&ptype_lock);
404 list_add_rcu(&pt->list, head);
405 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700407EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/**
410 * __dev_remove_pack - remove packet handler
411 * @pt: packet type declaration
412 *
413 * Remove a protocol handler that was previously added to the kernel
414 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
415 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900416 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * The packet type might still be in use by receivers
419 * and must not be freed until after all the CPU's have gone
420 * through a quiescent state.
421 */
422void __dev_remove_pack(struct packet_type *pt)
423{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000424 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct packet_type *pt1;
426
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000427 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 list_for_each_entry(pt1, head, list) {
430 if (pt == pt1) {
431 list_del_rcu(&pt->list);
432 goto out;
433 }
434 }
435
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000436 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000438 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700440EXPORT_SYMBOL(__dev_remove_pack);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/**
443 * dev_remove_pack - remove packet handler
444 * @pt: packet type declaration
445 *
446 * Remove a protocol handler that was previously added to the kernel
447 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
448 * from the kernel lists and can be freed or reused once this function
449 * returns.
450 *
451 * This call sleeps to guarantee that no CPU is looking at the packet
452 * type after return.
453 */
454void dev_remove_pack(struct packet_type *pt)
455{
456 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 synchronize_net();
459}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700460EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Vlad Yasevich62532da2012-11-15 08:49:10 +0000462
463/**
464 * dev_add_offload - register offload handlers
465 * @po: protocol offload declaration
466 *
467 * Add protocol offload handlers to the networking stack. The passed
468 * &proto_offload is linked into kernel lists and may not be freed until
469 * it has been removed from the kernel lists.
470 *
471 * This call does not sleep therefore it can not
472 * guarantee all CPU's that are in middle of receiving packets
473 * will see the new offload handlers (until the next received packet).
474 */
475void dev_add_offload(struct packet_offload *po)
476{
David S. Millerbdef7de2015-06-01 14:56:09 -0700477 struct packet_offload *elem;
Vlad Yasevich62532da2012-11-15 08:49:10 +0000478
479 spin_lock(&offload_lock);
David S. Millerbdef7de2015-06-01 14:56:09 -0700480 list_for_each_entry(elem, &offload_base, list) {
481 if (po->priority < elem->priority)
482 break;
483 }
484 list_add_rcu(&po->list, elem->list.prev);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000485 spin_unlock(&offload_lock);
486}
487EXPORT_SYMBOL(dev_add_offload);
488
489/**
490 * __dev_remove_offload - remove offload handler
491 * @po: packet offload declaration
492 *
493 * Remove a protocol offload handler that was previously added to the
494 * kernel offload handlers by dev_add_offload(). The passed &offload_type
495 * is removed from the kernel lists and can be freed or reused once this
496 * function returns.
497 *
498 * The packet type might still be in use by receivers
499 * and must not be freed until after all the CPU's have gone
500 * through a quiescent state.
501 */
stephen hemminger1d143d92013-12-29 14:01:29 -0800502static void __dev_remove_offload(struct packet_offload *po)
Vlad Yasevich62532da2012-11-15 08:49:10 +0000503{
504 struct list_head *head = &offload_base;
505 struct packet_offload *po1;
506
Eric Dumazetc53aa502012-11-16 08:08:23 +0000507 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000508
509 list_for_each_entry(po1, head, list) {
510 if (po == po1) {
511 list_del_rcu(&po->list);
512 goto out;
513 }
514 }
515
516 pr_warn("dev_remove_offload: %p not found\n", po);
517out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000518 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000519}
Vlad Yasevich62532da2012-11-15 08:49:10 +0000520
521/**
522 * dev_remove_offload - remove packet offload handler
523 * @po: packet offload declaration
524 *
525 * Remove a packet offload handler that was previously added to the kernel
526 * offload handlers by dev_add_offload(). The passed &offload_type is
527 * removed from the kernel lists and can be freed or reused once this
528 * function returns.
529 *
530 * This call sleeps to guarantee that no CPU is looking at the packet
531 * type after return.
532 */
533void dev_remove_offload(struct packet_offload *po)
534{
535 __dev_remove_offload(po);
536
537 synchronize_net();
538}
539EXPORT_SYMBOL(dev_remove_offload);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/******************************************************************************
542
543 Device Boot-time Settings Routines
544
545*******************************************************************************/
546
547/* Boot time configuration table */
548static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
549
550/**
551 * netdev_boot_setup_add - add new setup entry
552 * @name: name of the device
553 * @map: configured settings for the device
554 *
555 * Adds new setup entry to the dev_boot_setup list. The function
556 * returns 0 on error and 1 on success. This is a generic routine to
557 * all netdevices.
558 */
559static int netdev_boot_setup_add(char *name, struct ifmap *map)
560{
561 struct netdev_boot_setup *s;
562 int i;
563
564 s = dev_boot_setup;
565 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
566 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
567 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700568 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 memcpy(&s[i].map, map, sizeof(s[i].map));
570 break;
571 }
572 }
573
574 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
575}
576
577/**
578 * netdev_boot_setup_check - check boot time settings
579 * @dev: the netdevice
580 *
581 * Check boot time settings for the device.
582 * The found settings are set for the device to be used
583 * later in the device probing.
584 * Returns 0 if no settings found, 1 if they are.
585 */
586int netdev_boot_setup_check(struct net_device *dev)
587{
588 struct netdev_boot_setup *s = dev_boot_setup;
589 int i;
590
591 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
592 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700593 !strcmp(dev->name, s[i].name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 dev->irq = s[i].map.irq;
595 dev->base_addr = s[i].map.base_addr;
596 dev->mem_start = s[i].map.mem_start;
597 dev->mem_end = s[i].map.mem_end;
598 return 1;
599 }
600 }
601 return 0;
602}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700603EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605
606/**
607 * netdev_boot_base - get address from boot time settings
608 * @prefix: prefix for network device
609 * @unit: id for network device
610 *
611 * Check boot time settings for the base address of device.
612 * The found settings are set for the device to be used
613 * later in the device probing.
614 * Returns 0 if no settings found.
615 */
616unsigned long netdev_boot_base(const char *prefix, int unit)
617{
618 const struct netdev_boot_setup *s = dev_boot_setup;
619 char name[IFNAMSIZ];
620 int i;
621
622 sprintf(name, "%s%d", prefix, unit);
623
624 /*
625 * If device already registered then return base of 1
626 * to indicate not to probe for this interface
627 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700628 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 1;
630
631 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
632 if (!strcmp(name, s[i].name))
633 return s[i].map.base_addr;
634 return 0;
635}
636
637/*
638 * Saves at boot time configured settings for any netdevice.
639 */
640int __init netdev_boot_setup(char *str)
641{
642 int ints[5];
643 struct ifmap map;
644
645 str = get_options(str, ARRAY_SIZE(ints), ints);
646 if (!str || !*str)
647 return 0;
648
649 /* Save settings */
650 memset(&map, 0, sizeof(map));
651 if (ints[0] > 0)
652 map.irq = ints[1];
653 if (ints[0] > 1)
654 map.base_addr = ints[2];
655 if (ints[0] > 2)
656 map.mem_start = ints[3];
657 if (ints[0] > 3)
658 map.mem_end = ints[4];
659
660 /* Add new entry to the list */
661 return netdev_boot_setup_add(str, &map);
662}
663
664__setup("netdev=", netdev_boot_setup);
665
666/*******************************************************************************
667
668 Device Interface Subroutines
669
670*******************************************************************************/
671
672/**
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200673 * dev_get_iflink - get 'iflink' value of a interface
674 * @dev: targeted interface
675 *
676 * Indicates the ifindex the interface is linked to.
677 * Physical interfaces have the same 'ifindex' and 'iflink' values.
678 */
679
680int dev_get_iflink(const struct net_device *dev)
681{
682 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
683 return dev->netdev_ops->ndo_get_iflink(dev);
684
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +0200685 return dev->ifindex;
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200686}
687EXPORT_SYMBOL(dev_get_iflink);
688
689/**
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700690 * dev_fill_metadata_dst - Retrieve tunnel egress information.
691 * @dev: targeted interface
692 * @skb: The packet.
693 *
694 * For better visibility of tunnel traffic OVS needs to retrieve
695 * egress tunnel information for a packet. Following API allows
696 * user to get this info.
697 */
698int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
699{
700 struct ip_tunnel_info *info;
701
702 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
703 return -EINVAL;
704
705 info = skb_tunnel_info_unclone(skb);
706 if (!info)
707 return -ENOMEM;
708 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
709 return -EINVAL;
710
711 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
712}
713EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
714
715/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700717 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * @name: name to find
719 *
720 * Find an interface by name. Must be called under RTNL semaphore
721 * or @dev_base_lock. If the name is found a pointer to the device
722 * is returned. If the name is not found then %NULL is returned. The
723 * reference counters are not incremented so the caller must be
724 * careful with locks.
725 */
726
Eric W. Biederman881d9662007-09-17 11:56:21 -0700727struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700729 struct net_device *dev;
730 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Sasha Levinb67bfe02013-02-27 17:06:00 -0800732 hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!strncmp(dev->name, name, IFNAMSIZ))
734 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return NULL;
737}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700738EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740/**
Eric Dumazet72c95282009-10-30 07:11:27 +0000741 * dev_get_by_name_rcu - find a device by its name
742 * @net: the applicable net namespace
743 * @name: name to find
744 *
745 * Find an interface by name.
746 * If the name is found a pointer to the device is returned.
747 * If the name is not found then %NULL is returned.
748 * The reference counters are not incremented so the caller must be
749 * careful with locks. The caller must hold RCU lock.
750 */
751
752struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
753{
Eric Dumazet72c95282009-10-30 07:11:27 +0000754 struct net_device *dev;
755 struct hlist_head *head = dev_name_hash(net, name);
756
Sasha Levinb67bfe02013-02-27 17:06:00 -0800757 hlist_for_each_entry_rcu(dev, head, name_hlist)
Eric Dumazet72c95282009-10-30 07:11:27 +0000758 if (!strncmp(dev->name, name, IFNAMSIZ))
759 return dev;
760
761 return NULL;
762}
763EXPORT_SYMBOL(dev_get_by_name_rcu);
764
765/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700767 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * @name: name to find
769 *
770 * Find an interface by name. This can be called from any
771 * context and does its own locking. The returned handle has
772 * the usage count incremented and the caller must use dev_put() to
773 * release it when it is no longer needed. %NULL is returned if no
774 * matching device is found.
775 */
776
Eric W. Biederman881d9662007-09-17 11:56:21 -0700777struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct net_device *dev;
780
Eric Dumazet72c95282009-10-30 07:11:27 +0000781 rcu_read_lock();
782 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (dev)
784 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000785 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return dev;
787}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700788EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790/**
791 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700792 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * @ifindex: index of device
794 *
795 * Search for an interface by index. Returns %NULL if the device
796 * is not found or a pointer to the device. The device has not
797 * had its reference counter increased so the caller must be careful
798 * about locking. The caller must hold either the RTNL semaphore
799 * or @dev_base_lock.
800 */
801
Eric W. Biederman881d9662007-09-17 11:56:21 -0700802struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700804 struct net_device *dev;
805 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Sasha Levinb67bfe02013-02-27 17:06:00 -0800807 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (dev->ifindex == ifindex)
809 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return NULL;
812}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700813EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000815/**
816 * dev_get_by_index_rcu - find a device by its ifindex
817 * @net: the applicable net namespace
818 * @ifindex: index of device
819 *
820 * Search for an interface by index. Returns %NULL if the device
821 * is not found or a pointer to the device. The device has not
822 * had its reference counter increased so the caller must be careful
823 * about locking. The caller must hold RCU lock.
824 */
825
826struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
827{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000828 struct net_device *dev;
829 struct hlist_head *head = dev_index_hash(net, ifindex);
830
Sasha Levinb67bfe02013-02-27 17:06:00 -0800831 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000832 if (dev->ifindex == ifindex)
833 return dev;
834
835 return NULL;
836}
837EXPORT_SYMBOL(dev_get_by_index_rcu);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840/**
841 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700842 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * @ifindex: index of device
844 *
845 * Search for an interface by index. Returns NULL if the device
846 * is not found or a pointer to the device. The device returned has
847 * had a reference added and the pointer is safe until the user calls
848 * dev_put to indicate they have finished with it.
849 */
850
Eric W. Biederman881d9662007-09-17 11:56:21 -0700851struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct net_device *dev;
854
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000855 rcu_read_lock();
856 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (dev)
858 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000859 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return dev;
861}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700862EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864/**
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +0200865 * netdev_get_name - get a netdevice name, knowing its ifindex.
866 * @net: network namespace
867 * @name: a pointer to the buffer where the name will be stored.
868 * @ifindex: the ifindex of the interface to get the name from.
869 *
870 * The use of raw_seqcount_begin() and cond_resched() before
871 * retrying is required as we want to give the writers a chance
872 * to complete when CONFIG_PREEMPT is not set.
873 */
874int netdev_get_name(struct net *net, char *name, int ifindex)
875{
876 struct net_device *dev;
877 unsigned int seq;
878
879retry:
880 seq = raw_seqcount_begin(&devnet_rename_seq);
881 rcu_read_lock();
882 dev = dev_get_by_index_rcu(net, ifindex);
883 if (!dev) {
884 rcu_read_unlock();
885 return -ENODEV;
886 }
887
888 strcpy(name, dev->name);
889 rcu_read_unlock();
890 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
891 cond_resched();
892 goto retry;
893 }
894
895 return 0;
896}
897
898/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000899 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700900 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * @type: media type of device
902 * @ha: hardware address
903 *
904 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800905 * is not found or a pointer to the device.
906 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000907 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * and the caller must therefore be careful about locking
909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
911
Eric Dumazet941666c2010-12-05 01:23:53 +0000912struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
913 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct net_device *dev;
916
Eric Dumazet941666c2010-12-05 01:23:53 +0000917 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (dev->type == type &&
919 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700920 return dev;
921
922 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
Eric Dumazet941666c2010-12-05 01:23:53 +0000924EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300925
Eric W. Biederman881d9662007-09-17 11:56:21 -0700926struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700927{
928 struct net_device *dev;
929
930 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700931 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700932 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700933 return dev;
934
935 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700936}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700937EXPORT_SYMBOL(__dev_getfirstbyhwtype);
938
Eric W. Biederman881d9662007-09-17 11:56:21 -0700939struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000941 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000943 rcu_read_lock();
944 for_each_netdev_rcu(net, dev)
945 if (dev->type == type) {
946 dev_hold(dev);
947 ret = dev;
948 break;
949 }
950 rcu_read_unlock();
951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953EXPORT_SYMBOL(dev_getfirstbyhwtype);
954
955/**
WANG Cong6c555492014-09-11 15:35:09 -0700956 * __dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700957 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * @if_flags: IFF_* values
959 * @mask: bitmask of bits in if_flags to check
960 *
961 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000962 * is not found or a pointer to the device. Must be called inside
WANG Cong6c555492014-09-11 15:35:09 -0700963 * rtnl_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
965
WANG Cong6c555492014-09-11 15:35:09 -0700966struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
967 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700969 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
WANG Cong6c555492014-09-11 15:35:09 -0700971 ASSERT_RTNL();
972
Pavel Emelianov7562f872007-05-03 15:13:45 -0700973 ret = NULL;
WANG Cong6c555492014-09-11 15:35:09 -0700974 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700976 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
978 }
979 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700980 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
WANG Cong6c555492014-09-11 15:35:09 -0700982EXPORT_SYMBOL(__dev_get_by_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984/**
985 * dev_valid_name - check if name is okay for network device
986 * @name: name string
987 *
988 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700989 * to allow sysfs to work. We also disallow any kind of
990 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
David S. Miller95f050b2012-03-06 16:12:15 -0500992bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700994 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -0500995 return false;
Eric Dumazet0d1c0572018-04-05 06:39:26 -0700996 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -0500997 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700998 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -0500999 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001000
1001 while (*name) {
Matthew Thodea4176a92015-02-17 18:31:57 -06001002 if (*name == '/' || *name == ':' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -05001003 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -07001004 name++;
1005 }
David S. Miller95f050b2012-03-06 16:12:15 -05001006 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001008EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001011 * __dev_alloc_name - allocate a name for a device
1012 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001014 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 *
1016 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -07001017 * id. It scans list of devices to build up a free map, then chooses
1018 * the first empty slot. The caller must hold the dev_base or rtnl lock
1019 * while allocating the name and adding the device in order to avoid
1020 * duplicates.
1021 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1022 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
1024
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001025static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 const char *p;
1029 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001030 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct net_device *d;
1032
1033 p = strnchr(name, IFNAMSIZ-1, '%');
1034 if (p) {
1035 /*
1036 * Verify the string as this thing may have come from
1037 * the user. There must be either one "%d" and no other "%"
1038 * characters.
1039 */
1040 if (p[1] != 'd' || strchr(p + 2, '%'))
1041 return -EINVAL;
1042
1043 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001044 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!inuse)
1046 return -ENOMEM;
1047
Eric W. Biederman881d9662007-09-17 11:56:21 -07001048 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!sscanf(d->name, name, &i))
1050 continue;
1051 if (i < 0 || i >= max_netdevices)
1052 continue;
1053
1054 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001055 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!strncmp(buf, d->name, IFNAMSIZ))
1057 set_bit(i, inuse);
1058 }
1059
1060 i = find_first_zero_bit(inuse, max_netdevices);
1061 free_page((unsigned long) inuse);
1062 }
1063
Octavian Purdilad9031022009-11-18 02:36:59 +00001064 if (buf != name)
1065 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001066 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* It is possible to run out of possible slots
1070 * when the name is long and there isn't enough space left
1071 * for the digits, or if all bits are used.
1072 */
1073 return -ENFILE;
1074}
1075
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001076/**
1077 * dev_alloc_name - allocate a name for a device
1078 * @dev: device
1079 * @name: name format string
1080 *
1081 * Passed a format string - eg "lt%d" it will try and find a suitable
1082 * id. It scans list of devices to build up a free map, then chooses
1083 * the first empty slot. The caller must hold the dev_base or rtnl lock
1084 * while allocating the name and adding the device in order to avoid
1085 * duplicates.
1086 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1087 * Returns the number of the unit assigned or a negative errno code.
1088 */
1089
1090int dev_alloc_name(struct net_device *dev, const char *name)
1091{
1092 char buf[IFNAMSIZ];
1093 struct net *net;
1094 int ret;
1095
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001096 BUG_ON(!dev_net(dev));
1097 net = dev_net(dev);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001098 ret = __dev_alloc_name(net, name, buf);
1099 if (ret >= 0)
1100 strlcpy(dev->name, buf, IFNAMSIZ);
1101 return ret;
1102}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001103EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001104
Gao feng828de4f2012-09-13 20:58:27 +00001105static int dev_alloc_name_ns(struct net *net,
1106 struct net_device *dev,
1107 const char *name)
Octavian Purdilad9031022009-11-18 02:36:59 +00001108{
Gao feng828de4f2012-09-13 20:58:27 +00001109 char buf[IFNAMSIZ];
1110 int ret;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001111
Gao feng828de4f2012-09-13 20:58:27 +00001112 ret = __dev_alloc_name(net, name, buf);
1113 if (ret >= 0)
1114 strlcpy(dev->name, buf, IFNAMSIZ);
1115 return ret;
1116}
1117
Cong Wang3e2ab0c2017-10-13 11:58:53 -07001118int dev_get_valid_name(struct net *net, struct net_device *dev,
1119 const char *name)
Gao feng828de4f2012-09-13 20:58:27 +00001120{
1121 BUG_ON(!net);
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001122
Octavian Purdilad9031022009-11-18 02:36:59 +00001123 if (!dev_valid_name(name))
1124 return -EINVAL;
1125
Jiri Pirko1c5cae82011-04-30 01:21:32 +00001126 if (strchr(name, '%'))
Gao feng828de4f2012-09-13 20:58:27 +00001127 return dev_alloc_name_ns(net, dev, name);
Octavian Purdilad9031022009-11-18 02:36:59 +00001128 else if (__dev_get_by_name(net, name))
1129 return -EEXIST;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001130 else if (dev->name != name)
1131 strlcpy(dev->name, name, IFNAMSIZ);
Octavian Purdilad9031022009-11-18 02:36:59 +00001132
1133 return 0;
1134}
Cong Wang3e2ab0c2017-10-13 11:58:53 -07001135EXPORT_SYMBOL(dev_get_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137/**
1138 * dev_change_name - change name of a device
1139 * @dev: device
1140 * @newname: name (or format string) must be at least IFNAMSIZ
1141 *
1142 * Change name of a device, can pass format strings "eth%d".
1143 * for wildcarding.
1144 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001145int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Tom Gundersen238fa362014-07-14 16:37:23 +02001147 unsigned char old_assign_type;
Herbert Xufcc5a032007-07-30 17:03:38 -07001148 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001150 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001151 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001154 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001156 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (dev->flags & IFF_UP)
1158 return -EBUSY;
1159
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001160 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001161
1162 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001163 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001164 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001165 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001166
Herbert Xufcc5a032007-07-30 17:03:38 -07001167 memcpy(oldname, dev->name, IFNAMSIZ);
1168
Gao feng828de4f2012-09-13 20:58:27 +00001169 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001170 if (err < 0) {
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001171 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001172 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Veaceslav Falico6fe82a32014-07-17 20:33:32 +02001175 if (oldname[0] && !strchr(oldname, '%'))
1176 netdev_info(dev, "renamed from %s\n", oldname);
1177
Tom Gundersen238fa362014-07-14 16:37:23 +02001178 old_assign_type = dev->name_assign_type;
1179 dev->name_assign_type = NET_NAME_RENAMED;
1180
Herbert Xufcc5a032007-07-30 17:03:38 -07001181rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001182 ret = device_rename(&dev->dev, dev->name);
1183 if (ret) {
1184 memcpy(dev->name, oldname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001185 dev->name_assign_type = old_assign_type;
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001186 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001187 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001188 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001189
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001190 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001191
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001192 netdev_adjacent_rename_links(dev, oldname);
1193
Herbert Xu7f988ea2007-07-30 16:35:46 -07001194 write_lock_bh(&dev_base_lock);
Eric Dumazet372b2312011-05-17 13:56:59 -04001195 hlist_del_rcu(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001196 write_unlock_bh(&dev_base_lock);
1197
1198 synchronize_rcu();
1199
1200 write_lock_bh(&dev_base_lock);
1201 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001202 write_unlock_bh(&dev_base_lock);
1203
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001204 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001205 ret = notifier_to_errno(ret);
1206
1207 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001208 /* err >= 0 after dev_alloc_name() or stores the first errno */
1209 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001210 err = ret;
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001211 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001212 memcpy(dev->name, oldname, IFNAMSIZ);
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01001213 memcpy(oldname, newname, IFNAMSIZ);
Tom Gundersen238fa362014-07-14 16:37:23 +02001214 dev->name_assign_type = old_assign_type;
1215 old_assign_type = NET_NAME_RENAMED;
Herbert Xufcc5a032007-07-30 17:03:38 -07001216 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001217 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001218 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001219 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001220 }
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 return err;
1224}
1225
1226/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001227 * dev_set_alias - change ifalias of a device
1228 * @dev: device
1229 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001230 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001231 *
1232 * Set ifalias for a device,
1233 */
1234int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1235{
Alexey Khoroshilov7364e442012-08-08 00:33:25 +00001236 char *new_ifalias;
1237
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001238 ASSERT_RTNL();
1239
1240 if (len >= IFALIASZ)
1241 return -EINVAL;
1242
Oliver Hartkopp96ca4a22008-09-23 21:23:19 -07001243 if (!len) {
Sachin Kamat388dfc22012-11-20 00:57:04 +00001244 kfree(dev->ifalias);
1245 dev->ifalias = NULL;
Oliver Hartkopp96ca4a22008-09-23 21:23:19 -07001246 return 0;
1247 }
1248
Alexey Khoroshilov7364e442012-08-08 00:33:25 +00001249 new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1250 if (!new_ifalias)
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001251 return -ENOMEM;
Alexey Khoroshilov7364e442012-08-08 00:33:25 +00001252 dev->ifalias = new_ifalias;
Alexander Potapenkod2f459e2017-06-06 15:56:54 +02001253 memcpy(dev->ifalias, alias, len);
1254 dev->ifalias[len] = 0;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001255
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001256 return len;
1257}
1258
1259
1260/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001261 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001262 * @dev: device to cause notification
1263 *
1264 * Called to indicate a device has changed features.
1265 */
1266void netdev_features_change(struct net_device *dev)
1267{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001268 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001269}
1270EXPORT_SYMBOL(netdev_features_change);
1271
1272/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * netdev_state_change - device changes state
1274 * @dev: device to cause notification
1275 *
1276 * Called to indicate a device has changed state. This function calls
1277 * the notifier chains for netdev_chain and sends a NEWLINK message
1278 * to the routing socket.
1279 */
1280void netdev_state_change(struct net_device *dev)
1281{
1282 if (dev->flags & IFF_UP) {
Loic Prylli5495119462014-07-01 21:39:43 -07001283 struct netdev_notifier_change_info change_info;
1284
1285 change_info.flags_changed = 0;
1286 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
1287 &change_info.info);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001288 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001291EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Amerigo Wangee89bab2012-08-09 22:14:56 +00001293/**
1294 * netdev_notify_peers - notify network peers about existence of @dev
1295 * @dev: network device
1296 *
1297 * Generate traffic such that interested network peers are aware of
1298 * @dev, such as by generating a gratuitous ARP. This may be used when
1299 * a device wants to inform the rest of the network about some sort of
1300 * reconfiguration such as a failover event or virtual machine
1301 * migration.
1302 */
1303void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001304{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001305 rtnl_lock();
1306 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
Vlad Yasevich6c548e92017-03-14 08:58:08 -04001307 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
Amerigo Wangee89bab2012-08-09 22:14:56 +00001308 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001309}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001310EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001311
Patrick McHardybd380812010-02-26 06:34:53 +00001312static int __dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001314 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001315 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001317 ASSERT_RTNL();
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!netif_device_present(dev))
1320 return -ENODEV;
1321
Neil Hormanca99ca12013-02-05 08:05:43 +00001322 /* Block netpoll from trying to do any rx path servicing.
1323 * If we don't do this there is a chance ndo_poll_controller
1324 * or ndo_poll may be running while we open the device
1325 */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001326 netpoll_poll_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001327
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001328 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1329 ret = notifier_to_errno(ret);
1330 if (ret)
1331 return ret;
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001334
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001335 if (ops->ndo_validate_addr)
1336 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001337
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001338 if (!ret && ops->ndo_open)
1339 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Eric W. Biederman66b55522014-03-27 15:39:03 -07001341 netpoll_poll_enable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001342
Jeff Garzikbada3392007-10-23 20:19:37 -07001343 if (ret)
1344 clear_bit(__LINK_STATE_START, &dev->state);
1345 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 dev->flags |= IFF_UP;
Patrick McHardy4417da62007-06-27 01:28:10 -07001347 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001349 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return ret;
1353}
Patrick McHardybd380812010-02-26 06:34:53 +00001354
1355/**
1356 * dev_open - prepare an interface for use.
1357 * @dev: device to open
1358 *
1359 * Takes a device from down to up state. The device's private open
1360 * function is invoked and then the multicast lists are loaded. Finally
1361 * the device is moved into the up state and a %NETDEV_UP message is
1362 * sent to the netdev notifier chain.
1363 *
1364 * Calling this function on an active interface is a nop. On a failure
1365 * a negative errno code is returned.
1366 */
1367int dev_open(struct net_device *dev)
1368{
1369 int ret;
1370
Patrick McHardybd380812010-02-26 06:34:53 +00001371 if (dev->flags & IFF_UP)
1372 return 0;
1373
Patrick McHardybd380812010-02-26 06:34:53 +00001374 ret = __dev_open(dev);
1375 if (ret < 0)
1376 return ret;
1377
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001378 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Patrick McHardybd380812010-02-26 06:34:53 +00001379 call_netdevice_notifiers(NETDEV_UP, dev);
1380
1381 return ret;
1382}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001383EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Octavian Purdila44345722010-12-13 12:44:07 +00001385static int __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Octavian Purdila44345722010-12-13 12:44:07 +00001387 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001388
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001389 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001390 might_sleep();
1391
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001392 list_for_each_entry(dev, head, close_list) {
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001393 /* Temporarily disable netpoll until the interface is down */
Eric W. Biederman66b55522014-03-27 15:39:03 -07001394 netpoll_poll_disable(dev);
Eric W. Biederman3f4df202014-03-27 15:38:17 -07001395
Octavian Purdila44345722010-12-13 12:44:07 +00001396 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Octavian Purdila44345722010-12-13 12:44:07 +00001398 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Octavian Purdila44345722010-12-13 12:44:07 +00001400 /* Synchronize to scheduled poll. We cannot touch poll list, it
1401 * can be even on different cpu. So just clear netif_running().
1402 *
1403 * dev->stop() will invoke napi_disable() on all of it's
1404 * napi_struct instances on this device.
1405 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001406 smp_mb__after_atomic(); /* Commit netif_running(). */
Octavian Purdila44345722010-12-13 12:44:07 +00001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Octavian Purdila44345722010-12-13 12:44:07 +00001409 dev_deactivate_many(head);
1410
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001411 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001412 const struct net_device_ops *ops = dev->netdev_ops;
1413
1414 /*
1415 * Call the device specific close. This cannot fail.
1416 * Only if device is UP
1417 *
1418 * We allow it to be called even after a DETACH hot-plug
1419 * event.
1420 */
1421 if (ops->ndo_stop)
1422 ops->ndo_stop(dev);
1423
Octavian Purdila44345722010-12-13 12:44:07 +00001424 dev->flags &= ~IFF_UP;
Eric W. Biederman66b55522014-03-27 15:39:03 -07001425 netpoll_poll_enable(dev);
Octavian Purdila44345722010-12-13 12:44:07 +00001426 }
1427
1428 return 0;
1429}
1430
1431static int __dev_close(struct net_device *dev)
1432{
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001433 int retval;
Octavian Purdila44345722010-12-13 12:44:07 +00001434 LIST_HEAD(single);
1435
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001436 list_add(&dev->close_list, &single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001437 retval = __dev_close_many(&single);
1438 list_del(&single);
Neil Hormanca99ca12013-02-05 08:05:43 +00001439
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001440 return retval;
Octavian Purdila44345722010-12-13 12:44:07 +00001441}
1442
David S. Miller99c4a262015-03-18 22:52:33 -04001443int dev_close_many(struct list_head *head, bool unlink)
Octavian Purdila44345722010-12-13 12:44:07 +00001444{
1445 struct net_device *dev, *tmp;
Octavian Purdila44345722010-12-13 12:44:07 +00001446
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001447 /* Remove the devices that don't need to be closed */
1448 list_for_each_entry_safe(dev, tmp, head, close_list)
Octavian Purdila44345722010-12-13 12:44:07 +00001449 if (!(dev->flags & IFF_UP))
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001450 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001451
1452 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001453
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001454 list_for_each_entry_safe(dev, tmp, head, close_list) {
Alexei Starovoitov7f294052013-10-23 16:02:42 -07001455 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
Octavian Purdila44345722010-12-13 12:44:07 +00001456 call_netdevice_notifiers(NETDEV_DOWN, dev);
David S. Miller99c4a262015-03-18 22:52:33 -04001457 if (unlink)
1458 list_del_init(&dev->close_list);
Octavian Purdila44345722010-12-13 12:44:07 +00001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
David S. Miller99c4a262015-03-18 22:52:33 -04001463EXPORT_SYMBOL(dev_close_many);
Patrick McHardybd380812010-02-26 06:34:53 +00001464
1465/**
1466 * dev_close - shutdown an interface.
1467 * @dev: device to shutdown
1468 *
1469 * This function moves an active device into down state. A
1470 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1471 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1472 * chain.
1473 */
1474int dev_close(struct net_device *dev)
1475{
Eric Dumazete14a5992011-05-10 12:26:06 -07001476 if (dev->flags & IFF_UP) {
1477 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001478
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001479 list_add(&dev->close_list, &single);
David S. Miller99c4a262015-03-18 22:52:33 -04001480 dev_close_many(&single, true);
Eric Dumazete14a5992011-05-10 12:26:06 -07001481 list_del(&single);
1482 }
dingtianhongda6e3782013-05-27 19:53:31 +00001483 return 0;
Patrick McHardybd380812010-02-26 06:34:53 +00001484}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001485EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001488/**
1489 * dev_disable_lro - disable Large Receive Offload on a device
1490 * @dev: device
1491 *
1492 * Disable Large Receive Offload (LRO) on a net device. Must be
1493 * called under RTNL. This is needed if received packets may be
1494 * forwarded to another interface.
1495 */
1496void dev_disable_lro(struct net_device *dev)
1497{
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001498 struct net_device *lower_dev;
1499 struct list_head *iter;
Michal Kubeček529d0482013-11-15 06:18:50 +01001500
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001501 dev->wanted_features &= ~NETIF_F_LRO;
1502 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001503
Michał Mirosław22d59692011-04-21 12:42:15 +00001504 if (unlikely(dev->features & NETIF_F_LRO))
1505 netdev_WARN(dev, "failed to disable LRO!\n");
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001506
1507 netdev_for_each_lower_dev(dev, lower_dev, iter)
1508 dev_disable_lro(lower_dev);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001509}
1510EXPORT_SYMBOL(dev_disable_lro);
1511
Jiri Pirko351638e2013-05-28 01:30:21 +00001512static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1513 struct net_device *dev)
1514{
1515 struct netdev_notifier_info info;
1516
1517 netdev_notifier_info_init(&info, dev);
1518 return nb->notifier_call(nb, val, &info);
1519}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001520
Eric W. Biederman881d9662007-09-17 11:56:21 -07001521static int dev_boot_phase = 1;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523/**
1524 * register_netdevice_notifier - register a network notifier block
1525 * @nb: notifier
1526 *
1527 * Register a notifier to be called when network device events occur.
1528 * The notifier passed is linked into the kernel structures and must
1529 * not be reused until it has been unregistered. A negative errno code
1530 * is returned on a failure.
1531 *
1532 * When registered all registration and up events are replayed
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001533 * to the new notifier to allow device to have a race free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * view of the network device list.
1535 */
1536
1537int register_netdevice_notifier(struct notifier_block *nb)
1538{
1539 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001540 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001541 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 int err;
1543
1544 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001545 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001546 if (err)
1547 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001548 if (dev_boot_phase)
1549 goto unlock;
1550 for_each_net(net) {
1551 for_each_netdev(net, dev) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001552 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001553 err = notifier_to_errno(err);
1554 if (err)
1555 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Eric W. Biederman881d9662007-09-17 11:56:21 -07001557 if (!(dev->flags & IFF_UP))
1558 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001559
Jiri Pirko351638e2013-05-28 01:30:21 +00001560 call_netdevice_notifier(nb, NETDEV_UP, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001563
1564unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 rtnl_unlock();
1566 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001567
1568rollback:
1569 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001570 for_each_net(net) {
1571 for_each_netdev(net, dev) {
1572 if (dev == last)
RongQing.Li8f891482011-11-30 23:43:07 -05001573 goto outroll;
Herbert Xufcc5a032007-07-30 17:03:38 -07001574
Eric W. Biederman881d9662007-09-17 11:56:21 -07001575 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001576 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1577 dev);
1578 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001579 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001580 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001581 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001582 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001583
RongQing.Li8f891482011-11-30 23:43:07 -05001584outroll:
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001585 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001586 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001588EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590/**
1591 * unregister_netdevice_notifier - unregister a network notifier block
1592 * @nb: notifier
1593 *
1594 * Unregister a notifier previously registered by
1595 * register_netdevice_notifier(). The notifier is unlinked into the
1596 * kernel structures and may then be reused. A negative errno code
1597 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001598 *
1599 * After unregistering unregister and down device events are synthesized
1600 * for all devices on the device list to the removed notifier to remove
1601 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
1603
1604int unregister_netdevice_notifier(struct notifier_block *nb)
1605{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001606 struct net_device *dev;
1607 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001608 int err;
1609
1610 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001611 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001612 if (err)
1613 goto unlock;
1614
1615 for_each_net(net) {
1616 for_each_netdev(net, dev) {
1617 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001618 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1619 dev);
1620 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001621 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001622 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001623 }
1624 }
1625unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001626 rtnl_unlock();
1627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001629EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001632 * call_netdevice_notifiers_info - call all network notifier blocks
1633 * @val: value passed unmodified to notifier function
1634 * @dev: net_device pointer passed unmodified to notifier function
1635 * @info: notifier information data
1636 *
1637 * Call all network notifier blocks. Parameters and return value
1638 * are as for raw_notifier_call_chain().
1639 */
1640
stephen hemminger1d143d92013-12-29 14:01:29 -08001641static int call_netdevice_notifiers_info(unsigned long val,
1642 struct net_device *dev,
1643 struct netdev_notifier_info *info)
Jiri Pirko351638e2013-05-28 01:30:21 +00001644{
1645 ASSERT_RTNL();
1646 netdev_notifier_info_init(info, dev);
1647 return raw_notifier_call_chain(&netdev_chain, val, info);
1648}
Jiri Pirko351638e2013-05-28 01:30:21 +00001649
1650/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * call_netdevice_notifiers - call all network notifier blocks
1652 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001653 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *
1655 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001656 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 */
1658
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001659int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Jiri Pirko351638e2013-05-28 01:30:21 +00001661 struct netdev_notifier_info info;
1662
1663 return call_netdevice_notifiers_info(val, dev, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001665EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Sabrina Dubroca8d59c3a2018-10-09 17:48:14 +02001667/**
1668 * call_netdevice_notifiers_mtu - call all network notifier blocks
1669 * @val: value passed unmodified to notifier function
1670 * @dev: net_device pointer passed unmodified to notifier function
1671 * @arg: additional u32 argument passed to the notifier function
1672 *
1673 * Call all network notifier blocks. Parameters and return value
1674 * are as for raw_notifier_call_chain().
1675 */
1676static int call_netdevice_notifiers_mtu(unsigned long val,
1677 struct net_device *dev, u32 arg)
1678{
1679 struct netdev_notifier_info_ext info = {
1680 .info.dev = dev,
1681 .ext.mtu = arg,
1682 };
1683
1684 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1685
1686 return call_netdevice_notifiers_info(val, dev, &info.info);
1687}
1688
Pablo Neira1cf519002015-05-13 18:19:37 +02001689#ifdef CONFIG_NET_INGRESS
Daniel Borkmann45771392015-04-10 23:07:54 +02001690static struct static_key ingress_needed __read_mostly;
1691
1692void net_inc_ingress_queue(void)
1693{
1694 static_key_slow_inc(&ingress_needed);
1695}
1696EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1697
1698void net_dec_ingress_queue(void)
1699{
1700 static_key_slow_dec(&ingress_needed);
1701}
1702EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1703#endif
1704
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001705#ifdef CONFIG_NET_EGRESS
1706static struct static_key egress_needed __read_mostly;
1707
1708void net_inc_egress_queue(void)
1709{
1710 static_key_slow_inc(&egress_needed);
1711}
1712EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1713
1714void net_dec_egress_queue(void)
1715{
1716 static_key_slow_dec(&egress_needed);
1717}
1718EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1719#endif
1720
Ingo Molnarc5905af2012-02-24 08:31:31 +01001721static struct static_key netstamp_needed __read_mostly;
Eric Dumazetb90e5792011-11-28 11:16:50 +00001722#ifdef HAVE_JUMP_LABEL
Eric Dumazetb90e5792011-11-28 11:16:50 +00001723static atomic_t netstamp_needed_deferred;
Eric Dumazet3d87dce2017-03-01 14:28:39 -08001724static atomic_t netstamp_wanted;
Eric Dumazetd5b6fd72017-02-02 10:31:35 -08001725static void netstamp_clear(struct work_struct *work)
1726{
1727 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
Eric Dumazet3d87dce2017-03-01 14:28:39 -08001728 int wanted;
Eric Dumazetd5b6fd72017-02-02 10:31:35 -08001729
Eric Dumazet3d87dce2017-03-01 14:28:39 -08001730 wanted = atomic_add_return(deferred, &netstamp_wanted);
1731 if (wanted > 0)
1732 static_key_enable(&netstamp_needed);
1733 else
1734 static_key_disable(&netstamp_needed);
Eric Dumazetd5b6fd72017-02-02 10:31:35 -08001735}
1736static DECLARE_WORK(netstamp_work, netstamp_clear);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739void net_enable_timestamp(void)
1740{
Eric Dumazet3d87dce2017-03-01 14:28:39 -08001741#ifdef HAVE_JUMP_LABEL
1742 int wanted;
1743
1744 while (1) {
1745 wanted = atomic_read(&netstamp_wanted);
1746 if (wanted <= 0)
1747 break;
1748 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1749 return;
1750 }
1751 atomic_inc(&netstamp_needed_deferred);
1752 schedule_work(&netstamp_work);
1753#else
Ingo Molnarc5905af2012-02-24 08:31:31 +01001754 static_key_slow_inc(&netstamp_needed);
Eric Dumazet3d87dce2017-03-01 14:28:39 -08001755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001757EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759void net_disable_timestamp(void)
1760{
Eric Dumazetb90e5792011-11-28 11:16:50 +00001761#ifdef HAVE_JUMP_LABEL
Eric Dumazet3d87dce2017-03-01 14:28:39 -08001762 int wanted;
1763
1764 while (1) {
1765 wanted = atomic_read(&netstamp_wanted);
1766 if (wanted <= 1)
1767 break;
1768 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1769 return;
1770 }
1771 atomic_dec(&netstamp_needed_deferred);
Eric Dumazetd5b6fd72017-02-02 10:31:35 -08001772 schedule_work(&netstamp_work);
1773#else
Ingo Molnarc5905af2012-02-24 08:31:31 +01001774 static_key_slow_dec(&netstamp_needed);
Eric Dumazetd5b6fd72017-02-02 10:31:35 -08001775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001777EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Eric Dumazet3b098e22010-05-15 23:57:10 -07001779static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Eric Dumazet588f0332011-11-15 04:12:55 +00001781 skb->tstamp.tv64 = 0;
Ingo Molnarc5905af2012-02-24 08:31:31 +01001782 if (static_key_false(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001783 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Eric Dumazet588f0332011-11-15 04:12:55 +00001786#define net_timestamp_check(COND, SKB) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001787 if (static_key_false(&netstamp_needed)) { \
Eric Dumazet588f0332011-11-15 04:12:55 +00001788 if ((COND) && !(SKB)->tstamp.tv64) \
1789 __net_timestamp(SKB); \
1790 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07001791
Nikolay Aleksandrovf4b05d22016-04-28 17:59:28 +02001792bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001793{
1794 unsigned int len;
1795
1796 if (!(dev->flags & IFF_UP))
1797 return false;
1798
1799 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1800 if (skb->len <= len)
1801 return true;
1802
1803 /* if TSO is enabled, we don't care about the length as the packet
1804 * could be forwarded without being segmented before
1805 */
1806 if (skb_is_gso(skb))
1807 return true;
1808
1809 return false;
1810}
Vlad Yasevich1ee481f2014-03-27 17:32:29 -04001811EXPORT_SYMBOL_GPL(is_skb_forwardable);
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001812
Herbert Xua0265d22014-04-17 13:45:03 +08001813int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1814{
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001815 int ret = ____dev_forward_skb(dev, skb);
1816
1817 if (likely(!ret)) {
1818 skb->protocol = eth_type_trans(skb, dev);
1819 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Herbert Xua0265d22014-04-17 13:45:03 +08001820 }
1821
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001822 return ret;
Herbert Xua0265d22014-04-17 13:45:03 +08001823}
1824EXPORT_SYMBOL_GPL(__dev_forward_skb);
1825
Arnd Bergmann44540962009-11-26 06:07:08 +00001826/**
1827 * dev_forward_skb - loopback an skb to another netif
1828 *
1829 * @dev: destination network device
1830 * @skb: buffer to forward
1831 *
1832 * return values:
1833 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001834 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001835 *
1836 * dev_forward_skb can be used for injecting an skb from the
1837 * start_xmit function of one device into the receive queue
1838 * of another device.
1839 *
1840 * The receiving device may be in another namespace, so
1841 * we have to clear all information in the skb that could
1842 * impact namespace isolation.
1843 */
1844int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1845{
Herbert Xua0265d22014-04-17 13:45:03 +08001846 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001847}
1848EXPORT_SYMBOL_GPL(dev_forward_skb);
1849
Changli Gao71d9dec2010-12-15 19:57:25 +00001850static inline int deliver_skb(struct sk_buff *skb,
1851 struct packet_type *pt_prev,
1852 struct net_device *orig_dev)
1853{
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00001854 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
1855 return -ENOMEM;
Changli Gao71d9dec2010-12-15 19:57:25 +00001856 atomic_inc(&skb->users);
1857 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1858}
1859
Salam Noureddine7866a622015-01-27 11:35:48 -08001860static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1861 struct packet_type **pt,
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001862 struct net_device *orig_dev,
1863 __be16 type,
Salam Noureddine7866a622015-01-27 11:35:48 -08001864 struct list_head *ptype_list)
1865{
1866 struct packet_type *ptype, *pt_prev = *pt;
1867
1868 list_for_each_entry_rcu(ptype, ptype_list, list) {
1869 if (ptype->type != type)
1870 continue;
1871 if (pt_prev)
Jiri Pirkofbcb2172015-03-30 16:56:01 +02001872 deliver_skb(skb, pt_prev, orig_dev);
Salam Noureddine7866a622015-01-27 11:35:48 -08001873 pt_prev = ptype;
1874 }
1875 *pt = pt_prev;
1876}
1877
Eric Leblondc0de08d2012-08-16 22:02:58 +00001878static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1879{
Eric Leblonda3d744e2012-11-06 02:10:10 +00001880 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001881 return false;
1882
1883 if (ptype->id_match)
1884 return ptype->id_match(ptype, skb->sk);
1885 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1886 return true;
1887
1888 return false;
1889}
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891/*
1892 * Support routine. Sends outgoing frames to any network
1893 * taps currently in use.
1894 */
1895
David Ahern74b20582016-05-10 11:19:50 -07001896void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00001899 struct sk_buff *skb2 = NULL;
1900 struct packet_type *pt_prev = NULL;
Salam Noureddine7866a622015-01-27 11:35:48 -08001901 struct list_head *ptype_list = &ptype_all;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 rcu_read_lock();
Salam Noureddine7866a622015-01-27 11:35:48 -08001904again:
1905 list_for_each_entry_rcu(ptype, ptype_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* Never send packets back to the socket
1907 * they originated from - MvS (miquels@drinkel.ow.org)
1908 */
Salam Noureddine7866a622015-01-27 11:35:48 -08001909 if (skb_loop_sk(ptype, skb))
1910 continue;
Changli Gao71d9dec2010-12-15 19:57:25 +00001911
Salam Noureddine7866a622015-01-27 11:35:48 -08001912 if (pt_prev) {
1913 deliver_skb(skb2, pt_prev, skb->dev);
Changli Gao71d9dec2010-12-15 19:57:25 +00001914 pt_prev = ptype;
Salam Noureddine7866a622015-01-27 11:35:48 -08001915 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Salam Noureddine7866a622015-01-27 11:35:48 -08001917
1918 /* need to clone skb, done only once */
1919 skb2 = skb_clone(skb, GFP_ATOMIC);
1920 if (!skb2)
1921 goto out_unlock;
1922
1923 net_timestamp_set(skb2);
1924
1925 /* skb->nh should be correctly
1926 * set by sender, so that the second statement is
1927 * just protection against buggy protocols.
1928 */
1929 skb_reset_mac_header(skb2);
1930
1931 if (skb_network_header(skb2) < skb2->data ||
1932 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
1933 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1934 ntohs(skb2->protocol),
1935 dev->name);
1936 skb_reset_network_header(skb2);
1937 }
1938
1939 skb2->transport_header = skb2->network_header;
1940 skb2->pkt_type = PACKET_OUTGOING;
1941 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Salam Noureddine7866a622015-01-27 11:35:48 -08001943
1944 if (ptype_list == &ptype_all) {
1945 ptype_list = &dev->ptype_all;
1946 goto again;
1947 }
1948out_unlock:
Changli Gao71d9dec2010-12-15 19:57:25 +00001949 if (pt_prev)
1950 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 rcu_read_unlock();
1952}
David Ahern74b20582016-05-10 11:19:50 -07001953EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Ben Hutchings2c530402012-07-10 10:55:09 +00001955/**
1956 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00001957 * @dev: Network device
1958 * @txq: number of queues available
1959 *
1960 * If real_num_tx_queues is changed the tc mappings may no longer be
1961 * valid. To resolve this verify the tc mapping remains valid and if
1962 * not NULL the mapping. With no priorities mapping to this
1963 * offset/count pair it will no longer be used. In the worst case TC0
1964 * is invalid nothing can be done so disable priority mappings. If is
1965 * expected that drivers will fix this mapping if they can before
1966 * calling netif_set_real_num_tx_queues.
1967 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00001968static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00001969{
1970 int i;
1971 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1972
1973 /* If TC0 is invalidated disable TC mapping */
1974 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001975 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00001976 dev->num_tc = 0;
1977 return;
1978 }
1979
1980 /* Invalidated prio to tc mappings set to TC0 */
1981 for (i = 1; i < TC_BITMASK + 1; i++) {
1982 int q = netdev_get_prio_tc_map(dev, i);
1983
1984 tc = &dev->tc_to_txq[q];
1985 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001986 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
1987 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00001988 netdev_set_prio_tc_map(dev, i, 0);
1989 }
1990 }
1991}
1992
Alexander Duyck537c00d2013-01-10 08:57:02 +00001993#ifdef CONFIG_XPS
1994static DEFINE_MUTEX(xps_map_mutex);
1995#define xmap_dereference(P) \
1996 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1997
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00001998static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps,
1999 int cpu, u16 index)
2000{
2001 struct xps_map *map = NULL;
2002 int pos;
2003
2004 if (dev_maps)
2005 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2006
2007 for (pos = 0; map && pos < map->len; pos++) {
2008 if (map->queues[pos] == index) {
2009 if (map->len > 1) {
2010 map->queues[pos] = map->queues[--map->len];
2011 } else {
2012 RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL);
2013 kfree_rcu(map, rcu);
2014 map = NULL;
2015 }
2016 break;
2017 }
2018 }
2019
2020 return map;
2021}
2022
Alexander Duyck024e9672013-01-10 08:57:46 +00002023static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002024{
2025 struct xps_dev_maps *dev_maps;
Alexander Duyck024e9672013-01-10 08:57:46 +00002026 int cpu, i;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002027 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002028
2029 mutex_lock(&xps_map_mutex);
2030 dev_maps = xmap_dereference(dev->xps_maps);
2031
2032 if (!dev_maps)
2033 goto out_no_maps;
2034
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002035 for_each_possible_cpu(cpu) {
Alexander Duyck024e9672013-01-10 08:57:46 +00002036 for (i = index; i < dev->num_tx_queues; i++) {
2037 if (!remove_xps_queue(dev_maps, cpu, i))
2038 break;
2039 }
2040 if (i == dev->num_tx_queues)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002041 active = true;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002042 }
2043
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00002044 if (!active) {
Alexander Duyck537c00d2013-01-10 08:57:02 +00002045 RCU_INIT_POINTER(dev->xps_maps, NULL);
2046 kfree_rcu(dev_maps, rcu);
2047 }
2048
Alexander Duyck024e9672013-01-10 08:57:46 +00002049 for (i = index; i < dev->num_tx_queues; i++)
2050 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
2051 NUMA_NO_NODE);
2052
Alexander Duyck537c00d2013-01-10 08:57:02 +00002053out_no_maps:
2054 mutex_unlock(&xps_map_mutex);
2055}
2056
Alexander Duyck01c5f862013-01-10 08:57:35 +00002057static struct xps_map *expand_xps_map(struct xps_map *map,
2058 int cpu, u16 index)
2059{
2060 struct xps_map *new_map;
2061 int alloc_len = XPS_MIN_MAP_ALLOC;
2062 int i, pos;
2063
2064 for (pos = 0; map && pos < map->len; pos++) {
2065 if (map->queues[pos] != index)
2066 continue;
2067 return map;
2068 }
2069
2070 /* Need to add queue to this CPU's existing map */
2071 if (map) {
2072 if (pos < map->alloc_len)
2073 return map;
2074
2075 alloc_len = map->alloc_len * 2;
2076 }
2077
2078 /* Need to allocate new map to store queue on this CPU's map */
2079 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2080 cpu_to_node(cpu));
2081 if (!new_map)
2082 return NULL;
2083
2084 for (i = 0; i < pos; i++)
2085 new_map->queues[i] = map->queues[i];
2086 new_map->alloc_len = alloc_len;
2087 new_map->len = pos;
2088
2089 return new_map;
2090}
2091
Michael S. Tsirkin35735402013-10-02 09:14:06 +03002092int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2093 u16 index)
Alexander Duyck537c00d2013-01-10 08:57:02 +00002094{
Alexander Duyck01c5f862013-01-10 08:57:35 +00002095 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002096 struct xps_map *map, *new_map;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002097 int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002098 int cpu, numa_node_id = -2;
2099 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002100
2101 mutex_lock(&xps_map_mutex);
2102
2103 dev_maps = xmap_dereference(dev->xps_maps);
2104
Alexander Duyck01c5f862013-01-10 08:57:35 +00002105 /* allocate memory for queue storage */
2106 for_each_online_cpu(cpu) {
2107 if (!cpumask_test_cpu(cpu, mask))
2108 continue;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002109
Alexander Duyck01c5f862013-01-10 08:57:35 +00002110 if (!new_dev_maps)
2111 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002112 if (!new_dev_maps) {
2113 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002114 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00002115 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002116
2117 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
2118 NULL;
2119
2120 map = expand_xps_map(map, cpu, index);
2121 if (!map)
2122 goto error;
2123
2124 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
2125 }
2126
2127 if (!new_dev_maps)
2128 goto out_no_new_maps;
2129
2130 for_each_possible_cpu(cpu) {
2131 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
2132 /* add queue to CPU maps */
2133 int pos = 0;
2134
2135 map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2136 while ((pos < map->len) && (map->queues[pos] != index))
2137 pos++;
2138
2139 if (pos == map->len)
2140 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002141#ifdef CONFIG_NUMA
Alexander Duyck537c00d2013-01-10 08:57:02 +00002142 if (numa_node_id == -2)
2143 numa_node_id = cpu_to_node(cpu);
2144 else if (numa_node_id != cpu_to_node(cpu))
2145 numa_node_id = -1;
Alexander Duyck537c00d2013-01-10 08:57:02 +00002146#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00002147 } else if (dev_maps) {
2148 /* fill in the new device map from the old device map */
2149 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2150 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00002151 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00002152
Alexander Duyck537c00d2013-01-10 08:57:02 +00002153 }
2154
Alexander Duyck01c5f862013-01-10 08:57:35 +00002155 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2156
Alexander Duyck537c00d2013-01-10 08:57:02 +00002157 /* Cleanup old maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00002158 if (dev_maps) {
2159 for_each_possible_cpu(cpu) {
2160 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2161 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2162 if (map && map != new_map)
2163 kfree_rcu(map, rcu);
2164 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002165
Alexander Duyck537c00d2013-01-10 08:57:02 +00002166 kfree_rcu(dev_maps, rcu);
Alexander Duyck01c5f862013-01-10 08:57:35 +00002167 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00002168
Alexander Duyck01c5f862013-01-10 08:57:35 +00002169 dev_maps = new_dev_maps;
2170 active = true;
2171
2172out_no_new_maps:
2173 /* update Tx queue numa node */
Alexander Duyck537c00d2013-01-10 08:57:02 +00002174 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2175 (numa_node_id >= 0) ? numa_node_id :
2176 NUMA_NO_NODE);
2177
Alexander Duyck01c5f862013-01-10 08:57:35 +00002178 if (!dev_maps)
2179 goto out_no_maps;
2180
2181 /* removes queue from unused CPUs */
2182 for_each_possible_cpu(cpu) {
2183 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu))
2184 continue;
2185
2186 if (remove_xps_queue(dev_maps, cpu, index))
2187 active = true;
2188 }
2189
2190 /* free map if not active */
2191 if (!active) {
2192 RCU_INIT_POINTER(dev->xps_maps, NULL);
2193 kfree_rcu(dev_maps, rcu);
2194 }
2195
2196out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00002197 mutex_unlock(&xps_map_mutex);
2198
2199 return 0;
2200error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00002201 /* remove any maps that we added */
2202 for_each_possible_cpu(cpu) {
2203 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2204 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
2205 NULL;
2206 if (new_map && new_map != map)
2207 kfree(new_map);
2208 }
2209
Alexander Duyck537c00d2013-01-10 08:57:02 +00002210 mutex_unlock(&xps_map_mutex);
2211
Alexander Duyck537c00d2013-01-10 08:57:02 +00002212 kfree(new_dev_maps);
2213 return -ENOMEM;
2214}
2215EXPORT_SYMBOL(netif_set_xps_queue);
2216
2217#endif
John Fastabendf0796d52010-07-01 13:21:57 +00002218/*
2219 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2220 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2221 */
Tom Herberte6484932010-10-18 18:04:39 +00002222int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002223{
Jakub Kicinski106dc962018-02-12 21:35:31 -08002224 bool disabling;
Tom Herbert1d24eb42010-11-21 13:17:27 +00002225 int rc;
2226
Jakub Kicinski106dc962018-02-12 21:35:31 -08002227 disabling = txq < dev->real_num_tx_queues;
2228
Tom Herberte6484932010-10-18 18:04:39 +00002229 if (txq < 1 || txq > dev->num_tx_queues)
2230 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002231
Ben Hutchings5c565802011-02-15 19:39:21 +00002232 if (dev->reg_state == NETREG_REGISTERED ||
2233 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002234 ASSERT_RTNL();
2235
Tom Herbert1d24eb42010-11-21 13:17:27 +00002236 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2237 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002238 if (rc)
2239 return rc;
2240
John Fastabend4f57c082011-01-17 08:06:04 +00002241 if (dev->num_tc)
2242 netif_setup_tc(dev, txq);
2243
Jakub Kicinski106dc962018-02-12 21:35:31 -08002244 dev->real_num_tx_queues = txq;
2245
2246 if (disabling) {
2247 synchronize_net();
Tom Herberte6484932010-10-18 18:04:39 +00002248 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002249#ifdef CONFIG_XPS
2250 netif_reset_xps_queues_gt(dev, txq);
2251#endif
2252 }
Jakub Kicinski106dc962018-02-12 21:35:31 -08002253 } else {
2254 dev->real_num_tx_queues = txq;
John Fastabendf0796d52010-07-01 13:21:57 +00002255 }
Tom Herberte6484932010-10-18 18:04:39 +00002256
Tom Herberte6484932010-10-18 18:04:39 +00002257 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002258}
2259EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002260
Michael Daltona953be52014-01-16 22:23:28 -08002261#ifdef CONFIG_SYSFS
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002262/**
2263 * netif_set_real_num_rx_queues - set actual number of RX queues used
2264 * @dev: Network device
2265 * @rxq: Actual number of RX queues
2266 *
2267 * This must be called either with the rtnl_lock held or before
2268 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002269 * negative error code. If called before registration, it always
2270 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002271 */
2272int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2273{
2274 int rc;
2275
Tom Herbertbd25fa72010-10-18 18:00:16 +00002276 if (rxq < 1 || rxq > dev->num_rx_queues)
2277 return -EINVAL;
2278
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002279 if (dev->reg_state == NETREG_REGISTERED) {
2280 ASSERT_RTNL();
2281
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002282 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2283 rxq);
2284 if (rc)
2285 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002286 }
2287
2288 dev->real_num_rx_queues = rxq;
2289 return 0;
2290}
2291EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2292#endif
2293
Ben Hutchings2c530402012-07-10 10:55:09 +00002294/**
2295 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002296 *
2297 * This routine should set an upper limit on the number of RSS queues
2298 * used by default by multiqueue devices.
2299 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002300int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002301{
Hariprasad Shenai40e4e712016-06-08 18:09:08 +05302302 return is_kdump_kernel() ?
2303 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
Yuval Mintz16917b82012-07-01 03:18:50 +00002304}
2305EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2306
Eric Dumazet3bcb8462016-06-04 20:02:28 -07002307static void __netif_reschedule(struct Qdisc *q)
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002308{
2309 struct softnet_data *sd;
2310 unsigned long flags;
2311
2312 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05002313 sd = this_cpu_ptr(&softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002314 q->next_sched = NULL;
2315 *sd->output_queue_tailp = q;
2316 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002317 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2318 local_irq_restore(flags);
2319}
2320
David S. Miller37437bb2008-07-16 02:15:04 -07002321void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002322{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002323 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2324 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002325}
2326EXPORT_SYMBOL(__netif_schedule);
2327
Eric Dumazete6247022013-12-05 04:45:08 -08002328struct dev_kfree_skb_cb {
2329 enum skb_free_reason reason;
2330};
2331
2332static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002333{
Eric Dumazete6247022013-12-05 04:45:08 -08002334 return (struct dev_kfree_skb_cb *)skb->cb;
Denis Vlasenko56079432006-03-29 15:57:29 -08002335}
Denis Vlasenko56079432006-03-29 15:57:29 -08002336
John Fastabend46e5da42014-09-12 20:04:52 -07002337void netif_schedule_queue(struct netdev_queue *txq)
2338{
2339 rcu_read_lock();
2340 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2341 struct Qdisc *q = rcu_dereference(txq->qdisc);
2342
2343 __netif_schedule(q);
2344 }
2345 rcu_read_unlock();
2346}
2347EXPORT_SYMBOL(netif_schedule_queue);
2348
2349/**
2350 * netif_wake_subqueue - allow sending packets on subqueue
2351 * @dev: network device
2352 * @queue_index: sub queue index
2353 *
2354 * Resume individual transmit queue of a device with multiple transmit queues.
2355 */
2356void netif_wake_subqueue(struct net_device *dev, u16 queue_index)
2357{
2358 struct netdev_queue *txq = netdev_get_tx_queue(dev, queue_index);
2359
2360 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &txq->state)) {
2361 struct Qdisc *q;
2362
2363 rcu_read_lock();
2364 q = rcu_dereference(txq->qdisc);
2365 __netif_schedule(q);
2366 rcu_read_unlock();
2367 }
2368}
2369EXPORT_SYMBOL(netif_wake_subqueue);
2370
2371void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2372{
2373 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2374 struct Qdisc *q;
2375
2376 rcu_read_lock();
2377 q = rcu_dereference(dev_queue->qdisc);
2378 __netif_schedule(q);
2379 rcu_read_unlock();
2380 }
2381}
2382EXPORT_SYMBOL(netif_tx_wake_queue);
2383
Eric Dumazete6247022013-12-05 04:45:08 -08002384void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2385{
2386 unsigned long flags;
2387
Myungho Jung1dee03a2017-04-25 11:58:15 -07002388 if (unlikely(!skb))
2389 return;
2390
Eric Dumazete6247022013-12-05 04:45:08 -08002391 if (likely(atomic_read(&skb->users) == 1)) {
2392 smp_rmb();
2393 atomic_set(&skb->users, 0);
2394 } else if (likely(!atomic_dec_and_test(&skb->users))) {
2395 return;
2396 }
2397 get_kfree_skb_cb(skb)->reason = reason;
2398 local_irq_save(flags);
2399 skb->next = __this_cpu_read(softnet_data.completion_queue);
2400 __this_cpu_write(softnet_data.completion_queue, skb);
2401 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2402 local_irq_restore(flags);
2403}
2404EXPORT_SYMBOL(__dev_kfree_skb_irq);
2405
2406void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
Denis Vlasenko56079432006-03-29 15:57:29 -08002407{
2408 if (in_irq() || irqs_disabled())
Eric Dumazete6247022013-12-05 04:45:08 -08002409 __dev_kfree_skb_irq(skb, reason);
Denis Vlasenko56079432006-03-29 15:57:29 -08002410 else
2411 dev_kfree_skb(skb);
2412}
Eric Dumazete6247022013-12-05 04:45:08 -08002413EXPORT_SYMBOL(__dev_kfree_skb_any);
Denis Vlasenko56079432006-03-29 15:57:29 -08002414
2415
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002416/**
2417 * netif_device_detach - mark device as removed
2418 * @dev: network device
2419 *
2420 * Mark device as removed from system and therefore no longer available.
2421 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002422void netif_device_detach(struct net_device *dev)
2423{
2424 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2425 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002426 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002427 }
2428}
2429EXPORT_SYMBOL(netif_device_detach);
2430
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002431/**
2432 * netif_device_attach - mark device as attached
2433 * @dev: network device
2434 *
2435 * Mark device as attached from system and restart if needed.
2436 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002437void netif_device_attach(struct net_device *dev)
2438{
2439 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2440 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002441 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002442 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002443 }
2444}
2445EXPORT_SYMBOL(netif_device_attach);
2446
Jiri Pirko5605c762015-05-12 14:56:12 +02002447/*
2448 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2449 * to be used as a distribution range.
2450 */
2451u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
2452 unsigned int num_tx_queues)
2453{
2454 u32 hash;
2455 u16 qoffset = 0;
2456 u16 qcount = num_tx_queues;
2457
2458 if (skb_rx_queue_recorded(skb)) {
2459 hash = skb_get_rx_queue(skb);
2460 while (unlikely(hash >= num_tx_queues))
2461 hash -= num_tx_queues;
2462 return hash;
2463 }
2464
2465 if (dev->num_tc) {
2466 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2467 qoffset = dev->tc_to_txq[tc].offset;
2468 qcount = dev->tc_to_txq[tc].count;
2469 }
2470
2471 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2472}
2473EXPORT_SYMBOL(__skb_tx_hash);
2474
Ben Hutchings36c92472012-01-17 07:57:56 +00002475static void skb_warn_bad_offload(const struct sk_buff *skb)
2476{
Wei Tang84d15ae2016-06-16 21:17:49 +08002477 static const netdev_features_t null_features;
Ben Hutchings36c92472012-01-17 07:57:56 +00002478 struct net_device *dev = skb->dev;
Bjørn Mork88ad4172015-11-16 19:16:40 +01002479 const char *name = "";
Ben Hutchings36c92472012-01-17 07:57:56 +00002480
Ben Greearc846ad92013-04-19 10:45:52 +00002481 if (!net_ratelimit())
2482 return;
2483
Bjørn Mork88ad4172015-11-16 19:16:40 +01002484 if (dev) {
2485 if (dev->dev.parent)
2486 name = dev_driver_string(dev->dev.parent);
2487 else
2488 name = netdev_name(dev);
2489 }
Ben Hutchings36c92472012-01-17 07:57:56 +00002490 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2491 "gso_type=%d ip_summed=%d\n",
Bjørn Mork88ad4172015-11-16 19:16:40 +01002492 name, dev ? &dev->features : &null_features,
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002493 skb->sk ? &skb->sk->sk_route_caps : &null_features,
Ben Hutchings36c92472012-01-17 07:57:56 +00002494 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2495 skb_shinfo(skb)->gso_type, skb->ip_summed);
2496}
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498/*
2499 * Invalidate hardware checksum when packet is to be mangled, and
2500 * complete checksum manually on outgoing path.
2501 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002502int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Al Virod3bc23e2006-11-14 21:24:49 -08002504 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07002505 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Patrick McHardy84fa7932006-08-29 16:44:56 -07002507 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07002508 goto out_set_summed;
2509
2510 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00002511 skb_warn_bad_offload(skb);
2512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514
Eric Dumazetcef401d2013-01-25 20:34:37 +00002515 /* Before computing a checksum, we should make sure no frag could
2516 * be modified by an external entity : checksum could be wrong.
2517 */
2518 if (skb_has_shared_frag(skb)) {
2519 ret = __skb_linearize(skb);
2520 if (ret)
2521 goto out;
2522 }
2523
Michał Mirosław55508d62010-12-14 15:24:08 +00002524 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07002525 BUG_ON(offset >= skb_headlen(skb));
2526 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2527
2528 offset += skb->csum_offset;
2529 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2530
2531 if (skb_cloned(skb) &&
2532 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2534 if (ret)
2535 goto out;
2536 }
2537
Eric Dumazet4f2e4ad2016-10-29 11:02:36 -07002538 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
Herbert Xua430a432006-07-08 13:34:56 -07002539out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002541out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return ret;
2543}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002544EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Tom Herbert6ae23ad2015-12-14 11:19:46 -08002546/* skb_csum_offload_check - Driver helper function to determine if a device
2547 * with limited checksum offload capabilities is able to offload the checksum
2548 * for a given packet.
2549 *
2550 * Arguments:
2551 * skb - sk_buff for the packet in question
2552 * spec - contains the description of what device can offload
2553 * csum_encapped - returns true if the checksum being offloaded is
2554 * encpasulated. That is it is checksum for the transport header
2555 * in the inner headers.
2556 * checksum_help - when set indicates that helper function should
2557 * call skb_checksum_help if offload checks fail
2558 *
2559 * Returns:
2560 * true: Packet has passed the checksum checks and should be offloadable to
2561 * the device (a driver may still need to check for additional
2562 * restrictions of its device)
2563 * false: Checksum is not offloadable. If checksum_help was set then
2564 * skb_checksum_help was called to resolve checksum for non-GSO
2565 * packets and when IP protocol is not SCTP
2566 */
2567bool __skb_csum_offload_chk(struct sk_buff *skb,
2568 const struct skb_csum_offl_spec *spec,
2569 bool *csum_encapped,
2570 bool csum_help)
2571{
2572 struct iphdr *iph;
2573 struct ipv6hdr *ipv6;
2574 void *nhdr;
2575 int protocol;
2576 u8 ip_proto;
2577
2578 if (skb->protocol == htons(ETH_P_8021Q) ||
2579 skb->protocol == htons(ETH_P_8021AD)) {
2580 if (!spec->vlan_okay)
2581 goto need_help;
2582 }
2583
2584 /* We check whether the checksum refers to a transport layer checksum in
2585 * the outermost header or an encapsulated transport layer checksum that
2586 * corresponds to the inner headers of the skb. If the checksum is for
2587 * something else in the packet we need help.
2588 */
2589 if (skb_checksum_start_offset(skb) == skb_transport_offset(skb)) {
2590 /* Non-encapsulated checksum */
2591 protocol = eproto_to_ipproto(vlan_get_protocol(skb));
2592 nhdr = skb_network_header(skb);
2593 *csum_encapped = false;
2594 if (spec->no_not_encapped)
2595 goto need_help;
2596 } else if (skb->encapsulation && spec->encap_okay &&
2597 skb_checksum_start_offset(skb) ==
2598 skb_inner_transport_offset(skb)) {
2599 /* Encapsulated checksum */
2600 *csum_encapped = true;
2601 switch (skb->inner_protocol_type) {
2602 case ENCAP_TYPE_ETHER:
2603 protocol = eproto_to_ipproto(skb->inner_protocol);
2604 break;
2605 case ENCAP_TYPE_IPPROTO:
2606 protocol = skb->inner_protocol;
2607 break;
2608 }
2609 nhdr = skb_inner_network_header(skb);
2610 } else {
2611 goto need_help;
2612 }
2613
2614 switch (protocol) {
2615 case IPPROTO_IP:
2616 if (!spec->ipv4_okay)
2617 goto need_help;
2618 iph = nhdr;
2619 ip_proto = iph->protocol;
2620 if (iph->ihl != 5 && !spec->ip_options_okay)
2621 goto need_help;
2622 break;
2623 case IPPROTO_IPV6:
2624 if (!spec->ipv6_okay)
2625 goto need_help;
2626 if (spec->no_encapped_ipv6 && *csum_encapped)
2627 goto need_help;
2628 ipv6 = nhdr;
2629 nhdr += sizeof(*ipv6);
2630 ip_proto = ipv6->nexthdr;
2631 break;
2632 default:
2633 goto need_help;
2634 }
2635
2636ip_proto_again:
2637 switch (ip_proto) {
2638 case IPPROTO_TCP:
2639 if (!spec->tcp_okay ||
2640 skb->csum_offset != offsetof(struct tcphdr, check))
2641 goto need_help;
2642 break;
2643 case IPPROTO_UDP:
2644 if (!spec->udp_okay ||
2645 skb->csum_offset != offsetof(struct udphdr, check))
2646 goto need_help;
2647 break;
2648 case IPPROTO_SCTP:
2649 if (!spec->sctp_okay ||
2650 skb->csum_offset != offsetof(struct sctphdr, checksum))
2651 goto cant_help;
2652 break;
2653 case NEXTHDR_HOP:
2654 case NEXTHDR_ROUTING:
2655 case NEXTHDR_DEST: {
2656 u8 *opthdr = nhdr;
2657
2658 if (protocol != IPPROTO_IPV6 || !spec->ext_hdrs_okay)
2659 goto need_help;
2660
2661 ip_proto = opthdr[0];
2662 nhdr += (opthdr[1] + 1) << 3;
2663
2664 goto ip_proto_again;
2665 }
2666 default:
2667 goto need_help;
2668 }
2669
2670 /* Passed the tests for offloading checksum */
2671 return true;
2672
2673need_help:
2674 if (csum_help && !skb_shinfo(skb)->gso_size)
2675 skb_checksum_help(skb);
2676cant_help:
2677 return false;
2678}
2679EXPORT_SYMBOL(__skb_csum_offload_chk);
2680
Vlad Yasevich53d64712014-03-27 17:26:18 -04002681__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002682{
2683 __be16 type = skb->protocol;
2684
Pravin B Shelar19acc322013-05-07 20:41:07 +00002685 /* Tunnel gso handlers can set protocol to ethernet. */
2686 if (type == htons(ETH_P_TEB)) {
2687 struct ethhdr *eth;
2688
2689 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2690 return 0;
2691
Eric Dumazetfb9fa142018-03-26 08:08:07 -07002692 eth = (struct ethhdr *)skb->data;
Pravin B Shelar19acc322013-05-07 20:41:07 +00002693 type = eth->h_proto;
2694 }
2695
Toshiaki Makitad4bcef32015-01-29 20:37:07 +09002696 return __vlan_get_protocol(skb, type, depth);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002697}
2698
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002699/**
2700 * skb_mac_gso_segment - mac layer segmentation handler.
2701 * @skb: buffer to segment
2702 * @features: features for the output path (see dev->features)
2703 */
2704struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2705 netdev_features_t features)
2706{
2707 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2708 struct packet_offload *ptype;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002709 int vlan_depth = skb->mac_len;
2710 __be16 type = skb_network_protocol(skb, &vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002711
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002712 if (unlikely(!type))
2713 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002714
Vlad Yasevich53d64712014-03-27 17:26:18 -04002715 __skb_pull(skb, vlan_depth);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002716
2717 rcu_read_lock();
2718 list_for_each_entry_rcu(ptype, &offload_base, list) {
2719 if (ptype->type == type && ptype->callbacks.gso_segment) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002720 segs = ptype->callbacks.gso_segment(skb, features);
2721 break;
2722 }
2723 }
2724 rcu_read_unlock();
2725
2726 __skb_push(skb, skb->data - skb_mac_header(skb));
2727
2728 return segs;
2729}
2730EXPORT_SYMBOL(skb_mac_gso_segment);
2731
2732
Cong Wang12b00042013-02-05 16:36:38 +00002733/* openvswitch calls this on rx path, so we need a different check.
2734 */
2735static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
2736{
2737 if (tx_path)
Eric Dumazet48a70be2017-02-03 14:29:42 -08002738 return skb->ip_summed != CHECKSUM_PARTIAL &&
Willem de Bruijn69ffc962017-08-08 14:22:55 -04002739 skb->ip_summed != CHECKSUM_UNNECESSARY;
Eric Dumazet48a70be2017-02-03 14:29:42 -08002740
2741 return skb->ip_summed == CHECKSUM_NONE;
Cong Wang12b00042013-02-05 16:36:38 +00002742}
2743
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002744/**
Cong Wang12b00042013-02-05 16:36:38 +00002745 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002746 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002747 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00002748 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002749 *
2750 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07002751 *
2752 * It may return NULL if the skb requires no segmentation. This is
2753 * only possible when GSO is used for verifying header integrity.
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03002754 *
2755 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002756 */
Cong Wang12b00042013-02-05 16:36:38 +00002757struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2758 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002759{
Eric Dumazet26d01aa2017-01-31 10:20:32 -08002760 struct sk_buff *segs;
2761
Cong Wang12b00042013-02-05 16:36:38 +00002762 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002763 int err;
2764
Eric Dumazet26d01aa2017-01-31 10:20:32 -08002765 /* We're going to init ->check field in TCP or UDP header */
françois romieua40e0a62014-07-15 23:55:35 +02002766 err = skb_cow_head(skb, 0);
2767 if (err < 0)
Herbert Xua430a432006-07-08 13:34:56 -07002768 return ERR_PTR(err);
2769 }
2770
Alexander Duyck802ab552016-04-10 21:45:03 -04002771 /* Only report GSO partial support if it will enable us to
2772 * support segmentation on this frame without needing additional
2773 * work.
2774 */
2775 if (features & NETIF_F_GSO_PARTIAL) {
2776 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
2777 struct net_device *dev = skb->dev;
2778
2779 partial_features |= dev->features & dev->gso_partial_features;
2780 if (!skb_gso_ok(skb, features | partial_features))
2781 features &= ~NETIF_F_GSO_PARTIAL;
2782 }
2783
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +03002784 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
2785 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
2786
Pravin B Shelar68c33162013-02-14 14:02:41 +00002787 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Eric Dumazet3347c962013-10-19 11:42:56 -07002788 SKB_GSO_CB(skb)->encap_level = 0;
2789
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002790 skb_reset_mac_header(skb);
2791 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002792
Eric Dumazet26d01aa2017-01-31 10:20:32 -08002793 segs = skb_mac_gso_segment(skb, features);
2794
Willem de Bruijneb9c7c72017-12-12 11:39:04 -05002795 if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
Eric Dumazet26d01aa2017-01-31 10:20:32 -08002796 skb_warn_bad_offload(skb);
2797
2798 return segs;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002799}
Cong Wang12b00042013-02-05 16:36:38 +00002800EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002801
Herbert Xufb286bb2005-11-10 13:01:24 -08002802/* Take action when hardware reception checksum errors are detected. */
2803#ifdef CONFIG_BUG
2804void netdev_rx_csum_fault(struct net_device *dev)
2805{
2806 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002807 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08002808 dump_stack();
2809 }
2810}
2811EXPORT_SYMBOL(netdev_rx_csum_fault);
2812#endif
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/* Actually, we should eliminate this check as soon as we know, that:
2815 * 1. IOMMU is present and allows to map all the memory.
2816 * 2. No high memory really exists on this machine.
2817 */
2818
Florian Westphalc1e756b2014-05-05 15:00:44 +02002819static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Herbert Xu3d3a8532006-06-27 13:33:10 -07002821#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 int i;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002823 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002824 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2825 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2826 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002827 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00002828 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002831 if (PCI_DMA_BUS_IS_PHYS) {
2832 struct device *pdev = dev->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Eric Dumazet9092c652010-04-02 13:34:49 -07002834 if (!pdev)
2835 return 0;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002836 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002837 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2838 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002839 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2840 return 1;
2841 }
2842 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07002843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return 0;
2845}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Simon Horman3b392dd2014-06-04 08:53:17 +09002847/* If MPLS offload request, verify we are testing hardware MPLS features
2848 * instead of standard features for the netdev.
2849 */
Pravin B Shelard0edc7b2014-12-23 16:20:11 -08002850#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
Simon Horman3b392dd2014-06-04 08:53:17 +09002851static netdev_features_t net_mpls_features(struct sk_buff *skb,
2852 netdev_features_t features,
2853 __be16 type)
2854{
Simon Horman25cd9ba2014-10-06 05:05:13 -07002855 if (eth_p_mpls(type))
Simon Horman3b392dd2014-06-04 08:53:17 +09002856 features &= skb->dev->mpls_features;
2857
2858 return features;
2859}
2860#else
2861static netdev_features_t net_mpls_features(struct sk_buff *skb,
2862 netdev_features_t features,
2863 __be16 type)
2864{
2865 return features;
2866}
2867#endif
2868
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002869static netdev_features_t harmonize_features(struct sk_buff *skb,
Florian Westphalc1e756b2014-05-05 15:00:44 +02002870 netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00002871{
Vlad Yasevich53d64712014-03-27 17:26:18 -04002872 int tmp;
Simon Horman3b392dd2014-06-04 08:53:17 +09002873 __be16 type;
2874
2875 type = skb_network_protocol(skb, &tmp);
2876 features = net_mpls_features(skb, features, type);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002877
Ed Cashinc0d680e2012-09-19 15:49:00 +00002878 if (skb->ip_summed != CHECKSUM_NONE &&
Simon Horman3b392dd2014-06-04 08:53:17 +09002879 !can_checksum_protocol(features, type)) {
Alexander Duyck996e8022016-05-02 09:25:10 -07002880 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Jesse Grossf01a5232011-01-09 06:23:31 +00002881 }
Eric Dumazet948e1372017-01-18 12:12:17 -08002882 if (illegal_highdma(skb->dev, skb))
2883 features &= ~NETIF_F_SG;
Jesse Grossf01a5232011-01-09 06:23:31 +00002884
2885 return features;
2886}
2887
Toshiaki Makitae38f3022015-03-27 14:31:13 +09002888netdev_features_t passthru_features_check(struct sk_buff *skb,
2889 struct net_device *dev,
2890 netdev_features_t features)
2891{
2892 return features;
2893}
2894EXPORT_SYMBOL(passthru_features_check);
2895
Toshiaki Makita9d33bff2018-04-17 18:46:14 +09002896static netdev_features_t dflt_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09002897 struct net_device *dev,
2898 netdev_features_t features)
2899{
2900 return vlan_features_check(skb, features);
2901}
2902
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002903static netdev_features_t gso_features_check(const struct sk_buff *skb,
2904 struct net_device *dev,
2905 netdev_features_t features)
2906{
2907 u16 gso_segs = skb_shinfo(skb)->gso_segs;
2908
2909 if (gso_segs > dev->gso_max_segs)
2910 return features & ~NETIF_F_GSO_MASK;
2911
Alexander Duyck802ab552016-04-10 21:45:03 -04002912 /* Support for GSO partial features requires software
2913 * intervention before we can actually process the packets
2914 * so we need to strip support for any partial features now
2915 * and we can pull them back in after we have partially
2916 * segmented the frame.
2917 */
2918 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
2919 features &= ~dev->gso_partial_features;
2920
2921 /* Make sure to clear the IPv4 ID mangling feature if the
2922 * IPv4 header has the potential to be fragmented.
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002923 */
2924 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2925 struct iphdr *iph = skb->encapsulation ?
2926 inner_ip_hdr(skb) : ip_hdr(skb);
2927
2928 if (!(iph->frag_off & htons(IP_DF)))
2929 features &= ~NETIF_F_TSO_MANGLEID;
2930 }
2931
2932 return features;
2933}
2934
Florian Westphalc1e756b2014-05-05 15:00:44 +02002935netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00002936{
Jesse Gross5f352272014-12-23 22:37:26 -08002937 struct net_device *dev = skb->dev;
Eric Dumazetfcbeb972014-10-05 10:11:27 -07002938 netdev_features_t features = dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00002939
Alexander Duyckcbc53e02016-04-10 21:44:51 -04002940 if (skb_is_gso(skb))
2941 features = gso_features_check(skb, dev, features);
Ben Hutchings30b678d2012-07-30 15:57:00 +00002942
Jesse Gross5f352272014-12-23 22:37:26 -08002943 /* If encapsulation offload request, verify we are testing
2944 * hardware encapsulation features instead of standard
2945 * features for the netdev
2946 */
2947 if (skb->encapsulation)
2948 features &= dev->hw_enc_features;
2949
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +09002950 if (skb_vlan_tagged(skb))
2951 features = netdev_intersect_features(features,
2952 dev->vlan_features |
2953 NETIF_F_HW_VLAN_CTAG_TX |
2954 NETIF_F_HW_VLAN_STAG_TX);
Jesse Gross58e998c2010-10-29 12:14:55 +00002955
Jesse Gross5f352272014-12-23 22:37:26 -08002956 if (dev->netdev_ops->ndo_features_check)
2957 features &= dev->netdev_ops->ndo_features_check(skb, dev,
2958 features);
Toshiaki Makita8cb65d02015-03-27 14:31:12 +09002959 else
2960 features &= dflt_features_check(skb, dev, features);
Jesse Gross5f352272014-12-23 22:37:26 -08002961
Florian Westphalc1e756b2014-05-05 15:00:44 +02002962 return harmonize_features(skb, features);
Jesse Gross58e998c2010-10-29 12:14:55 +00002963}
Florian Westphalc1e756b2014-05-05 15:00:44 +02002964EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00002965
David S. Miller2ea25512014-08-29 21:10:01 -07002966static int xmit_one(struct sk_buff *skb, struct net_device *dev,
David S. Miller95f6b3d2014-08-29 21:57:30 -07002967 struct netdev_queue *txq, bool more)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002968{
David S. Miller2ea25512014-08-29 21:10:01 -07002969 unsigned int len;
2970 int rc;
Stephen Hemminger00829822008-11-20 20:14:53 -08002971
Salam Noureddine7866a622015-01-27 11:35:48 -08002972 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
David S. Miller2ea25512014-08-29 21:10:01 -07002973 dev_queue_xmit_nit(skb, dev);
Jesse Grossfc741212011-01-09 06:23:32 +00002974
David S. Miller2ea25512014-08-29 21:10:01 -07002975 len = skb->len;
2976 trace_net_dev_start_xmit(skb, dev);
David S. Miller95f6b3d2014-08-29 21:57:30 -07002977 rc = netdev_start_xmit(skb, dev, txq, more);
David S. Miller2ea25512014-08-29 21:10:01 -07002978 trace_net_dev_xmit(skb, rc, dev, len);
Eric Dumazetadf30902009-06-02 05:19:30 +00002979
Patrick McHardy572a9d72009-11-10 06:14:14 +00002980 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002981}
David S. Miller2ea25512014-08-29 21:10:01 -07002982
David S. Miller8dcda222014-09-01 15:06:40 -07002983struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
2984 struct netdev_queue *txq, int *ret)
David S. Miller7f2e8702014-08-29 21:19:14 -07002985{
2986 struct sk_buff *skb = first;
2987 int rc = NETDEV_TX_OK;
2988
2989 while (skb) {
2990 struct sk_buff *next = skb->next;
2991
2992 skb->next = NULL;
David S. Miller95f6b3d2014-08-29 21:57:30 -07002993 rc = xmit_one(skb, dev, txq, next != NULL);
David S. Miller7f2e8702014-08-29 21:19:14 -07002994 if (unlikely(!dev_xmit_complete(rc))) {
2995 skb->next = next;
2996 goto out;
2997 }
2998
2999 skb = next;
3000 if (netif_xmit_stopped(txq) && skb) {
3001 rc = NETDEV_TX_BUSY;
3002 break;
3003 }
3004 }
3005
3006out:
3007 *ret = rc;
3008 return skb;
3009}
3010
Eric Dumazet1ff0dc92014-10-06 11:26:27 -07003011static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3012 netdev_features_t features)
David S. Millereae3f882014-08-30 15:17:13 -07003013{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003014 if (skb_vlan_tag_present(skb) &&
Jiri Pirko59682502014-11-19 14:04:59 +01003015 !vlan_hw_offload_capable(features, skb->vlan_proto))
3016 skb = __vlan_hwaccel_push_inside(skb);
David S. Millereae3f882014-08-30 15:17:13 -07003017 return skb;
3018}
3019
Eric Dumazet55a93b32014-10-03 15:31:07 -07003020static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev)
David S. Millereae3f882014-08-30 15:17:13 -07003021{
3022 netdev_features_t features;
3023
David S. Millereae3f882014-08-30 15:17:13 -07003024 features = netif_skb_features(skb);
3025 skb = validate_xmit_vlan(skb, features);
3026 if (unlikely(!skb))
3027 goto out_null;
3028
Johannes Berg8b86a612015-04-17 15:45:04 +02003029 if (netif_needs_gso(skb, features)) {
David S. Millerce937182014-08-30 19:22:20 -07003030 struct sk_buff *segs;
3031
3032 segs = skb_gso_segment(skb, features);
Jason Wangcecda692014-09-19 16:04:38 +08003033 if (IS_ERR(segs)) {
Jason Wangaf6dabc2014-12-19 11:09:13 +08003034 goto out_kfree_skb;
Jason Wangcecda692014-09-19 16:04:38 +08003035 } else if (segs) {
3036 consume_skb(skb);
3037 skb = segs;
3038 }
David S. Millereae3f882014-08-30 15:17:13 -07003039 } else {
3040 if (skb_needs_linearize(skb, features) &&
3041 __skb_linearize(skb))
3042 goto out_kfree_skb;
3043
3044 /* If packet is not checksummed and device does not
3045 * support checksumming for this protocol, complete
3046 * checksumming here.
3047 */
3048 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3049 if (skb->encapsulation)
3050 skb_set_inner_transport_header(skb,
3051 skb_checksum_start_offset(skb));
3052 else
3053 skb_set_transport_header(skb,
3054 skb_checksum_start_offset(skb));
Tom Herberta1882222015-12-14 11:19:43 -08003055 if (!(features & NETIF_F_CSUM_MASK) &&
David S. Millereae3f882014-08-30 15:17:13 -07003056 skb_checksum_help(skb))
3057 goto out_kfree_skb;
3058 }
3059 }
3060
3061 return skb;
3062
3063out_kfree_skb:
3064 kfree_skb(skb);
3065out_null:
Eric Dumazetd21fd632016-04-12 21:50:07 -07003066 atomic_long_inc(&dev->tx_dropped);
David S. Millereae3f882014-08-30 15:17:13 -07003067 return NULL;
3068}
3069
Eric Dumazet55a93b32014-10-03 15:31:07 -07003070struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev)
3071{
3072 struct sk_buff *next, *head = NULL, *tail;
3073
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003074 for (; skb != NULL; skb = next) {
Eric Dumazet55a93b32014-10-03 15:31:07 -07003075 next = skb->next;
3076 skb->next = NULL;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003077
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003078 /* in case skb wont be segmented, point to itself */
3079 skb->prev = skb;
3080
3081 skb = validate_xmit_skb(skb, dev);
3082 if (!skb)
3083 continue;
3084
3085 if (!head)
3086 head = skb;
3087 else
3088 tail->next = skb;
3089 /* If skb was segmented, skb->prev points to
3090 * the last segment. If not, it still contains skb.
3091 */
3092 tail = skb->prev;
Eric Dumazet55a93b32014-10-03 15:31:07 -07003093 }
3094 return head;
3095}
Willem de Bruijn104ba782016-10-26 11:23:07 -04003096EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07003097
Eric Dumazet1def9232013-01-10 12:36:42 +00003098static void qdisc_pkt_len_init(struct sk_buff *skb)
3099{
3100 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3101
3102 qdisc_skb_cb(skb)->pkt_len = skb->len;
3103
3104 /* To get more precise estimation of bytes sent on wire,
3105 * we add to pkt_len the headers size of all segments
3106 */
3107 if (shinfo->gso_size) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08003108 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00003109 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00003110
Eric Dumazet757b8b12013-01-15 21:14:21 -08003111 /* mac layer + network layer */
3112 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3113
3114 /* + transport layer */
Eric Dumazeta44d9112018-01-18 19:59:19 -08003115 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3116 const struct tcphdr *th;
3117 struct tcphdr _tcphdr;
3118
3119 th = skb_header_pointer(skb, skb_transport_offset(skb),
3120 sizeof(_tcphdr), &_tcphdr);
3121 if (likely(th))
3122 hdr_len += __tcp_hdrlen(th);
3123 } else {
3124 struct udphdr _udphdr;
3125
3126 if (skb_header_pointer(skb, skb_transport_offset(skb),
3127 sizeof(_udphdr), &_udphdr))
3128 hdr_len += sizeof(struct udphdr);
3129 }
Jason Wang15e5a032013-03-25 20:19:59 +00003130
3131 if (shinfo->gso_type & SKB_GSO_DODGY)
3132 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3133 shinfo->gso_size);
3134
3135 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00003136 }
3137}
3138
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003139static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3140 struct net_device *dev,
3141 struct netdev_queue *txq)
3142{
3143 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet520ac302016-06-21 23:16:49 -07003144 struct sk_buff *to_free = NULL;
Eric Dumazeta2da5702011-01-20 03:48:19 +00003145 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003146 int rc;
3147
Eric Dumazeta2da5702011-01-20 03:48:19 +00003148 qdisc_calculate_pkt_len(skb, q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003149 /*
3150 * Heuristic to force contended enqueues to serialize on a
3151 * separate lock before trying to get qdisc main lock.
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -07003152 * This permits qdisc->running owner to get the lock more
Ying Xue9bf2b8c2014-06-26 15:56:31 +08003153 * often and dequeue packets faster.
Eric Dumazet79640a42010-06-02 05:09:29 -07003154 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00003155 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003156 if (unlikely(contended))
3157 spin_lock(&q->busylock);
3158
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003159 spin_lock(root_lock);
3160 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
Eric Dumazet520ac302016-06-21 23:16:49 -07003161 __qdisc_drop(skb, &to_free);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003162 rc = NET_XMIT_DROP;
3163 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07003164 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003165 /*
3166 * This is a work-conserving queue; there are no old skbs
3167 * waiting to be sent out; and the qdisc is not running -
3168 * xmit the skb directly.
3169 */
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003170
Eric Dumazetbfe0d022011-01-09 08:30:54 +00003171 qdisc_bstats_update(q, skb);
3172
Eric Dumazet55a93b32014-10-03 15:31:07 -07003173 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
Eric Dumazet79640a42010-06-02 05:09:29 -07003174 if (unlikely(contended)) {
3175 spin_unlock(&q->busylock);
3176 contended = false;
3177 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003178 __qdisc_run(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07003179 } else
Eric Dumazetbc135b22010-06-02 03:23:51 -07003180 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003181
3182 rc = NET_XMIT_SUCCESS;
3183 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -07003184 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07003185 if (qdisc_run_begin(q)) {
3186 if (unlikely(contended)) {
3187 spin_unlock(&q->busylock);
3188 contended = false;
3189 }
3190 __qdisc_run(q);
3191 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003192 }
3193 spin_unlock(root_lock);
Eric Dumazet520ac302016-06-21 23:16:49 -07003194 if (unlikely(to_free))
3195 kfree_skb_list(to_free);
Eric Dumazet79640a42010-06-02 05:09:29 -07003196 if (unlikely(contended))
3197 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003198 return rc;
3199}
3200
Daniel Borkmann86f85152013-12-29 17:27:11 +01003201#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
Neil Horman5bc14212011-11-22 05:10:51 +00003202static void skb_update_prio(struct sk_buff *skb)
3203{
Eric Dumazet28984ba02018-03-14 09:04:16 -07003204 const struct netprio_map *map;
3205 const struct sock *sk;
3206 unsigned int prioidx;
Neil Horman5bc14212011-11-22 05:10:51 +00003207
Eric Dumazet28984ba02018-03-14 09:04:16 -07003208 if (skb->priority)
3209 return;
3210 map = rcu_dereference_bh(skb->dev->priomap);
3211 if (!map)
3212 return;
3213 sk = skb_to_full_sk(skb);
3214 if (!sk)
3215 return;
Eric Dumazet91c68ce2012-07-08 21:45:10 +00003216
Eric Dumazet28984ba02018-03-14 09:04:16 -07003217 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3218
3219 if (prioidx < map->priomap_len)
3220 skb->priority = map->priomap[prioidx];
Neil Horman5bc14212011-11-22 05:10:51 +00003221}
3222#else
3223#define skb_update_prio(skb)
3224#endif
3225
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +02003226DEFINE_PER_CPU(int, xmit_recursion);
3227EXPORT_SYMBOL(xmit_recursion);
3228
Dave Jonesd29f7492008-07-22 14:09:06 -07003229/**
Michel Machado95603e22012-06-12 10:16:35 +00003230 * dev_loopback_xmit - loop back @skb
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003231 * @net: network namespace this loopback is happening in
3232 * @sk: sk needed to be a netfilter okfn
Michel Machado95603e22012-06-12 10:16:35 +00003233 * @skb: buffer to transmit
3234 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05003235int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Michel Machado95603e22012-06-12 10:16:35 +00003236{
3237 skb_reset_mac_header(skb);
3238 __skb_pull(skb, skb_network_offset(skb));
3239 skb->pkt_type = PACKET_LOOPBACK;
3240 skb->ip_summed = CHECKSUM_UNNECESSARY;
3241 WARN_ON(!skb_dst(skb));
3242 skb_dst_force(skb);
3243 netif_rx_ni(skb);
3244 return 0;
3245}
3246EXPORT_SYMBOL(dev_loopback_xmit);
3247
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003248#ifdef CONFIG_NET_EGRESS
3249static struct sk_buff *
3250sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3251{
3252 struct tcf_proto *cl = rcu_dereference_bh(dev->egress_cl_list);
3253 struct tcf_result cl_res;
3254
3255 if (!cl)
3256 return skb;
3257
3258 /* skb->tc_verd and qdisc_skb_cb(skb)->pkt_len were already set
3259 * earlier by the caller.
3260 */
3261 qdisc_bstats_cpu_update(cl->q, skb);
3262
3263 switch (tc_classify(skb, cl, &cl_res, false)) {
3264 case TC_ACT_OK:
3265 case TC_ACT_RECLASSIFY:
3266 skb->tc_index = TC_H_MIN(cl_res.classid);
3267 break;
3268 case TC_ACT_SHOT:
3269 qdisc_qstats_cpu_drop(cl->q);
3270 *ret = NET_XMIT_DROP;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003271 kfree_skb(skb);
3272 return NULL;
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003273 case TC_ACT_STOLEN:
3274 case TC_ACT_QUEUED:
3275 *ret = NET_XMIT_SUCCESS;
Daniel Borkmann7e2c3ae2016-05-15 23:28:29 +02003276 consume_skb(skb);
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003277 return NULL;
3278 case TC_ACT_REDIRECT:
3279 /* No need to push/pop skb's mac_header here on egress! */
3280 skb_do_redirect(skb);
3281 *ret = NET_XMIT_SUCCESS;
3282 return NULL;
3283 default:
3284 break;
3285 }
3286
3287 return skb;
3288}
3289#endif /* CONFIG_NET_EGRESS */
3290
Jiri Pirko638b2a62015-05-12 14:56:13 +02003291static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
3292{
3293#ifdef CONFIG_XPS
3294 struct xps_dev_maps *dev_maps;
3295 struct xps_map *map;
3296 int queue_index = -1;
3297
3298 rcu_read_lock();
3299 dev_maps = rcu_dereference(dev->xps_maps);
3300 if (dev_maps) {
3301 map = rcu_dereference(
3302 dev_maps->cpu_map[skb->sender_cpu - 1]);
3303 if (map) {
3304 if (map->len == 1)
3305 queue_index = map->queues[0];
3306 else
3307 queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
3308 map->len)];
3309 if (unlikely(queue_index >= dev->real_num_tx_queues))
3310 queue_index = -1;
3311 }
3312 }
3313 rcu_read_unlock();
3314
3315 return queue_index;
3316#else
3317 return -1;
3318#endif
3319}
3320
3321static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
3322{
3323 struct sock *sk = skb->sk;
3324 int queue_index = sk_tx_queue_get(sk);
3325
3326 if (queue_index < 0 || skb->ooo_okay ||
3327 queue_index >= dev->real_num_tx_queues) {
3328 int new_index = get_xps_queue(dev, skb);
3329 if (new_index < 0)
3330 new_index = skb_tx_hash(dev, skb);
3331
3332 if (queue_index != new_index && sk &&
Eric Dumazet004a5d02015-10-04 21:08:10 -07003333 sk_fullsock(sk) &&
Jiri Pirko638b2a62015-05-12 14:56:13 +02003334 rcu_access_pointer(sk->sk_dst_cache))
3335 sk_tx_queue_set(sk, new_index);
3336
3337 queue_index = new_index;
3338 }
3339
3340 return queue_index;
3341}
3342
3343struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3344 struct sk_buff *skb,
3345 void *accel_priv)
3346{
3347 int queue_index = 0;
3348
3349#ifdef CONFIG_XPS
Eric Dumazet52bd2d62015-11-18 06:30:50 -08003350 u32 sender_cpu = skb->sender_cpu - 1;
3351
3352 if (sender_cpu >= (u32)NR_CPUS)
Jiri Pirko638b2a62015-05-12 14:56:13 +02003353 skb->sender_cpu = raw_smp_processor_id() + 1;
3354#endif
3355
3356 if (dev->real_num_tx_queues != 1) {
3357 const struct net_device_ops *ops = dev->netdev_ops;
3358 if (ops->ndo_select_queue)
3359 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
3360 __netdev_pick_tx);
3361 else
3362 queue_index = __netdev_pick_tx(dev, skb);
3363
3364 if (!accel_priv)
3365 queue_index = netdev_cap_txqueue(dev, queue_index);
3366 }
3367
3368 skb_set_queue_mapping(skb, queue_index);
3369 return netdev_get_tx_queue(dev, queue_index);
3370}
3371
Michel Machado95603e22012-06-12 10:16:35 +00003372/**
Jason Wang9d08dd32014-01-20 11:25:13 +08003373 * __dev_queue_xmit - transmit a buffer
Dave Jonesd29f7492008-07-22 14:09:06 -07003374 * @skb: buffer to transmit
Jason Wang9d08dd32014-01-20 11:25:13 +08003375 * @accel_priv: private data used for L2 forwarding offload
Dave Jonesd29f7492008-07-22 14:09:06 -07003376 *
3377 * Queue a buffer for transmission to a network device. The caller must
3378 * have set the device and priority and built the buffer before calling
3379 * this function. The function can be called from an interrupt.
3380 *
3381 * A negative errno code is returned on a failure. A success does not
3382 * guarantee the frame will be transmitted as it may be dropped due
3383 * to congestion or traffic shaping.
3384 *
3385 * -----------------------------------------------------------------------------------
3386 * I notice this method can also return errors from the queue disciplines,
3387 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3388 * be positive.
3389 *
3390 * Regardless of the return value, the skb is consumed, so it is currently
3391 * difficult to retry a send to this method. (You can bump the ref count
3392 * before sending to hold a reference for retry if you are careful.)
3393 *
3394 * When calling this method, interrupts MUST be enabled. This is because
3395 * the BH enable code must have IRQs enabled so that it will not deadlock.
3396 * --BLG
3397 */
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05303398static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07003401 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 struct Qdisc *q;
3403 int rc = -ENOMEM;
3404
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00003405 skb_reset_mac_header(skb);
3406
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003407 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3408 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3409
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003410 /* Disable soft irqs for various locks below. Also
3411 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003413 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Neil Horman5bc14212011-11-22 05:10:51 +00003415 skb_update_prio(skb);
3416
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003417 qdisc_pkt_len_init(skb);
3418#ifdef CONFIG_NET_CLS_ACT
3419 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
3420# ifdef CONFIG_NET_EGRESS
3421 if (static_key_false(&egress_needed)) {
3422 skb = sch_handle_egress(skb, &rc, dev);
3423 if (!skb)
3424 goto out;
3425 }
3426# endif
3427#endif
Eric Dumazet02875872014-10-05 18:38:35 -07003428 /* If device/qdisc don't need skb->dst, release it right now while
3429 * its hot in this cpu cache.
3430 */
3431 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3432 skb_dst_drop(skb);
3433 else
3434 skb_dst_force(skb);
3435
Jason Wangf663dd92014-01-10 16:18:26 +08003436 txq = netdev_pick_tx(dev, skb, accel_priv);
Paul E. McKenneya898def2010-02-22 17:04:49 -08003437 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07003438
Koki Sanagicf66ba52010-08-23 18:45:02 +09003439 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00003441 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07003442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444
3445 /* The device has no queue. Common case for software devices:
3446 loopback, all the sorts of tunnels...
3447
Herbert Xu932ff272006-06-09 12:20:56 -07003448 Really, it is unlikely that netif_tx_lock protection is necessary
3449 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 counters.)
3451 However, it is possible, that they rely on protection
3452 made by us here.
3453
3454 Check this and shot the lock. It is not prone from deadlocks.
3455 Either shot noqueue qdisc, it is even simpler 8)
3456 */
3457 if (dev->flags & IFF_UP) {
3458 int cpu = smp_processor_id(); /* ok because BHs are off */
3459
David S. Millerc773e842008-07-08 23:13:53 -07003460 if (txq->xmit_lock_owner != cpu) {
Daniel Borkmanna70b5062016-06-10 21:19:06 +02003461 if (unlikely(__this_cpu_read(xmit_recursion) >
3462 XMIT_RECURSION_LIMIT))
Eric Dumazet745e20f2010-09-29 13:23:09 -07003463 goto recursion_alert;
3464
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003465 skb = validate_xmit_skb(skb, dev);
3466 if (!skb)
Eric Dumazetd21fd632016-04-12 21:50:07 -07003467 goto out;
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003468
David S. Millerc773e842008-07-08 23:13:53 -07003469 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Tom Herbert734664982011-11-28 16:32:44 +00003471 if (!netif_xmit_stopped(txq)) {
Eric Dumazet745e20f2010-09-29 13:23:09 -07003472 __this_cpu_inc(xmit_recursion);
David S. Millerce937182014-08-30 19:22:20 -07003473 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
Eric Dumazet745e20f2010-09-29 13:23:09 -07003474 __this_cpu_dec(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00003475 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07003476 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 goto out;
3478 }
3479 }
David S. Millerc773e842008-07-08 23:13:53 -07003480 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00003481 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3482 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 } else {
3484 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07003485 * unfortunately
3486 */
3487recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00003488 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3489 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
3491 }
3492
3493 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07003494 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Eric Dumazet015f0682014-03-27 08:45:56 -07003496 atomic_long_inc(&dev->tx_dropped);
Jesper Dangaard Brouer1f595332014-09-03 17:56:09 +02003497 kfree_skb_list(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return rc;
3499out:
Herbert Xud4828d82006-06-22 02:28:18 -07003500 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return rc;
3502}
Jason Wangf663dd92014-01-10 16:18:26 +08003503
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003504int dev_queue_xmit(struct sk_buff *skb)
Jason Wangf663dd92014-01-10 16:18:26 +08003505{
3506 return __dev_queue_xmit(skb, NULL);
3507}
Eric W. Biederman2b4aa3c2015-09-15 20:04:07 -05003508EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Jason Wangf663dd92014-01-10 16:18:26 +08003510int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
3511{
3512 return __dev_queue_xmit(skb, accel_priv);
3513}
3514EXPORT_SYMBOL(dev_queue_xmit_accel);
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517/*=======================================================================
3518 Receiver routines
3519 =======================================================================*/
3520
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003521int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00003522EXPORT_SYMBOL(netdev_max_backlog);
3523
Eric Dumazet3b098e22010-05-15 23:57:10 -07003524int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07003525int netdev_budget __read_mostly = 300;
3526int weight_p __read_mostly = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003528/* Called with irq disabled */
3529static inline void ____napi_schedule(struct softnet_data *sd,
3530 struct napi_struct *napi)
3531{
3532 list_add_tail(&napi->poll_list, &sd->poll_list);
3533 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3534}
3535
Eric Dumazetdf334542010-03-24 19:13:54 +00003536#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07003537
3538/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00003539struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07003540EXPORT_SYMBOL(rps_sock_flow_table);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003541u32 rps_cpu_mask __read_mostly;
3542EXPORT_SYMBOL(rps_cpu_mask);
Tom Herbertfec5e652010-04-16 16:01:27 -07003543
Ingo Molnarc5905af2012-02-24 08:31:31 +01003544struct static_key rps_needed __read_mostly;
Jason Wang3df97ba2016-04-25 23:13:42 -04003545EXPORT_SYMBOL(rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +00003546
Ben Hutchingsc4454772011-01-19 11:03:53 +00003547static struct rps_dev_flow *
3548set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3549 struct rps_dev_flow *rflow, u16 next_cpu)
3550{
Eric Dumazeta31196b2015-04-25 09:35:24 -07003551 if (next_cpu < nr_cpu_ids) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00003552#ifdef CONFIG_RFS_ACCEL
3553 struct netdev_rx_queue *rxqueue;
3554 struct rps_dev_flow_table *flow_table;
3555 struct rps_dev_flow *old_rflow;
3556 u32 flow_id;
3557 u16 rxq_index;
3558 int rc;
3559
3560 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00003561 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3562 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00003563 goto out;
3564 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3565 if (rxq_index == skb_get_rx_queue(skb))
3566 goto out;
3567
3568 rxqueue = dev->_rx + rxq_index;
3569 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3570 if (!flow_table)
3571 goto out;
Tom Herbert61b905d2014-03-24 15:34:47 -07003572 flow_id = skb_get_hash(skb) & flow_table->mask;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003573 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3574 rxq_index, flow_id);
3575 if (rc < 0)
3576 goto out;
3577 old_rflow = rflow;
3578 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00003579 rflow->filter = rc;
3580 if (old_rflow->filter == rflow->filter)
3581 old_rflow->filter = RPS_NO_FILTER;
3582 out:
3583#endif
3584 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00003585 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003586 }
3587
Ben Hutchings09994d12011-10-03 04:42:46 +00003588 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003589 return rflow;
3590}
3591
Tom Herbert0a9627f2010-03-16 08:03:29 +00003592/*
3593 * get_rps_cpu is called from netif_receive_skb and returns the target
3594 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003595 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00003596 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003597static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3598 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003599{
Eric Dumazet567e4b72015-02-06 12:59:01 -08003600 const struct rps_sock_flow_table *sock_flow_table;
3601 struct netdev_rx_queue *rxqueue = dev->_rx;
Tom Herbertfec5e652010-04-16 16:01:27 -07003602 struct rps_dev_flow_table *flow_table;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003603 struct rps_map *map;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003604 int cpu = -1;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003605 u32 tcpu;
Tom Herbert61b905d2014-03-24 15:34:47 -07003606 u32 hash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003607
Tom Herbert0a9627f2010-03-16 08:03:29 +00003608 if (skb_rx_queue_recorded(skb)) {
3609 u16 index = skb_get_rx_queue(skb);
Eric Dumazet567e4b72015-02-06 12:59:01 -08003610
Ben Hutchings62fe0b42010-09-27 08:24:33 +00003611 if (unlikely(index >= dev->real_num_rx_queues)) {
3612 WARN_ONCE(dev->real_num_rx_queues > 1,
3613 "%s received packet on queue %u, but number "
3614 "of RX queues is %u\n",
3615 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003616 goto done;
3617 }
Eric Dumazet567e4b72015-02-06 12:59:01 -08003618 rxqueue += index;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003619 }
3620
Eric Dumazet567e4b72015-02-06 12:59:01 -08003621 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
3622
3623 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3624 map = rcu_dereference(rxqueue->rps_map);
3625 if (!flow_table && !map)
3626 goto done;
3627
Changli Gao2d47b452010-08-17 19:00:56 +00003628 skb_reset_network_header(skb);
Tom Herbert61b905d2014-03-24 15:34:47 -07003629 hash = skb_get_hash(skb);
3630 if (!hash)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003631 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003632
Tom Herbertfec5e652010-04-16 16:01:27 -07003633 sock_flow_table = rcu_dereference(rps_sock_flow_table);
3634 if (flow_table && sock_flow_table) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003635 struct rps_dev_flow *rflow;
Eric Dumazet567e4b72015-02-06 12:59:01 -08003636 u32 next_cpu;
3637 u32 ident;
Tom Herbertfec5e652010-04-16 16:01:27 -07003638
Eric Dumazet567e4b72015-02-06 12:59:01 -08003639 /* First check into global flow table if there is a match */
3640 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
3641 if ((ident ^ hash) & ~rps_cpu_mask)
3642 goto try_rps;
3643
3644 next_cpu = ident & rps_cpu_mask;
3645
3646 /* OK, now we know there is a match,
3647 * we can look at the local (per receive queue) flow table
3648 */
Tom Herbert61b905d2014-03-24 15:34:47 -07003649 rflow = &flow_table->flows[hash & flow_table->mask];
Tom Herbertfec5e652010-04-16 16:01:27 -07003650 tcpu = rflow->cpu;
3651
Tom Herbertfec5e652010-04-16 16:01:27 -07003652 /*
3653 * If the desired CPU (where last recvmsg was done) is
3654 * different from current CPU (one in the rx-queue flow
3655 * table entry), switch if one of the following holds:
Eric Dumazeta31196b2015-04-25 09:35:24 -07003656 * - Current CPU is unset (>= nr_cpu_ids).
Tom Herbertfec5e652010-04-16 16:01:27 -07003657 * - Current CPU is offline.
3658 * - The current CPU's queue tail has advanced beyond the
3659 * last packet that was enqueued using this table entry.
3660 * This guarantees that all previous packets for the flow
3661 * have been dequeued, thus preserving in order delivery.
3662 */
3663 if (unlikely(tcpu != next_cpu) &&
Eric Dumazeta31196b2015-04-25 09:35:24 -07003664 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
Tom Herbertfec5e652010-04-16 16:01:27 -07003665 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00003666 rflow->last_qtail)) >= 0)) {
3667 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003668 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00003669 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00003670
Eric Dumazeta31196b2015-04-25 09:35:24 -07003671 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003672 *rflowp = rflow;
3673 cpu = tcpu;
3674 goto done;
3675 }
3676 }
3677
Eric Dumazet567e4b72015-02-06 12:59:01 -08003678try_rps:
3679
Tom Herbert0a9627f2010-03-16 08:03:29 +00003680 if (map) {
Daniel Borkmann8fc54f62014-08-23 20:58:54 +02003681 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
Tom Herbert0a9627f2010-03-16 08:03:29 +00003682 if (cpu_online(tcpu)) {
3683 cpu = tcpu;
3684 goto done;
3685 }
3686 }
3687
3688done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00003689 return cpu;
3690}
3691
Ben Hutchingsc4454772011-01-19 11:03:53 +00003692#ifdef CONFIG_RFS_ACCEL
3693
3694/**
3695 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3696 * @dev: Device on which the filter was set
3697 * @rxq_index: RX queue index
3698 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3699 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3700 *
3701 * Drivers that implement ndo_rx_flow_steer() should periodically call
3702 * this function for each installed filter and remove the filters for
3703 * which it returns %true.
3704 */
3705bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3706 u32 flow_id, u16 filter_id)
3707{
3708 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3709 struct rps_dev_flow_table *flow_table;
3710 struct rps_dev_flow *rflow;
3711 bool expire = true;
Eric Dumazeta31196b2015-04-25 09:35:24 -07003712 unsigned int cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003713
3714 rcu_read_lock();
3715 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3716 if (flow_table && flow_id <= flow_table->mask) {
3717 rflow = &flow_table->flows[flow_id];
3718 cpu = ACCESS_ONCE(rflow->cpu);
Eric Dumazeta31196b2015-04-25 09:35:24 -07003719 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
Ben Hutchingsc4454772011-01-19 11:03:53 +00003720 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3721 rflow->last_qtail) <
3722 (int)(10 * flow_table->mask)))
3723 expire = false;
3724 }
3725 rcu_read_unlock();
3726 return expire;
3727}
3728EXPORT_SYMBOL(rps_may_expire_flow);
3729
3730#endif /* CONFIG_RFS_ACCEL */
3731
Tom Herbert0a9627f2010-03-16 08:03:29 +00003732/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003733static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003734{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003735 struct softnet_data *sd = data;
3736
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003737 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00003738 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003739}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003740
Tom Herbertfec5e652010-04-16 16:01:27 -07003741#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00003742
3743/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003744 * Check if this softnet_data structure is another cpu one
3745 * If yes, queue it to our IPI list and return 1
3746 * If no, return 0
3747 */
3748static int rps_ipi_queued(struct softnet_data *sd)
3749{
3750#ifdef CONFIG_RPS
Christoph Lameter903ceff2014-08-17 12:30:35 -05003751 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003752
3753 if (sd != mysd) {
3754 sd->rps_ipi_next = mysd->rps_ipi_list;
3755 mysd->rps_ipi_list = sd;
3756
3757 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3758 return 1;
3759 }
3760#endif /* CONFIG_RPS */
3761 return 0;
3762}
3763
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003764#ifdef CONFIG_NET_FLOW_LIMIT
3765int netdev_flow_limit_table_len __read_mostly = (1 << 12);
3766#endif
3767
3768static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
3769{
3770#ifdef CONFIG_NET_FLOW_LIMIT
3771 struct sd_flow_limit *fl;
3772 struct softnet_data *sd;
3773 unsigned int old_flow, new_flow;
3774
3775 if (qlen < (netdev_max_backlog >> 1))
3776 return false;
3777
Christoph Lameter903ceff2014-08-17 12:30:35 -05003778 sd = this_cpu_ptr(&softnet_data);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003779
3780 rcu_read_lock();
3781 fl = rcu_dereference(sd->flow_limit);
3782 if (fl) {
Tom Herbert3958afa1b2013-12-15 22:12:06 -08003783 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003784 old_flow = fl->history[fl->history_head];
3785 fl->history[fl->history_head] = new_flow;
3786
3787 fl->history_head++;
3788 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
3789
3790 if (likely(fl->buckets[old_flow]))
3791 fl->buckets[old_flow]--;
3792
3793 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
3794 fl->count++;
3795 rcu_read_unlock();
3796 return true;
3797 }
3798 }
3799 rcu_read_unlock();
3800#endif
3801 return false;
3802}
3803
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003804/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00003805 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3806 * queue (may be a remote CPU queue).
3807 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003808static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3809 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003810{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003811 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003812 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003813 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003814
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003815 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003816
3817 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003818
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003819 rps_lock(sd);
Julian Anastasove9e4dd32015-07-09 09:59:09 +03003820 if (!netif_running(skb->dev))
3821 goto drop;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003822 qlen = skb_queue_len(&sd->input_pkt_queue);
3823 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Li RongQinge008f3f2014-12-08 09:42:55 +08003824 if (qlen) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00003825enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003826 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003827 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003828 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00003829 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003830 return NET_RX_SUCCESS;
3831 }
3832
Eric Dumazetebda37c22010-05-06 23:51:21 +00003833 /* Schedule NAPI for backlog device
3834 * We can use non atomic operation since we own the queue lock
3835 */
3836 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003837 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003838 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003839 }
3840 goto enqueue;
3841 }
3842
Julian Anastasove9e4dd32015-07-09 09:59:09 +03003843drop:
Changli Gaodee42872010-05-02 05:42:16 +00003844 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003845 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003846
Tom Herbert0a9627f2010-03-16 08:03:29 +00003847 local_irq_restore(flags);
3848
Eric Dumazetcaf586e2010-09-30 21:06:55 +00003849 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003850 kfree_skb(skb);
3851 return NET_RX_DROP;
3852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00003854static int netif_rx_internal(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003856 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Eric Dumazet588f0332011-11-15 04:12:55 +00003858 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Koki Sanagicf66ba52010-08-23 18:45:02 +09003860 trace_netif_rx(skb);
Eric Dumazetdf334542010-03-24 19:13:54 +00003861#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01003862 if (static_key_false(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003863 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003864 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Changli Gaocece1942010-08-07 20:35:43 -07003866 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003867 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07003868
3869 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003870 if (cpu < 0)
3871 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07003872
3873 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3874
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003875 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07003876 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00003877 } else
3878#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07003879 {
3880 unsigned int qtail;
3881 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
3882 put_cpu();
3883 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003884 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00003886
3887/**
3888 * netif_rx - post buffer to the network code
3889 * @skb: buffer to post
3890 *
3891 * This function receives a packet from a device driver and queues it for
3892 * the upper (protocol) levels to process. It always succeeds. The buffer
3893 * may be dropped during processing for congestion control or by the
3894 * protocol layers.
3895 *
3896 * return values:
3897 * NET_RX_SUCCESS (no congestion)
3898 * NET_RX_DROP (packet was dropped)
3899 *
3900 */
3901
3902int netif_rx(struct sk_buff *skb)
3903{
3904 trace_netif_rx_entry(skb);
3905
3906 return netif_rx_internal(skb);
3907}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003908EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
3910int netif_rx_ni(struct sk_buff *skb)
3911{
3912 int err;
3913
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00003914 trace_netif_rx_ni_entry(skb);
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 preempt_disable();
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00003917 err = netif_rx_internal(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 if (local_softirq_pending())
3919 do_softirq();
3920 preempt_enable();
3921
3922 return err;
3923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924EXPORT_SYMBOL(netif_rx_ni);
3925
Emese Revfy0766f782016-06-20 20:42:34 +02003926static __latent_entropy void net_tx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Christoph Lameter903ceff2014-08-17 12:30:35 -05003928 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 if (sd->completion_queue) {
3931 struct sk_buff *clist;
3932
3933 local_irq_disable();
3934 clist = sd->completion_queue;
3935 sd->completion_queue = NULL;
3936 local_irq_enable();
3937
3938 while (clist) {
3939 struct sk_buff *skb = clist;
3940 clist = clist->next;
3941
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003942 WARN_ON(atomic_read(&skb->users));
Eric Dumazete6247022013-12-05 04:45:08 -08003943 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
3944 trace_consume_skb(skb);
3945 else
3946 trace_kfree_skb(skb, net_tx_action);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01003947
3948 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
3949 __kfree_skb(skb);
3950 else
3951 __kfree_skb_defer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +01003953
3954 __kfree_skb_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956
3957 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07003958 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 local_irq_disable();
3961 head = sd->output_queue;
3962 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00003963 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 local_irq_enable();
3965
3966 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07003967 struct Qdisc *q = head;
3968 spinlock_t *root_lock;
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 head = head->next_sched;
3971
David S. Miller5fb66222008-08-02 20:02:43 -07003972 root_lock = qdisc_lock(q);
Eric Dumazet3bcb8462016-06-04 20:02:28 -07003973 spin_lock(root_lock);
3974 /* We need to make sure head->next_sched is read
3975 * before clearing __QDISC_STATE_SCHED
3976 */
3977 smp_mb__before_atomic();
3978 clear_bit(__QDISC_STATE_SCHED, &q->state);
3979 qdisc_run(q);
3980 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982 }
3983}
3984
Javier Martinez Canillas181402a2016-09-09 08:43:15 -04003985#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +00003986/* This hook is defined here for ATM LANE */
3987int (*br_fdb_test_addr_hook)(struct net_device *dev,
3988 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07003989EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00003990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Daniel Borkmann1f211a12016-01-07 22:29:47 +01003992static inline struct sk_buff *
3993sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
3994 struct net_device *orig_dev)
Herbert Xuf697c3e2007-10-14 00:38:47 -07003995{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02003996#ifdef CONFIG_NET_CLS_ACT
Daniel Borkmannd2788d32015-05-09 22:51:32 +02003997 struct tcf_proto *cl = rcu_dereference_bh(skb->dev->ingress_cl_list);
3998 struct tcf_result cl_res;
Eric Dumazet24824a02010-10-02 06:11:55 +00003999
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004000 /* If there's at least one ingress present somewhere (so
4001 * we get here via enabled static key), remaining devices
4002 * that are not configured with an ingress qdisc will bail
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004003 * out here.
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004004 */
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004005 if (!cl)
Daniel Borkmann45771392015-04-10 23:07:54 +02004006 return skb;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004007 if (*pt_prev) {
4008 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4009 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004010 }
4011
Florian Westphal33654952015-05-14 00:36:28 +02004012 qdisc_skb_cb(skb)->pkt_len = skb->len;
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004013 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
Eric Dumazet24ea5912015-07-06 05:18:03 -07004014 qdisc_bstats_cpu_update(cl->q, skb);
Daniel Borkmannc9e99fd2015-05-09 22:51:31 +02004015
Daniel Borkmann3b3ae882015-08-26 23:00:06 +02004016 switch (tc_classify(skb, cl, &cl_res, false)) {
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004017 case TC_ACT_OK:
4018 case TC_ACT_RECLASSIFY:
4019 skb->tc_index = TC_H_MIN(cl_res.classid);
4020 break;
4021 case TC_ACT_SHOT:
Eric Dumazet24ea5912015-07-06 05:18:03 -07004022 qdisc_qstats_cpu_drop(cl->q);
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004023 kfree_skb(skb);
4024 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004025 case TC_ACT_STOLEN:
4026 case TC_ACT_QUEUED:
Eric Dumazet8a3a4c62016-05-06 15:55:50 -07004027 consume_skb(skb);
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004028 return NULL;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07004029 case TC_ACT_REDIRECT:
4030 /* skb_mac_header check was done by cls/act_bpf, so
4031 * we can safely push the L2 header back before
4032 * redirecting to another netdev
4033 */
4034 __skb_push(skb, skb->mac_len);
4035 skb_do_redirect(skb);
4036 return NULL;
Daniel Borkmannd2788d32015-05-09 22:51:32 +02004037 default:
4038 break;
Herbert Xuf697c3e2007-10-14 00:38:47 -07004039 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004040#endif /* CONFIG_NET_CLS_ACT */
Herbert Xuf697c3e2007-10-14 00:38:47 -07004041 return skb;
4042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004044/**
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07004045 * netdev_is_rx_handler_busy - check if receive handler is registered
4046 * @dev: device to check
4047 *
4048 * Check if a receive handler is already registered for a given device.
4049 * Return true if there one.
4050 *
4051 * The caller must hold the rtnl_mutex.
4052 */
4053bool netdev_is_rx_handler_busy(struct net_device *dev)
4054{
4055 ASSERT_RTNL();
4056 return dev && rtnl_dereference(dev->rx_handler);
4057}
4058EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4059
4060/**
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004061 * netdev_rx_handler_register - register receive handler
4062 * @dev: device to register a handler for
4063 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00004064 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004065 *
Masanari Iidae2278672014-02-18 22:54:36 +09004066 * Register a receive handler for a device. This handler will then be
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004067 * called from __netif_receive_skb. A negative errno code is returned
4068 * on a failure.
4069 *
4070 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004071 *
4072 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004073 */
4074int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00004075 rx_handler_func_t *rx_handler,
4076 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004077{
4078 ASSERT_RTNL();
4079
4080 if (dev->rx_handler)
4081 return -EBUSY;
4082
Eric Dumazet00cfec32013-03-29 03:01:22 +00004083 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00004084 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004085 rcu_assign_pointer(dev->rx_handler, rx_handler);
4086
4087 return 0;
4088}
4089EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4090
4091/**
4092 * netdev_rx_handler_unregister - unregister receive handler
4093 * @dev: device to unregister a handler from
4094 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00004095 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004096 *
4097 * The caller must hold the rtnl_mutex.
4098 */
4099void netdev_rx_handler_unregister(struct net_device *dev)
4100{
4101
4102 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004103 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00004104 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4105 * section has a guarantee to see a non NULL rx_handler_data
4106 * as well.
4107 */
4108 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00004109 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004110}
4111EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4112
Mel Gormanb4b9e352012-07-31 16:44:26 -07004113/*
4114 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4115 * the special handling of PFMEMALLOC skbs.
4116 */
4117static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4118{
4119 switch (skb->protocol) {
Joe Perches2b8837a2014-03-12 10:04:17 -07004120 case htons(ETH_P_ARP):
4121 case htons(ETH_P_IP):
4122 case htons(ETH_P_IPV6):
4123 case htons(ETH_P_8021Q):
4124 case htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07004125 return true;
4126 default:
4127 return false;
4128 }
4129}
4130
Pablo Neirae687ad62015-05-13 18:19:38 +02004131static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4132 int *ret, struct net_device *orig_dev)
4133{
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004134#ifdef CONFIG_NETFILTER_INGRESS
Pablo Neirae687ad62015-05-13 18:19:38 +02004135 if (nf_hook_ingress_active(skb)) {
Aaron Conole2c1e2702016-09-21 11:35:03 -04004136 int ingress_retval;
4137
Pablo Neirae687ad62015-05-13 18:19:38 +02004138 if (*pt_prev) {
4139 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4140 *pt_prev = NULL;
4141 }
4142
Aaron Conole2c1e2702016-09-21 11:35:03 -04004143 rcu_read_lock();
4144 ingress_retval = nf_hook_ingress(skb);
4145 rcu_read_unlock();
4146 return ingress_retval;
Pablo Neirae687ad62015-05-13 18:19:38 +02004147 }
Daniel Borkmanne7582ba2015-05-19 22:33:25 +02004148#endif /* CONFIG_NETFILTER_INGRESS */
Pablo Neirae687ad62015-05-13 18:19:38 +02004149 return 0;
4150}
Pablo Neirae687ad62015-05-13 18:19:38 +02004151
David S. Miller9754e292013-02-14 15:57:38 -05004152static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004155 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07004156 struct net_device *orig_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004157 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 int ret = NET_RX_DROP;
Al Viro252e3342006-11-14 20:48:11 -08004159 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Eric Dumazet588f0332011-11-15 04:12:55 +00004161 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07004162
Koki Sanagicf66ba52010-08-23 18:45:02 +09004163 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08004164
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07004165 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00004166
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07004167 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00004168 if (!skb_transport_header_was_set(skb))
4169 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00004170 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 pt_prev = NULL;
4173
David S. Miller63d8ea72011-02-28 10:48:59 -08004174another_round:
David S. Millerb6858172012-07-23 16:27:54 -07004175 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08004176
4177 __this_cpu_inc(softnet_data.processed);
4178
Patrick McHardy8ad227f2013-04-19 02:04:31 +00004179 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4180 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004181 skb = skb_vlan_untag(skb);
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004182 if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004183 goto out;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00004184 }
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186#ifdef CONFIG_NET_CLS_ACT
4187 if (skb->tc_verd & TC_NCLS) {
4188 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
4189 goto ncls;
4190 }
4191#endif
4192
David S. Miller9754e292013-02-14 15:57:38 -05004193 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07004194 goto skip_taps;
4195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Salam Noureddine7866a622015-01-27 11:35:48 -08004197 if (pt_prev)
4198 ret = deliver_skb(skb, pt_prev, orig_dev);
4199 pt_prev = ptype;
4200 }
4201
4202 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
4203 if (pt_prev)
4204 ret = deliver_skb(skb, pt_prev, orig_dev);
4205 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
4207
Mel Gormanb4b9e352012-07-31 16:44:26 -07004208skip_taps:
Pablo Neira1cf519002015-05-13 18:19:37 +02004209#ifdef CONFIG_NET_INGRESS
Daniel Borkmann45771392015-04-10 23:07:54 +02004210 if (static_key_false(&ingress_needed)) {
Daniel Borkmann1f211a12016-01-07 22:29:47 +01004211 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
Daniel Borkmann45771392015-04-10 23:07:54 +02004212 if (!skb)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004213 goto out;
Pablo Neirae687ad62015-05-13 18:19:38 +02004214
4215 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
Julian Anastasov2c17d272015-07-09 09:59:10 +03004216 goto out;
Daniel Borkmann45771392015-04-10 23:07:54 +02004217 }
Pablo Neira1cf519002015-05-13 18:19:37 +02004218#endif
4219#ifdef CONFIG_NET_CLS_ACT
Daniel Borkmann45771392015-04-10 23:07:54 +02004220 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221ncls:
4222#endif
David S. Miller9754e292013-02-14 15:57:38 -05004223 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07004224 goto drop;
4225
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004226 if (skb_vlan_tag_present(skb)) {
John Fastabend24257172011-10-10 09:16:41 +00004227 if (pt_prev) {
4228 ret = deliver_skb(skb, pt_prev, orig_dev);
4229 pt_prev = NULL;
4230 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004231 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00004232 goto another_round;
4233 else if (unlikely(!skb))
Julian Anastasov2c17d272015-07-09 09:59:10 +03004234 goto out;
John Fastabend24257172011-10-10 09:16:41 +00004235 }
4236
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004237 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004238 if (rx_handler) {
4239 if (pt_prev) {
4240 ret = deliver_skb(skb, pt_prev, orig_dev);
4241 pt_prev = NULL;
4242 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004243 switch (rx_handler(&skb)) {
4244 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00004245 ret = NET_RX_SUCCESS;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004246 goto out;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004247 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08004248 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00004249 case RX_HANDLER_EXACT:
4250 deliver_exact = true;
4251 case RX_HANDLER_PASS:
4252 break;
4253 default:
4254 BUG();
4255 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00004256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004258 if (unlikely(skb_vlan_tag_present(skb))) {
4259 if (skb_vlan_tag_get_id(skb))
Eric Dumazetd4b812d2013-07-18 07:19:26 -07004260 skb->pkt_type = PACKET_OTHERHOST;
4261 /* Note: we might in the future use prio bits
4262 * and set skb->priority like in vlan_do_receive()
4263 * For the time being, just ignore Priority Code Point
4264 */
4265 skb->vlan_tci = 0;
4266 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00004267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 type = skb->protocol;
Salam Noureddine7866a622015-01-27 11:35:48 -08004269
4270 /* deliver only exact match when indicated */
4271 if (likely(!deliver_exact)) {
4272 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4273 &ptype_base[ntohs(type) &
4274 PTYPE_HASH_MASK]);
4275 }
4276
4277 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4278 &orig_dev->ptype_specific);
4279
4280 if (unlikely(skb->dev != orig_dev)) {
4281 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4282 &skb->dev->ptype_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
4284
4285 if (pt_prev) {
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00004286 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00004287 goto drop;
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00004288 else
4289 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07004291drop:
Jarod Wilson6e7333d2016-02-01 18:51:05 -05004292 if (!deliver_exact)
4293 atomic_long_inc(&skb->dev->rx_dropped);
4294 else
4295 atomic_long_inc(&skb->dev->rx_nohandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 kfree_skb(skb);
4297 /* Jamal, now you will not able to escape explaining
4298 * me how you were going to use this. :-)
4299 */
4300 ret = NET_RX_DROP;
4301 }
4302
Julian Anastasov2c17d272015-07-09 09:59:10 +03004303out:
David S. Miller9754e292013-02-14 15:57:38 -05004304 return ret;
4305}
4306
4307static int __netif_receive_skb(struct sk_buff *skb)
4308{
4309 int ret;
4310
4311 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
4312 unsigned long pflags = current->flags;
4313
4314 /*
4315 * PFMEMALLOC skbs are special, they should
4316 * - be delivered to SOCK_MEMALLOC sockets only
4317 * - stay away from userspace
4318 * - have bounded memory usage
4319 *
4320 * Use PF_MEMALLOC as this saves us from propagating the allocation
4321 * context down to all allocation sites.
4322 */
4323 current->flags |= PF_MEMALLOC;
4324 ret = __netif_receive_skb_core(skb, true);
4325 tsk_restore_flags(current, pflags, PF_MEMALLOC);
4326 } else
4327 ret = __netif_receive_skb_core(skb, false);
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return ret;
4330}
Tom Herbert0a9627f2010-03-16 08:03:29 +00004331
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004332static int netif_receive_skb_internal(struct sk_buff *skb)
Tom Herbert0a9627f2010-03-16 08:03:29 +00004333{
Julian Anastasov2c17d272015-07-09 09:59:10 +03004334 int ret;
4335
Eric Dumazet588f0332011-11-15 04:12:55 +00004336 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07004337
Richard Cochranc1f19b52010-07-17 08:49:36 +00004338 if (skb_defer_rx_timestamp(skb))
4339 return NET_RX_SUCCESS;
4340
Julian Anastasov2c17d272015-07-09 09:59:10 +03004341 rcu_read_lock();
4342
Eric Dumazetdf334542010-03-24 19:13:54 +00004343#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01004344 if (static_key_false(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07004345 struct rps_dev_flow voidflow, *rflow = &voidflow;
Julian Anastasov2c17d272015-07-09 09:59:10 +03004346 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07004347
Eric Dumazet3b098e22010-05-15 23:57:10 -07004348 if (cpu >= 0) {
4349 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4350 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00004351 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07004352 }
Tom Herbertfec5e652010-04-16 16:01:27 -07004353 }
Tom Herbert1e94d722010-03-18 17:45:44 -07004354#endif
Julian Anastasov2c17d272015-07-09 09:59:10 +03004355 ret = __netif_receive_skb(skb);
4356 rcu_read_unlock();
4357 return ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00004358}
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004359
4360/**
4361 * netif_receive_skb - process receive buffer from network
4362 * @skb: buffer to process
4363 *
4364 * netif_receive_skb() is the main receive data processing function.
4365 * It always succeeds. The buffer may be dropped during processing
4366 * for congestion control or by the protocol layers.
4367 *
4368 * This function may only be called from softirq context and interrupts
4369 * should be enabled.
4370 *
4371 * Return values (usually ignored):
4372 * NET_RX_SUCCESS: no congestion
4373 * NET_RX_DROP: packet was dropped
4374 */
Eric W. Biederman04eb4482015-09-15 20:04:15 -05004375int netif_receive_skb(struct sk_buff *skb)
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004376{
4377 trace_netif_receive_skb_entry(skb);
4378
4379 return netif_receive_skb_internal(skb);
4380}
Eric W. Biederman04eb4482015-09-15 20:04:15 -05004381EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Eric Dumazet41852492016-08-26 12:50:39 -07004383DEFINE_PER_CPU(struct work_struct, flush_works);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004384
4385/* Network device is going away, flush any packets still pending */
4386static void flush_backlog(struct work_struct *work)
4387{
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004388 struct sk_buff *skb, *tmp;
4389 struct softnet_data *sd;
4390
4391 local_bh_disable();
4392 sd = this_cpu_ptr(&softnet_data);
4393
4394 local_irq_disable();
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004395 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07004396 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07004397 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004398 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004399 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004400 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004401 }
Changli Gao6e7676c2010-04-27 15:07:33 -07004402 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00004403 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004404 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07004405
4406 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
Eric Dumazet41852492016-08-26 12:50:39 -07004407 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
Changli Gao6e7676c2010-04-27 15:07:33 -07004408 __skb_unlink(skb, &sd->process_queue);
4409 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004410 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07004411 }
4412 }
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004413 local_bh_enable();
4414}
4415
Eric Dumazet41852492016-08-26 12:50:39 -07004416static void flush_all_backlogs(void)
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004417{
4418 unsigned int cpu;
4419
4420 get_online_cpus();
4421
Eric Dumazet41852492016-08-26 12:50:39 -07004422 for_each_online_cpu(cpu)
4423 queue_work_on(cpu, system_highpri_wq,
4424 per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004425
4426 for_each_online_cpu(cpu)
Eric Dumazet41852492016-08-26 12:50:39 -07004427 flush_work(per_cpu_ptr(&flush_works, cpu));
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004428
4429 put_online_cpus();
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07004430}
4431
Herbert Xud565b0a2008-12-15 23:38:52 -08004432static int napi_gro_complete(struct sk_buff *skb)
4433{
Vlad Yasevich22061d82012-11-15 08:49:11 +00004434 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08004435 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004436 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08004437 int err = -ENOENT;
4438
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004439 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
4440
Herbert Xufc59f9a2009-04-14 15:11:06 -07004441 if (NAPI_GRO_CB(skb)->count == 1) {
4442 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004443 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07004444 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004445
4446 rcu_read_lock();
4447 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004448 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08004449 continue;
4450
Jerry Chu299603e82013-12-11 20:53:45 -08004451 err = ptype->callbacks.gro_complete(skb, 0);
Herbert Xud565b0a2008-12-15 23:38:52 -08004452 break;
4453 }
4454 rcu_read_unlock();
4455
4456 if (err) {
4457 WARN_ON(&ptype->list == head);
4458 kfree_skb(skb);
4459 return NET_RX_SUCCESS;
4460 }
4461
4462out:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004463 return netif_receive_skb_internal(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004464}
4465
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004466/* napi->gro_list contains packets ordered by age.
4467 * youngest packets at the head of it.
4468 * Complete skbs in reverse order to reduce latencies.
4469 */
4470void napi_gro_flush(struct napi_struct *napi, bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08004471{
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004472 struct sk_buff *skb, *prev = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08004473
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004474 /* scan list and build reverse chain */
4475 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
4476 skb->prev = prev;
4477 prev = skb;
Herbert Xud565b0a2008-12-15 23:38:52 -08004478 }
4479
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004480 for (skb = prev; skb; skb = prev) {
4481 skb->next = NULL;
4482
4483 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
4484 return;
4485
4486 prev = skb->prev;
4487 napi_gro_complete(skb);
4488 napi->gro_count--;
4489 }
4490
Herbert Xud565b0a2008-12-15 23:38:52 -08004491 napi->gro_list = NULL;
4492}
Eric Dumazet86cac582010-08-31 18:25:32 +00004493EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08004494
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004495static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
4496{
4497 struct sk_buff *p;
4498 unsigned int maclen = skb->dev->hard_header_len;
Tom Herbert0b4cec82014-01-15 08:58:06 -08004499 u32 hash = skb_get_hash_raw(skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004500
4501 for (p = napi->gro_list; p; p = p->next) {
4502 unsigned long diffs;
4503
Tom Herbert0b4cec82014-01-15 08:58:06 -08004504 NAPI_GRO_CB(p)->flush = 0;
4505
4506 if (hash != skb_get_hash_raw(p)) {
4507 NAPI_GRO_CB(p)->same_flow = 0;
4508 continue;
4509 }
4510
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004511 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
4512 diffs |= p->vlan_tci ^ skb->vlan_tci;
Jesse Grossce87fc62016-01-20 17:59:49 -08004513 diffs |= skb_metadata_dst_cmp(p, skb);
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004514 if (maclen == ETH_HLEN)
4515 diffs |= compare_ether_header(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07004516 skb_mac_header(skb));
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004517 else if (!diffs)
4518 diffs = memcmp(skb_mac_header(p),
Eric Dumazeta50e2332014-03-29 21:28:21 -07004519 skb_mac_header(skb),
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004520 maclen);
4521 NAPI_GRO_CB(p)->same_flow = !diffs;
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004522 }
4523}
4524
Jerry Chu299603e82013-12-11 20:53:45 -08004525static void skb_gro_reset_offset(struct sk_buff *skb)
4526{
4527 const struct skb_shared_info *pinfo = skb_shinfo(skb);
4528 const skb_frag_t *frag0 = &pinfo->frags[0];
4529
4530 NAPI_GRO_CB(skb)->data_offset = 0;
4531 NAPI_GRO_CB(skb)->frag0 = NULL;
4532 NAPI_GRO_CB(skb)->frag0_len = 0;
4533
4534 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
4535 pinfo->nr_frags &&
4536 !PageHighMem(skb_frag_page(frag0))) {
4537 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
Eric Dumazet934ca012017-01-10 19:52:43 -08004538 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
4539 skb_frag_size(frag0),
4540 skb->end - skb->tail);
Herbert Xud565b0a2008-12-15 23:38:52 -08004541 }
4542}
4543
Eric Dumazeta50e2332014-03-29 21:28:21 -07004544static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
4545{
4546 struct skb_shared_info *pinfo = skb_shinfo(skb);
4547
4548 BUG_ON(skb->end - skb->tail < grow);
4549
4550 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
4551
4552 skb->data_len -= grow;
4553 skb->tail += grow;
4554
4555 pinfo->frags[0].page_offset += grow;
4556 skb_frag_size_sub(&pinfo->frags[0], grow);
4557
4558 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
4559 skb_frag_unref(skb, 0);
4560 memmove(pinfo->frags, pinfo->frags + 1,
4561 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
4562 }
4563}
4564
Rami Rosenbb728822012-11-28 21:55:25 +00004565static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08004566{
4567 struct sk_buff **pp = NULL;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004568 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08004569 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00004570 struct list_head *head = &offload_base;
Herbert Xu0da2afd52008-12-26 14:57:42 -08004571 int same_flow;
Ben Hutchings5b252f02009-10-29 07:17:09 +00004572 enum gro_result ret;
Eric Dumazeta50e2332014-03-29 21:28:21 -07004573 int grow;
Herbert Xud565b0a2008-12-15 23:38:52 -08004574
Eric W. Biederman9c62a682014-03-14 20:51:52 -07004575 if (!(skb->dev->features & NETIF_F_GRO))
Herbert Xud565b0a2008-12-15 23:38:52 -08004576 goto normal;
4577
Tom Herbert5a212322014-08-31 15:12:41 -07004578 if (skb_is_gso(skb) || skb_has_frag_list(skb) || skb->csum_bad)
Herbert Xuf17f5c92009-01-14 14:36:12 -08004579 goto normal;
4580
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004581 gro_list_prepare(napi, skb);
4582
Herbert Xud565b0a2008-12-15 23:38:52 -08004583 rcu_read_lock();
4584 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004585 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08004586 continue;
4587
Herbert Xu86911732009-01-29 14:19:50 +00004588 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00004589 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004590 NAPI_GRO_CB(skb)->same_flow = 0;
4591 NAPI_GRO_CB(skb)->flush = 0;
Herbert Xu5d38a072009-01-04 16:13:40 -08004592 NAPI_GRO_CB(skb)->free = 0;
Jesse Grossfac8e0f2016-03-19 09:32:01 -07004593 NAPI_GRO_CB(skb)->encap_mark = 0;
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +02004594 NAPI_GRO_CB(skb)->recursion_counter = 0;
Alexander Duycka0ca1532016-04-05 09:13:39 -07004595 NAPI_GRO_CB(skb)->is_fou = 0;
Alexander Duyck15305452016-04-10 21:44:57 -04004596 NAPI_GRO_CB(skb)->is_atomic = 1;
Tom Herbert15e23962015-02-10 16:30:31 -08004597 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004598
Tom Herbert662880f2014-08-27 21:26:56 -07004599 /* Setup for GRO checksum validation */
4600 switch (skb->ip_summed) {
4601 case CHECKSUM_COMPLETE:
4602 NAPI_GRO_CB(skb)->csum = skb->csum;
4603 NAPI_GRO_CB(skb)->csum_valid = 1;
4604 NAPI_GRO_CB(skb)->csum_cnt = 0;
4605 break;
4606 case CHECKSUM_UNNECESSARY:
4607 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
4608 NAPI_GRO_CB(skb)->csum_valid = 0;
4609 break;
4610 default:
4611 NAPI_GRO_CB(skb)->csum_cnt = 0;
4612 NAPI_GRO_CB(skb)->csum_valid = 0;
4613 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004614
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00004615 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004616 break;
4617 }
4618 rcu_read_unlock();
4619
4620 if (&ptype->list == head)
4621 goto normal;
4622
Herbert Xu0da2afd52008-12-26 14:57:42 -08004623 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004624 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08004625
Herbert Xud565b0a2008-12-15 23:38:52 -08004626 if (pp) {
4627 struct sk_buff *nskb = *pp;
4628
4629 *pp = nskb->next;
4630 nskb->next = NULL;
4631 napi_gro_complete(nskb);
Herbert Xu4ae55442009-02-08 18:00:36 +00004632 napi->gro_count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08004633 }
4634
Herbert Xu0da2afd52008-12-26 14:57:42 -08004635 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08004636 goto ok;
4637
Eric Dumazet600adc12014-01-09 14:12:19 -08004638 if (NAPI_GRO_CB(skb)->flush)
Herbert Xud565b0a2008-12-15 23:38:52 -08004639 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08004640
Eric Dumazet600adc12014-01-09 14:12:19 -08004641 if (unlikely(napi->gro_count >= MAX_GRO_SKBS)) {
4642 struct sk_buff *nskb = napi->gro_list;
4643
4644 /* locate the end of the list to select the 'oldest' flow */
4645 while (nskb->next) {
4646 pp = &nskb->next;
4647 nskb = *pp;
4648 }
4649 *pp = NULL;
4650 nskb->next = NULL;
4651 napi_gro_complete(nskb);
4652 } else {
4653 napi->gro_count++;
4654 }
Herbert Xud565b0a2008-12-15 23:38:52 -08004655 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004656 NAPI_GRO_CB(skb)->age = jiffies;
Eric Dumazet29e98242014-05-16 11:34:37 -07004657 NAPI_GRO_CB(skb)->last = skb;
Herbert Xu86911732009-01-29 14:19:50 +00004658 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004659 skb->next = napi->gro_list;
4660 napi->gro_list = skb;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004661 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08004662
Herbert Xuad0f9902009-02-01 01:24:55 -08004663pull:
Eric Dumazeta50e2332014-03-29 21:28:21 -07004664 grow = skb_gro_offset(skb) - skb_headlen(skb);
4665 if (grow > 0)
4666 gro_pull_from_frag0(skb, grow);
Herbert Xud565b0a2008-12-15 23:38:52 -08004667ok:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004668 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08004669
4670normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08004671 ret = GRO_NORMAL;
4672 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08004673}
Herbert Xu96e93ea2009-01-06 10:49:34 -08004674
Jerry Chubf5a7552014-01-07 10:23:19 -08004675struct packet_offload *gro_find_receive_by_type(__be16 type)
4676{
4677 struct list_head *offload_head = &offload_base;
4678 struct packet_offload *ptype;
4679
4680 list_for_each_entry_rcu(ptype, offload_head, list) {
4681 if (ptype->type != type || !ptype->callbacks.gro_receive)
4682 continue;
4683 return ptype;
4684 }
4685 return NULL;
4686}
Or Gerlitze27a2f82014-01-20 13:59:20 +02004687EXPORT_SYMBOL(gro_find_receive_by_type);
Jerry Chubf5a7552014-01-07 10:23:19 -08004688
4689struct packet_offload *gro_find_complete_by_type(__be16 type)
4690{
4691 struct list_head *offload_head = &offload_base;
4692 struct packet_offload *ptype;
4693
4694 list_for_each_entry_rcu(ptype, offload_head, list) {
4695 if (ptype->type != type || !ptype->callbacks.gro_complete)
4696 continue;
4697 return ptype;
4698 }
4699 return NULL;
4700}
Or Gerlitze27a2f82014-01-20 13:59:20 +02004701EXPORT_SYMBOL(gro_find_complete_by_type);
Herbert Xu96e93ea2009-01-06 10:49:34 -08004702
Michal Kubeček06732802017-06-29 11:13:36 +02004703static void napi_skb_free_stolen_head(struct sk_buff *skb)
4704{
4705 skb_dst_drop(skb);
4706 kmem_cache_free(skbuff_head_cache, skb);
4707}
4708
Rami Rosenbb728822012-11-28 21:55:25 +00004709static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08004710{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004711 switch (ret) {
4712 case GRO_NORMAL:
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004713 if (netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004714 ret = GRO_DROP;
4715 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08004716
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004717 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08004718 kfree_skb(skb);
4719 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00004720
Eric Dumazetdaa86542012-04-19 07:07:40 +00004721 case GRO_MERGED_FREE:
Michal Kubeček06732802017-06-29 11:13:36 +02004722 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
4723 napi_skb_free_stolen_head(skb);
4724 else
Eric Dumazetd7e88832012-04-30 08:10:34 +00004725 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00004726 break;
4727
Ben Hutchings5b252f02009-10-29 07:17:09 +00004728 case GRO_HELD:
4729 case GRO_MERGED:
4730 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08004731 }
4732
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004733 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004734}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004735
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004736gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004737{
Eric Dumazet93f93a42015-11-18 06:30:59 -08004738 skb_mark_napi_id(skb, napi);
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004739 trace_napi_gro_receive_entry(skb);
Herbert Xu86911732009-01-29 14:19:50 +00004740
Eric Dumazeta50e2332014-03-29 21:28:21 -07004741 skb_gro_reset_offset(skb);
4742
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004743 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08004744}
4745EXPORT_SYMBOL(napi_gro_receive);
4746
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00004747static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08004748{
Eric Dumazet93a35f52014-10-23 06:30:30 -07004749 if (unlikely(skb->pfmemalloc)) {
4750 consume_skb(skb);
4751 return;
4752 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08004753 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00004754 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
4755 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Jesse Gross3701e512010-10-20 13:56:06 +00004756 skb->vlan_tci = 0;
Herbert Xu66c46d72011-01-29 20:44:54 -08004757 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08004758 skb->skb_iif = 0;
Eric Dumazet4a80b1f2018-11-17 21:57:02 -08004759
4760 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
4761 skb->pkt_type = PACKET_HOST;
4762
Jerry Chuc3caf112014-07-14 15:54:46 -07004763 skb->encapsulation = 0;
4764 skb_shinfo(skb)->gso_type = 0;
Eric Dumazete33d0ba2014-04-03 09:28:10 -07004765 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
Herbert Xu96e93ea2009-01-06 10:49:34 -08004766
4767 napi->skb = skb;
4768}
Herbert Xu96e93ea2009-01-06 10:49:34 -08004769
Herbert Xu76620aa2009-04-16 02:02:07 -07004770struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08004771{
Herbert Xu5d38a072009-01-04 16:13:40 -08004772 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08004773
4774 if (!skb) {
Alexander Duyckfd11a832014-12-09 19:40:49 -08004775 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
Eric Dumazete2f9dc32015-11-19 12:11:23 -08004776 if (skb) {
4777 napi->skb = skb;
4778 skb_mark_napi_id(skb, napi);
4779 }
Herbert Xu5d38a072009-01-04 16:13:40 -08004780 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08004781 return skb;
4782}
Herbert Xu76620aa2009-04-16 02:02:07 -07004783EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08004784
Eric Dumazeta50e2332014-03-29 21:28:21 -07004785static gro_result_t napi_frags_finish(struct napi_struct *napi,
4786 struct sk_buff *skb,
4787 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004788{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004789 switch (ret) {
4790 case GRO_NORMAL:
Eric Dumazeta50e2332014-03-29 21:28:21 -07004791 case GRO_HELD:
4792 __skb_push(skb, ETH_HLEN);
4793 skb->protocol = eth_type_trans(skb, skb->dev);
4794 if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004795 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00004796 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004797
4798 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004799 napi_reuse_skb(napi, skb);
4800 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00004801
Michal Kubeček06732802017-06-29 11:13:36 +02004802 case GRO_MERGED_FREE:
4803 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
4804 napi_skb_free_stolen_head(skb);
4805 else
4806 napi_reuse_skb(napi, skb);
4807 break;
4808
Ben Hutchings5b252f02009-10-29 07:17:09 +00004809 case GRO_MERGED:
4810 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004811 }
4812
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004813 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004814}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00004815
Eric Dumazeta50e2332014-03-29 21:28:21 -07004816/* Upper GRO stack assumes network header starts at gro_offset=0
4817 * Drivers could call both napi_gro_frags() and napi_gro_receive()
4818 * We copy ethernet header into skb->data to have a common layout.
4819 */
Eric Dumazet4adb9c42012-05-18 20:49:06 +00004820static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08004821{
Herbert Xu76620aa2009-04-16 02:02:07 -07004822 struct sk_buff *skb = napi->skb;
Eric Dumazeta50e2332014-03-29 21:28:21 -07004823 const struct ethhdr *eth;
4824 unsigned int hlen = sizeof(*eth);
Herbert Xu76620aa2009-04-16 02:02:07 -07004825
4826 napi->skb = NULL;
4827
Eric Dumazeta50e2332014-03-29 21:28:21 -07004828 skb_reset_mac_header(skb);
4829 skb_gro_reset_offset(skb);
4830
Eric Dumazeta50e2332014-03-29 21:28:21 -07004831 if (unlikely(skb_gro_header_hard(skb, hlen))) {
4832 eth = skb_gro_header_slow(skb, hlen, 0);
4833 if (unlikely(!eth)) {
Aaron Conole4da46ce2016-04-02 15:26:43 -04004834 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
4835 __func__, napi->dev->name);
Eric Dumazeta50e2332014-03-29 21:28:21 -07004836 napi_reuse_skb(napi, skb);
4837 return NULL;
4838 }
4839 } else {
Eric Dumazet12855df2019-05-29 15:36:10 -07004840 eth = (const struct ethhdr *)skb->data;
Eric Dumazeta50e2332014-03-29 21:28:21 -07004841 gro_pull_from_frag0(skb, hlen);
4842 NAPI_GRO_CB(skb)->frag0 += hlen;
4843 NAPI_GRO_CB(skb)->frag0_len -= hlen;
Herbert Xu76620aa2009-04-16 02:02:07 -07004844 }
Eric Dumazeta50e2332014-03-29 21:28:21 -07004845 __skb_pull(skb, hlen);
4846
4847 /*
4848 * This works because the only protocols we care about don't require
4849 * special handling.
4850 * We'll fix it up properly in napi_frags_finish()
4851 */
4852 skb->protocol = eth->h_proto;
Herbert Xu76620aa2009-04-16 02:02:07 -07004853
Herbert Xu76620aa2009-04-16 02:02:07 -07004854 return skb;
4855}
Herbert Xu76620aa2009-04-16 02:02:07 -07004856
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004857gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07004858{
4859 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08004860
4861 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004862 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08004863
Ben Hutchingsae78dbf2014-01-10 22:17:24 +00004864 trace_napi_gro_frags_entry(skb);
4865
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004866 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
Herbert Xu5d38a072009-01-04 16:13:40 -08004867}
4868EXPORT_SYMBOL(napi_gro_frags);
4869
Tom Herbert573e8fc2014-08-22 13:33:47 -07004870/* Compute the checksum from gro_offset and return the folded value
4871 * after adding in any pseudo checksum.
4872 */
4873__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
4874{
4875 __wsum wsum;
4876 __sum16 sum;
4877
4878 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
4879
4880 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
4881 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
4882 if (likely(!sum)) {
4883 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
4884 !skb->csum_complete_sw)
4885 netdev_rx_csum_fault(skb->dev);
4886 }
4887
4888 NAPI_GRO_CB(skb)->csum = wsum;
4889 NAPI_GRO_CB(skb)->csum_valid = 1;
4890
4891 return sum;
4892}
4893EXPORT_SYMBOL(__skb_gro_checksum_complete);
4894
Eric Dumazete326bed2010-04-22 00:22:45 -07004895/*
Zhi Yong Wu855abcf2014-01-01 04:34:50 +08004896 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
Eric Dumazete326bed2010-04-22 00:22:45 -07004897 * Note: called with local irq disabled, but exits with local irq enabled.
4898 */
4899static void net_rps_action_and_irq_enable(struct softnet_data *sd)
4900{
4901#ifdef CONFIG_RPS
4902 struct softnet_data *remsd = sd->rps_ipi_list;
4903
4904 if (remsd) {
4905 sd->rps_ipi_list = NULL;
4906
4907 local_irq_enable();
4908
4909 /* Send pending IPI's to kick RPS processing on remote cpus. */
4910 while (remsd) {
4911 struct softnet_data *next = remsd->rps_ipi_next;
4912
4913 if (cpu_online(remsd->cpu))
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +01004914 smp_call_function_single_async(remsd->cpu,
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +01004915 &remsd->csd);
Eric Dumazete326bed2010-04-22 00:22:45 -07004916 remsd = next;
4917 }
4918 } else
4919#endif
4920 local_irq_enable();
4921}
4922
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08004923static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
4924{
4925#ifdef CONFIG_RPS
4926 return sd->rps_ipi_list != NULL;
4927#else
4928 return false;
4929#endif
4930}
4931
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004932static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004934 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004935 bool again = true;
4936 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Eric Dumazete326bed2010-04-22 00:22:45 -07004938 /* Check if we have pending ipi, its better to send them now,
4939 * not waiting net_rx_action() end.
4940 */
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08004941 if (sd_has_rps_ipi_waiting(sd)) {
Eric Dumazete326bed2010-04-22 00:22:45 -07004942 local_irq_disable();
4943 net_rps_action_and_irq_enable(sd);
4944 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08004945
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004946 napi->weight = weight_p;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004947 while (again) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Changli Gao6e7676c2010-04-27 15:07:33 -07004950 while ((skb = __skb_dequeue(&sd->process_queue))) {
Julian Anastasov2c17d272015-07-09 09:59:10 +03004951 rcu_read_lock();
Changli Gao6e7676c2010-04-27 15:07:33 -07004952 __netif_receive_skb(skb);
Julian Anastasov2c17d272015-07-09 09:59:10 +03004953 rcu_read_unlock();
Tom Herbert76cc8b12010-05-20 18:37:59 +00004954 input_queue_head_incr(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004955 if (++work >= quota)
Tom Herbert76cc8b12010-05-20 18:37:59 +00004956 return work;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004957
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004960 local_irq_disable();
Changli Gao6e7676c2010-04-27 15:07:33 -07004961 rps_lock(sd);
Tom Herbert11ef7a82014-06-30 09:50:40 -07004962 if (skb_queue_empty(&sd->input_pkt_queue)) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004963 /*
4964 * Inline a custom version of __napi_complete().
4965 * only current cpu owns and manipulates this napi,
Tom Herbert11ef7a82014-06-30 09:50:40 -07004966 * and NAPI_STATE_SCHED is the only possible flag set
4967 * on backlog.
4968 * We can use a plain write instead of clear_bit(),
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004969 * and we dont need an smp_mb() memory barrier.
4970 */
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004971 napi->state = 0;
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004972 again = false;
4973 } else {
4974 skb_queue_splice_tail_init(&sd->input_pkt_queue,
4975 &sd->process_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07004976 }
4977 rps_unlock(sd);
Paolo Abeni145dd5f2016-08-25 15:58:44 +02004978 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07004979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004981 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004984/**
4985 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07004986 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004987 *
Eric Dumazetbc9ad162014-10-28 18:05:13 -07004988 * The entry's receive function will be scheduled to run.
4989 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004990 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08004991void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004992{
4993 unsigned long flags;
4994
4995 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -05004996 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004997 local_irq_restore(flags);
4998}
4999EXPORT_SYMBOL(__napi_schedule);
5000
Eric Dumazetbc9ad162014-10-28 18:05:13 -07005001/**
5002 * __napi_schedule_irqoff - schedule for receive
5003 * @n: entry to schedule
5004 *
5005 * Variant of __napi_schedule() assuming hard irqs are masked
5006 */
5007void __napi_schedule_irqoff(struct napi_struct *n)
5008{
5009 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5010}
5011EXPORT_SYMBOL(__napi_schedule_irqoff);
5012
Herbert Xud565b0a2008-12-15 23:38:52 -08005013void __napi_complete(struct napi_struct *n)
5014{
5015 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
Herbert Xud565b0a2008-12-15 23:38:52 -08005016
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005017 list_del_init(&n->poll_list);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005018 smp_mb__before_atomic();
Herbert Xud565b0a2008-12-15 23:38:52 -08005019 clear_bit(NAPI_STATE_SCHED, &n->state);
5020}
5021EXPORT_SYMBOL(__napi_complete);
5022
Eric Dumazet3b47d302014-11-06 21:09:44 -08005023void napi_complete_done(struct napi_struct *n, int work_done)
Herbert Xud565b0a2008-12-15 23:38:52 -08005024{
5025 unsigned long flags;
5026
5027 /*
5028 * don't let napi dequeue from the cpu poll list
5029 * just in case its running on a different cpu
5030 */
5031 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
5032 return;
5033
Eric Dumazet3b47d302014-11-06 21:09:44 -08005034 if (n->gro_list) {
5035 unsigned long timeout = 0;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005036
Eric Dumazet3b47d302014-11-06 21:09:44 -08005037 if (work_done)
5038 timeout = n->dev->gro_flush_timeout;
5039
5040 if (timeout)
5041 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5042 HRTIMER_MODE_REL_PINNED);
5043 else
5044 napi_gro_flush(n, false);
5045 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005046 if (likely(list_empty(&n->poll_list))) {
5047 WARN_ON_ONCE(!test_and_clear_bit(NAPI_STATE_SCHED, &n->state));
5048 } else {
5049 /* If n->poll_list is not empty, we need to mask irqs */
5050 local_irq_save(flags);
5051 __napi_complete(n);
5052 local_irq_restore(flags);
5053 }
Herbert Xud565b0a2008-12-15 23:38:52 -08005054}
Eric Dumazet3b47d302014-11-06 21:09:44 -08005055EXPORT_SYMBOL(napi_complete_done);
Herbert Xud565b0a2008-12-15 23:38:52 -08005056
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005057/* must be called under rcu_read_lock(), as we dont take a reference */
Eric Dumazet02d62e82015-11-18 06:30:52 -08005058static struct napi_struct *napi_by_id(unsigned int napi_id)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005059{
5060 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
5061 struct napi_struct *napi;
5062
5063 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
5064 if (napi->napi_id == napi_id)
5065 return napi;
5066
5067 return NULL;
5068}
Eric Dumazet02d62e82015-11-18 06:30:52 -08005069
5070#if defined(CONFIG_NET_RX_BUSY_POLL)
Eric Dumazetce6aea92015-11-18 06:30:54 -08005071#define BUSY_POLL_BUDGET 8
Eric Dumazet02d62e82015-11-18 06:30:52 -08005072bool sk_busy_loop(struct sock *sk, int nonblock)
5073{
5074 unsigned long end_time = !nonblock ? sk_busy_loop_end_time(sk) : 0;
Eric Dumazetce6aea92015-11-18 06:30:54 -08005075 int (*busy_poll)(struct napi_struct *dev);
Eric Dumazet02d62e82015-11-18 06:30:52 -08005076 struct napi_struct *napi;
5077 int rc = false;
5078
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005079 rcu_read_lock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005080
5081 napi = napi_by_id(sk->sk_napi_id);
5082 if (!napi)
5083 goto out;
5084
Eric Dumazetce6aea92015-11-18 06:30:54 -08005085 /* Note: ndo_busy_poll method is optional in linux-4.5 */
5086 busy_poll = napi->dev->netdev_ops->ndo_busy_poll;
Eric Dumazet02d62e82015-11-18 06:30:52 -08005087
5088 do {
Eric Dumazetce6aea92015-11-18 06:30:54 -08005089 rc = 0;
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005090 local_bh_disable();
Eric Dumazetce6aea92015-11-18 06:30:54 -08005091 if (busy_poll) {
5092 rc = busy_poll(napi);
5093 } else if (napi_schedule_prep(napi)) {
5094 void *have = netpoll_poll_lock(napi);
5095
5096 if (test_bit(NAPI_STATE_SCHED, &napi->state)) {
5097 rc = napi->poll(napi, BUSY_POLL_BUDGET);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02005098 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
Eric Dumazetce6aea92015-11-18 06:30:54 -08005099 if (rc == BUSY_POLL_BUDGET) {
5100 napi_complete_done(napi, rc);
5101 napi_schedule(napi);
5102 }
5103 }
5104 netpoll_poll_unlock(have);
5105 }
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005106 if (rc > 0)
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005107 __NET_ADD_STATS(sock_net(sk),
5108 LINUX_MIB_BUSYPOLLRXPACKETS, rc);
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005109 local_bh_enable();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005110
5111 if (rc == LL_FLUSH_FAILED)
5112 break; /* permanent failure */
5113
Eric Dumazet02d62e82015-11-18 06:30:52 -08005114 cpu_relax();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005115 } while (!nonblock && skb_queue_empty(&sk->sk_receive_queue) &&
5116 !need_resched() && !busy_loop_timeout(end_time));
5117
5118 rc = !skb_queue_empty(&sk->sk_receive_queue);
5119out:
Eric Dumazet2a028ec2015-11-18 06:30:53 -08005120 rcu_read_unlock();
Eric Dumazet02d62e82015-11-18 06:30:52 -08005121 return rc;
5122}
5123EXPORT_SYMBOL(sk_busy_loop);
5124
5125#endif /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005126
5127void napi_hash_add(struct napi_struct *napi)
5128{
Eric Dumazetd64b5e82015-11-18 06:31:00 -08005129 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
5130 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005131 return;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005132
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005133 spin_lock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005134
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005135 /* 0..NR_CPUS+1 range is reserved for sender_cpu use */
5136 do {
5137 if (unlikely(++napi_gen_id < NR_CPUS + 1))
5138 napi_gen_id = NR_CPUS + 1;
5139 } while (napi_by_id(napi_gen_id));
5140 napi->napi_id = napi_gen_id;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005141
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005142 hlist_add_head_rcu(&napi->napi_hash_node,
5143 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005144
Eric Dumazet52bd2d62015-11-18 06:30:50 -08005145 spin_unlock(&napi_hash_lock);
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005146}
5147EXPORT_SYMBOL_GPL(napi_hash_add);
5148
5149/* Warning : caller is responsible to make sure rcu grace period
5150 * is respected before freeing memory containing @napi
5151 */
Eric Dumazet34cbe272015-11-18 06:31:02 -08005152bool napi_hash_del(struct napi_struct *napi)
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005153{
Eric Dumazet34cbe272015-11-18 06:31:02 -08005154 bool rcu_sync_needed = false;
5155
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005156 spin_lock(&napi_hash_lock);
5157
Eric Dumazet34cbe272015-11-18 06:31:02 -08005158 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
5159 rcu_sync_needed = true;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005160 hlist_del_rcu(&napi->napi_hash_node);
Eric Dumazet34cbe272015-11-18 06:31:02 -08005161 }
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005162 spin_unlock(&napi_hash_lock);
Eric Dumazet34cbe272015-11-18 06:31:02 -08005163 return rcu_sync_needed;
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03005164}
5165EXPORT_SYMBOL_GPL(napi_hash_del);
5166
Eric Dumazet3b47d302014-11-06 21:09:44 -08005167static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
5168{
5169 struct napi_struct *napi;
5170
5171 napi = container_of(timer, struct napi_struct, timer);
5172 if (napi->gro_list)
5173 napi_schedule(napi);
5174
5175 return HRTIMER_NORESTART;
5176}
5177
Herbert Xud565b0a2008-12-15 23:38:52 -08005178void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
5179 int (*poll)(struct napi_struct *, int), int weight)
5180{
5181 INIT_LIST_HEAD(&napi->poll_list);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005182 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
5183 napi->timer.function = napi_watchdog;
Herbert Xu4ae55442009-02-08 18:00:36 +00005184 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005185 napi->gro_list = NULL;
Herbert Xu5d38a072009-01-04 16:13:40 -08005186 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08005187 napi->poll = poll;
Eric Dumazet82dc3c62013-03-05 15:57:22 +00005188 if (weight > NAPI_POLL_WEIGHT)
5189 pr_err_once("netif_napi_add() called with weight %d on device %s\n",
5190 weight, dev->name);
Herbert Xud565b0a2008-12-15 23:38:52 -08005191 napi->weight = weight;
5192 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08005193 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08005194#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08005195 spin_lock_init(&napi->poll_lock);
5196 napi->poll_owner = -1;
5197#endif
5198 set_bit(NAPI_STATE_SCHED, &napi->state);
Eric Dumazet93d05d42015-11-18 06:31:03 -08005199 napi_hash_add(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08005200}
5201EXPORT_SYMBOL(netif_napi_add);
5202
Eric Dumazet3b47d302014-11-06 21:09:44 -08005203void napi_disable(struct napi_struct *n)
5204{
5205 might_sleep();
5206 set_bit(NAPI_STATE_DISABLE, &n->state);
5207
5208 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
5209 msleep(1);
Neil Horman2d8bff12015-09-23 14:57:58 -04005210 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
5211 msleep(1);
Eric Dumazet3b47d302014-11-06 21:09:44 -08005212
5213 hrtimer_cancel(&n->timer);
5214
5215 clear_bit(NAPI_STATE_DISABLE, &n->state);
5216}
5217EXPORT_SYMBOL(napi_disable);
5218
Eric Dumazet93d05d42015-11-18 06:31:03 -08005219/* Must be called in process context */
Herbert Xud565b0a2008-12-15 23:38:52 -08005220void netif_napi_del(struct napi_struct *napi)
5221{
Eric Dumazet93d05d42015-11-18 06:31:03 -08005222 might_sleep();
5223 if (napi_hash_del(napi))
5224 synchronize_net();
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08005225 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07005226 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08005227
Eric Dumazet289dccb2013-12-20 14:29:08 -08005228 kfree_skb_list(napi->gro_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08005229 napi->gro_list = NULL;
Herbert Xu4ae55442009-02-08 18:00:36 +00005230 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005231}
5232EXPORT_SYMBOL(netif_napi_del);
5233
Herbert Xu726ce702014-12-21 07:16:21 +11005234static int napi_poll(struct napi_struct *n, struct list_head *repoll)
5235{
5236 void *have;
5237 int work, weight;
5238
5239 list_del_init(&n->poll_list);
5240
5241 have = netpoll_poll_lock(n);
5242
5243 weight = n->weight;
5244
5245 /* This NAPI_STATE_SCHED test is for avoiding a race
5246 * with netpoll's poll_napi(). Only the entity which
5247 * obtains the lock and sees NAPI_STATE_SCHED set will
5248 * actually make the ->poll() call. Therefore we avoid
5249 * accidentally calling ->poll() when NAPI is not scheduled.
5250 */
5251 work = 0;
5252 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
5253 work = n->poll(n, weight);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +02005254 trace_napi_poll(n, work, weight);
Herbert Xu726ce702014-12-21 07:16:21 +11005255 }
5256
5257 WARN_ON_ONCE(work > weight);
5258
5259 if (likely(work < weight))
5260 goto out_unlock;
5261
5262 /* Drivers must not modify the NAPI state if they
5263 * consume the entire weight. In such cases this code
5264 * still "owns" the NAPI instance and therefore can
5265 * move the instance around on the list at-will.
5266 */
5267 if (unlikely(napi_disable_pending(n))) {
5268 napi_complete(n);
5269 goto out_unlock;
5270 }
5271
5272 if (n->gro_list) {
5273 /* flush too old packets
5274 * If HZ < 1000, flush all packets.
5275 */
5276 napi_gro_flush(n, HZ >= 1000);
5277 }
5278
Herbert Xu001ce542014-12-21 07:16:22 +11005279 /* Some drivers may have called napi_schedule
5280 * prior to exhausting their budget.
5281 */
5282 if (unlikely(!list_empty(&n->poll_list))) {
5283 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
5284 n->dev ? n->dev->name : "backlog");
5285 goto out_unlock;
5286 }
5287
Herbert Xu726ce702014-12-21 07:16:21 +11005288 list_add_tail(&n->poll_list, repoll);
5289
5290out_unlock:
5291 netpoll_poll_unlock(have);
5292
5293 return work;
5294}
5295
Emese Revfy0766f782016-06-20 20:42:34 +02005296static __latent_entropy void net_rx_action(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297{
Christoph Lameter903ceff2014-08-17 12:30:35 -05005298 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
Stephen Hemminger24f8b232008-11-03 17:14:38 -08005299 unsigned long time_limit = jiffies + 2;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07005300 int budget = netdev_budget;
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005301 LIST_HEAD(list);
5302 LIST_HEAD(repoll);
Matt Mackall53fb95d2005-08-11 19:27:43 -07005303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 local_irq_disable();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005305 list_splice_init(&sd->poll_list, &list);
5306 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005308 for (;;) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005309 struct napi_struct *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005311 if (list_empty(&list)) {
5312 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
5313 return;
5314 break;
5315 }
5316
Herbert Xu6bd373e2014-12-21 07:16:24 +11005317 n = list_first_entry(&list, struct napi_struct, poll_list);
5318 budget -= napi_poll(n, &repoll);
5319
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005320 /* If softirq window is exhausted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08005321 * Allow this to run for 2 jiffies since which will allow
5322 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005323 */
Herbert Xuceb8d5b2014-12-21 07:16:25 +11005324 if (unlikely(budget <= 0 ||
5325 time_after_eq(jiffies, time_limit))) {
5326 sd->time_squeeze++;
5327 break;
5328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 }
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005330
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +01005331 __kfree_skb_flush();
Eric Dumazetd75b1ad2014-11-02 06:19:33 -08005332 local_irq_disable();
5333
5334 list_splice_tail_init(&sd->poll_list, &list);
5335 list_splice_tail(&repoll, &list);
5336 list_splice(&list, &sd->poll_list);
5337 if (!list_empty(&sd->poll_list))
5338 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
5339
Eric Dumazete326bed2010-04-22 00:22:45 -07005340 net_rps_action_and_irq_enable(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005343struct netdev_adjacent {
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005344 struct net_device *dev;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005345
5346 /* upper master flag, there can only be one master device per list */
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005347 bool master;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005348
Veaceslav Falico5d261912013-08-28 23:25:05 +02005349 /* counter for the number of times this device was added to us */
5350 u16 ref_nr;
5351
Veaceslav Falico402dae92013-09-25 09:20:09 +02005352 /* private field for the users */
5353 void *private;
5354
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005355 struct list_head list;
5356 struct rcu_head rcu;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005357};
5358
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005359static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005360 struct list_head *adj_list)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005361{
Veaceslav Falico5d261912013-08-28 23:25:05 +02005362 struct netdev_adjacent *adj;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005363
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005364 list_for_each_entry(adj, adj_list, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02005365 if (adj->dev == adj_dev)
5366 return adj;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005367 }
5368 return NULL;
5369}
5370
5371/**
5372 * netdev_has_upper_dev - Check if device is linked to an upper device
5373 * @dev: device
5374 * @upper_dev: upper device to check
5375 *
5376 * Find out if a device is linked to specified upper device and return true
5377 * in case it is. Note that this checks only immediate upper device,
5378 * not through a complete stack of devices. The caller must hold the RTNL lock.
5379 */
5380bool netdev_has_upper_dev(struct net_device *dev,
5381 struct net_device *upper_dev)
5382{
5383 ASSERT_RTNL();
5384
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005385 return __netdev_find_adj(upper_dev, &dev->all_adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005386}
5387EXPORT_SYMBOL(netdev_has_upper_dev);
5388
5389/**
5390 * netdev_has_any_upper_dev - Check if device is linked to some device
5391 * @dev: device
5392 *
5393 * Find out if a device is linked to an upper device and return true in case
5394 * it is. The caller must hold the RTNL lock.
5395 */
Ido Schimmel73ee5a732017-09-01 10:52:31 +02005396bool netdev_has_any_upper_dev(struct net_device *dev)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005397{
5398 ASSERT_RTNL();
5399
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005400 return !list_empty(&dev->all_adj_list.upper);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005401}
Ido Schimmel73ee5a732017-09-01 10:52:31 +02005402EXPORT_SYMBOL(netdev_has_any_upper_dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005403
5404/**
5405 * netdev_master_upper_dev_get - Get master upper device
5406 * @dev: device
5407 *
5408 * Find a master upper device and return pointer to it or NULL in case
5409 * it's not there. The caller must hold the RTNL lock.
5410 */
5411struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
5412{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005413 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005414
5415 ASSERT_RTNL();
5416
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005417 if (list_empty(&dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005418 return NULL;
5419
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005420 upper = list_first_entry(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005421 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005422 if (likely(upper->master))
5423 return upper->dev;
5424 return NULL;
5425}
5426EXPORT_SYMBOL(netdev_master_upper_dev_get);
5427
Veaceslav Falicob6ccba42013-09-25 09:20:23 +02005428void *netdev_adjacent_get_private(struct list_head *adj_list)
5429{
5430 struct netdev_adjacent *adj;
5431
5432 adj = list_entry(adj_list, struct netdev_adjacent, list);
5433
5434 return adj->private;
5435}
5436EXPORT_SYMBOL(netdev_adjacent_get_private);
5437
Veaceslav Falico31088a12013-09-25 09:20:12 +02005438/**
Vlad Yasevich44a40852014-05-16 17:20:38 -04005439 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
5440 * @dev: device
5441 * @iter: list_head ** of the current position
5442 *
5443 * Gets the next device from the dev's upper list, starting from iter
5444 * position. The caller must hold RCU read lock.
5445 */
5446struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
5447 struct list_head **iter)
5448{
5449 struct netdev_adjacent *upper;
5450
5451 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5452
5453 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5454
5455 if (&upper->list == &dev->adj_list.upper)
5456 return NULL;
5457
5458 *iter = &upper->list;
5459
5460 return upper->dev;
5461}
5462EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
5463
5464/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02005465 * netdev_all_upper_get_next_dev_rcu - Get the next dev from upper list
Veaceslav Falico48311f42013-08-28 23:25:07 +02005466 * @dev: device
5467 * @iter: list_head ** of the current position
5468 *
5469 * Gets the next device from the dev's upper list, starting from iter
5470 * position. The caller must hold RCU read lock.
5471 */
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005472struct net_device *netdev_all_upper_get_next_dev_rcu(struct net_device *dev,
5473 struct list_head **iter)
Veaceslav Falico48311f42013-08-28 23:25:07 +02005474{
5475 struct netdev_adjacent *upper;
5476
John Fastabend85328242013-11-26 06:33:52 +00005477 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
Veaceslav Falico48311f42013-08-28 23:25:07 +02005478
5479 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5480
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005481 if (&upper->list == &dev->all_adj_list.upper)
Veaceslav Falico48311f42013-08-28 23:25:07 +02005482 return NULL;
5483
5484 *iter = &upper->list;
5485
5486 return upper->dev;
5487}
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005488EXPORT_SYMBOL(netdev_all_upper_get_next_dev_rcu);
Veaceslav Falico48311f42013-08-28 23:25:07 +02005489
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005490/**
Veaceslav Falico31088a12013-09-25 09:20:12 +02005491 * netdev_lower_get_next_private - Get the next ->private from the
5492 * lower neighbour list
5493 * @dev: device
5494 * @iter: list_head ** of the current position
5495 *
5496 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5497 * list, starting from iter position. The caller must hold either hold the
5498 * RTNL lock or its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00005499 * list will remain unchanged.
Veaceslav Falico31088a12013-09-25 09:20:12 +02005500 */
5501void *netdev_lower_get_next_private(struct net_device *dev,
5502 struct list_head **iter)
5503{
5504 struct netdev_adjacent *lower;
5505
5506 lower = list_entry(*iter, struct netdev_adjacent, list);
5507
5508 if (&lower->list == &dev->adj_list.lower)
5509 return NULL;
5510
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02005511 *iter = lower->list.next;
Veaceslav Falico31088a12013-09-25 09:20:12 +02005512
5513 return lower->private;
5514}
5515EXPORT_SYMBOL(netdev_lower_get_next_private);
5516
5517/**
5518 * netdev_lower_get_next_private_rcu - Get the next ->private from the
5519 * lower neighbour list, RCU
5520 * variant
5521 * @dev: device
5522 * @iter: list_head ** of the current position
5523 *
5524 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5525 * list, starting from iter position. The caller must hold RCU read lock.
5526 */
5527void *netdev_lower_get_next_private_rcu(struct net_device *dev,
5528 struct list_head **iter)
5529{
5530 struct netdev_adjacent *lower;
5531
5532 WARN_ON_ONCE(!rcu_read_lock_held());
5533
5534 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5535
5536 if (&lower->list == &dev->adj_list.lower)
5537 return NULL;
5538
Veaceslav Falico6859e7d2014-04-07 11:25:12 +02005539 *iter = &lower->list;
Veaceslav Falico31088a12013-09-25 09:20:12 +02005540
5541 return lower->private;
5542}
5543EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
5544
5545/**
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005546 * netdev_lower_get_next - Get the next device from the lower neighbour
5547 * list
5548 * @dev: device
5549 * @iter: list_head ** of the current position
5550 *
5551 * Gets the next netdev_adjacent from the dev's lower neighbour
5552 * list, starting from iter position. The caller must hold RTNL lock or
5553 * its own locking that guarantees that the neighbour lower
subashab@codeaurora.orgb4691392015-07-24 03:03:29 +00005554 * list will remain unchanged.
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005555 */
5556void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
5557{
5558 struct netdev_adjacent *lower;
5559
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01005560 lower = list_entry(*iter, struct netdev_adjacent, list);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005561
5562 if (&lower->list == &dev->adj_list.lower)
5563 return NULL;
5564
Nikolay Aleksandrovcfdd28b2016-02-17 18:00:31 +01005565 *iter = lower->list.next;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04005566
5567 return lower->dev;
5568}
5569EXPORT_SYMBOL(netdev_lower_get_next);
5570
5571/**
Jiri Pirko7ce856a2016-07-04 08:23:12 +02005572 * netdev_all_lower_get_next - Get the next device from all lower neighbour list
5573 * @dev: device
5574 * @iter: list_head ** of the current position
5575 *
5576 * Gets the next netdev_adjacent from the dev's all lower neighbour
5577 * list, starting from iter position. The caller must hold RTNL lock or
5578 * its own locking that guarantees that the neighbour all lower
5579 * list will remain unchanged.
5580 */
5581struct net_device *netdev_all_lower_get_next(struct net_device *dev, struct list_head **iter)
5582{
5583 struct netdev_adjacent *lower;
5584
5585 lower = list_entry(*iter, struct netdev_adjacent, list);
5586
5587 if (&lower->list == &dev->all_adj_list.lower)
5588 return NULL;
5589
5590 *iter = lower->list.next;
5591
5592 return lower->dev;
5593}
5594EXPORT_SYMBOL(netdev_all_lower_get_next);
5595
5596/**
5597 * netdev_all_lower_get_next_rcu - Get the next device from all
5598 * lower neighbour list, RCU variant
5599 * @dev: device
5600 * @iter: list_head ** of the current position
5601 *
5602 * Gets the next netdev_adjacent from the dev's all lower neighbour
5603 * list, starting from iter position. The caller must hold RCU read lock.
5604 */
5605struct net_device *netdev_all_lower_get_next_rcu(struct net_device *dev,
5606 struct list_head **iter)
5607{
5608 struct netdev_adjacent *lower;
5609
Ido Schimmele4961b02016-10-19 16:57:08 +03005610 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
Jiri Pirko7ce856a2016-07-04 08:23:12 +02005611
Ido Schimmele4961b02016-10-19 16:57:08 +03005612 if (&lower->list == &dev->all_adj_list.lower)
5613 return NULL;
5614
5615 *iter = &lower->list;
5616
5617 return lower->dev;
Jiri Pirko7ce856a2016-07-04 08:23:12 +02005618}
5619EXPORT_SYMBOL(netdev_all_lower_get_next_rcu);
5620
5621/**
dingtianhonge001bfa2013-12-13 10:19:55 +08005622 * netdev_lower_get_first_private_rcu - Get the first ->private from the
5623 * lower neighbour list, RCU
5624 * variant
5625 * @dev: device
5626 *
5627 * Gets the first netdev_adjacent->private from the dev's lower neighbour
5628 * list. The caller must hold RCU read lock.
5629 */
5630void *netdev_lower_get_first_private_rcu(struct net_device *dev)
5631{
5632 struct netdev_adjacent *lower;
5633
5634 lower = list_first_or_null_rcu(&dev->adj_list.lower,
5635 struct netdev_adjacent, list);
5636 if (lower)
5637 return lower->private;
5638 return NULL;
5639}
5640EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
5641
5642/**
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005643 * netdev_master_upper_dev_get_rcu - Get master upper device
5644 * @dev: device
5645 *
5646 * Find a master upper device and return pointer to it or NULL in case
5647 * it's not there. The caller must hold the RCU read lock.
5648 */
5649struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
5650{
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005651 struct netdev_adjacent *upper;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005652
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005653 upper = list_first_or_null_rcu(&dev->adj_list.upper,
Veaceslav Falicoaa9d8562013-08-28 23:25:04 +02005654 struct netdev_adjacent, list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005655 if (upper && likely(upper->master))
5656 return upper->dev;
5657 return NULL;
5658}
5659EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
5660
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05305661static int netdev_adjacent_sysfs_add(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01005662 struct net_device *adj_dev,
5663 struct list_head *dev_list)
5664{
5665 char linkname[IFNAMSIZ+7];
5666 sprintf(linkname, dev_list == &dev->adj_list.upper ?
5667 "upper_%s" : "lower_%s", adj_dev->name);
5668 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
5669 linkname);
5670}
Rashika Kheria0a59f3a2014-02-09 20:26:25 +05305671static void netdev_adjacent_sysfs_del(struct net_device *dev,
Veaceslav Falico3ee32702014-01-14 21:58:50 +01005672 char *name,
5673 struct list_head *dev_list)
5674{
5675 char linkname[IFNAMSIZ+7];
5676 sprintf(linkname, dev_list == &dev->adj_list.upper ?
5677 "upper_%s" : "lower_%s", name);
5678 sysfs_remove_link(&(dev->dev.kobj), linkname);
5679}
5680
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04005681static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
5682 struct net_device *adj_dev,
5683 struct list_head *dev_list)
5684{
5685 return (dev_list == &dev->adj_list.upper ||
5686 dev_list == &dev->adj_list.lower) &&
5687 net_eq(dev_net(dev), dev_net(adj_dev));
5688}
Veaceslav Falico3ee32702014-01-14 21:58:50 +01005689
Veaceslav Falico5d261912013-08-28 23:25:05 +02005690static int __netdev_adjacent_dev_insert(struct net_device *dev,
5691 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06005692 u16 ref_nr,
Veaceslav Falico7863c052013-09-25 09:20:06 +02005693 struct list_head *dev_list,
Veaceslav Falico402dae92013-09-25 09:20:09 +02005694 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02005695{
5696 struct netdev_adjacent *adj;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005697 int ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005698
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005699 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005700
5701 if (adj) {
Andrew Collins93409032016-10-03 13:43:02 -06005702 adj->ref_nr += ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005703 return 0;
5704 }
5705
5706 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
5707 if (!adj)
5708 return -ENOMEM;
5709
5710 adj->dev = adj_dev;
5711 adj->master = master;
Andrew Collins93409032016-10-03 13:43:02 -06005712 adj->ref_nr = ref_nr;
Veaceslav Falico402dae92013-09-25 09:20:09 +02005713 adj->private = private;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005714 dev_hold(adj_dev);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005715
5716 pr_debug("dev_hold for %s, because of link added from %s to %s\n",
5717 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005718
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04005719 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
Veaceslav Falico3ee32702014-01-14 21:58:50 +01005720 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
Veaceslav Falico5831d66e2013-09-25 09:20:32 +02005721 if (ret)
5722 goto free_adj;
5723 }
5724
Veaceslav Falico7863c052013-09-25 09:20:06 +02005725 /* Ensure that master link is always the first item in list. */
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005726 if (master) {
5727 ret = sysfs_create_link(&(dev->dev.kobj),
5728 &(adj_dev->dev.kobj), "master");
5729 if (ret)
Veaceslav Falico5831d66e2013-09-25 09:20:32 +02005730 goto remove_symlinks;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005731
Veaceslav Falico7863c052013-09-25 09:20:06 +02005732 list_add_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005733 } else {
Veaceslav Falico7863c052013-09-25 09:20:06 +02005734 list_add_tail_rcu(&adj->list, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005735 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02005736
5737 return 0;
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005738
Veaceslav Falico5831d66e2013-09-25 09:20:32 +02005739remove_symlinks:
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04005740 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01005741 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005742free_adj:
5743 kfree(adj);
Nikolay Aleksandrov974daef2013-10-23 15:28:56 +02005744 dev_put(adj_dev);
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005745
5746 return ret;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005747}
5748
stephen hemminger1d143d92013-12-29 14:01:29 -08005749static void __netdev_adjacent_dev_remove(struct net_device *dev,
5750 struct net_device *adj_dev,
Andrew Collins93409032016-10-03 13:43:02 -06005751 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08005752 struct list_head *dev_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02005753{
5754 struct netdev_adjacent *adj;
5755
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005756 adj = __netdev_find_adj(adj_dev, dev_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005757
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005758 if (!adj) {
5759 pr_err("tried to remove device %s from %s\n",
5760 dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005761 BUG();
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005762 }
Veaceslav Falico5d261912013-08-28 23:25:05 +02005763
Andrew Collins93409032016-10-03 13:43:02 -06005764 if (adj->ref_nr > ref_nr) {
5765 pr_debug("%s to %s ref_nr-%d = %d\n", dev->name, adj_dev->name,
5766 ref_nr, adj->ref_nr-ref_nr);
5767 adj->ref_nr -= ref_nr;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005768 return;
5769 }
5770
Veaceslav Falico842d67a2013-09-25 09:20:31 +02005771 if (adj->master)
5772 sysfs_remove_link(&(dev->dev.kobj), "master");
5773
Alexander Y. Fomichev7ce64c72014-09-15 14:22:35 +04005774 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
Veaceslav Falico3ee32702014-01-14 21:58:50 +01005775 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
Veaceslav Falico5831d66e2013-09-25 09:20:32 +02005776
Veaceslav Falico5d261912013-08-28 23:25:05 +02005777 list_del_rcu(&adj->list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005778 pr_debug("dev_put for %s, because link removed from %s to %s\n",
5779 adj_dev->name, dev->name, adj_dev->name);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005780 dev_put(adj_dev);
5781 kfree_rcu(adj, rcu);
5782}
5783
stephen hemminger1d143d92013-12-29 14:01:29 -08005784static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
5785 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06005786 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08005787 struct list_head *up_list,
5788 struct list_head *down_list,
5789 void *private, bool master)
Veaceslav Falico5d261912013-08-28 23:25:05 +02005790{
5791 int ret;
5792
Andrew Collins93409032016-10-03 13:43:02 -06005793 ret = __netdev_adjacent_dev_insert(dev, upper_dev, ref_nr, up_list,
5794 private, master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005795 if (ret)
5796 return ret;
5797
Andrew Collins93409032016-10-03 13:43:02 -06005798 ret = __netdev_adjacent_dev_insert(upper_dev, dev, ref_nr, down_list,
5799 private, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005800 if (ret) {
Andrew Collins93409032016-10-03 13:43:02 -06005801 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005802 return ret;
5803 }
5804
5805 return 0;
5806}
5807
stephen hemminger1d143d92013-12-29 14:01:29 -08005808static int __netdev_adjacent_dev_link(struct net_device *dev,
Andrew Collins93409032016-10-03 13:43:02 -06005809 struct net_device *upper_dev,
5810 u16 ref_nr)
Veaceslav Falico5d261912013-08-28 23:25:05 +02005811{
Andrew Collins93409032016-10-03 13:43:02 -06005812 return __netdev_adjacent_dev_link_lists(dev, upper_dev, ref_nr,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005813 &dev->all_adj_list.upper,
5814 &upper_dev->all_adj_list.lower,
Veaceslav Falico402dae92013-09-25 09:20:09 +02005815 NULL, false);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005816}
5817
stephen hemminger1d143d92013-12-29 14:01:29 -08005818static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
5819 struct net_device *upper_dev,
Andrew Collins93409032016-10-03 13:43:02 -06005820 u16 ref_nr,
stephen hemminger1d143d92013-12-29 14:01:29 -08005821 struct list_head *up_list,
5822 struct list_head *down_list)
Veaceslav Falico5d261912013-08-28 23:25:05 +02005823{
Andrew Collins93409032016-10-03 13:43:02 -06005824 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
5825 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005826}
5827
stephen hemminger1d143d92013-12-29 14:01:29 -08005828static void __netdev_adjacent_dev_unlink(struct net_device *dev,
Andrew Collins93409032016-10-03 13:43:02 -06005829 struct net_device *upper_dev,
5830 u16 ref_nr)
Veaceslav Falico5d261912013-08-28 23:25:05 +02005831{
Andrew Collins93409032016-10-03 13:43:02 -06005832 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, ref_nr,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005833 &dev->all_adj_list.upper,
5834 &upper_dev->all_adj_list.lower);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005835}
5836
stephen hemminger1d143d92013-12-29 14:01:29 -08005837static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
5838 struct net_device *upper_dev,
5839 void *private, bool master)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005840{
Andrew Collins93409032016-10-03 13:43:02 -06005841 int ret = __netdev_adjacent_dev_link(dev, upper_dev, 1);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005842
5843 if (ret)
5844 return ret;
5845
Andrew Collins93409032016-10-03 13:43:02 -06005846 ret = __netdev_adjacent_dev_link_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005847 &dev->adj_list.upper,
5848 &upper_dev->adj_list.lower,
Veaceslav Falico402dae92013-09-25 09:20:09 +02005849 private, master);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005850 if (ret) {
Andrew Collins93409032016-10-03 13:43:02 -06005851 __netdev_adjacent_dev_unlink(dev, upper_dev, 1);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005852 return ret;
5853 }
5854
5855 return 0;
5856}
5857
stephen hemminger1d143d92013-12-29 14:01:29 -08005858static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
5859 struct net_device *upper_dev)
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005860{
Andrew Collins93409032016-10-03 13:43:02 -06005861 __netdev_adjacent_dev_unlink(dev, upper_dev, 1);
5862 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005863 &dev->adj_list.upper,
5864 &upper_dev->adj_list.lower);
5865}
Veaceslav Falico5d261912013-08-28 23:25:05 +02005866
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005867static int __netdev_upper_dev_link(struct net_device *dev,
Veaceslav Falico402dae92013-09-25 09:20:09 +02005868 struct net_device *upper_dev, bool master,
Jiri Pirko29bf24a2015-12-03 12:12:11 +01005869 void *upper_priv, void *upper_info)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005870{
Jiri Pirko0e4ead92015-08-27 09:31:18 +02005871 struct netdev_notifier_changeupper_info changeupper_info;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005872 struct netdev_adjacent *i, *j, *to_i, *to_j;
5873 int ret = 0;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005874
5875 ASSERT_RTNL();
5876
5877 if (dev == upper_dev)
5878 return -EBUSY;
5879
5880 /* To prevent loops, check if dev is not upper device to upper_dev. */
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005881 if (__netdev_find_adj(dev, &upper_dev->all_adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005882 return -EBUSY;
5883
Michal Kubeček6ea29da2015-09-24 10:59:05 +02005884 if (__netdev_find_adj(upper_dev, &dev->adj_list.upper))
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005885 return -EEXIST;
5886
5887 if (master && netdev_master_upper_dev_get(dev))
5888 return -EBUSY;
5889
Jiri Pirko0e4ead92015-08-27 09:31:18 +02005890 changeupper_info.upper_dev = upper_dev;
5891 changeupper_info.master = master;
5892 changeupper_info.linking = true;
Jiri Pirko29bf24a2015-12-03 12:12:11 +01005893 changeupper_info.upper_info = upper_info;
Jiri Pirko0e4ead92015-08-27 09:31:18 +02005894
Jiri Pirko573c7ba2015-10-16 14:01:22 +02005895 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, dev,
5896 &changeupper_info.info);
5897 ret = notifier_to_errno(ret);
5898 if (ret)
5899 return ret;
5900
Jiri Pirko6dffb042015-12-03 12:12:10 +01005901 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
Veaceslav Falico402dae92013-09-25 09:20:09 +02005902 master);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005903 if (ret)
5904 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005905
Veaceslav Falico5d261912013-08-28 23:25:05 +02005906 /* Now that we linked these devs, make all the upper_dev's
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005907 * all_adj_list.upper visible to every dev's all_adj_list.lower an
Veaceslav Falico5d261912013-08-28 23:25:05 +02005908 * versa, and don't forget the devices itself. All of these
5909 * links are non-neighbours.
5910 */
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005911 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
5912 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
5913 pr_debug("Interlinking %s with %s, non-neighbour\n",
5914 i->dev->name, j->dev->name);
Andrew Collins93409032016-10-03 13:43:02 -06005915 ret = __netdev_adjacent_dev_link(i->dev, j->dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005916 if (ret)
5917 goto rollback_mesh;
5918 }
5919 }
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005920
Veaceslav Falico5d261912013-08-28 23:25:05 +02005921 /* add dev to every upper_dev's upper device */
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005922 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
5923 pr_debug("linking %s's upper device %s with %s\n",
5924 upper_dev->name, i->dev->name, dev->name);
Andrew Collins93409032016-10-03 13:43:02 -06005925 ret = __netdev_adjacent_dev_link(dev, i->dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005926 if (ret)
5927 goto rollback_upper_mesh;
5928 }
5929
5930 /* add upper_dev to every dev's lower device */
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005931 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
5932 pr_debug("linking %s's lower device %s with %s\n", dev->name,
5933 i->dev->name, upper_dev->name);
Andrew Collins93409032016-10-03 13:43:02 -06005934 ret = __netdev_adjacent_dev_link(i->dev, upper_dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005935 if (ret)
5936 goto rollback_lower_mesh;
5937 }
5938
Ido Schimmelb03804e2015-12-03 12:12:03 +01005939 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
5940 &changeupper_info.info);
5941 ret = notifier_to_errno(ret);
5942 if (ret)
5943 goto rollback_lower_mesh;
5944
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005945 return 0;
Veaceslav Falico5d261912013-08-28 23:25:05 +02005946
5947rollback_lower_mesh:
5948 to_i = i;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005949 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02005950 if (i == to_i)
5951 break;
Andrew Collins93409032016-10-03 13:43:02 -06005952 __netdev_adjacent_dev_unlink(i->dev, upper_dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005953 }
5954
5955 i = NULL;
5956
5957rollback_upper_mesh:
5958 to_i = i;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005959 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02005960 if (i == to_i)
5961 break;
Andrew Collins93409032016-10-03 13:43:02 -06005962 __netdev_adjacent_dev_unlink(dev, i->dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005963 }
5964
5965 i = j = NULL;
5966
5967rollback_mesh:
5968 to_i = i;
5969 to_j = j;
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005970 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
5971 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
Veaceslav Falico5d261912013-08-28 23:25:05 +02005972 if (i == to_i && j == to_j)
5973 break;
Andrew Collins93409032016-10-03 13:43:02 -06005974 __netdev_adjacent_dev_unlink(i->dev, j->dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005975 }
5976 if (i == to_i)
5977 break;
5978 }
5979
Veaceslav Falico2f268f12013-09-25 09:20:07 +02005980 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02005981
5982 return ret;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005983}
5984
5985/**
5986 * netdev_upper_dev_link - Add a link to the upper device
5987 * @dev: device
5988 * @upper_dev: new upper device
5989 *
5990 * Adds a link to device which is upper to this one. The caller must hold
5991 * the RTNL lock. On a failure a negative errno code is returned.
5992 * On success the reference counts are adjusted and the function
5993 * returns zero.
5994 */
5995int netdev_upper_dev_link(struct net_device *dev,
5996 struct net_device *upper_dev)
5997{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01005998 return __netdev_upper_dev_link(dev, upper_dev, false, NULL, NULL);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005999}
6000EXPORT_SYMBOL(netdev_upper_dev_link);
6001
6002/**
6003 * netdev_master_upper_dev_link - Add a master link to the upper device
6004 * @dev: device
6005 * @upper_dev: new upper device
Jiri Pirko6dffb042015-12-03 12:12:10 +01006006 * @upper_priv: upper device private
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006007 * @upper_info: upper info to be passed down via notifier
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006008 *
6009 * Adds a link to device which is upper to this one. In this case, only
6010 * one master upper device can be linked, although other non-master devices
6011 * might be linked as well. The caller must hold the RTNL lock.
6012 * On a failure a negative errno code is returned. On success the reference
6013 * counts are adjusted and the function returns zero.
6014 */
6015int netdev_master_upper_dev_link(struct net_device *dev,
Jiri Pirko6dffb042015-12-03 12:12:10 +01006016 struct net_device *upper_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006017 void *upper_priv, void *upper_info)
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006018{
Jiri Pirko29bf24a2015-12-03 12:12:11 +01006019 return __netdev_upper_dev_link(dev, upper_dev, true,
6020 upper_priv, upper_info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006021}
6022EXPORT_SYMBOL(netdev_master_upper_dev_link);
6023
6024/**
6025 * netdev_upper_dev_unlink - Removes a link to upper device
6026 * @dev: device
6027 * @upper_dev: new upper device
6028 *
6029 * Removes a link to device which is upper to this one. The caller must hold
6030 * the RTNL lock.
6031 */
6032void netdev_upper_dev_unlink(struct net_device *dev,
6033 struct net_device *upper_dev)
6034{
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006035 struct netdev_notifier_changeupper_info changeupper_info;
Veaceslav Falico5d261912013-08-28 23:25:05 +02006036 struct netdev_adjacent *i, *j;
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006037 ASSERT_RTNL();
6038
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006039 changeupper_info.upper_dev = upper_dev;
6040 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
6041 changeupper_info.linking = false;
6042
Jiri Pirko573c7ba2015-10-16 14:01:22 +02006043 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, dev,
6044 &changeupper_info.info);
6045
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006046 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006047
6048 /* Here is the tricky part. We must remove all dev's lower
6049 * devices from all upper_dev's upper devices and vice
6050 * versa, to maintain the graph relationship.
6051 */
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006052 list_for_each_entry(i, &dev->all_adj_list.lower, list)
6053 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list)
Andrew Collins93409032016-10-03 13:43:02 -06006054 __netdev_adjacent_dev_unlink(i->dev, j->dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006055
6056 /* remove also the devices itself from lower/upper device
6057 * list
6058 */
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006059 list_for_each_entry(i, &dev->all_adj_list.lower, list)
Andrew Collins93409032016-10-03 13:43:02 -06006060 __netdev_adjacent_dev_unlink(i->dev, upper_dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006061
Veaceslav Falico2f268f12013-09-25 09:20:07 +02006062 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list)
Andrew Collins93409032016-10-03 13:43:02 -06006063 __netdev_adjacent_dev_unlink(dev, i->dev, i->ref_nr);
Veaceslav Falico5d261912013-08-28 23:25:05 +02006064
Jiri Pirko0e4ead92015-08-27 09:31:18 +02006065 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
6066 &changeupper_info.info);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006067}
6068EXPORT_SYMBOL(netdev_upper_dev_unlink);
6069
Moni Shoua61bd3852015-02-03 16:48:29 +02006070/**
6071 * netdev_bonding_info_change - Dispatch event about slave change
6072 * @dev: device
Masanari Iida4a26e4532015-02-14 22:26:34 +09006073 * @bonding_info: info to dispatch
Moni Shoua61bd3852015-02-03 16:48:29 +02006074 *
6075 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
6076 * The caller must hold the RTNL lock.
6077 */
6078void netdev_bonding_info_change(struct net_device *dev,
6079 struct netdev_bonding_info *bonding_info)
6080{
6081 struct netdev_notifier_bonding_info info;
6082
6083 memcpy(&info.bonding_info, bonding_info,
6084 sizeof(struct netdev_bonding_info));
6085 call_netdevice_notifiers_info(NETDEV_BONDING_INFO, dev,
6086 &info.info);
6087}
6088EXPORT_SYMBOL(netdev_bonding_info_change);
6089
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08006090static void netdev_adjacent_add_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006091{
6092 struct netdev_adjacent *iter;
6093
6094 struct net *net = dev_net(dev);
6095
6096 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006097 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006098 continue;
6099 netdev_adjacent_sysfs_add(iter->dev, dev,
6100 &iter->dev->adj_list.lower);
6101 netdev_adjacent_sysfs_add(dev, iter->dev,
6102 &dev->adj_list.upper);
6103 }
6104
6105 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006106 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006107 continue;
6108 netdev_adjacent_sysfs_add(iter->dev, dev,
6109 &iter->dev->adj_list.upper);
6110 netdev_adjacent_sysfs_add(dev, iter->dev,
6111 &dev->adj_list.lower);
6112 }
6113}
6114
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08006115static void netdev_adjacent_del_links(struct net_device *dev)
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006116{
6117 struct netdev_adjacent *iter;
6118
6119 struct net *net = dev_net(dev);
6120
6121 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006122 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006123 continue;
6124 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6125 &iter->dev->adj_list.lower);
6126 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6127 &dev->adj_list.upper);
6128 }
6129
6130 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006131 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006132 continue;
6133 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6134 &iter->dev->adj_list.upper);
6135 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6136 &dev->adj_list.lower);
6137 }
6138}
6139
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006140void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
Veaceslav Falico402dae92013-09-25 09:20:09 +02006141{
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006142 struct netdev_adjacent *iter;
Veaceslav Falico402dae92013-09-25 09:20:09 +02006143
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006144 struct net *net = dev_net(dev);
6145
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006146 list_for_each_entry(iter, &dev->adj_list.upper, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006147 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006148 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006149 netdev_adjacent_sysfs_del(iter->dev, oldname,
6150 &iter->dev->adj_list.lower);
6151 netdev_adjacent_sysfs_add(iter->dev, dev,
6152 &iter->dev->adj_list.lower);
6153 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02006154
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006155 list_for_each_entry(iter, &dev->adj_list.lower, list) {
Wei Tangbe4da0e2016-06-16 21:30:12 +08006156 if (!net_eq(net, dev_net(iter->dev)))
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04006157 continue;
Veaceslav Falico5bb025f2014-01-14 21:58:51 +01006158 netdev_adjacent_sysfs_del(iter->dev, oldname,
6159 &iter->dev->adj_list.upper);
6160 netdev_adjacent_sysfs_add(iter->dev, dev,
6161 &iter->dev->adj_list.upper);
6162 }
Veaceslav Falico402dae92013-09-25 09:20:09 +02006163}
Veaceslav Falico402dae92013-09-25 09:20:09 +02006164
6165void *netdev_lower_dev_get_private(struct net_device *dev,
6166 struct net_device *lower_dev)
6167{
6168 struct netdev_adjacent *lower;
6169
6170 if (!lower_dev)
6171 return NULL;
Michal Kubeček6ea29da2015-09-24 10:59:05 +02006172 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
Veaceslav Falico402dae92013-09-25 09:20:09 +02006173 if (!lower)
6174 return NULL;
6175
6176 return lower->private;
6177}
6178EXPORT_SYMBOL(netdev_lower_dev_get_private);
6179
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006180
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006181int dev_get_nest_level(struct net_device *dev)
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006182{
6183 struct net_device *lower = NULL;
6184 struct list_head *iter;
6185 int max_nest = -1;
6186 int nest;
6187
6188 ASSERT_RTNL();
6189
6190 netdev_for_each_lower_dev(dev, lower, iter) {
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006191 nest = dev_get_nest_level(lower);
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006192 if (max_nest < nest)
6193 max_nest = nest;
6194 }
6195
Sabrina Dubroca952fcfd2016-08-12 16:10:33 +02006196 return max_nest + 1;
Vlad Yasevich4085ebe2014-05-16 17:04:53 -04006197}
6198EXPORT_SYMBOL(dev_get_nest_level);
6199
Jiri Pirko04d48262015-12-03 12:12:15 +01006200/**
6201 * netdev_lower_change - Dispatch event about lower device state change
6202 * @lower_dev: device
6203 * @lower_state_info: state to dispatch
6204 *
6205 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
6206 * The caller must hold the RTNL lock.
6207 */
6208void netdev_lower_state_changed(struct net_device *lower_dev,
6209 void *lower_state_info)
6210{
6211 struct netdev_notifier_changelowerstate_info changelowerstate_info;
6212
6213 ASSERT_RTNL();
6214 changelowerstate_info.lower_state_info = lower_state_info;
6215 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, lower_dev,
6216 &changelowerstate_info.info);
6217}
6218EXPORT_SYMBOL(netdev_lower_state_changed);
6219
Jiri Pirko18bfb922016-07-05 11:27:38 +02006220int netdev_default_l2upper_neigh_construct(struct net_device *dev,
6221 struct neighbour *n)
6222{
6223 struct net_device *lower_dev, *stop_dev;
6224 struct list_head *iter;
6225 int err;
6226
6227 netdev_for_each_lower_dev(dev, lower_dev, iter) {
6228 if (!lower_dev->netdev_ops->ndo_neigh_construct)
6229 continue;
6230 err = lower_dev->netdev_ops->ndo_neigh_construct(lower_dev, n);
6231 if (err) {
6232 stop_dev = lower_dev;
6233 goto rollback;
6234 }
6235 }
6236 return 0;
6237
6238rollback:
6239 netdev_for_each_lower_dev(dev, lower_dev, iter) {
6240 if (lower_dev == stop_dev)
6241 break;
6242 if (!lower_dev->netdev_ops->ndo_neigh_destroy)
6243 continue;
6244 lower_dev->netdev_ops->ndo_neigh_destroy(lower_dev, n);
6245 }
6246 return err;
6247}
6248EXPORT_SYMBOL_GPL(netdev_default_l2upper_neigh_construct);
6249
6250void netdev_default_l2upper_neigh_destroy(struct net_device *dev,
6251 struct neighbour *n)
6252{
6253 struct net_device *lower_dev;
6254 struct list_head *iter;
6255
6256 netdev_for_each_lower_dev(dev, lower_dev, iter) {
6257 if (!lower_dev->netdev_ops->ndo_neigh_destroy)
6258 continue;
6259 lower_dev->netdev_ops->ndo_neigh_destroy(lower_dev, n);
6260 }
6261}
6262EXPORT_SYMBOL_GPL(netdev_default_l2upper_neigh_destroy);
6263
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006264static void dev_change_rx_flags(struct net_device *dev, int flags)
6265{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006266 const struct net_device_ops *ops = dev->netdev_ops;
6267
Vlad Yasevichd2615bf2013-11-19 20:47:15 -05006268 if (ops->ndo_change_rx_flags)
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006269 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006270}
6271
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006272static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
Patrick McHardy4417da62007-06-27 01:28:10 -07006273{
Eric Dumazetb536db92011-11-30 21:42:26 +00006274 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06006275 kuid_t uid;
6276 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07006277
Patrick McHardy24023452007-07-14 18:51:31 -07006278 ASSERT_RTNL();
6279
Wang Chendad9b332008-06-18 01:48:28 -07006280 dev->flags |= IFF_PROMISC;
6281 dev->promiscuity += inc;
6282 if (dev->promiscuity == 0) {
6283 /*
6284 * Avoid overflow.
6285 * If inc causes overflow, untouch promisc and return error.
6286 */
6287 if (inc < 0)
6288 dev->flags &= ~IFF_PROMISC;
6289 else {
6290 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006291 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
6292 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07006293 return -EOVERFLOW;
6294 }
6295 }
Patrick McHardy4417da62007-06-27 01:28:10 -07006296 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006297 pr_info("device %s %s promiscuous mode\n",
6298 dev->name,
6299 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11006300 if (audit_enabled) {
6301 current_uid_gid(&uid, &gid);
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05006302 audit_log(current->audit_context, GFP_ATOMIC,
6303 AUDIT_ANOM_PROMISCUOUS,
6304 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
6305 dev->name, (dev->flags & IFF_PROMISC),
6306 (old_flags & IFF_PROMISC),
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07006307 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06006308 from_kuid(&init_user_ns, uid),
6309 from_kgid(&init_user_ns, gid),
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05006310 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11006311 }
Patrick McHardy24023452007-07-14 18:51:31 -07006312
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006313 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07006314 }
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006315 if (notify)
6316 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
Wang Chendad9b332008-06-18 01:48:28 -07006317 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07006318}
6319
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320/**
6321 * dev_set_promiscuity - update promiscuity count on a device
6322 * @dev: device
6323 * @inc: modifier
6324 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07006325 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 * remains above zero the interface remains promiscuous. Once it hits zero
6327 * the device reverts back to normal filtering operation. A negative inc
6328 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07006329 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 */
Wang Chendad9b332008-06-18 01:48:28 -07006331int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332{
Eric Dumazetb536db92011-11-30 21:42:26 +00006333 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07006334 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006336 err = __dev_set_promiscuity(dev, inc, true);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07006337 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07006338 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07006339 if (dev->flags != old_flags)
6340 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07006341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006343EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006345static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346{
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006347 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
Patrick McHardy24023452007-07-14 18:51:31 -07006349 ASSERT_RTNL();
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07006352 dev->allmulti += inc;
6353 if (dev->allmulti == 0) {
6354 /*
6355 * Avoid overflow.
6356 * If inc causes overflow, untouch allmulti and return error.
6357 */
6358 if (inc < 0)
6359 dev->flags &= ~IFF_ALLMULTI;
6360 else {
6361 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006362 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
6363 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07006364 return -EOVERFLOW;
6365 }
6366 }
Patrick McHardy24023452007-07-14 18:51:31 -07006367 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006368 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07006369 dev_set_rx_mode(dev);
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006370 if (notify)
6371 __dev_notify_flags(dev, old_flags,
6372 dev->gflags ^ old_gflags);
Patrick McHardy24023452007-07-14 18:51:31 -07006373 }
Wang Chendad9b332008-06-18 01:48:28 -07006374 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07006375}
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006376
6377/**
6378 * dev_set_allmulti - update allmulti count on a device
6379 * @dev: device
6380 * @inc: modifier
6381 *
6382 * Add or remove reception of all multicast frames to a device. While the
6383 * count in the device remains above zero the interface remains listening
6384 * to all interfaces. Once it hits zero the device reverts back to normal
6385 * filtering operation. A negative @inc value is used to drop the counter
6386 * when releasing a resource needing all multicasts.
6387 * Return 0 if successful or a negative errno code on error.
6388 */
6389
6390int dev_set_allmulti(struct net_device *dev, int inc)
6391{
6392 return __dev_set_allmulti(dev, inc, true);
6393}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006394EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07006395
6396/*
6397 * Upload unicast and multicast address lists to device and
6398 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08006399 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07006400 * are present.
6401 */
6402void __dev_set_rx_mode(struct net_device *dev)
6403{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006404 const struct net_device_ops *ops = dev->netdev_ops;
6405
Patrick McHardy4417da62007-06-27 01:28:10 -07006406 /* dev_open will call this function so the list will stay sane. */
6407 if (!(dev->flags&IFF_UP))
6408 return;
6409
6410 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09006411 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07006412
Jiri Pirko01789342011-08-16 06:29:00 +00006413 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07006414 /* Unicast addresses changes may only happen under the rtnl,
6415 * therefore calling __dev_set_promiscuity here is safe.
6416 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08006417 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006418 __dev_set_promiscuity(dev, 1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07006419 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08006420 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006421 __dev_set_promiscuity(dev, -1, false);
Joe Perches2d348d12011-07-25 16:17:35 -07006422 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07006423 }
Patrick McHardy4417da62007-06-27 01:28:10 -07006424 }
Jiri Pirko01789342011-08-16 06:29:00 +00006425
6426 if (ops->ndo_set_rx_mode)
6427 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07006428}
6429
6430void dev_set_rx_mode(struct net_device *dev)
6431{
David S. Millerb9e40852008-07-15 00:15:08 -07006432 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07006433 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07006434 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435}
6436
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006437/**
6438 * dev_get_flags - get flags reported to userspace
6439 * @dev: device
6440 *
6441 * Get the combination of flag bits exported through APIs to userspace.
6442 */
Eric Dumazet95c96172012-04-15 05:58:06 +00006443unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444{
Eric Dumazet95c96172012-04-15 05:58:06 +00006445 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
6447 flags = (dev->flags & ~(IFF_PROMISC |
6448 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08006449 IFF_RUNNING |
6450 IFF_LOWER_UP |
6451 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 (dev->gflags & (IFF_PROMISC |
6453 IFF_ALLMULTI));
6454
Stefan Rompfb00055a2006-03-20 17:09:11 -08006455 if (netif_running(dev)) {
6456 if (netif_oper_up(dev))
6457 flags |= IFF_RUNNING;
6458 if (netif_carrier_ok(dev))
6459 flags |= IFF_LOWER_UP;
6460 if (netif_dormant(dev))
6461 flags |= IFF_DORMANT;
6462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
6464 return flags;
6465}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006466EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
Patrick McHardybd380812010-02-26 06:34:53 +00006468int __dev_change_flags(struct net_device *dev, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469{
Eric Dumazetb536db92011-11-30 21:42:26 +00006470 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00006471 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
Patrick McHardy24023452007-07-14 18:51:31 -07006473 ASSERT_RTNL();
6474
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 /*
6476 * Set the flags on our device.
6477 */
6478
6479 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
6480 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
6481 IFF_AUTOMEDIA)) |
6482 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
6483 IFF_ALLMULTI));
6484
6485 /*
6486 * Load in the correct multicast list now the flags have changed.
6487 */
6488
Patrick McHardyb6c40d62008-10-07 15:26:48 -07006489 if ((old_flags ^ flags) & IFF_MULTICAST)
6490 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07006491
Patrick McHardy4417da62007-06-27 01:28:10 -07006492 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
6494 /*
6495 * Have we downed the interface. We handle IFF_UP ourselves
6496 * according to user attempts to set it, rather than blindly
6497 * setting it.
6498 */
6499
6500 ret = 0;
Peter Pan(潘卫平)d215d102014-06-16 21:57:22 +08006501 if ((old_flags ^ flags) & IFF_UP)
Patrick McHardybd380812010-02-26 06:34:53 +00006502 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006505 int inc = (flags & IFF_PROMISC) ? 1 : -1;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006506 unsigned int old_flags = dev->flags;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006507
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 dev->gflags ^= IFF_PROMISC;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006509
6510 if (__dev_set_promiscuity(dev, inc, false) >= 0)
6511 if (dev->flags != old_flags)
6512 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 }
6514
6515 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
6516 is important. Some (broken) drivers set IFF_PROMISC, when
6517 IFF_ALLMULTI is requested not asking us and not reporting.
6518 */
6519 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006520 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
6521
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 dev->gflags ^= IFF_ALLMULTI;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006523 __dev_set_allmulti(dev, inc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 }
6525
Patrick McHardybd380812010-02-26 06:34:53 +00006526 return ret;
6527}
6528
Nicolas Dichtela528c212013-09-25 12:02:44 +02006529void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
6530 unsigned int gchanges)
Patrick McHardybd380812010-02-26 06:34:53 +00006531{
6532 unsigned int changes = dev->flags ^ old_flags;
6533
Nicolas Dichtela528c212013-09-25 12:02:44 +02006534 if (gchanges)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07006535 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
Nicolas Dichtela528c212013-09-25 12:02:44 +02006536
Patrick McHardybd380812010-02-26 06:34:53 +00006537 if (changes & IFF_UP) {
6538 if (dev->flags & IFF_UP)
6539 call_netdevice_notifiers(NETDEV_UP, dev);
6540 else
6541 call_netdevice_notifiers(NETDEV_DOWN, dev);
6542 }
6543
6544 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00006545 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
6546 struct netdev_notifier_change_info change_info;
6547
6548 change_info.flags_changed = changes;
6549 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
6550 &change_info.info);
6551 }
Patrick McHardybd380812010-02-26 06:34:53 +00006552}
6553
6554/**
6555 * dev_change_flags - change device settings
6556 * @dev: device
6557 * @flags: device state flags
6558 *
6559 * Change settings on device based state flags. The flags are
6560 * in the userspace exported format.
6561 */
Eric Dumazetb536db92011-11-30 21:42:26 +00006562int dev_change_flags(struct net_device *dev, unsigned int flags)
Patrick McHardybd380812010-02-26 06:34:53 +00006563{
Eric Dumazetb536db92011-11-30 21:42:26 +00006564 int ret;
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006565 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
Patrick McHardybd380812010-02-26 06:34:53 +00006566
6567 ret = __dev_change_flags(dev, flags);
6568 if (ret < 0)
6569 return ret;
6570
Nicolas Dichtel991fb3f2013-09-25 12:02:45 +02006571 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
Nicolas Dichtela528c212013-09-25 12:02:44 +02006572 __dev_notify_flags(dev, old_flags, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 return ret;
6574}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006575EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006577static int __dev_set_mtu(struct net_device *dev, int new_mtu)
6578{
6579 const struct net_device_ops *ops = dev->netdev_ops;
6580
6581 if (ops->ndo_change_mtu)
6582 return ops->ndo_change_mtu(dev, new_mtu);
6583
6584 dev->mtu = new_mtu;
6585 return 0;
6586}
6587
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006588/**
6589 * dev_set_mtu - Change maximum transfer unit
6590 * @dev: device
6591 * @new_mtu: new transfer unit
6592 *
6593 * Change the maximum transfer size of the network device.
6594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595int dev_set_mtu(struct net_device *dev, int new_mtu)
6596{
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006597 int err, orig_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
6599 if (new_mtu == dev->mtu)
6600 return 0;
6601
6602 /* MTU must be positive. */
6603 if (new_mtu < 0)
6604 return -EINVAL;
6605
6606 if (!netif_device_present(dev))
6607 return -ENODEV;
6608
Veaceslav Falico1d486bf2014-01-16 00:02:18 +01006609 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
6610 err = notifier_to_errno(err);
6611 if (err)
6612 return err;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006613
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006614 orig_mtu = dev->mtu;
6615 err = __dev_set_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006617 if (!err) {
Sabrina Dubroca8d59c3a2018-10-09 17:48:14 +02006618 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
6619 orig_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006620 err = notifier_to_errno(err);
6621 if (err) {
6622 /* setting mtu back and notifying everyone again,
6623 * so that they have a chance to revert changes.
6624 */
6625 __dev_set_mtu(dev, orig_mtu);
Sabrina Dubroca8d59c3a2018-10-09 17:48:14 +02006626 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
6627 new_mtu);
Veaceslav Falico2315dc92014-01-10 16:56:25 +01006628 }
6629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 return err;
6631}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006632EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006634/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00006635 * dev_set_group - Change group this device belongs to
6636 * @dev: device
6637 * @new_group: group this device should belong to
6638 */
6639void dev_set_group(struct net_device *dev, int new_group)
6640{
6641 dev->group = new_group;
6642}
6643EXPORT_SYMBOL(dev_set_group);
6644
6645/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006646 * dev_set_mac_address - Change Media Access Control Address
6647 * @dev: device
6648 * @sa: new address
6649 *
6650 * Change the hardware (MAC) address of the device
6651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
6653{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006654 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 int err;
6656
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006657 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 return -EOPNOTSUPP;
6659 if (sa->sa_family != dev->type)
6660 return -EINVAL;
6661 if (!netif_device_present(dev))
6662 return -ENODEV;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08006663 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00006664 if (err)
6665 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00006666 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00006667 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04006668 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00006669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07006671EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Jiri Pirko4bf84c32012-12-27 23:49:37 +00006673/**
6674 * dev_change_carrier - Change device carrier
6675 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00006676 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00006677 *
6678 * Change device carrier
6679 */
6680int dev_change_carrier(struct net_device *dev, bool new_carrier)
6681{
6682 const struct net_device_ops *ops = dev->netdev_ops;
6683
6684 if (!ops->ndo_change_carrier)
6685 return -EOPNOTSUPP;
6686 if (!netif_device_present(dev))
6687 return -ENODEV;
6688 return ops->ndo_change_carrier(dev, new_carrier);
6689}
6690EXPORT_SYMBOL(dev_change_carrier);
6691
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692/**
Jiri Pirko66b52b02013-07-29 18:16:49 +02006693 * dev_get_phys_port_id - Get device physical port ID
6694 * @dev: device
6695 * @ppid: port ID
6696 *
6697 * Get device physical port ID
6698 */
6699int dev_get_phys_port_id(struct net_device *dev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01006700 struct netdev_phys_item_id *ppid)
Jiri Pirko66b52b02013-07-29 18:16:49 +02006701{
6702 const struct net_device_ops *ops = dev->netdev_ops;
6703
6704 if (!ops->ndo_get_phys_port_id)
6705 return -EOPNOTSUPP;
6706 return ops->ndo_get_phys_port_id(dev, ppid);
6707}
6708EXPORT_SYMBOL(dev_get_phys_port_id);
6709
6710/**
David Aherndb24a902015-03-17 20:23:15 -06006711 * dev_get_phys_port_name - Get device physical port name
6712 * @dev: device
6713 * @name: port name
Luis de Bethencourted49e652016-03-21 16:31:14 +00006714 * @len: limit of bytes to copy to name
David Aherndb24a902015-03-17 20:23:15 -06006715 *
6716 * Get device physical port name
6717 */
6718int dev_get_phys_port_name(struct net_device *dev,
6719 char *name, size_t len)
6720{
6721 const struct net_device_ops *ops = dev->netdev_ops;
6722
6723 if (!ops->ndo_get_phys_port_name)
6724 return -EOPNOTSUPP;
6725 return ops->ndo_get_phys_port_name(dev, name, len);
6726}
6727EXPORT_SYMBOL(dev_get_phys_port_name);
6728
6729/**
Anuradha Karuppiahd746d702015-07-14 13:43:19 -07006730 * dev_change_proto_down - update protocol port state information
6731 * @dev: device
6732 * @proto_down: new value
6733 *
6734 * This info can be used by switch drivers to set the phys state of the
6735 * port.
6736 */
6737int dev_change_proto_down(struct net_device *dev, bool proto_down)
6738{
6739 const struct net_device_ops *ops = dev->netdev_ops;
6740
6741 if (!ops->ndo_change_proto_down)
6742 return -EOPNOTSUPP;
6743 if (!netif_device_present(dev))
6744 return -ENODEV;
6745 return ops->ndo_change_proto_down(dev, proto_down);
6746}
6747EXPORT_SYMBOL(dev_change_proto_down);
6748
6749/**
Brenden Blancoa7862b42016-07-19 12:16:48 -07006750 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
6751 * @dev: device
6752 * @fd: new program fd or negative value to clear
6753 *
6754 * Set or clear a bpf program for a device
6755 */
6756int dev_change_xdp_fd(struct net_device *dev, int fd)
6757{
6758 const struct net_device_ops *ops = dev->netdev_ops;
6759 struct bpf_prog *prog = NULL;
6760 struct netdev_xdp xdp = {};
6761 int err;
6762
6763 if (!ops->ndo_xdp)
6764 return -EOPNOTSUPP;
6765 if (fd >= 0) {
6766 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
6767 if (IS_ERR(prog))
6768 return PTR_ERR(prog);
6769 }
6770
6771 xdp.command = XDP_SETUP_PROG;
6772 xdp.prog = prog;
6773 err = ops->ndo_xdp(dev, &xdp);
6774 if (err < 0 && prog)
6775 bpf_prog_put(prog);
6776
6777 return err;
6778}
6779EXPORT_SYMBOL(dev_change_xdp_fd);
6780
6781/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07006783 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 *
6785 * Returns a suitable unique value for a new device interface
6786 * number. The caller must hold the rtnl semaphore or the
6787 * dev_base_lock to be sure it remains unique.
6788 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07006789static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00006791 int ifindex = net->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 for (;;) {
6793 if (++ifindex <= 0)
6794 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07006795 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00006796 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 }
6798}
6799
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08006801static LIST_HEAD(net_todo_list);
Cong Wang200b9162014-05-12 15:11:20 -07006802DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Stephen Hemminger6f05f622007-03-08 20:46:03 -08006804static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 list_add_tail(&dev->todo_list, &net_todo_list);
Eric W. Biederman50624c92013-09-23 21:19:49 -07006807 dev_net(dev)->dev_unreg_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808}
6809
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006810static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006811{
Krishna Kumare93737b2009-12-08 22:26:02 +00006812 struct net_device *dev, *tmp;
Eric W. Biederman5cde2822013-10-05 19:26:05 -07006813 LIST_HEAD(close_head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006814
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006815 BUG_ON(dev_boot_phase);
6816 ASSERT_RTNL();
6817
Krishna Kumare93737b2009-12-08 22:26:02 +00006818 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006819 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00006820 * for initialization unwind. Remove those
6821 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006822 */
6823 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006824 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
6825 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006826
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006827 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00006828 list_del(&dev->unreg_list);
6829 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006830 }
Eric Dumazet449f4542011-05-19 12:24:16 +00006831 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006832 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00006833 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006834
Octavian Purdila44345722010-12-13 12:44:07 +00006835 /* If device is running, close it first. */
Eric W. Biederman5cde2822013-10-05 19:26:05 -07006836 list_for_each_entry(dev, head, unreg_list)
6837 list_add_tail(&dev->close_list, &close_head);
David S. Miller99c4a262015-03-18 22:52:33 -04006838 dev_close_many(&close_head, true);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006839
Octavian Purdila44345722010-12-13 12:44:07 +00006840 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006841 /* And unlink it from device chain. */
6842 unlist_netdevice(dev);
6843
6844 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006845 }
Eric Dumazet41852492016-08-26 12:50:39 -07006846 flush_all_backlogs();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006847
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006848 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006849
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006850 list_for_each_entry(dev, head, unreg_list) {
Mahesh Bandewar395eea62014-12-03 13:46:24 -08006851 struct sk_buff *skb = NULL;
6852
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006853 /* Shutdown queueing discipline. */
6854 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006855
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006856
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006857 /* Notify protocols, that we are about to destroy
6858 this device. They should clean all the things.
6859 */
6860 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6861
Mahesh Bandewar395eea62014-12-03 13:46:24 -08006862 if (!dev->rtnl_link_ops ||
6863 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
6864 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U,
6865 GFP_KERNEL);
6866
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006867 /*
6868 * Flush the unicast and multicast chains
6869 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00006870 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00006871 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006872
6873 if (dev->netdev_ops->ndo_uninit)
6874 dev->netdev_ops->ndo_uninit(dev);
6875
Mahesh Bandewar395eea62014-12-03 13:46:24 -08006876 if (skb)
6877 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
Roopa Prabhu56bfa7e2014-05-01 11:40:30 -07006878
Jiri Pirko9ff162a2013-01-03 22:48:49 +00006879 /* Notifier chain MUST detach us all upper devices. */
6880 WARN_ON(netdev_has_any_upper_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006881
6882 /* Remove entries from kobject tree */
6883 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00006884#ifdef CONFIG_XPS
6885 /* Remove XPS queueing entries */
6886 netif_reset_xps_queues_gt(dev, 0);
6887#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006888 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006889
Eric W. Biederman850a5452011-10-13 22:25:23 +00006890 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006891
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00006892 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006893 dev_put(dev);
6894}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006895
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006896static void rollback_registered(struct net_device *dev)
6897{
6898 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006899
Eric Dumazet9b5e3832009-10-27 07:04:19 +00006900 list_add(&dev->unreg_list, &single);
6901 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00006902 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07006903}
6904
Jarod Wilsonfd867d52015-11-02 21:55:59 -05006905static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
6906 struct net_device *upper, netdev_features_t features)
6907{
6908 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
6909 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05006910 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05006911
Hauke Mehrtens47dc74c2019-02-15 17:58:54 +01006912 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05006913 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05006914 if (!(upper->wanted_features & feature)
6915 && (features & feature)) {
6916 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
6917 &feature, upper->name);
6918 features &= ~feature;
6919 }
6920 }
6921
6922 return features;
6923}
6924
6925static void netdev_sync_lower_features(struct net_device *upper,
6926 struct net_device *lower, netdev_features_t features)
6927{
6928 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
6929 netdev_features_t feature;
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05006930 int feature_bit;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05006931
Hauke Mehrtens47dc74c2019-02-15 17:58:54 +01006932 for_each_netdev_feature(upper_disables, feature_bit) {
Jarod Wilson5ba3f7d2015-11-03 10:15:59 -05006933 feature = __NETIF_F_BIT(feature_bit);
Jarod Wilsonfd867d52015-11-02 21:55:59 -05006934 if (!(features & feature) && (lower->features & feature)) {
6935 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
6936 &feature, lower->name);
6937 lower->wanted_features &= ~feature;
6938 netdev_update_features(lower);
6939
6940 if (unlikely(lower->features & feature))
6941 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
6942 &feature, lower->name);
6943 }
6944 }
6945}
6946
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006947static netdev_features_t netdev_fix_features(struct net_device *dev,
6948 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07006949{
Michał Mirosław57422dc2011-01-22 12:14:12 +00006950 /* Fix illegal checksum combinations */
6951 if ((features & NETIF_F_HW_CSUM) &&
6952 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04006953 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00006954 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
6955 }
6956
Herbert Xub63365a2008-10-23 01:11:29 -07006957 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00006958 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04006959 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00006960 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07006961 }
6962
Pravin B Shelarec5f0612013-03-07 09:28:01 +00006963 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
6964 !(features & NETIF_F_IP_CSUM)) {
6965 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
6966 features &= ~NETIF_F_TSO;
6967 features &= ~NETIF_F_TSO_ECN;
6968 }
6969
6970 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
6971 !(features & NETIF_F_IPV6_CSUM)) {
6972 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
6973 features &= ~NETIF_F_TSO6;
6974 }
6975
Alexander Duyckb1dc4972016-05-02 09:38:24 -07006976 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
6977 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
6978 features &= ~NETIF_F_TSO_MANGLEID;
6979
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00006980 /* TSO ECN requires that TSO is present as well. */
6981 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
6982 features &= ~NETIF_F_TSO_ECN;
6983
Michał Mirosław212b5732011-02-15 16:59:16 +00006984 /* Software GSO depends on SG. */
6985 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04006986 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00006987 features &= ~NETIF_F_GSO;
6988 }
6989
Michał Mirosławacd11302011-01-24 15:45:15 -08006990 /* UFO needs SG and checksumming */
Herbert Xub63365a2008-10-23 01:11:29 -07006991 if (features & NETIF_F_UFO) {
Michał Mirosław79032642010-11-30 06:38:00 +00006992 /* maybe split UFO into V4 and V6? */
Tom Herbertc8cd0982015-12-14 11:19:44 -08006993 if (!(features & NETIF_F_HW_CSUM) &&
6994 ((features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) !=
6995 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04006996 netdev_dbg(dev,
Michał Mirosławacd11302011-01-24 15:45:15 -08006997 "Dropping NETIF_F_UFO since no checksum offload features.\n");
Herbert Xub63365a2008-10-23 01:11:29 -07006998 features &= ~NETIF_F_UFO;
6999 }
7000
7001 if (!(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04007002 netdev_dbg(dev,
Michał Mirosławacd11302011-01-24 15:45:15 -08007003 "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
Herbert Xub63365a2008-10-23 01:11:29 -07007004 features &= ~NETIF_F_UFO;
7005 }
7006 }
7007
Alexander Duyck802ab552016-04-10 21:45:03 -04007008 /* GSO partial features require GSO partial be set */
7009 if ((features & dev->gso_partial_features) &&
7010 !(features & NETIF_F_GSO_PARTIAL)) {
7011 netdev_dbg(dev,
7012 "Dropping partially supported GSO features since no GSO partial.\n");
7013 features &= ~dev->gso_partial_features;
7014 }
7015
Jiri Pirkod0290212014-04-02 23:09:31 +02007016#ifdef CONFIG_NET_RX_BUSY_POLL
7017 if (dev->netdev_ops->ndo_busy_poll)
7018 features |= NETIF_F_BUSY_POLL;
7019 else
7020#endif
7021 features &= ~NETIF_F_BUSY_POLL;
7022
Herbert Xub63365a2008-10-23 01:11:29 -07007023 return features;
7024}
Herbert Xub63365a2008-10-23 01:11:29 -07007025
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007026int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00007027{
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007028 struct net_device *upper, *lower;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007029 netdev_features_t features;
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007030 struct list_head *iter;
Jarod Wilsone7868a82015-11-03 23:09:32 -05007031 int err = -1;
Michał Mirosław5455c692011-02-15 16:59:17 +00007032
Michał Mirosław87267482011-04-12 09:56:38 +00007033 ASSERT_RTNL();
7034
Michał Mirosław5455c692011-02-15 16:59:17 +00007035 features = netdev_get_wanted_features(dev);
7036
7037 if (dev->netdev_ops->ndo_fix_features)
7038 features = dev->netdev_ops->ndo_fix_features(dev, features);
7039
7040 /* driver might be less strict about feature dependencies */
7041 features = netdev_fix_features(dev, features);
7042
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007043 /* some features can't be enabled if they're off an an upper device */
7044 netdev_for_each_upper_dev_rcu(dev, upper, iter)
7045 features = netdev_sync_upper_features(dev, upper, features);
7046
Michał Mirosław5455c692011-02-15 16:59:17 +00007047 if (dev->features == features)
Jarod Wilsone7868a82015-11-03 23:09:32 -05007048 goto sync_lower;
Michał Mirosław5455c692011-02-15 16:59:17 +00007049
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007050 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
7051 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00007052
7053 if (dev->netdev_ops->ndo_set_features)
7054 err = dev->netdev_ops->ndo_set_features(dev, features);
Nikolay Aleksandrov5f8dc332015-11-13 14:54:01 +01007055 else
7056 err = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00007057
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007058 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00007059 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007060 "set_features() failed (%d); wanted %pNF, left %pNF\n",
7061 err, &features, &dev->features);
Nikolay Aleksandrov17b85d22015-11-17 15:49:06 +01007062 /* return non-0 since some features might have changed and
7063 * it's better to fire a spurious notification than miss it
7064 */
7065 return -1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007066 }
7067
Jarod Wilsone7868a82015-11-03 23:09:32 -05007068sync_lower:
Jarod Wilsonfd867d52015-11-02 21:55:59 -05007069 /* some features must be disabled on lower devices when disabled
7070 * on an upper device (think: bonding master or bridge)
7071 */
7072 netdev_for_each_lower_dev(dev, lower, iter)
7073 netdev_sync_lower_features(dev, lower, features);
7074
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007075 if (!err)
7076 dev->features = features;
7077
Jarod Wilsone7868a82015-11-03 23:09:32 -05007078 return err < 0 ? 0 : 1;
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007079}
7080
Michał Mirosławafe12cc2011-05-07 03:22:17 +00007081/**
7082 * netdev_update_features - recalculate device features
7083 * @dev: the device to check
7084 *
7085 * Recalculate dev->features set and send notifications if it
7086 * has changed. Should be called after driver or hardware dependent
7087 * conditions might have changed that influence the features.
7088 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007089void netdev_update_features(struct net_device *dev)
7090{
7091 if (__netdev_update_features(dev))
7092 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00007093}
7094EXPORT_SYMBOL(netdev_update_features);
7095
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00007097 * netdev_change_features - recalculate device features
7098 * @dev: the device to check
7099 *
7100 * Recalculate dev->features set and send notifications even
7101 * if they have not changed. Should be called instead of
7102 * netdev_update_features() if also dev->vlan_features might
7103 * have changed to allow the changes to be propagated to stacked
7104 * VLAN devices.
7105 */
7106void netdev_change_features(struct net_device *dev)
7107{
7108 __netdev_update_features(dev);
7109 netdev_features_change(dev);
7110}
7111EXPORT_SYMBOL(netdev_change_features);
7112
7113/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007114 * netif_stacked_transfer_operstate - transfer operstate
7115 * @rootdev: the root or lower level device to transfer state from
7116 * @dev: the device to transfer operstate to
7117 *
7118 * Transfer operational state from root to device. This is normally
7119 * called when a stacking relationship exists between the root
7120 * device and the device(a leaf device).
7121 */
7122void netif_stacked_transfer_operstate(const struct net_device *rootdev,
7123 struct net_device *dev)
7124{
7125 if (rootdev->operstate == IF_OPER_DORMANT)
7126 netif_dormant_on(dev);
7127 else
7128 netif_dormant_off(dev);
7129
7130 if (netif_carrier_ok(rootdev)) {
7131 if (!netif_carrier_ok(dev))
7132 netif_carrier_on(dev);
7133 } else {
7134 if (netif_carrier_ok(dev))
7135 netif_carrier_off(dev);
7136 }
7137}
7138EXPORT_SYMBOL(netif_stacked_transfer_operstate);
7139
Michael Daltona953be52014-01-16 22:23:28 -08007140#ifdef CONFIG_SYSFS
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007141static int netif_alloc_rx_queues(struct net_device *dev)
7142{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007143 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00007144 struct netdev_rx_queue *rx;
Pankaj Gupta10595902015-01-12 11:41:28 +05307145 size_t sz = count * sizeof(*rx);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007146
Tom Herbertbd25fa72010-10-18 18:00:16 +00007147 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007148
Pankaj Gupta10595902015-01-12 11:41:28 +05307149 rx = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
7150 if (!rx) {
7151 rx = vzalloc(sz);
7152 if (!rx)
7153 return -ENOMEM;
7154 }
Tom Herbertbd25fa72010-10-18 18:00:16 +00007155 dev->_rx = rx;
7156
Tom Herbertbd25fa72010-10-18 18:00:16 +00007157 for (i = 0; i < count; i++)
Tom Herbertfe822242010-11-09 10:47:38 +00007158 rx[i].dev = dev;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007159 return 0;
7160}
Tom Herbertbf264142010-11-26 08:36:09 +00007161#endif
Eric Dumazet1b4bf462010-09-23 17:26:35 +00007162
Changli Gaoaa942102010-12-04 02:31:41 +00007163static void netdev_init_one_queue(struct net_device *dev,
7164 struct netdev_queue *queue, void *_unused)
7165{
7166 /* Initialize queue lock */
7167 spin_lock_init(&queue->_xmit_lock);
7168 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
7169 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00007170 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00007171 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00007172#ifdef CONFIG_BQL
7173 dql_init(&queue->dql, HZ);
7174#endif
Changli Gaoaa942102010-12-04 02:31:41 +00007175}
7176
Eric Dumazet60877a32013-06-20 01:15:51 -07007177static void netif_free_tx_queues(struct net_device *dev)
7178{
WANG Cong4cb28972014-06-02 15:55:22 -07007179 kvfree(dev->_tx);
Eric Dumazet60877a32013-06-20 01:15:51 -07007180}
7181
Tom Herberte6484932010-10-18 18:04:39 +00007182static int netif_alloc_netdev_queues(struct net_device *dev)
7183{
7184 unsigned int count = dev->num_tx_queues;
7185 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07007186 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00007187
Eric Dumazetd3397272015-07-06 17:13:26 +02007188 if (count < 1 || count > 0xffff)
7189 return -EINVAL;
Tom Herberte6484932010-10-18 18:04:39 +00007190
Eric Dumazet60877a32013-06-20 01:15:51 -07007191 tx = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
7192 if (!tx) {
7193 tx = vzalloc(sz);
7194 if (!tx)
7195 return -ENOMEM;
7196 }
Tom Herberte6484932010-10-18 18:04:39 +00007197 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00007198
Tom Herberte6484932010-10-18 18:04:39 +00007199 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
7200 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00007201
7202 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00007203}
7204
Denys Vlasenkoa2029242015-05-11 21:17:53 +02007205void netif_tx_stop_all_queues(struct net_device *dev)
7206{
7207 unsigned int i;
7208
7209 for (i = 0; i < dev->num_tx_queues; i++) {
7210 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
7211 netif_tx_stop_queue(txq);
7212 }
7213}
7214EXPORT_SYMBOL(netif_tx_stop_all_queues);
7215
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08007216/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 * register_netdevice - register a network device
7218 * @dev: device to register
7219 *
7220 * Take a completed network device structure and add it to the kernel
7221 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7222 * chain. 0 is returned on success. A negative errno code is returned
7223 * on a failure to set up the device, or if the name is a duplicate.
7224 *
7225 * Callers must hold the rtnl semaphore. You may want
7226 * register_netdev() instead of this.
7227 *
7228 * BUGS:
7229 * The locking appears insufficient to guarantee two parallel registers
7230 * will not get the same name.
7231 */
7232
7233int register_netdevice(struct net_device *dev)
7234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007236 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238 BUG_ON(dev_boot_phase);
7239 ASSERT_RTNL();
7240
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007241 might_sleep();
7242
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 /* When net_device's are persistent, this will be fatal. */
7244 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007245 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246
David S. Millerf1f28aa2008-07-15 00:08:33 -07007247 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07007248 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
Gao feng828de4f2012-09-13 20:58:27 +00007250 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00007251 if (ret < 0)
7252 goto out;
7253
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007255 if (dev->netdev_ops->ndo_init) {
7256 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 if (ret) {
7258 if (ret > 0)
7259 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08007260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 }
7262 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007263
Patrick McHardyf6469682013-04-19 02:04:27 +00007264 if (((dev->hw_features | dev->features) &
7265 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00007266 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
7267 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
7268 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
7269 ret = -EINVAL;
7270 goto err_uninit;
7271 }
7272
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00007273 ret = -EBUSY;
7274 if (!dev->ifindex)
7275 dev->ifindex = dev_new_index(net);
7276 else if (__dev_get_by_index(net, dev->ifindex))
7277 goto err_uninit;
7278
Michał Mirosław5455c692011-02-15 16:59:17 +00007279 /* Transfer changeable features to wanted_features and enable
7280 * software offloads (GSO and GRO).
7281 */
7282 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00007283 dev->features |= NETIF_F_SOFT_FEATURES;
7284 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007286 if (!(dev->flags & IFF_LOOPBACK))
Michał Mirosław34324dc2011-11-15 15:29:55 +00007287 dev->hw_features |= NETIF_F_NOCACHE_COPY;
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007288
Alexander Duyck7f348a62016-04-20 16:51:00 -04007289 /* If IPv4 TCP segmentation offload is supported we should also
7290 * allow the device to enable segmenting the frame with the option
7291 * of ignoring a static IP ID value. This doesn't enable the
7292 * feature itself but allows the user to enable it later.
7293 */
Alexander Duyckcbc53e02016-04-10 21:44:51 -04007294 if (dev->hw_features & NETIF_F_TSO)
7295 dev->hw_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyck7f348a62016-04-20 16:51:00 -04007296 if (dev->vlan_features & NETIF_F_TSO)
7297 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
7298 if (dev->mpls_features & NETIF_F_TSO)
7299 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
7300 if (dev->hw_enc_features & NETIF_F_TSO)
7301 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07007302
Michał Mirosław1180e7d2011-07-14 14:41:11 -07007303 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00007304 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07007305 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00007306
Pravin B Shelaree579672013-03-07 09:28:08 +00007307 /* Make NETIF_F_SG inheritable to tunnel devices.
7308 */
Alexander Duyck802ab552016-04-10 21:45:03 -04007309 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
Pravin B Shelaree579672013-03-07 09:28:08 +00007310
Simon Horman0d89d202013-05-23 21:02:52 +00007311 /* Make NETIF_F_SG inheritable to MPLS.
7312 */
7313 dev->mpls_features |= NETIF_F_SG;
7314
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00007315 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
7316 ret = notifier_to_errno(ret);
7317 if (ret)
7318 goto err_uninit;
7319
Eric W. Biederman8b41d182007-09-26 22:02:53 -07007320 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007321 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007322 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007323 dev->reg_state = NETREG_REGISTERED;
7324
Michał Mirosław6cb6a272011-04-02 22:48:47 -07007325 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00007326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 /*
7328 * Default initial state at registry is that the
7329 * device is present.
7330 */
7331
7332 set_bit(__LINK_STATE_PRESENT, &dev->state);
7333
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01007334 linkwatch_init_dev(dev);
7335
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02007338 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04007339 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
Jiri Pirko948b3372013-01-08 01:38:25 +00007341 /* If the device has permanent device address, driver should
7342 * set dev_addr and also addr_assign_type should be set to
7343 * NET_ADDR_PERM (default value).
7344 */
7345 if (dev->addr_assign_type == NET_ADDR_PERM)
7346 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
7347
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07007349 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07007350 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07007351 if (ret) {
7352 rollback_registered(dev);
7353 dev->reg_state = NETREG_UNREGISTERED;
7354 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00007355 /*
7356 * Prevent userspace races by waiting until the network
7357 * device is fully setup before sending notifications.
7358 */
Patrick McHardya2835762010-02-26 06:34:51 +00007359 if (!dev->rtnl_link_ops ||
7360 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
Alexei Starovoitov7f294052013-10-23 16:02:42 -07007361 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
7363out:
7364 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007365
7366err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08007367 if (dev->netdev_ops->ndo_uninit)
7368 dev->netdev_ops->ndo_uninit(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07007369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007371EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
7373/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007374 * init_dummy_netdev - init a dummy network device for NAPI
7375 * @dev: device to init
7376 *
7377 * This takes a network device structure and initialize the minimum
7378 * amount of fields so it can be used to schedule NAPI polls without
7379 * registering a full blown interface. This is to be used by drivers
7380 * that need to tie several hardware interfaces to a single NAPI
7381 * poll scheduler due to HW limitations.
7382 */
7383int init_dummy_netdev(struct net_device *dev)
7384{
7385 /* Clear everything. Note we don't initialize spinlocks
7386 * are they aren't supposed to be taken by any of the
7387 * NAPI code and this dummy netdev is supposed to be
7388 * only ever used for NAPI polls
7389 */
7390 memset(dev, 0, sizeof(struct net_device));
7391
7392 /* make sure we BUG if trying to hit standard
7393 * register/unregister code path
7394 */
7395 dev->reg_state = NETREG_DUMMY;
7396
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007397 /* NAPI wants this */
7398 INIT_LIST_HEAD(&dev->napi_list);
7399
7400 /* a dummy interface is started by default */
7401 set_bit(__LINK_STATE_PRESENT, &dev->state);
7402 set_bit(__LINK_STATE_START, &dev->state);
7403
Eric Dumazet29b44332010-10-11 10:22:12 +00007404 /* Note : We dont allocate pcpu_refcnt for dummy devices,
7405 * because users of this 'device' dont need to change
7406 * its refcount.
7407 */
7408
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08007409 return 0;
7410}
7411EXPORT_SYMBOL_GPL(init_dummy_netdev);
7412
7413
7414/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 * register_netdev - register a network device
7416 * @dev: device to register
7417 *
7418 * Take a completed network device structure and add it to the kernel
7419 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7420 * chain. 0 is returned on success. A negative errno code is returned
7421 * on a failure to set up the device, or if the name is a duplicate.
7422 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07007423 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 * and expands the device name if you passed a format string to
7425 * alloc_netdev.
7426 */
7427int register_netdev(struct net_device *dev)
7428{
7429 int err;
7430
7431 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 rtnl_unlock();
7434 return err;
7435}
7436EXPORT_SYMBOL(register_netdev);
7437
Eric Dumazet29b44332010-10-11 10:22:12 +00007438int netdev_refcnt_read(const struct net_device *dev)
7439{
7440 int i, refcnt = 0;
7441
7442 for_each_possible_cpu(i)
7443 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
7444 return refcnt;
7445}
7446EXPORT_SYMBOL(netdev_refcnt_read);
7447
Ben Hutchings2c530402012-07-10 10:55:09 +00007448/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00007450 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 *
7452 * This is called when unregistering network devices.
7453 *
7454 * Any protocol or device that holds a reference should register
7455 * for netdevice notification, and cleanup and put back the
7456 * reference if they receive an UNREGISTER event.
7457 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007458 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 */
7460static void netdev_wait_allrefs(struct net_device *dev)
7461{
7462 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00007463 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
Eric Dumazete014deb2009-11-17 05:59:21 +00007465 linkwatch_forget_dev(dev);
7466
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00007468 refcnt = netdev_refcnt_read(dev);
7469
7470 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08007472 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
7474 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07007475 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
Eric Dumazet748e2d92012-08-22 21:50:59 +00007477 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007478 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00007479 rtnl_lock();
7480
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007481 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
7483 &dev->state)) {
7484 /* We must not have linkwatch events
7485 * pending on unregister. If this
7486 * happens, we simply run the queue
7487 * unscheduled, resulting in a noop
7488 * for this device.
7489 */
7490 linkwatch_run_queue();
7491 }
7492
Stephen Hemminger6756ae42006-03-20 22:23:58 -08007493 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
7495 rebroadcast_time = jiffies;
7496 }
7497
7498 msleep(250);
7499
Eric Dumazet29b44332010-10-11 10:22:12 +00007500 refcnt = netdev_refcnt_read(dev);
7501
Eric Dumazet350d6592019-05-16 08:09:57 -07007502 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007503 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
7504 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 warning_time = jiffies;
7506 }
7507 }
7508}
7509
7510/* The sequence is:
7511 *
7512 * rtnl_lock();
7513 * ...
7514 * register_netdevice(x1);
7515 * register_netdevice(x2);
7516 * ...
7517 * unregister_netdevice(y1);
7518 * unregister_netdevice(y2);
7519 * ...
7520 * rtnl_unlock();
7521 * free_netdev(y1);
7522 * free_netdev(y2);
7523 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07007524 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007526 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 * without deadlocking with linkwatch via keventd.
7528 * 2) Since we run with the RTNL semaphore not held, we can sleep
7529 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07007530 *
7531 * We must not return until all unregister events added during
7532 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534void netdev_run_todo(void)
7535{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07007536 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07007539 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07007540
7541 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07007542
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007543
7544 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00007545 if (!list_empty(&list))
7546 rcu_barrier();
7547
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 while (!list_empty(&list)) {
7549 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00007550 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 list_del(&dev->todo_list);
7552
Eric Dumazet748e2d92012-08-22 21:50:59 +00007553 rtnl_lock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007554 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Eric Dumazet748e2d92012-08-22 21:50:59 +00007555 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00007556
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007557 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007558 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007560 dump_stack();
7561 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007563
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007564 dev->reg_state = NETREG_UNREGISTERED;
7565
7566 netdev_wait_allrefs(dev);
7567
7568 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00007569 BUG_ON(netdev_refcnt_read(dev));
Salam Noureddine7866a622015-01-27 11:35:48 -08007570 BUG_ON(!list_empty(&dev->ptype_all));
7571 BUG_ON(!list_empty(&dev->ptype_specific));
Eric Dumazet33d480c2011-08-11 19:30:52 +00007572 WARN_ON(rcu_access_pointer(dev->ip_ptr));
7573 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
Ilpo Järvinen547b7922008-07-25 21:43:18 -07007574 WARN_ON(dev->dn_ptr);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007575
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07007576 if (dev->destructor)
7577 dev->destructor(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07007578
Eric W. Biederman50624c92013-09-23 21:19:49 -07007579 /* Report a network device has been unregistered */
7580 rtnl_lock();
7581 dev_net(dev)->dev_unreg_count--;
7582 __rtnl_unlock();
7583 wake_up(&netdev_unregistering_wq);
7584
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07007585 /* Free network device */
7586 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588}
7589
Jarod Wilson92566452016-02-01 18:51:04 -05007590/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
7591 * all the same fields in the same order as net_device_stats, with only
7592 * the type differing, but rtnl_link_stats64 may have additional fields
7593 * at the end for newer counters.
Ben Hutchings3cfde792010-07-09 09:11:52 +00007594 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00007595void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
7596 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00007597{
7598#if BITS_PER_LONG == 64
Jarod Wilson92566452016-02-01 18:51:04 -05007599 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
Alban Browaeys05e165e2017-07-03 03:20:13 +02007600 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
Jarod Wilson92566452016-02-01 18:51:04 -05007601 /* zero out counters that only exist in rtnl_link_stats64 */
7602 memset((char *)stats64 + sizeof(*netdev_stats), 0,
7603 sizeof(*stats64) - sizeof(*netdev_stats));
Ben Hutchings3cfde792010-07-09 09:11:52 +00007604#else
Jarod Wilson92566452016-02-01 18:51:04 -05007605 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
Ben Hutchings3cfde792010-07-09 09:11:52 +00007606 const unsigned long *src = (const unsigned long *)netdev_stats;
7607 u64 *dst = (u64 *)stats64;
7608
Jarod Wilson92566452016-02-01 18:51:04 -05007609 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00007610 for (i = 0; i < n; i++)
7611 dst[i] = src[i];
Jarod Wilson92566452016-02-01 18:51:04 -05007612 /* zero out counters that only exist in rtnl_link_stats64 */
7613 memset((char *)stats64 + n * sizeof(u64), 0,
7614 sizeof(*stats64) - n * sizeof(u64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00007615#endif
7616}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00007617EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00007618
Eric Dumazetd83345a2009-11-16 03:36:51 +00007619/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007620 * dev_get_stats - get network device statistics
7621 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07007622 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007623 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00007624 * Get network statistics from device. Return @storage.
7625 * The device driver may provide its own method by setting
7626 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
7627 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007628 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00007629struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
7630 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00007631{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007632 const struct net_device_ops *ops = dev->netdev_ops;
7633
Eric Dumazet28172732010-07-07 14:58:56 -07007634 if (ops->ndo_get_stats64) {
7635 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00007636 ops->ndo_get_stats64(dev, storage);
7637 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00007638 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00007639 } else {
7640 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07007641 }
Eric Dumazet3f04c322017-06-27 07:02:20 -07007642 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
7643 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
7644 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
Eric Dumazet28172732010-07-07 14:58:56 -07007645 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07007646}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08007647EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07007648
Eric Dumazet24824a02010-10-02 06:11:55 +00007649struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07007650{
Eric Dumazet24824a02010-10-02 06:11:55 +00007651 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07007652
Eric Dumazet24824a02010-10-02 06:11:55 +00007653#ifdef CONFIG_NET_CLS_ACT
7654 if (queue)
7655 return queue;
7656 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
7657 if (!queue)
7658 return NULL;
7659 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet2ce1ee12015-02-04 13:37:44 -08007660 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00007661 queue->qdisc_sleeping = &noop_qdisc;
7662 rcu_assign_pointer(dev->ingress_queue, queue);
7663#endif
7664 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07007665}
7666
Eric Dumazet2c60db02012-09-16 09:17:26 +00007667static const struct ethtool_ops default_ethtool_ops;
7668
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00007669void netdev_set_default_ethtool_ops(struct net_device *dev,
7670 const struct ethtool_ops *ops)
7671{
7672 if (dev->ethtool_ops == &default_ethtool_ops)
7673 dev->ethtool_ops = ops;
7674}
7675EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
7676
Eric Dumazet74d332c2013-10-30 13:10:44 -07007677void netdev_freemem(struct net_device *dev)
7678{
7679 char *addr = (char *)dev - dev->padded;
7680
WANG Cong4cb28972014-06-02 15:55:22 -07007681 kvfree(addr);
Eric Dumazet74d332c2013-10-30 13:10:44 -07007682}
7683
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684/**
Tom Herbert36909ea2011-01-09 19:36:31 +00007685 * alloc_netdev_mqs - allocate network device
Tom Gundersenc835a672014-07-14 16:37:24 +02007686 * @sizeof_priv: size of private data to allocate space for
7687 * @name: device name format string
7688 * @name_assign_type: origin of device name
7689 * @setup: callback to initialize device
7690 * @txqs: the number of TX subqueues to allocate
7691 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 *
7693 * Allocates a struct net_device with private data area for driver use
Li Zhong90e51ad2013-11-22 15:04:46 +08007694 * and performs basic initialization. Also allocates subqueue structs
Tom Herbert36909ea2011-01-09 19:36:31 +00007695 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 */
Tom Herbert36909ea2011-01-09 19:36:31 +00007697struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
Tom Gundersenc835a672014-07-14 16:37:24 +02007698 unsigned char name_assign_type,
Tom Herbert36909ea2011-01-09 19:36:31 +00007699 void (*setup)(struct net_device *),
7700 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 struct net_device *dev;
Stephen Hemminger79439862008-07-21 13:28:44 -07007703 size_t alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00007704 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07007706 BUG_ON(strlen(name) >= sizeof(dev->name));
7707
Tom Herbert36909ea2011-01-09 19:36:31 +00007708 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007709 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00007710 return NULL;
7711 }
7712
Michael Daltona953be52014-01-16 22:23:28 -08007713#ifdef CONFIG_SYSFS
Tom Herbert36909ea2011-01-09 19:36:31 +00007714 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00007715 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00007716 return NULL;
7717 }
7718#endif
7719
David S. Millerfd2ea0a2008-07-17 01:56:23 -07007720 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07007721 if (sizeof_priv) {
7722 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00007723 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07007724 alloc_size += sizeof_priv;
7725 }
7726 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00007727 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728
Eric Dumazet74d332c2013-10-30 13:10:44 -07007729 p = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
7730 if (!p)
7731 p = vzalloc(alloc_size);
Joe Perches62b59422013-02-04 16:48:16 +00007732 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00007735 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00007737
Eric Dumazet29b44332010-10-11 10:22:12 +00007738 dev->pcpu_refcnt = alloc_percpu(int);
7739 if (!dev->pcpu_refcnt)
Eric Dumazet74d332c2013-10-30 13:10:44 -07007740 goto free_dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00007741
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00007743 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744
Jiri Pirko22bedad32010-04-01 21:22:57 +00007745 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00007746 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00007747
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09007748 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07007750 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00007751 dev->gso_max_segs = GSO_MAX_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752
Herbert Xud565b0a2008-12-15 23:38:52 -08007753 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00007754 INIT_LIST_HEAD(&dev->unreg_list);
Eric W. Biederman5cde2822013-10-05 19:26:05 -07007755 INIT_LIST_HEAD(&dev->close_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00007756 INIT_LIST_HEAD(&dev->link_watch_list);
Veaceslav Falico2f268f12013-09-25 09:20:07 +02007757 INIT_LIST_HEAD(&dev->adj_list.upper);
7758 INIT_LIST_HEAD(&dev->adj_list.lower);
7759 INIT_LIST_HEAD(&dev->all_adj_list.upper);
7760 INIT_LIST_HEAD(&dev->all_adj_list.lower);
Salam Noureddine7866a622015-01-27 11:35:48 -08007761 INIT_LIST_HEAD(&dev->ptype_all);
7762 INIT_LIST_HEAD(&dev->ptype_specific);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02007763#ifdef CONFIG_NET_SCHED
7764 hash_init(dev->qdisc_hash);
7765#endif
Eric Dumazet02875872014-10-05 18:38:35 -07007766 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08007768
Phil Suttera8131042016-02-17 15:37:43 +01007769 if (!dev->tx_queue_len) {
Phil Sutterf84bb1e2015-08-27 21:21:36 +02007770 dev->priv_flags |= IFF_NO_QUEUE;
Phil Suttera8131042016-02-17 15:37:43 +01007771 dev->tx_queue_len = 1;
7772 }
Phil Sutter906470c2015-08-18 10:30:48 +02007773
David S. Miller8d3bdbd2011-02-08 15:02:50 -08007774 dev->num_tx_queues = txqs;
7775 dev->real_num_tx_queues = txqs;
7776 if (netif_alloc_netdev_queues(dev))
7777 goto free_all;
7778
Michael Daltona953be52014-01-16 22:23:28 -08007779#ifdef CONFIG_SYSFS
David S. Miller8d3bdbd2011-02-08 15:02:50 -08007780 dev->num_rx_queues = rxqs;
7781 dev->real_num_rx_queues = rxqs;
7782 if (netif_alloc_rx_queues(dev))
7783 goto free_all;
7784#endif
7785
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 strcpy(dev->name, name);
Tom Gundersenc835a672014-07-14 16:37:24 +02007787 dev->name_assign_type = name_assign_type;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00007788 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00007789 if (!dev->ethtool_ops)
7790 dev->ethtool_ops = &default_ethtool_ops;
Pablo Neirae687ad62015-05-13 18:19:38 +02007791
7792 nf_hook_ingress_init(dev);
7793
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00007795
David S. Miller8d3bdbd2011-02-08 15:02:50 -08007796free_all:
7797 free_netdev(dev);
7798 return NULL;
7799
Eric Dumazet29b44332010-10-11 10:22:12 +00007800free_pcpu:
7801 free_percpu(dev->pcpu_refcnt);
Eric Dumazet74d332c2013-10-30 13:10:44 -07007802free_dev:
7803 netdev_freemem(dev);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00007804 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805}
Tom Herbert36909ea2011-01-09 19:36:31 +00007806EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
7808/**
7809 * free_netdev - free network device
7810 * @dev: device
7811 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007812 * This function does the last stage of destroying an allocated device
7813 * interface. The reference to the device object is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 * If this is the last reference then it will be freed.
Eric Dumazet93d05d42015-11-18 06:31:03 -08007815 * Must be called in process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 */
7817void free_netdev(struct net_device *dev)
7818{
Herbert Xud565b0a2008-12-15 23:38:52 -08007819 struct napi_struct *p, *n;
7820
Eric Dumazet93d05d42015-11-18 06:31:03 -08007821 might_sleep();
Eric Dumazet60877a32013-06-20 01:15:51 -07007822 netif_free_tx_queues(dev);
Michael Daltona953be52014-01-16 22:23:28 -08007823#ifdef CONFIG_SYSFS
Pankaj Gupta10595902015-01-12 11:41:28 +05307824 kvfree(dev->_rx);
Tom Herbertfe822242010-11-09 10:47:38 +00007825#endif
David S. Millere8a04642008-07-17 00:34:19 -07007826
Eric Dumazet33d480c2011-08-11 19:30:52 +00007827 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00007828
Jiri Pirkof001fde2009-05-05 02:48:28 +00007829 /* Flush device addresses */
7830 dev_addr_flush(dev);
7831
Herbert Xud565b0a2008-12-15 23:38:52 -08007832 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
7833 netif_napi_del(p);
7834
Eric Dumazet29b44332010-10-11 10:22:12 +00007835 free_percpu(dev->pcpu_refcnt);
7836 dev->pcpu_refcnt = NULL;
7837
Stephen Hemminger3041a062006-05-26 13:25:24 -07007838 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 if (dev->reg_state == NETREG_UNINITIALIZED) {
Eric Dumazet74d332c2013-10-30 13:10:44 -07007840 netdev_freemem(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 return;
7842 }
7843
7844 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
7845 dev->reg_state = NETREG_RELEASED;
7846
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07007847 /* will free via device release */
7848 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007850EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007851
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07007852/**
7853 * synchronize_net - Synchronize with packet receive processing
7854 *
7855 * Wait for packets currently being received to be done.
7856 * Does not block later packets from starting.
7857 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09007858void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859{
7860 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00007861 if (rtnl_is_locked())
7862 synchronize_rcu_expedited();
7863 else
7864 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07007866EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867
7868/**
Eric Dumazet44a08732009-10-27 07:03:04 +00007869 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00007871 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08007872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08007874 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00007875 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 *
7877 * Callers must hold the rtnl semaphore. You may want
7878 * unregister_netdev() instead of this.
7879 */
7880
Eric Dumazet44a08732009-10-27 07:03:04 +00007881void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882{
Herbert Xua6620712007-12-12 19:21:56 -08007883 ASSERT_RTNL();
7884
Eric Dumazet44a08732009-10-27 07:03:04 +00007885 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00007886 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00007887 } else {
7888 rollback_registered(dev);
7889 /* Finish processing unregister after unlock */
7890 net_set_todo(dev);
7891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892}
Eric Dumazet44a08732009-10-27 07:03:04 +00007893EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
7895/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007896 * unregister_netdevice_many - unregister many devices
7897 * @head: list of devices
Eric Dumazet87757a92014-06-06 06:44:03 -07007898 *
7899 * Note: As most callers use a stack allocated list_head,
7900 * we force a list_del() to make sure stack wont be corrupted later.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007901 */
7902void unregister_netdevice_many(struct list_head *head)
7903{
7904 struct net_device *dev;
7905
7906 if (!list_empty(head)) {
7907 rollback_registered_many(head);
7908 list_for_each_entry(dev, head, unreg_list)
7909 net_set_todo(dev);
Eric Dumazet87757a92014-06-06 06:44:03 -07007910 list_del(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007911 }
7912}
Eric Dumazet63c80992009-10-27 07:06:49 +00007913EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00007914
7915/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 * unregister_netdev - remove device from the kernel
7917 * @dev: device
7918 *
7919 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08007920 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 *
7922 * This is just a wrapper for unregister_netdevice that takes
7923 * the rtnl semaphore. In general you want to use this and not
7924 * unregister_netdevice.
7925 */
7926void unregister_netdev(struct net_device *dev)
7927{
7928 rtnl_lock();
7929 unregister_netdevice(dev);
7930 rtnl_unlock();
7931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932EXPORT_SYMBOL(unregister_netdev);
7933
Eric W. Biedermance286d32007-09-12 13:53:49 +02007934/**
7935 * dev_change_net_namespace - move device to different nethost namespace
7936 * @dev: device
7937 * @net: network namespace
7938 * @pat: If not NULL name pattern to try if the current device name
7939 * is already taken in the destination network namespace.
7940 *
7941 * This function shuts down a device interface and moves it
7942 * to a new network namespace. On success 0 is returned, on
7943 * a failure a netagive errno code is returned.
7944 *
7945 * Callers must hold the rtnl semaphore.
7946 */
7947
7948int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
7949{
Eric W. Biedermance286d32007-09-12 13:53:49 +02007950 int err;
7951
7952 ASSERT_RTNL();
7953
7954 /* Don't allow namespace local devices to be moved. */
7955 err = -EINVAL;
7956 if (dev->features & NETIF_F_NETNS_LOCAL)
7957 goto out;
7958
7959 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02007960 if (dev->reg_state != NETREG_REGISTERED)
7961 goto out;
7962
7963 /* Get out if there is nothing todo */
7964 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09007965 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02007966 goto out;
7967
7968 /* Pick the destination device name, and ensure
7969 * we can use it in the destination network namespace.
7970 */
7971 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00007972 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02007973 /* We get here if we can't use the current device name */
7974 if (!pat)
7975 goto out;
Li RongQing53adf732018-06-19 17:23:17 +08007976 err = dev_get_valid_name(net, dev, pat);
7977 if (err < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02007978 goto out;
7979 }
7980
7981 /*
7982 * And now a mini version of register_netdevice unregister_netdevice.
7983 */
7984
7985 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07007986 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02007987
7988 /* And unlink it from device chain */
Eric W. Biedermance286d32007-09-12 13:53:49 +02007989 unlist_netdevice(dev);
7990
7991 synchronize_net();
7992
7993 /* Shutdown queueing discipline. */
7994 dev_shutdown(dev);
7995
7996 /* Notify protocols, that we are about to destroy
7997 this device. They should clean all the things.
David Lamparter3b27e102010-09-17 03:22:19 +00007998
7999 Note that dev->reg_state stays at NETREG_REGISTERED.
8000 This is wanted because this way 8021q and macvlan know
8001 the device is just moving and can keep their slaves up.
Eric W. Biedermance286d32007-09-12 13:53:49 +02008002 */
8003 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +00008004 rcu_barrier();
8005 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008006 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008007
8008 /*
8009 * Flush the unicast and multicast chains
8010 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00008011 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00008012 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008013
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008014 /* Send a netdev-removed uevent to the old namespace */
8015 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008016 netdev_adjacent_del_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008017
Eric W. Biedermance286d32007-09-12 13:53:49 +02008018 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09008019 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008020
Eric W. Biedermance286d32007-09-12 13:53:49 +02008021 /* If there is an ifindex conflict assign a new one */
Nicolas Dichtel7a66bbc2015-04-02 17:07:09 +02008022 if (__dev_get_by_index(net, dev->ifindex))
Eric W. Biedermance286d32007-09-12 13:53:49 +02008023 dev->ifindex = dev_new_index(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008024
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008025 /* Send a netdev-add uevent to the new namespace */
8026 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
Alexander Y. Fomichev4c754312014-08-25 16:26:45 +04008027 netdev_adjacent_add_links(dev);
Serge Hallyn4e66ae22012-12-03 16:17:12 +00008028
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008029 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07008030 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008031 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008032
8033 /* Add the device back in the hashes */
8034 list_netdevice(dev);
8035
8036 /* Notify protocols, that a new device appeared. */
8037 call_netdevice_notifiers(NETDEV_REGISTER, dev);
8038
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008039 /*
8040 * Prevent userspace races by waiting until the network
8041 * device is fully setup before sending notifications.
8042 */
Alexei Starovoitov7f294052013-10-23 16:02:42 -07008043 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
Eric W. Biedermand90a9092009-12-12 22:11:15 +00008044
Eric W. Biedermance286d32007-09-12 13:53:49 +02008045 synchronize_net();
8046 err = 0;
8047out:
8048 return err;
8049}
Johannes Berg463d0182009-07-14 00:33:35 +02008050EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008051
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052static int dev_cpu_callback(struct notifier_block *nfb,
8053 unsigned long action,
8054 void *ocpu)
8055{
8056 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057 struct sk_buff *skb;
8058 unsigned int cpu, oldcpu = (unsigned long)ocpu;
8059 struct softnet_data *sd, *oldsd;
8060
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008061 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062 return NOTIFY_OK;
8063
8064 local_irq_disable();
8065 cpu = smp_processor_id();
8066 sd = &per_cpu(softnet_data, cpu);
8067 oldsd = &per_cpu(softnet_data, oldcpu);
8068
8069 /* Find end of our completion_queue. */
8070 list_skb = &sd->completion_queue;
8071 while (*list_skb)
8072 list_skb = &(*list_skb)->next;
8073 /* Append completion queue from offline CPU. */
8074 *list_skb = oldsd->completion_queue;
8075 oldsd->completion_queue = NULL;
8076
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00008078 if (oldsd->output_queue) {
8079 *sd->output_queue_tailp = oldsd->output_queue;
8080 sd->output_queue_tailp = oldsd->output_queue_tailp;
8081 oldsd->output_queue = NULL;
8082 oldsd->output_queue_tailp = &oldsd->output_queue;
8083 }
Eric Dumazetac64da02015-01-15 17:04:22 -08008084 /* Append NAPI poll list from offline CPU, with one exception :
8085 * process_backlog() must be called by cpu owning percpu backlog.
8086 * We properly handle process_queue & input_pkt_queue later.
8087 */
8088 while (!list_empty(&oldsd->poll_list)) {
8089 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
8090 struct napi_struct,
8091 poll_list);
8092
8093 list_del_init(&napi->poll_list);
8094 if (napi->poll == process_backlog)
8095 napi->state = 0;
8096 else
8097 ____napi_schedule(sd, napi);
Heiko Carstens264524d2011-06-06 20:50:03 +00008098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099
8100 raise_softirq_irqoff(NET_TX_SOFTIRQ);
8101 local_irq_enable();
8102
8103 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00008104 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08008105 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00008106 input_queue_head_incr(oldsd);
8107 }
Eric Dumazetac64da02015-01-15 17:04:22 -08008108 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
Eric Dumazet91e83132015-02-05 14:58:14 -08008109 netif_rx_ni(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00008110 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07008111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112
8113 return NOTIFY_OK;
8114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115
8116
Herbert Xu7f353bf2007-08-10 15:47:58 -07008117/**
Herbert Xub63365a2008-10-23 01:11:29 -07008118 * netdev_increment_features - increment feature set by one
8119 * @all: current feature set
8120 * @one: new feature set
8121 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07008122 *
8123 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07008124 * @one to the master device with current feature set @all. Will not
8125 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07008126 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008127netdev_features_t netdev_increment_features(netdev_features_t all,
8128 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07008129{
Tom Herbertc8cd0982015-12-14 11:19:44 -08008130 if (mask & NETIF_F_HW_CSUM)
Tom Herberta1882222015-12-14 11:19:43 -08008131 mask |= NETIF_F_CSUM_MASK;
Michał Mirosław1742f182011-04-22 06:31:16 +00008132 mask |= NETIF_F_VLAN_CHALLENGED;
8133
Tom Herberta1882222015-12-14 11:19:43 -08008134 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
Michał Mirosław1742f182011-04-22 06:31:16 +00008135 all &= one | ~NETIF_F_ALL_FOR_ALL;
8136
Michał Mirosław1742f182011-04-22 06:31:16 +00008137 /* If one device supports hw checksumming, set for all. */
Tom Herbertc8cd0982015-12-14 11:19:44 -08008138 if (all & NETIF_F_HW_CSUM)
8139 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -07008140
8141 return all;
8142}
Herbert Xub63365a2008-10-23 01:11:29 -07008143EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07008144
Baruch Siach430f03c2013-06-02 20:43:55 +00008145static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008146{
8147 int i;
8148 struct hlist_head *hash;
8149
8150 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
8151 if (hash != NULL)
8152 for (i = 0; i < NETDEV_HASHENTRIES; i++)
8153 INIT_HLIST_HEAD(&hash[i]);
8154
8155 return hash;
8156}
8157
Eric W. Biederman881d9662007-09-17 11:56:21 -07008158/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07008159static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07008160{
Rustad, Mark D734b6542012-07-18 09:06:07 +00008161 if (net != &init_net)
8162 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07008163
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008164 net->dev_name_head = netdev_create_hash();
8165 if (net->dev_name_head == NULL)
8166 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008167
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008168 net->dev_index_head = netdev_create_hash();
8169 if (net->dev_index_head == NULL)
8170 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008171
8172 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07008173
8174err_idx:
8175 kfree(net->dev_name_head);
8176err_name:
8177 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07008178}
8179
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008180/**
8181 * netdev_drivername - network driver for the device
8182 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07008183 *
8184 * Determine network driver for device.
8185 */
David S. Miller3019de12011-06-06 16:41:33 -07008186const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -07008187{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07008188 const struct device_driver *driver;
8189 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -07008190 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -07008191
8192 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008193 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -07008194 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008195
8196 driver = parent->driver;
8197 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -07008198 return driver->name;
8199 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07008200}
8201
Joe Perches6ea754e2014-09-22 11:10:50 -07008202static void __netdev_printk(const char *level, const struct net_device *dev,
8203 struct va_format *vaf)
Joe Perches256df2f2010-06-27 01:02:35 +00008204{
Joe Perchesb004ff42012-09-12 20:12:19 -07008205 if (dev && dev->dev.parent) {
Joe Perches6ea754e2014-09-22 11:10:50 -07008206 dev_printk_emit(level[1] - '0',
8207 dev->dev.parent,
8208 "%s %s %s%s: %pV",
8209 dev_driver_string(dev->dev.parent),
8210 dev_name(dev->dev.parent),
8211 netdev_name(dev), netdev_reg_state(dev),
8212 vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008213 } else if (dev) {
Joe Perches6ea754e2014-09-22 11:10:50 -07008214 printk("%s%s%s: %pV",
8215 level, netdev_name(dev), netdev_reg_state(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008216 } else {
Joe Perches6ea754e2014-09-22 11:10:50 -07008217 printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008218 }
Joe Perches256df2f2010-06-27 01:02:35 +00008219}
8220
Joe Perches6ea754e2014-09-22 11:10:50 -07008221void netdev_printk(const char *level, const struct net_device *dev,
8222 const char *format, ...)
Joe Perches256df2f2010-06-27 01:02:35 +00008223{
8224 struct va_format vaf;
8225 va_list args;
Joe Perches256df2f2010-06-27 01:02:35 +00008226
8227 va_start(args, format);
8228
8229 vaf.fmt = format;
8230 vaf.va = &args;
8231
Joe Perches6ea754e2014-09-22 11:10:50 -07008232 __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07008233
Joe Perches256df2f2010-06-27 01:02:35 +00008234 va_end(args);
Joe Perches256df2f2010-06-27 01:02:35 +00008235}
8236EXPORT_SYMBOL(netdev_printk);
8237
8238#define define_netdev_printk_level(func, level) \
Joe Perches6ea754e2014-09-22 11:10:50 -07008239void func(const struct net_device *dev, const char *fmt, ...) \
Joe Perches256df2f2010-06-27 01:02:35 +00008240{ \
Joe Perches256df2f2010-06-27 01:02:35 +00008241 struct va_format vaf; \
8242 va_list args; \
8243 \
8244 va_start(args, fmt); \
8245 \
8246 vaf.fmt = fmt; \
8247 vaf.va = &args; \
8248 \
Joe Perches6ea754e2014-09-22 11:10:50 -07008249 __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -07008250 \
Joe Perches256df2f2010-06-27 01:02:35 +00008251 va_end(args); \
Joe Perches256df2f2010-06-27 01:02:35 +00008252} \
8253EXPORT_SYMBOL(func);
8254
8255define_netdev_printk_level(netdev_emerg, KERN_EMERG);
8256define_netdev_printk_level(netdev_alert, KERN_ALERT);
8257define_netdev_printk_level(netdev_crit, KERN_CRIT);
8258define_netdev_printk_level(netdev_err, KERN_ERR);
8259define_netdev_printk_level(netdev_warn, KERN_WARNING);
8260define_netdev_printk_level(netdev_notice, KERN_NOTICE);
8261define_netdev_printk_level(netdev_info, KERN_INFO);
8262
Pavel Emelyanov46650792007-10-08 20:38:39 -07008263static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07008264{
8265 kfree(net->dev_name_head);
8266 kfree(net->dev_index_head);
8267}
8268
Denis V. Lunev022cbae2007-11-13 03:23:50 -08008269static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07008270 .init = netdev_init,
8271 .exit = netdev_exit,
8272};
8273
Pavel Emelyanov46650792007-10-08 20:38:39 -07008274static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02008275{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008276 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02008277 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008278 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02008279 * initial network namespace
8280 */
8281 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008282 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008283 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008284 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02008285
8286 /* Ignore unmoveable devices (i.e. loopback) */
8287 if (dev->features & NETIF_F_NETNS_LOCAL)
8288 continue;
8289
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00008290 /* Leave virtual devices for the generic cleanup */
8291 if (dev->rtnl_link_ops)
8292 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08008293
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008294 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008295 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
8296 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02008297 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00008298 pr_emerg("%s: failed to move %s to init_net: %d\n",
8299 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07008300 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02008301 }
8302 }
8303 rtnl_unlock();
8304}
8305
Eric W. Biederman50624c92013-09-23 21:19:49 -07008306static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
8307{
8308 /* Return with the rtnl_lock held when there are no network
8309 * devices unregistering in any network namespace in net_list.
8310 */
8311 struct net *net;
8312 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +01008313 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008314
Peter Zijlstraff960a72014-10-29 17:04:56 +01008315 add_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008316 for (;;) {
Eric W. Biederman50624c92013-09-23 21:19:49 -07008317 unregistering = false;
8318 rtnl_lock();
8319 list_for_each_entry(net, net_list, exit_list) {
8320 if (net->dev_unreg_count > 0) {
8321 unregistering = true;
8322 break;
8323 }
8324 }
8325 if (!unregistering)
8326 break;
8327 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +01008328
8329 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008330 }
Peter Zijlstraff960a72014-10-29 17:04:56 +01008331 remove_wait_queue(&netdev_unregistering_wq, &wait);
Eric W. Biederman50624c92013-09-23 21:19:49 -07008332}
8333
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008334static void __net_exit default_device_exit_batch(struct list_head *net_list)
8335{
8336 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04008337 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008338 * Do this across as many network namespaces as possible to
8339 * improve batching efficiency.
8340 */
8341 struct net_device *dev;
8342 struct net *net;
8343 LIST_HEAD(dev_kill_list);
8344
Eric W. Biederman50624c92013-09-23 21:19:49 -07008345 /* To prevent network device cleanup code from dereferencing
8346 * loopback devices or network devices that have been freed
8347 * wait here for all pending unregistrations to complete,
8348 * before unregistring the loopback device and allowing the
8349 * network namespace be freed.
8350 *
8351 * The netdev todo list containing all network devices
8352 * unregistrations that happen in default_device_exit_batch
8353 * will run in the rtnl_unlock() at the end of
8354 * default_device_exit_batch.
8355 */
8356 rtnl_lock_unregistering(net_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008357 list_for_each_entry(net, net_list, exit_list) {
8358 for_each_netdev_reverse(net, dev) {
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02008359 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008360 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
8361 else
8362 unregister_netdevice_queue(dev, &dev_kill_list);
8363 }
8364 }
8365 unregister_netdevice_many(&dev_kill_list);
8366 rtnl_unlock();
8367}
8368
Denis V. Lunev022cbae2007-11-13 03:23:50 -08008369static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02008370 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00008371 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02008372};
8373
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374/*
8375 * Initialize the DEV module. At boot time this walks the device list and
8376 * unhooks any devices that fail to initialise (normally hardware not
8377 * present) and leaves us with a valid list of present and active devices.
8378 *
8379 */
8380
8381/*
8382 * This is called single threaded during boot, so no need
8383 * to take the rtnl semaphore.
8384 */
8385static int __init net_dev_init(void)
8386{
8387 int i, rc = -ENOMEM;
8388
8389 BUG_ON(!dev_boot_phase);
8390
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391 if (dev_proc_init())
8392 goto out;
8393
Eric W. Biederman8b41d182007-09-26 22:02:53 -07008394 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 goto out;
8396
8397 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08008398 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 INIT_LIST_HEAD(&ptype_base[i]);
8400
Vlad Yasevich62532da2012-11-15 08:49:10 +00008401 INIT_LIST_HEAD(&offload_base);
8402
Eric W. Biederman881d9662007-09-17 11:56:21 -07008403 if (register_pernet_subsys(&netdev_net_ops))
8404 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405
8406 /*
8407 * Initialise the packet receive queues.
8408 */
8409
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07008410 for_each_possible_cpu(i) {
Eric Dumazet41852492016-08-26 12:50:39 -07008411 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008412 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
Eric Dumazet41852492016-08-26 12:50:39 -07008414 INIT_WORK(flush, flush_backlog);
8415
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008416 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07008417 skb_queue_head_init(&sd->process_queue);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008418 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00008419 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00008420#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008421 sd->csd.func = rps_trigger_softirq;
8422 sd->csd.info = sd;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008423 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07008424#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00008425
Eric Dumazete36fa2f2010-04-19 21:17:14 +00008426 sd->backlog.poll = process_backlog;
8427 sd->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 }
8429
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 dev_boot_phase = 0;
8431
Eric W. Biederman505d4f72008-11-07 22:54:20 -08008432 /* The loopback device is special if any other network devices
8433 * is present in a network namespace the loopback device must
8434 * be present. Since we now dynamically allocate and free the
8435 * loopback device ensure this invariant is maintained by
8436 * keeping the loopback device as the first device on the
8437 * list of network devices. Ensuring the loopback devices
8438 * is the first device that appears and the last network device
8439 * that disappears.
8440 */
8441 if (register_pernet_device(&loopback_net_ops))
8442 goto out;
8443
8444 if (register_pernet_device(&default_device_ops))
8445 goto out;
8446
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008447 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
8448 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449
8450 hotcpu_notifier(dev_cpu_callback, 0);
Thomas Graff38a9eb2015-07-21 10:43:56 +02008451 dst_subsys_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 rc = 0;
8453out:
8454 return rc;
8455}
8456
8457subsys_initcall(net_dev_init);