blob: 6a93cd8cd2646d8dae8fcd6803ce6a89bd48f6fb [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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020097#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <net/sock.h>
99#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <net/dst.h>
102#include <net/pkt_sched.h>
103#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000104#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <linux/highmem.h>
106#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/netpoll.h>
109#include <linux/rcupdate.h>
110#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500113#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700114#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700115#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700116#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700117#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700118#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700119#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700120#include <net/ip.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700121#include <linux/ipv6.h>
122#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700123#include <linux/jhash.h>
124#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700125#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900126#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900127#include <trace/events/skb.h>
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +0000128#include <linux/pci.h>
Stephen Rothwellcaeda9b2010-09-16 21:39:16 -0700129#include <linux/inetdevice.h>
Ben Hutchingsc4454772011-01-19 11:03:53 +0000130#include <linux/cpu_rmap.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +0100131#include <linux/static_key.h>
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300132#include <linux/hashtable.h>
Eric Dumazet60877a32013-06-20 01:15:51 -0700133#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700135#include "net-sysfs.h"
136
Herbert Xud565b0a2008-12-15 23:38:52 -0800137/* Instead of increasing this, you should create a hash table. */
138#define MAX_GRO_SKBS 8
139
Herbert Xu5d38a072009-01-04 16:13:40 -0800140/* This should be increased if a protocol with a bigger head is added. */
141#define GRO_MAX_HEAD (MAX_HEADER + 128)
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static DEFINE_SPINLOCK(ptype_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000144static DEFINE_SPINLOCK(offload_lock);
Cong Wang900ff8c2013-02-18 19:20:33 +0000145struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
146struct list_head ptype_all __read_mostly; /* Taps */
Vlad Yasevich62532da2012-11-15 08:49:10 +0000147static struct list_head offload_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700150 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * semaphore.
152 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800153 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
155 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700156 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * actual updates. This allows pure readers to access the list even
158 * while a writer is preparing to update it.
159 *
160 * To put it another way, dev_base_lock is held for writing only to
161 * protect against pure readers; the rtnl semaphore provides the
162 * protection against other writers.
163 *
164 * See, for example usages, register_netdevice() and
165 * unregister_netdevice(), which must be called with the rtnl
166 * semaphore held.
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169EXPORT_SYMBOL(dev_base_lock);
170
Eliezer Tamiraf12fa62013-06-10 11:39:41 +0300171/* protects napi_hash addition/deletion and napi_gen_id */
172static DEFINE_SPINLOCK(napi_hash_lock);
173
174static unsigned int napi_gen_id;
175static DEFINE_HASHTABLE(napi_hash, 8);
176
Eric Dumazet30e6c9f2012-12-20 17:25:08 +0000177seqcount_t devnet_rename_seq;
Brian Haleyc91f6df2012-11-26 05:21:08 +0000178
Thomas Graf4e985ad2011-06-21 03:11:20 +0000179static inline void dev_base_seq_inc(struct net *net)
180{
181 while (++net->dev_base_seq == 0);
182}
183
Eric W. Biederman881d9662007-09-17 11:56:21 -0700184static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Eric Dumazet95c96172012-04-15 05:58:06 +0000186 unsigned int hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
187
stephen hemminger08e98972009-11-10 07:20:34 +0000188 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Eric W. Biederman881d9662007-09-17 11:56:21 -0700191static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700193 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000196static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000197{
198#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000199 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000200#endif
201}
202
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000203static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000204{
205#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000206 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000207#endif
208}
209
Eric W. Biedermance286d32007-09-12 13:53:49 +0200210/* Device list insertion */
dingtianhong53759be2013-04-17 22:17:50 +0000211static void list_netdevice(struct net_device *dev)
Eric W. Biedermance286d32007-09-12 13:53:49 +0200212{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900213 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200214
215 ASSERT_RTNL();
216
217 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800218 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000219 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000220 hlist_add_head_rcu(&dev->index_hlist,
221 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200222 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000223
224 dev_base_seq_inc(net);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200225}
226
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000227/* Device list removal
228 * caller must respect a RCU grace period before freeing/reusing dev
229 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200230static void unlist_netdevice(struct net_device *dev)
231{
232 ASSERT_RTNL();
233
234 /* Unlink dev from the device chain */
235 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800236 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000237 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000238 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200239 write_unlock_bh(&dev_base_lock);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000240
241 dev_base_seq_inc(dev_net(dev));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * Our notifier list
246 */
247
Alan Sternf07d5b92006-05-09 15:23:03 -0700248static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/*
251 * Device drivers call our routines to queue packets here. We empty the
252 * queue in the local softnet handler.
253 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700254
Eric Dumazet9958da02010-04-17 04:17:02 +0000255DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700256EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David S. Millercf508b12008-07-22 14:16:42 -0700258#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700259/*
David S. Millerc773e842008-07-08 23:13:53 -0700260 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700261 * according to dev->type
262 */
263static const unsigned short netdev_lock_type[] =
264 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
265 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
266 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
267 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
268 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
269 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
270 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
271 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
272 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
273 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
274 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
275 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
Paul Gortmaker211ed862012-05-10 17:14:35 -0400276 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
277 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
278 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700279
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700280static const char *const netdev_lock_name[] =
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700281 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
282 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
283 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
284 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
285 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
286 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
287 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
288 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
289 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
290 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
291 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
292 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
Paul Gortmaker211ed862012-05-10 17:14:35 -0400293 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
294 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
295 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700296
297static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700298static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700299
300static inline unsigned short netdev_lock_pos(unsigned short dev_type)
301{
302 int i;
303
304 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
305 if (netdev_lock_type[i] == dev_type)
306 return i;
307 /* the last key is used by default */
308 return ARRAY_SIZE(netdev_lock_type) - 1;
309}
310
David S. Millercf508b12008-07-22 14:16:42 -0700311static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
312 unsigned short dev_type)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700313{
314 int i;
315
316 i = netdev_lock_pos(dev_type);
317 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
318 netdev_lock_name[i]);
319}
David S. Millercf508b12008-07-22 14:16:42 -0700320
321static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
322{
323 int i;
324
325 i = netdev_lock_pos(dev->type);
326 lockdep_set_class_and_name(&dev->addr_list_lock,
327 &netdev_addr_lock_key[i],
328 netdev_lock_name[i]);
329}
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700330#else
David S. Millercf508b12008-07-22 14:16:42 -0700331static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
332 unsigned short dev_type)
333{
334}
335static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700336{
337}
338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/*******************************************************************************
341
342 Protocol management and registration routines
343
344*******************************************************************************/
345
346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * Add a protocol ID to the list. Now that the input handler is
348 * smarter we can dispense with all the messy stuff that used to be
349 * here.
350 *
351 * BEWARE!!! Protocol handlers, mangling input packets,
352 * MUST BE last in hash buckets and checking protocol handlers
353 * MUST start from promiscuous ptype_all chain in net_bh.
354 * It is true now, do not change it.
355 * Explanation follows: if protocol handler, mangling packet, will
356 * be the first on list, it is not able to sense, that packet
357 * is cloned and should be copied-on-write, so that it will
358 * change it and subsequent readers will get broken packet.
359 * --ANK (980803)
360 */
361
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000362static inline struct list_head *ptype_head(const struct packet_type *pt)
363{
364 if (pt->type == htons(ETH_P_ALL))
365 return &ptype_all;
366 else
367 return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/**
371 * dev_add_pack - add packet handler
372 * @pt: packet type declaration
373 *
374 * Add a protocol handler to the networking stack. The passed &packet_type
375 * is linked into kernel lists and may not be freed until it has been
376 * removed from the kernel lists.
377 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900378 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * guarantee all CPU's that are in middle of receiving packets
380 * will see the new packet type (until the next received packet).
381 */
382
383void dev_add_pack(struct packet_type *pt)
384{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000385 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000387 spin_lock(&ptype_lock);
388 list_add_rcu(&pt->list, head);
389 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700391EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
394 * __dev_remove_pack - remove packet handler
395 * @pt: packet type declaration
396 *
397 * Remove a protocol handler that was previously added to the kernel
398 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
399 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900400 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *
402 * The packet type might still be in use by receivers
403 * and must not be freed until after all the CPU's have gone
404 * through a quiescent state.
405 */
406void __dev_remove_pack(struct packet_type *pt)
407{
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000408 struct list_head *head = ptype_head(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct packet_type *pt1;
410
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000411 spin_lock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 list_for_each_entry(pt1, head, list) {
414 if (pt == pt1) {
415 list_del_rcu(&pt->list);
416 goto out;
417 }
418 }
419
Joe Perches7b6cd1c2012-02-01 10:54:43 +0000420 pr_warn("dev_remove_pack: %p not found\n", pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421out:
Eric Dumazetc07b68e2010-09-02 03:53:46 +0000422 spin_unlock(&ptype_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700424EXPORT_SYMBOL(__dev_remove_pack);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/**
427 * dev_remove_pack - remove packet handler
428 * @pt: packet type declaration
429 *
430 * Remove a protocol handler that was previously added to the kernel
431 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
432 * from the kernel lists and can be freed or reused once this function
433 * returns.
434 *
435 * This call sleeps to guarantee that no CPU is looking at the packet
436 * type after return.
437 */
438void dev_remove_pack(struct packet_type *pt)
439{
440 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 synchronize_net();
443}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700444EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Vlad Yasevich62532da2012-11-15 08:49:10 +0000446
447/**
448 * dev_add_offload - register offload handlers
449 * @po: protocol offload declaration
450 *
451 * Add protocol offload handlers to the networking stack. The passed
452 * &proto_offload is linked into kernel lists and may not be freed until
453 * it has been removed from the kernel lists.
454 *
455 * This call does not sleep therefore it can not
456 * guarantee all CPU's that are in middle of receiving packets
457 * will see the new offload handlers (until the next received packet).
458 */
459void dev_add_offload(struct packet_offload *po)
460{
461 struct list_head *head = &offload_base;
462
463 spin_lock(&offload_lock);
464 list_add_rcu(&po->list, head);
465 spin_unlock(&offload_lock);
466}
467EXPORT_SYMBOL(dev_add_offload);
468
469/**
470 * __dev_remove_offload - remove offload handler
471 * @po: packet offload declaration
472 *
473 * Remove a protocol offload handler that was previously added to the
474 * kernel offload handlers by dev_add_offload(). The passed &offload_type
475 * is removed from the kernel lists and can be freed or reused once this
476 * function returns.
477 *
478 * The packet type might still be in use by receivers
479 * and must not be freed until after all the CPU's have gone
480 * through a quiescent state.
481 */
482void __dev_remove_offload(struct packet_offload *po)
483{
484 struct list_head *head = &offload_base;
485 struct packet_offload *po1;
486
Eric Dumazetc53aa502012-11-16 08:08:23 +0000487 spin_lock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000488
489 list_for_each_entry(po1, head, list) {
490 if (po == po1) {
491 list_del_rcu(&po->list);
492 goto out;
493 }
494 }
495
496 pr_warn("dev_remove_offload: %p not found\n", po);
497out:
Eric Dumazetc53aa502012-11-16 08:08:23 +0000498 spin_unlock(&offload_lock);
Vlad Yasevich62532da2012-11-15 08:49:10 +0000499}
500EXPORT_SYMBOL(__dev_remove_offload);
501
502/**
503 * dev_remove_offload - remove packet offload handler
504 * @po: packet offload declaration
505 *
506 * Remove a packet offload handler that was previously added to the kernel
507 * offload handlers by dev_add_offload(). The passed &offload_type is
508 * removed from the kernel lists and can be freed or reused once this
509 * function returns.
510 *
511 * This call sleeps to guarantee that no CPU is looking at the packet
512 * type after return.
513 */
514void dev_remove_offload(struct packet_offload *po)
515{
516 __dev_remove_offload(po);
517
518 synchronize_net();
519}
520EXPORT_SYMBOL(dev_remove_offload);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/******************************************************************************
523
524 Device Boot-time Settings Routines
525
526*******************************************************************************/
527
528/* Boot time configuration table */
529static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
530
531/**
532 * netdev_boot_setup_add - add new setup entry
533 * @name: name of the device
534 * @map: configured settings for the device
535 *
536 * Adds new setup entry to the dev_boot_setup list. The function
537 * returns 0 on error and 1 on success. This is a generic routine to
538 * all netdevices.
539 */
540static int netdev_boot_setup_add(char *name, struct ifmap *map)
541{
542 struct netdev_boot_setup *s;
543 int i;
544
545 s = dev_boot_setup;
546 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
547 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
548 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700549 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 memcpy(&s[i].map, map, sizeof(s[i].map));
551 break;
552 }
553 }
554
555 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
556}
557
558/**
559 * netdev_boot_setup_check - check boot time settings
560 * @dev: the netdevice
561 *
562 * Check boot time settings for the device.
563 * The found settings are set for the device to be used
564 * later in the device probing.
565 * Returns 0 if no settings found, 1 if they are.
566 */
567int netdev_boot_setup_check(struct net_device *dev)
568{
569 struct netdev_boot_setup *s = dev_boot_setup;
570 int i;
571
572 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
573 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700574 !strcmp(dev->name, s[i].name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dev->irq = s[i].map.irq;
576 dev->base_addr = s[i].map.base_addr;
577 dev->mem_start = s[i].map.mem_start;
578 dev->mem_end = s[i].map.mem_end;
579 return 1;
580 }
581 }
582 return 0;
583}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700584EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586
587/**
588 * netdev_boot_base - get address from boot time settings
589 * @prefix: prefix for network device
590 * @unit: id for network device
591 *
592 * Check boot time settings for the base address of device.
593 * The found settings are set for the device to be used
594 * later in the device probing.
595 * Returns 0 if no settings found.
596 */
597unsigned long netdev_boot_base(const char *prefix, int unit)
598{
599 const struct netdev_boot_setup *s = dev_boot_setup;
600 char name[IFNAMSIZ];
601 int i;
602
603 sprintf(name, "%s%d", prefix, unit);
604
605 /*
606 * If device already registered then return base of 1
607 * to indicate not to probe for this interface
608 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700609 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 1;
611
612 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
613 if (!strcmp(name, s[i].name))
614 return s[i].map.base_addr;
615 return 0;
616}
617
618/*
619 * Saves at boot time configured settings for any netdevice.
620 */
621int __init netdev_boot_setup(char *str)
622{
623 int ints[5];
624 struct ifmap map;
625
626 str = get_options(str, ARRAY_SIZE(ints), ints);
627 if (!str || !*str)
628 return 0;
629
630 /* Save settings */
631 memset(&map, 0, sizeof(map));
632 if (ints[0] > 0)
633 map.irq = ints[1];
634 if (ints[0] > 1)
635 map.base_addr = ints[2];
636 if (ints[0] > 2)
637 map.mem_start = ints[3];
638 if (ints[0] > 3)
639 map.mem_end = ints[4];
640
641 /* Add new entry to the list */
642 return netdev_boot_setup_add(str, &map);
643}
644
645__setup("netdev=", netdev_boot_setup);
646
647/*******************************************************************************
648
649 Device Interface Subroutines
650
651*******************************************************************************/
652
653/**
654 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700655 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * @name: name to find
657 *
658 * Find an interface by name. Must be called under RTNL semaphore
659 * or @dev_base_lock. If the name is found a pointer to the device
660 * is returned. If the name is not found then %NULL is returned. The
661 * reference counters are not incremented so the caller must be
662 * careful with locks.
663 */
664
Eric W. Biederman881d9662007-09-17 11:56:21 -0700665struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700667 struct net_device *dev;
668 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Sasha Levinb67bfe02013-02-27 17:06:00 -0800670 hlist_for_each_entry(dev, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!strncmp(dev->name, name, IFNAMSIZ))
672 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return NULL;
675}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700676EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/**
Eric Dumazet72c95282009-10-30 07:11:27 +0000679 * dev_get_by_name_rcu - find a device by its name
680 * @net: the applicable net namespace
681 * @name: name to find
682 *
683 * Find an interface by name.
684 * If the name is found a pointer to the device is returned.
685 * If the name is not found then %NULL is returned.
686 * The reference counters are not incremented so the caller must be
687 * careful with locks. The caller must hold RCU lock.
688 */
689
690struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
691{
Eric Dumazet72c95282009-10-30 07:11:27 +0000692 struct net_device *dev;
693 struct hlist_head *head = dev_name_hash(net, name);
694
Sasha Levinb67bfe02013-02-27 17:06:00 -0800695 hlist_for_each_entry_rcu(dev, head, name_hlist)
Eric Dumazet72c95282009-10-30 07:11:27 +0000696 if (!strncmp(dev->name, name, IFNAMSIZ))
697 return dev;
698
699 return NULL;
700}
701EXPORT_SYMBOL(dev_get_by_name_rcu);
702
703/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700705 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * @name: name to find
707 *
708 * Find an interface by name. This can be called from any
709 * context and does its own locking. The returned handle has
710 * the usage count incremented and the caller must use dev_put() to
711 * release it when it is no longer needed. %NULL is returned if no
712 * matching device is found.
713 */
714
Eric W. Biederman881d9662007-09-17 11:56:21 -0700715struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct net_device *dev;
718
Eric Dumazet72c95282009-10-30 07:11:27 +0000719 rcu_read_lock();
720 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (dev)
722 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000723 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return dev;
725}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700726EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728/**
729 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700730 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * @ifindex: index of device
732 *
733 * Search for an interface by index. Returns %NULL if the device
734 * is not found or a pointer to the device. The device has not
735 * had its reference counter increased so the caller must be careful
736 * about locking. The caller must hold either the RTNL semaphore
737 * or @dev_base_lock.
738 */
739
Eric W. Biederman881d9662007-09-17 11:56:21 -0700740struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700742 struct net_device *dev;
743 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Sasha Levinb67bfe02013-02-27 17:06:00 -0800745 hlist_for_each_entry(dev, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (dev->ifindex == ifindex)
747 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return NULL;
750}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700751EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000753/**
754 * dev_get_by_index_rcu - find a device by its ifindex
755 * @net: the applicable net namespace
756 * @ifindex: index of device
757 *
758 * Search for an interface by index. Returns %NULL if the device
759 * is not found or a pointer to the device. The device has not
760 * had its reference counter increased so the caller must be careful
761 * about locking. The caller must hold RCU lock.
762 */
763
764struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
765{
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000766 struct net_device *dev;
767 struct hlist_head *head = dev_index_hash(net, ifindex);
768
Sasha Levinb67bfe02013-02-27 17:06:00 -0800769 hlist_for_each_entry_rcu(dev, head, index_hlist)
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000770 if (dev->ifindex == ifindex)
771 return dev;
772
773 return NULL;
774}
775EXPORT_SYMBOL(dev_get_by_index_rcu);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778/**
779 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700780 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * @ifindex: index of device
782 *
783 * Search for an interface by index. Returns NULL if the device
784 * is not found or a pointer to the device. The device returned has
785 * had a reference added and the pointer is safe until the user calls
786 * dev_put to indicate they have finished with it.
787 */
788
Eric W. Biederman881d9662007-09-17 11:56:21 -0700789struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct net_device *dev;
792
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000793 rcu_read_lock();
794 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (dev)
796 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000797 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return dev;
799}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700800EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802/**
Eric Dumazet941666c2010-12-05 01:23:53 +0000803 * dev_getbyhwaddr_rcu - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700804 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * @type: media type of device
806 * @ha: hardware address
807 *
808 * Search for an interface by MAC address. Returns NULL if the device
Eric Dumazetc5066532011-01-24 13:16:16 -0800809 * is not found or a pointer to the device.
810 * The caller must hold RCU or RTNL.
Eric Dumazet941666c2010-12-05 01:23:53 +0000811 * The returned device has not had its ref count increased
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * and the caller must therefore be careful about locking
813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
815
Eric Dumazet941666c2010-12-05 01:23:53 +0000816struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
817 const char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct net_device *dev;
820
Eric Dumazet941666c2010-12-05 01:23:53 +0000821 for_each_netdev_rcu(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (dev->type == type &&
823 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700824 return dev;
825
826 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
Eric Dumazet941666c2010-12-05 01:23:53 +0000828EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300829
Eric W. Biederman881d9662007-09-17 11:56:21 -0700830struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700831{
832 struct net_device *dev;
833
834 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700835 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700836 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700837 return dev;
838
839 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700840}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700841EXPORT_SYMBOL(__dev_getfirstbyhwtype);
842
Eric W. Biederman881d9662007-09-17 11:56:21 -0700843struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000845 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000847 rcu_read_lock();
848 for_each_netdev_rcu(net, dev)
849 if (dev->type == type) {
850 dev_hold(dev);
851 ret = dev;
852 break;
853 }
854 rcu_read_unlock();
855 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857EXPORT_SYMBOL(dev_getfirstbyhwtype);
858
859/**
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000860 * dev_get_by_flags_rcu - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700861 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * @if_flags: IFF_* values
863 * @mask: bitmask of bits in if_flags to check
864 *
865 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000866 * is not found or a pointer to the device. Must be called inside
867 * rcu_read_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000870struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700871 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700873 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Pavel Emelianov7562f872007-05-03 15:13:45 -0700875 ret = NULL;
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800876 for_each_netdev_rcu(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700878 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
880 }
881 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000884EXPORT_SYMBOL(dev_get_by_flags_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886/**
887 * dev_valid_name - check if name is okay for network device
888 * @name: name string
889 *
890 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700891 * to allow sysfs to work. We also disallow any kind of
892 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
David S. Miller95f050b2012-03-06 16:12:15 -0500894bool dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700896 if (*name == '\0')
David S. Miller95f050b2012-03-06 16:12:15 -0500897 return false;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -0700898 if (strlen(name) >= IFNAMSIZ)
David S. Miller95f050b2012-03-06 16:12:15 -0500899 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700900 if (!strcmp(name, ".") || !strcmp(name, ".."))
David S. Miller95f050b2012-03-06 16:12:15 -0500901 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700902
903 while (*name) {
904 if (*name == '/' || isspace(*name))
David S. Miller95f050b2012-03-06 16:12:15 -0500905 return false;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700906 name++;
907 }
David S. Miller95f050b2012-03-06 16:12:15 -0500908 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700910EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200913 * __dev_alloc_name - allocate a name for a device
914 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200916 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
918 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700919 * id. It scans list of devices to build up a free map, then chooses
920 * the first empty slot. The caller must hold the dev_base or rtnl lock
921 * while allocating the name and adding the device in order to avoid
922 * duplicates.
923 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
924 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200927static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 const char *p;
931 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700932 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct net_device *d;
934
935 p = strnchr(name, IFNAMSIZ-1, '%');
936 if (p) {
937 /*
938 * Verify the string as this thing may have come from
939 * the user. There must be either one "%d" and no other "%"
940 * characters.
941 */
942 if (p[1] != 'd' || strchr(p + 2, '%'))
943 return -EINVAL;
944
945 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700946 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!inuse)
948 return -ENOMEM;
949
Eric W. Biederman881d9662007-09-17 11:56:21 -0700950 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!sscanf(d->name, name, &i))
952 continue;
953 if (i < 0 || i >= max_netdevices)
954 continue;
955
956 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200957 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!strncmp(buf, d->name, IFNAMSIZ))
959 set_bit(i, inuse);
960 }
961
962 i = find_first_zero_bit(inuse, max_netdevices);
963 free_page((unsigned long) inuse);
964 }
965
Octavian Purdilad9031022009-11-18 02:36:59 +0000966 if (buf != name)
967 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200968 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* It is possible to run out of possible slots
972 * when the name is long and there isn't enough space left
973 * for the digits, or if all bits are used.
974 */
975 return -ENFILE;
976}
977
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200978/**
979 * dev_alloc_name - allocate a name for a device
980 * @dev: device
981 * @name: name format string
982 *
983 * Passed a format string - eg "lt%d" it will try and find a suitable
984 * id. It scans list of devices to build up a free map, then chooses
985 * the first empty slot. The caller must hold the dev_base or rtnl lock
986 * while allocating the name and adding the device in order to avoid
987 * duplicates.
988 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
989 * Returns the number of the unit assigned or a negative errno code.
990 */
991
992int dev_alloc_name(struct net_device *dev, const char *name)
993{
994 char buf[IFNAMSIZ];
995 struct net *net;
996 int ret;
997
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900998 BUG_ON(!dev_net(dev));
999 net = dev_net(dev);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001000 ret = __dev_alloc_name(net, name, buf);
1001 if (ret >= 0)
1002 strlcpy(dev->name, buf, IFNAMSIZ);
1003 return ret;
1004}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001005EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +02001006
Gao feng828de4f2012-09-13 20:58:27 +00001007static int dev_alloc_name_ns(struct net *net,
1008 struct net_device *dev,
1009 const char *name)
Octavian Purdilad9031022009-11-18 02:36:59 +00001010{
Gao feng828de4f2012-09-13 20:58:27 +00001011 char buf[IFNAMSIZ];
1012 int ret;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001013
Gao feng828de4f2012-09-13 20:58:27 +00001014 ret = __dev_alloc_name(net, name, buf);
1015 if (ret >= 0)
1016 strlcpy(dev->name, buf, IFNAMSIZ);
1017 return ret;
1018}
1019
1020static int dev_get_valid_name(struct net *net,
1021 struct net_device *dev,
1022 const char *name)
1023{
1024 BUG_ON(!net);
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001025
Octavian Purdilad9031022009-11-18 02:36:59 +00001026 if (!dev_valid_name(name))
1027 return -EINVAL;
1028
Jiri Pirko1c5cae82011-04-30 01:21:32 +00001029 if (strchr(name, '%'))
Gao feng828de4f2012-09-13 20:58:27 +00001030 return dev_alloc_name_ns(net, dev, name);
Octavian Purdilad9031022009-11-18 02:36:59 +00001031 else if (__dev_get_by_name(net, name))
1032 return -EEXIST;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001033 else if (dev->name != name)
1034 strlcpy(dev->name, name, IFNAMSIZ);
Octavian Purdilad9031022009-11-18 02:36:59 +00001035
1036 return 0;
1037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039/**
1040 * dev_change_name - change name of a device
1041 * @dev: device
1042 * @newname: name (or format string) must be at least IFNAMSIZ
1043 *
1044 * Change name of a device, can pass format strings "eth%d".
1045 * for wildcarding.
1046 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07001047int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Herbert Xufcc5a032007-07-30 17:03:38 -07001049 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -07001051 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001052 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001055 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001057 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (dev->flags & IFF_UP)
1059 return -EBUSY;
1060
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001061 write_seqcount_begin(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001062
1063 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001064 write_seqcount_end(&devnet_rename_seq);
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001065 return 0;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001066 }
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -07001067
Herbert Xufcc5a032007-07-30 17:03:38 -07001068 memcpy(oldname, dev->name, IFNAMSIZ);
1069
Gao feng828de4f2012-09-13 20:58:27 +00001070 err = dev_get_valid_name(net, dev, newname);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001071 if (err < 0) {
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001072 write_seqcount_end(&devnet_rename_seq);
Octavian Purdilad9031022009-11-18 02:36:59 +00001073 return err;
Brian Haleyc91f6df2012-11-26 05:21:08 +00001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Herbert Xufcc5a032007-07-30 17:03:38 -07001076rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001077 ret = device_rename(&dev->dev, dev->name);
1078 if (ret) {
1079 memcpy(dev->name, oldname, IFNAMSIZ);
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001080 write_seqcount_end(&devnet_rename_seq);
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001081 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001082 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001083
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001084 write_seqcount_end(&devnet_rename_seq);
Brian Haleyc91f6df2012-11-26 05:21:08 +00001085
Herbert Xu7f988ea2007-07-30 16:35:46 -07001086 write_lock_bh(&dev_base_lock);
Eric Dumazet372b2312011-05-17 13:56:59 -04001087 hlist_del_rcu(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001088 write_unlock_bh(&dev_base_lock);
1089
1090 synchronize_rcu();
1091
1092 write_lock_bh(&dev_base_lock);
1093 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001094 write_unlock_bh(&dev_base_lock);
1095
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001096 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001097 ret = notifier_to_errno(ret);
1098
1099 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001100 /* err >= 0 after dev_alloc_name() or stores the first errno */
1101 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001102 err = ret;
Eric Dumazet30e6c9f2012-12-20 17:25:08 +00001103 write_seqcount_begin(&devnet_rename_seq);
Herbert Xufcc5a032007-07-30 17:03:38 -07001104 memcpy(dev->name, oldname, IFNAMSIZ);
1105 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001106 } else {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001107 pr_err("%s: name change rollback failed: %d\n",
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001108 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001109 }
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 return err;
1113}
1114
1115/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001116 * dev_set_alias - change ifalias of a device
1117 * @dev: device
1118 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001119 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001120 *
1121 * Set ifalias for a device,
1122 */
1123int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1124{
Alexey Khoroshilov7364e442012-08-08 00:33:25 +00001125 char *new_ifalias;
1126
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001127 ASSERT_RTNL();
1128
1129 if (len >= IFALIASZ)
1130 return -EINVAL;
1131
Oliver Hartkopp96ca4a22008-09-23 21:23:19 -07001132 if (!len) {
Sachin Kamat388dfc22012-11-20 00:57:04 +00001133 kfree(dev->ifalias);
1134 dev->ifalias = NULL;
Oliver Hartkopp96ca4a22008-09-23 21:23:19 -07001135 return 0;
1136 }
1137
Alexey Khoroshilov7364e442012-08-08 00:33:25 +00001138 new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1139 if (!new_ifalias)
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001140 return -ENOMEM;
Alexey Khoroshilov7364e442012-08-08 00:33:25 +00001141 dev->ifalias = new_ifalias;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001142
1143 strlcpy(dev->ifalias, alias, len+1);
1144 return len;
1145}
1146
1147
1148/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001149 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001150 * @dev: device to cause notification
1151 *
1152 * Called to indicate a device has changed features.
1153 */
1154void netdev_features_change(struct net_device *dev)
1155{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001156 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001157}
1158EXPORT_SYMBOL(netdev_features_change);
1159
1160/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * netdev_state_change - device changes state
1162 * @dev: device to cause notification
1163 *
1164 * Called to indicate a device has changed state. This function calls
1165 * the notifier chains for netdev_chain and sends a NEWLINK message
1166 * to the routing socket.
1167 */
1168void netdev_state_change(struct net_device *dev)
1169{
1170 if (dev->flags & IFF_UP) {
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001171 call_netdevice_notifiers(NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1173 }
1174}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001175EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Amerigo Wangee89bab2012-08-09 22:14:56 +00001177/**
1178 * netdev_notify_peers - notify network peers about existence of @dev
1179 * @dev: network device
1180 *
1181 * Generate traffic such that interested network peers are aware of
1182 * @dev, such as by generating a gratuitous ARP. This may be used when
1183 * a device wants to inform the rest of the network about some sort of
1184 * reconfiguration such as a failover event or virtual machine
1185 * migration.
1186 */
1187void netdev_notify_peers(struct net_device *dev)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001188{
Amerigo Wangee89bab2012-08-09 22:14:56 +00001189 rtnl_lock();
1190 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1191 rtnl_unlock();
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001192}
Amerigo Wangee89bab2012-08-09 22:14:56 +00001193EXPORT_SYMBOL(netdev_notify_peers);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001194
Patrick McHardybd380812010-02-26 06:34:53 +00001195static int __dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001197 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001198 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001200 ASSERT_RTNL();
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!netif_device_present(dev))
1203 return -ENODEV;
1204
Neil Hormanca99ca12013-02-05 08:05:43 +00001205 /* Block netpoll from trying to do any rx path servicing.
1206 * If we don't do this there is a chance ndo_poll_controller
1207 * or ndo_poll may be running while we open the device
1208 */
dingtianhongda6e3782013-05-27 19:53:31 +00001209 netpoll_rx_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001210
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001211 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1212 ret = notifier_to_errno(ret);
1213 if (ret)
1214 return ret;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001217
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001218 if (ops->ndo_validate_addr)
1219 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001220
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001221 if (!ret && ops->ndo_open)
1222 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Neil Hormanca99ca12013-02-05 08:05:43 +00001224 netpoll_rx_enable(dev);
1225
Jeff Garzikbada3392007-10-23 20:19:37 -07001226 if (ret)
1227 clear_bit(__LINK_STATE_START, &dev->state);
1228 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 dev->flags |= IFF_UP;
David S. Millerb4bd07c2009-02-06 22:06:43 -08001230 net_dmaengine_get();
Patrick McHardy4417da62007-06-27 01:28:10 -07001231 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 dev_activate(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04001233 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return ret;
1237}
Patrick McHardybd380812010-02-26 06:34:53 +00001238
1239/**
1240 * dev_open - prepare an interface for use.
1241 * @dev: device to open
1242 *
1243 * Takes a device from down to up state. The device's private open
1244 * function is invoked and then the multicast lists are loaded. Finally
1245 * the device is moved into the up state and a %NETDEV_UP message is
1246 * sent to the netdev notifier chain.
1247 *
1248 * Calling this function on an active interface is a nop. On a failure
1249 * a negative errno code is returned.
1250 */
1251int dev_open(struct net_device *dev)
1252{
1253 int ret;
1254
Patrick McHardybd380812010-02-26 06:34:53 +00001255 if (dev->flags & IFF_UP)
1256 return 0;
1257
Patrick McHardybd380812010-02-26 06:34:53 +00001258 ret = __dev_open(dev);
1259 if (ret < 0)
1260 return ret;
1261
Patrick McHardybd380812010-02-26 06:34:53 +00001262 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1263 call_netdevice_notifiers(NETDEV_UP, dev);
1264
1265 return ret;
1266}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001267EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Octavian Purdila44345722010-12-13 12:44:07 +00001269static int __dev_close_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Octavian Purdila44345722010-12-13 12:44:07 +00001271 struct net_device *dev;
Patrick McHardybd380812010-02-26 06:34:53 +00001272
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001273 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001274 might_sleep();
1275
Octavian Purdila44345722010-12-13 12:44:07 +00001276 list_for_each_entry(dev, head, unreg_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001277 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Octavian Purdila44345722010-12-13 12:44:07 +00001279 clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Octavian Purdila44345722010-12-13 12:44:07 +00001281 /* Synchronize to scheduled poll. We cannot touch poll list, it
1282 * can be even on different cpu. So just clear netif_running().
1283 *
1284 * dev->stop() will invoke napi_disable() on all of it's
1285 * napi_struct instances on this device.
1286 */
1287 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Octavian Purdila44345722010-12-13 12:44:07 +00001290 dev_deactivate_many(head);
1291
1292 list_for_each_entry(dev, head, unreg_list) {
1293 const struct net_device_ops *ops = dev->netdev_ops;
1294
1295 /*
1296 * Call the device specific close. This cannot fail.
1297 * Only if device is UP
1298 *
1299 * We allow it to be called even after a DETACH hot-plug
1300 * event.
1301 */
1302 if (ops->ndo_stop)
1303 ops->ndo_stop(dev);
1304
Octavian Purdila44345722010-12-13 12:44:07 +00001305 dev->flags &= ~IFF_UP;
Octavian Purdila44345722010-12-13 12:44:07 +00001306 net_dmaengine_put();
1307 }
1308
1309 return 0;
1310}
1311
1312static int __dev_close(struct net_device *dev)
1313{
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001314 int retval;
Octavian Purdila44345722010-12-13 12:44:07 +00001315 LIST_HEAD(single);
1316
Neil Hormanca99ca12013-02-05 08:05:43 +00001317 /* Temporarily disable netpoll until the interface is down */
dingtianhongda6e3782013-05-27 19:53:31 +00001318 netpoll_rx_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001319
Octavian Purdila44345722010-12-13 12:44:07 +00001320 list_add(&dev->unreg_list, &single);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001321 retval = __dev_close_many(&single);
1322 list_del(&single);
Neil Hormanca99ca12013-02-05 08:05:43 +00001323
1324 netpoll_rx_enable(dev);
Linus Torvaldsf87e6f42011-02-17 22:54:38 +00001325 return retval;
Octavian Purdila44345722010-12-13 12:44:07 +00001326}
1327
Eric Dumazet3fbd8752011-01-19 21:23:22 +00001328static int dev_close_many(struct list_head *head)
Octavian Purdila44345722010-12-13 12:44:07 +00001329{
1330 struct net_device *dev, *tmp;
1331 LIST_HEAD(tmp_list);
1332
1333 list_for_each_entry_safe(dev, tmp, head, unreg_list)
1334 if (!(dev->flags & IFF_UP))
1335 list_move(&dev->unreg_list, &tmp_list);
1336
1337 __dev_close_many(head);
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001338
Octavian Purdila44345722010-12-13 12:44:07 +00001339 list_for_each_entry(dev, head, unreg_list) {
1340 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1341 call_netdevice_notifiers(NETDEV_DOWN, dev);
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Octavian Purdila44345722010-12-13 12:44:07 +00001344 /* rollback_registered_many needs the complete original list */
1345 list_splice(&tmp_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347}
Patrick McHardybd380812010-02-26 06:34:53 +00001348
1349/**
1350 * dev_close - shutdown an interface.
1351 * @dev: device to shutdown
1352 *
1353 * This function moves an active device into down state. A
1354 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1355 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1356 * chain.
1357 */
1358int dev_close(struct net_device *dev)
1359{
Eric Dumazete14a5992011-05-10 12:26:06 -07001360 if (dev->flags & IFF_UP) {
1361 LIST_HEAD(single);
Patrick McHardybd380812010-02-26 06:34:53 +00001362
Neil Hormanca99ca12013-02-05 08:05:43 +00001363 /* Block netpoll rx while the interface is going down */
dingtianhongda6e3782013-05-27 19:53:31 +00001364 netpoll_rx_disable(dev);
Neil Hormanca99ca12013-02-05 08:05:43 +00001365
Eric Dumazete14a5992011-05-10 12:26:06 -07001366 list_add(&dev->unreg_list, &single);
1367 dev_close_many(&single);
1368 list_del(&single);
Neil Hormanca99ca12013-02-05 08:05:43 +00001369
1370 netpoll_rx_enable(dev);
Eric Dumazete14a5992011-05-10 12:26:06 -07001371 }
dingtianhongda6e3782013-05-27 19:53:31 +00001372 return 0;
Patrick McHardybd380812010-02-26 06:34:53 +00001373}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001374EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001377/**
1378 * dev_disable_lro - disable Large Receive Offload on a device
1379 * @dev: device
1380 *
1381 * Disable Large Receive Offload (LRO) on a net device. Must be
1382 * called under RTNL. This is needed if received packets may be
1383 * forwarded to another interface.
1384 */
1385void dev_disable_lro(struct net_device *dev)
1386{
Neil Hormanf11970e2011-05-24 08:31:09 +00001387 /*
1388 * If we're trying to disable lro on a vlan device
1389 * use the underlying physical device instead
1390 */
1391 if (is_vlan_dev(dev))
1392 dev = vlan_dev_real_dev(dev);
1393
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001394 dev->wanted_features &= ~NETIF_F_LRO;
1395 netdev_update_features(dev);
Michał Mirosław27660512011-03-18 16:56:34 +00001396
Michał Mirosław22d59692011-04-21 12:42:15 +00001397 if (unlikely(dev->features & NETIF_F_LRO))
1398 netdev_WARN(dev, "failed to disable LRO!\n");
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001399}
1400EXPORT_SYMBOL(dev_disable_lro);
1401
Jiri Pirko351638e2013-05-28 01:30:21 +00001402static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1403 struct net_device *dev)
1404{
1405 struct netdev_notifier_info info;
1406
1407 netdev_notifier_info_init(&info, dev);
1408 return nb->notifier_call(nb, val, &info);
1409}
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001410
Eric W. Biederman881d9662007-09-17 11:56:21 -07001411static int dev_boot_phase = 1;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/**
1414 * register_netdevice_notifier - register a network notifier block
1415 * @nb: notifier
1416 *
1417 * Register a notifier to be called when network device events occur.
1418 * The notifier passed is linked into the kernel structures and must
1419 * not be reused until it has been unregistered. A negative errno code
1420 * is returned on a failure.
1421 *
1422 * When registered all registration and up events are replayed
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001423 * to the new notifier to allow device to have a race free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * view of the network device list.
1425 */
1426
1427int register_netdevice_notifier(struct notifier_block *nb)
1428{
1429 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001430 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001431 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 int err;
1433
1434 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001435 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001436 if (err)
1437 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001438 if (dev_boot_phase)
1439 goto unlock;
1440 for_each_net(net) {
1441 for_each_netdev(net, dev) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001442 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001443 err = notifier_to_errno(err);
1444 if (err)
1445 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Eric W. Biederman881d9662007-09-17 11:56:21 -07001447 if (!(dev->flags & IFF_UP))
1448 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001449
Jiri Pirko351638e2013-05-28 01:30:21 +00001450 call_netdevice_notifier(nb, NETDEV_UP, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001453
1454unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rtnl_unlock();
1456 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001457
1458rollback:
1459 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001460 for_each_net(net) {
1461 for_each_netdev(net, dev) {
1462 if (dev == last)
RongQing.Li8f891482011-11-30 23:43:07 -05001463 goto outroll;
Herbert Xufcc5a032007-07-30 17:03:38 -07001464
Eric W. Biederman881d9662007-09-17 11:56:21 -07001465 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001466 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1467 dev);
1468 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001469 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001470 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001471 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001472 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001473
RongQing.Li8f891482011-11-30 23:43:07 -05001474outroll:
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001475 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001476 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001478EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480/**
1481 * unregister_netdevice_notifier - unregister a network notifier block
1482 * @nb: notifier
1483 *
1484 * Unregister a notifier previously registered by
1485 * register_netdevice_notifier(). The notifier is unlinked into the
1486 * kernel structures and may then be reused. A negative errno code
1487 * is returned on a failure.
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001488 *
1489 * After unregistering unregister and down device events are synthesized
1490 * for all devices on the device list to the removed notifier to remove
1491 * the need for special case cleanup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 */
1493
1494int unregister_netdevice_notifier(struct notifier_block *nb)
1495{
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001496 struct net_device *dev;
1497 struct net *net;
Herbert Xu9f514952006-03-25 01:24:25 -08001498 int err;
1499
1500 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001501 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001502 if (err)
1503 goto unlock;
1504
1505 for_each_net(net) {
1506 for_each_netdev(net, dev) {
1507 if (dev->flags & IFF_UP) {
Jiri Pirko351638e2013-05-28 01:30:21 +00001508 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1509 dev);
1510 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001511 }
Jiri Pirko351638e2013-05-28 01:30:21 +00001512 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
Eric W. Biederman7d3d43d2012-04-06 15:33:35 +00001513 }
1514 }
1515unlock:
Herbert Xu9f514952006-03-25 01:24:25 -08001516 rtnl_unlock();
1517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001519EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521/**
Jiri Pirko351638e2013-05-28 01:30:21 +00001522 * call_netdevice_notifiers_info - call all network notifier blocks
1523 * @val: value passed unmodified to notifier function
1524 * @dev: net_device pointer passed unmodified to notifier function
1525 * @info: notifier information data
1526 *
1527 * Call all network notifier blocks. Parameters and return value
1528 * are as for raw_notifier_call_chain().
1529 */
1530
1531int call_netdevice_notifiers_info(unsigned long val, struct net_device *dev,
1532 struct netdev_notifier_info *info)
1533{
1534 ASSERT_RTNL();
1535 netdev_notifier_info_init(info, dev);
1536 return raw_notifier_call_chain(&netdev_chain, val, info);
1537}
1538EXPORT_SYMBOL(call_netdevice_notifiers_info);
1539
1540/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * call_netdevice_notifiers - call all network notifier blocks
1542 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001543 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *
1545 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001546 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
1548
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001549int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Jiri Pirko351638e2013-05-28 01:30:21 +00001551 struct netdev_notifier_info info;
1552
1553 return call_netdevice_notifiers_info(val, dev, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
stephen hemmingeredf947f2011-03-24 13:24:01 +00001555EXPORT_SYMBOL(call_netdevice_notifiers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Ingo Molnarc5905af2012-02-24 08:31:31 +01001557static struct static_key netstamp_needed __read_mostly;
Eric Dumazetb90e5792011-11-28 11:16:50 +00001558#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01001559/* We are not allowed to call static_key_slow_dec() from irq context
Eric Dumazetb90e5792011-11-28 11:16:50 +00001560 * If net_disable_timestamp() is called from irq context, defer the
Ingo Molnarc5905af2012-02-24 08:31:31 +01001561 * static_key_slow_dec() calls.
Eric Dumazetb90e5792011-11-28 11:16:50 +00001562 */
1563static atomic_t netstamp_needed_deferred;
1564#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566void net_enable_timestamp(void)
1567{
Eric Dumazetb90e5792011-11-28 11:16:50 +00001568#ifdef HAVE_JUMP_LABEL
1569 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
1570
1571 if (deferred) {
1572 while (--deferred)
Ingo Molnarc5905af2012-02-24 08:31:31 +01001573 static_key_slow_dec(&netstamp_needed);
Eric Dumazetb90e5792011-11-28 11:16:50 +00001574 return;
1575 }
1576#endif
Ingo Molnarc5905af2012-02-24 08:31:31 +01001577 static_key_slow_inc(&netstamp_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001579EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581void net_disable_timestamp(void)
1582{
Eric Dumazetb90e5792011-11-28 11:16:50 +00001583#ifdef HAVE_JUMP_LABEL
1584 if (in_interrupt()) {
1585 atomic_inc(&netstamp_needed_deferred);
1586 return;
1587 }
1588#endif
Ingo Molnarc5905af2012-02-24 08:31:31 +01001589 static_key_slow_dec(&netstamp_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001591EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Eric Dumazet3b098e22010-05-15 23:57:10 -07001593static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Eric Dumazet588f0332011-11-15 04:12:55 +00001595 skb->tstamp.tv64 = 0;
Ingo Molnarc5905af2012-02-24 08:31:31 +01001596 if (static_key_false(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001597 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Eric Dumazet588f0332011-11-15 04:12:55 +00001600#define net_timestamp_check(COND, SKB) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001601 if (static_key_false(&netstamp_needed)) { \
Eric Dumazet588f0332011-11-15 04:12:55 +00001602 if ((COND) && !(SKB)->tstamp.tv64) \
1603 __net_timestamp(SKB); \
1604 } \
Eric Dumazet3b098e22010-05-15 23:57:10 -07001605
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001606static inline bool is_skb_forwardable(struct net_device *dev,
1607 struct sk_buff *skb)
1608{
1609 unsigned int len;
1610
1611 if (!(dev->flags & IFF_UP))
1612 return false;
1613
1614 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1615 if (skb->len <= len)
1616 return true;
1617
1618 /* if TSO is enabled, we don't care about the length as the packet
1619 * could be forwarded without being segmented before
1620 */
1621 if (skb_is_gso(skb))
1622 return true;
1623
1624 return false;
1625}
1626
Arnd Bergmann44540962009-11-26 06:07:08 +00001627/**
1628 * dev_forward_skb - loopback an skb to another netif
1629 *
1630 * @dev: destination network device
1631 * @skb: buffer to forward
1632 *
1633 * return values:
1634 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001635 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001636 *
1637 * dev_forward_skb can be used for injecting an skb from the
1638 * start_xmit function of one device into the receive queue
1639 * of another device.
1640 *
1641 * The receiving device may be in another namespace, so
1642 * we have to clear all information in the skb that could
1643 * impact namespace isolation.
1644 */
1645int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1646{
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001647 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
1648 if (skb_copy_ubufs(skb, GFP_ATOMIC)) {
1649 atomic_long_inc(&dev->rx_dropped);
1650 kfree_skb(skb);
1651 return NET_RX_DROP;
1652 }
1653 }
1654
Daniel Lezcano79b569f2011-03-30 02:42:17 -07001655 if (unlikely(!is_skb_forwardable(dev, skb))) {
Eric Dumazetcaf586e2010-09-30 21:06:55 +00001656 atomic_long_inc(&dev->rx_dropped);
Eric Dumazet6ec82562010-05-06 00:53:53 -07001657 kfree_skb(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001658 return NET_RX_DROP;
Eric Dumazet6ec82562010-05-06 00:53:53 -07001659 }
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02001660 skb_scrub_packet(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001661 skb->protocol = eth_type_trans(skb, dev);
Isaku Yamahata06a23fe2013-07-02 20:30:10 +09001662
1663 /* eth_type_trans() can set pkt_type.
1664 * clear pkt_type _after_ calling eth_type_trans()
1665 */
1666 skb->pkt_type = PACKET_HOST;
1667
Arnd Bergmann44540962009-11-26 06:07:08 +00001668 return netif_rx(skb);
1669}
1670EXPORT_SYMBOL_GPL(dev_forward_skb);
1671
Changli Gao71d9dec2010-12-15 19:57:25 +00001672static inline int deliver_skb(struct sk_buff *skb,
1673 struct packet_type *pt_prev,
1674 struct net_device *orig_dev)
1675{
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00001676 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
1677 return -ENOMEM;
Changli Gao71d9dec2010-12-15 19:57:25 +00001678 atomic_inc(&skb->users);
1679 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1680}
1681
Eric Leblondc0de08d2012-08-16 22:02:58 +00001682static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1683{
Eric Leblonda3d744e2012-11-06 02:10:10 +00001684 if (!ptype->af_packet_priv || !skb->sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001685 return false;
1686
1687 if (ptype->id_match)
1688 return ptype->id_match(ptype, skb->sk);
1689 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1690 return true;
1691
1692 return false;
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695/*
1696 * Support routine. Sends outgoing frames to any network
1697 * taps currently in use.
1698 */
1699
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001700static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 struct packet_type *ptype;
Changli Gao71d9dec2010-12-15 19:57:25 +00001703 struct sk_buff *skb2 = NULL;
1704 struct packet_type *pt_prev = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 rcu_read_lock();
1707 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1708 /* Never send packets back to the socket
1709 * they originated from - MvS (miquels@drinkel.ow.org)
1710 */
1711 if ((ptype->dev == dev || !ptype->dev) &&
Eric Leblondc0de08d2012-08-16 22:02:58 +00001712 (!skb_loop_sk(ptype, skb))) {
Changli Gao71d9dec2010-12-15 19:57:25 +00001713 if (pt_prev) {
1714 deliver_skb(skb2, pt_prev, skb->dev);
1715 pt_prev = ptype;
1716 continue;
1717 }
1718
1719 skb2 = skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (!skb2)
1721 break;
1722
Eric Dumazet70978182010-12-20 21:22:51 +00001723 net_timestamp_set(skb2);
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 /* skb->nh should be correctly
1726 set by sender, so that the second statement is
1727 just protection against buggy protocols.
1728 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001729 skb_reset_mac_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001731 if (skb_network_header(skb2) < skb2->data ||
Simon Hormanced14f62013-05-28 20:34:25 +00001732 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00001733 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1734 ntohs(skb2->protocol),
1735 dev->name);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001736 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001739 skb2->transport_header = skb2->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 skb2->pkt_type = PACKET_OUTGOING;
Changli Gao71d9dec2010-12-15 19:57:25 +00001741 pt_prev = ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
Changli Gao71d9dec2010-12-15 19:57:25 +00001744 if (pt_prev)
1745 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 rcu_read_unlock();
1747}
1748
Ben Hutchings2c530402012-07-10 10:55:09 +00001749/**
1750 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
John Fastabend4f57c082011-01-17 08:06:04 +00001751 * @dev: Network device
1752 * @txq: number of queues available
1753 *
1754 * If real_num_tx_queues is changed the tc mappings may no longer be
1755 * valid. To resolve this verify the tc mapping remains valid and if
1756 * not NULL the mapping. With no priorities mapping to this
1757 * offset/count pair it will no longer be used. In the worst case TC0
1758 * is invalid nothing can be done so disable priority mappings. If is
1759 * expected that drivers will fix this mapping if they can before
1760 * calling netif_set_real_num_tx_queues.
1761 */
Eric Dumazetbb134d22011-01-20 19:18:08 +00001762static void netif_setup_tc(struct net_device *dev, unsigned int txq)
John Fastabend4f57c082011-01-17 08:06:04 +00001763{
1764 int i;
1765 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1766
1767 /* If TC0 is invalidated disable TC mapping */
1768 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001769 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
John Fastabend4f57c082011-01-17 08:06:04 +00001770 dev->num_tc = 0;
1771 return;
1772 }
1773
1774 /* Invalidated prio to tc mappings set to TC0 */
1775 for (i = 1; i < TC_BITMASK + 1; i++) {
1776 int q = netdev_get_prio_tc_map(dev, i);
1777
1778 tc = &dev->tc_to_txq[q];
1779 if (tc->offset + tc->count > txq) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00001780 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
1781 i, q);
John Fastabend4f57c082011-01-17 08:06:04 +00001782 netdev_set_prio_tc_map(dev, i, 0);
1783 }
1784 }
1785}
1786
Alexander Duyck537c00d2013-01-10 08:57:02 +00001787#ifdef CONFIG_XPS
1788static DEFINE_MUTEX(xps_map_mutex);
1789#define xmap_dereference(P) \
1790 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1791
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00001792static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps,
1793 int cpu, u16 index)
1794{
1795 struct xps_map *map = NULL;
1796 int pos;
1797
1798 if (dev_maps)
1799 map = xmap_dereference(dev_maps->cpu_map[cpu]);
1800
1801 for (pos = 0; map && pos < map->len; pos++) {
1802 if (map->queues[pos] == index) {
1803 if (map->len > 1) {
1804 map->queues[pos] = map->queues[--map->len];
1805 } else {
1806 RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL);
1807 kfree_rcu(map, rcu);
1808 map = NULL;
1809 }
1810 break;
1811 }
1812 }
1813
1814 return map;
1815}
1816
Alexander Duyck024e9672013-01-10 08:57:46 +00001817static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
Alexander Duyck537c00d2013-01-10 08:57:02 +00001818{
1819 struct xps_dev_maps *dev_maps;
Alexander Duyck024e9672013-01-10 08:57:46 +00001820 int cpu, i;
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00001821 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001822
1823 mutex_lock(&xps_map_mutex);
1824 dev_maps = xmap_dereference(dev->xps_maps);
1825
1826 if (!dev_maps)
1827 goto out_no_maps;
1828
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00001829 for_each_possible_cpu(cpu) {
Alexander Duyck024e9672013-01-10 08:57:46 +00001830 for (i = index; i < dev->num_tx_queues; i++) {
1831 if (!remove_xps_queue(dev_maps, cpu, i))
1832 break;
1833 }
1834 if (i == dev->num_tx_queues)
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00001835 active = true;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001836 }
1837
Alexander Duyck10cdc3f2013-01-10 08:57:17 +00001838 if (!active) {
Alexander Duyck537c00d2013-01-10 08:57:02 +00001839 RCU_INIT_POINTER(dev->xps_maps, NULL);
1840 kfree_rcu(dev_maps, rcu);
1841 }
1842
Alexander Duyck024e9672013-01-10 08:57:46 +00001843 for (i = index; i < dev->num_tx_queues; i++)
1844 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
1845 NUMA_NO_NODE);
1846
Alexander Duyck537c00d2013-01-10 08:57:02 +00001847out_no_maps:
1848 mutex_unlock(&xps_map_mutex);
1849}
1850
Alexander Duyck01c5f862013-01-10 08:57:35 +00001851static struct xps_map *expand_xps_map(struct xps_map *map,
1852 int cpu, u16 index)
1853{
1854 struct xps_map *new_map;
1855 int alloc_len = XPS_MIN_MAP_ALLOC;
1856 int i, pos;
1857
1858 for (pos = 0; map && pos < map->len; pos++) {
1859 if (map->queues[pos] != index)
1860 continue;
1861 return map;
1862 }
1863
1864 /* Need to add queue to this CPU's existing map */
1865 if (map) {
1866 if (pos < map->alloc_len)
1867 return map;
1868
1869 alloc_len = map->alloc_len * 2;
1870 }
1871
1872 /* Need to allocate new map to store queue on this CPU's map */
1873 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
1874 cpu_to_node(cpu));
1875 if (!new_map)
1876 return NULL;
1877
1878 for (i = 0; i < pos; i++)
1879 new_map->queues[i] = map->queues[i];
1880 new_map->alloc_len = alloc_len;
1881 new_map->len = pos;
1882
1883 return new_map;
1884}
1885
Alexander Duyck537c00d2013-01-10 08:57:02 +00001886int netif_set_xps_queue(struct net_device *dev, struct cpumask *mask, u16 index)
1887{
Alexander Duyck01c5f862013-01-10 08:57:35 +00001888 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001889 struct xps_map *map, *new_map;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001890 int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES);
Alexander Duyck01c5f862013-01-10 08:57:35 +00001891 int cpu, numa_node_id = -2;
1892 bool active = false;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001893
1894 mutex_lock(&xps_map_mutex);
1895
1896 dev_maps = xmap_dereference(dev->xps_maps);
1897
Alexander Duyck01c5f862013-01-10 08:57:35 +00001898 /* allocate memory for queue storage */
1899 for_each_online_cpu(cpu) {
1900 if (!cpumask_test_cpu(cpu, mask))
1901 continue;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001902
Alexander Duyck01c5f862013-01-10 08:57:35 +00001903 if (!new_dev_maps)
1904 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00001905 if (!new_dev_maps) {
1906 mutex_unlock(&xps_map_mutex);
Alexander Duyck01c5f862013-01-10 08:57:35 +00001907 return -ENOMEM;
Alexander Duyck2bb60cb2013-02-22 06:38:44 +00001908 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00001909
1910 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
1911 NULL;
1912
1913 map = expand_xps_map(map, cpu, index);
1914 if (!map)
1915 goto error;
1916
1917 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
1918 }
1919
1920 if (!new_dev_maps)
1921 goto out_no_new_maps;
1922
1923 for_each_possible_cpu(cpu) {
1924 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
1925 /* add queue to CPU maps */
1926 int pos = 0;
1927
1928 map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
1929 while ((pos < map->len) && (map->queues[pos] != index))
1930 pos++;
1931
1932 if (pos == map->len)
1933 map->queues[map->len++] = index;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001934#ifdef CONFIG_NUMA
Alexander Duyck537c00d2013-01-10 08:57:02 +00001935 if (numa_node_id == -2)
1936 numa_node_id = cpu_to_node(cpu);
1937 else if (numa_node_id != cpu_to_node(cpu))
1938 numa_node_id = -1;
Alexander Duyck537c00d2013-01-10 08:57:02 +00001939#endif
Alexander Duyck01c5f862013-01-10 08:57:35 +00001940 } else if (dev_maps) {
1941 /* fill in the new device map from the old device map */
1942 map = xmap_dereference(dev_maps->cpu_map[cpu]);
1943 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
Alexander Duyck537c00d2013-01-10 08:57:02 +00001944 }
Alexander Duyck01c5f862013-01-10 08:57:35 +00001945
Alexander Duyck537c00d2013-01-10 08:57:02 +00001946 }
1947
Alexander Duyck01c5f862013-01-10 08:57:35 +00001948 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
1949
Alexander Duyck537c00d2013-01-10 08:57:02 +00001950 /* Cleanup old maps */
Alexander Duyck01c5f862013-01-10 08:57:35 +00001951 if (dev_maps) {
1952 for_each_possible_cpu(cpu) {
1953 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
1954 map = xmap_dereference(dev_maps->cpu_map[cpu]);
1955 if (map && map != new_map)
1956 kfree_rcu(map, rcu);
1957 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00001958
Alexander Duyck537c00d2013-01-10 08:57:02 +00001959 kfree_rcu(dev_maps, rcu);
Alexander Duyck01c5f862013-01-10 08:57:35 +00001960 }
Alexander Duyck537c00d2013-01-10 08:57:02 +00001961
Alexander Duyck01c5f862013-01-10 08:57:35 +00001962 dev_maps = new_dev_maps;
1963 active = true;
1964
1965out_no_new_maps:
1966 /* update Tx queue numa node */
Alexander Duyck537c00d2013-01-10 08:57:02 +00001967 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
1968 (numa_node_id >= 0) ? numa_node_id :
1969 NUMA_NO_NODE);
1970
Alexander Duyck01c5f862013-01-10 08:57:35 +00001971 if (!dev_maps)
1972 goto out_no_maps;
1973
1974 /* removes queue from unused CPUs */
1975 for_each_possible_cpu(cpu) {
1976 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu))
1977 continue;
1978
1979 if (remove_xps_queue(dev_maps, cpu, index))
1980 active = true;
1981 }
1982
1983 /* free map if not active */
1984 if (!active) {
1985 RCU_INIT_POINTER(dev->xps_maps, NULL);
1986 kfree_rcu(dev_maps, rcu);
1987 }
1988
1989out_no_maps:
Alexander Duyck537c00d2013-01-10 08:57:02 +00001990 mutex_unlock(&xps_map_mutex);
1991
1992 return 0;
1993error:
Alexander Duyck01c5f862013-01-10 08:57:35 +00001994 /* remove any maps that we added */
1995 for_each_possible_cpu(cpu) {
1996 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
1997 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
1998 NULL;
1999 if (new_map && new_map != map)
2000 kfree(new_map);
2001 }
2002
Alexander Duyck537c00d2013-01-10 08:57:02 +00002003 mutex_unlock(&xps_map_mutex);
2004
Alexander Duyck537c00d2013-01-10 08:57:02 +00002005 kfree(new_dev_maps);
2006 return -ENOMEM;
2007}
2008EXPORT_SYMBOL(netif_set_xps_queue);
2009
2010#endif
John Fastabendf0796d52010-07-01 13:21:57 +00002011/*
2012 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2013 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2014 */
Tom Herberte6484932010-10-18 18:04:39 +00002015int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
John Fastabendf0796d52010-07-01 13:21:57 +00002016{
Tom Herbert1d24eb42010-11-21 13:17:27 +00002017 int rc;
2018
Tom Herberte6484932010-10-18 18:04:39 +00002019 if (txq < 1 || txq > dev->num_tx_queues)
2020 return -EINVAL;
John Fastabendf0796d52010-07-01 13:21:57 +00002021
Ben Hutchings5c565802011-02-15 19:39:21 +00002022 if (dev->reg_state == NETREG_REGISTERED ||
2023 dev->reg_state == NETREG_UNREGISTERING) {
Tom Herberte6484932010-10-18 18:04:39 +00002024 ASSERT_RTNL();
2025
Tom Herbert1d24eb42010-11-21 13:17:27 +00002026 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2027 txq);
Tom Herbertbf264142010-11-26 08:36:09 +00002028 if (rc)
2029 return rc;
2030
John Fastabend4f57c082011-01-17 08:06:04 +00002031 if (dev->num_tc)
2032 netif_setup_tc(dev, txq);
2033
Alexander Duyck024e9672013-01-10 08:57:46 +00002034 if (txq < dev->real_num_tx_queues) {
Tom Herberte6484932010-10-18 18:04:39 +00002035 qdisc_reset_all_tx_gt(dev, txq);
Alexander Duyck024e9672013-01-10 08:57:46 +00002036#ifdef CONFIG_XPS
2037 netif_reset_xps_queues_gt(dev, txq);
2038#endif
2039 }
John Fastabendf0796d52010-07-01 13:21:57 +00002040 }
Tom Herberte6484932010-10-18 18:04:39 +00002041
2042 dev->real_num_tx_queues = txq;
2043 return 0;
John Fastabendf0796d52010-07-01 13:21:57 +00002044}
2045EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08002046
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002047#ifdef CONFIG_RPS
2048/**
2049 * netif_set_real_num_rx_queues - set actual number of RX queues used
2050 * @dev: Network device
2051 * @rxq: Actual number of RX queues
2052 *
2053 * This must be called either with the rtnl_lock held or before
2054 * registration of the net device. Returns 0 on success, or a
Ben Hutchings4e7f7952010-10-08 10:33:39 -07002055 * negative error code. If called before registration, it always
2056 * succeeds.
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002057 */
2058int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2059{
2060 int rc;
2061
Tom Herbertbd25fa72010-10-18 18:00:16 +00002062 if (rxq < 1 || rxq > dev->num_rx_queues)
2063 return -EINVAL;
2064
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002065 if (dev->reg_state == NETREG_REGISTERED) {
2066 ASSERT_RTNL();
2067
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002068 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2069 rxq);
2070 if (rc)
2071 return rc;
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002072 }
2073
2074 dev->real_num_rx_queues = rxq;
2075 return 0;
2076}
2077EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2078#endif
2079
Ben Hutchings2c530402012-07-10 10:55:09 +00002080/**
2081 * netif_get_num_default_rss_queues - default number of RSS queues
Yuval Mintz16917b82012-07-01 03:18:50 +00002082 *
2083 * This routine should set an upper limit on the number of RSS queues
2084 * used by default by multiqueue devices.
2085 */
Ben Hutchingsa55b1382012-07-10 10:54:38 +00002086int netif_get_num_default_rss_queues(void)
Yuval Mintz16917b82012-07-01 03:18:50 +00002087{
2088 return min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
2089}
2090EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2091
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002092static inline void __netif_reschedule(struct Qdisc *q)
2093{
2094 struct softnet_data *sd;
2095 unsigned long flags;
2096
2097 local_irq_save(flags);
2098 sd = &__get_cpu_var(softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00002099 q->next_sched = NULL;
2100 *sd->output_queue_tailp = q;
2101 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002102 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2103 local_irq_restore(flags);
2104}
2105
David S. Miller37437bb2008-07-16 02:15:04 -07002106void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08002107{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002108 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2109 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08002110}
2111EXPORT_SYMBOL(__netif_schedule);
2112
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002113void dev_kfree_skb_irq(struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08002114{
David S. Miller3578b0c2010-08-03 00:24:04 -07002115 if (atomic_dec_and_test(&skb->users)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002116 struct softnet_data *sd;
2117 unsigned long flags;
Denis Vlasenko56079432006-03-29 15:57:29 -08002118
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002119 local_irq_save(flags);
2120 sd = &__get_cpu_var(softnet_data);
2121 skb->next = sd->completion_queue;
2122 sd->completion_queue = skb;
2123 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2124 local_irq_restore(flags);
2125 }
Denis Vlasenko56079432006-03-29 15:57:29 -08002126}
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002127EXPORT_SYMBOL(dev_kfree_skb_irq);
Denis Vlasenko56079432006-03-29 15:57:29 -08002128
2129void dev_kfree_skb_any(struct sk_buff *skb)
2130{
2131 if (in_irq() || irqs_disabled())
2132 dev_kfree_skb_irq(skb);
2133 else
2134 dev_kfree_skb(skb);
2135}
2136EXPORT_SYMBOL(dev_kfree_skb_any);
2137
2138
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002139/**
2140 * netif_device_detach - mark device as removed
2141 * @dev: network device
2142 *
2143 * Mark device as removed from system and therefore no longer available.
2144 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002145void netif_device_detach(struct net_device *dev)
2146{
2147 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2148 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002149 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002150 }
2151}
2152EXPORT_SYMBOL(netif_device_detach);
2153
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002154/**
2155 * netif_device_attach - mark device as attached
2156 * @dev: network device
2157 *
2158 * Mark device as attached from system and restart if needed.
2159 */
Denis Vlasenko56079432006-03-29 15:57:29 -08002160void netif_device_attach(struct net_device *dev)
2161{
2162 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2163 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00002164 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002165 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08002166 }
2167}
2168EXPORT_SYMBOL(netif_device_attach);
2169
Ben Hutchings36c92472012-01-17 07:57:56 +00002170static void skb_warn_bad_offload(const struct sk_buff *skb)
2171{
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002172 static const netdev_features_t null_features = 0;
Ben Hutchings36c92472012-01-17 07:57:56 +00002173 struct net_device *dev = skb->dev;
2174 const char *driver = "";
2175
Ben Greearc846ad92013-04-19 10:45:52 +00002176 if (!net_ratelimit())
2177 return;
2178
Ben Hutchings36c92472012-01-17 07:57:56 +00002179 if (dev && dev->dev.parent)
2180 driver = dev_driver_string(dev->dev.parent);
2181
2182 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2183 "gso_type=%d ip_summed=%d\n",
Michał Mirosław65e9d2f2012-01-17 10:00:40 +00002184 driver, dev ? &dev->features : &null_features,
2185 skb->sk ? &skb->sk->sk_route_caps : &null_features,
Ben Hutchings36c92472012-01-17 07:57:56 +00002186 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2187 skb_shinfo(skb)->gso_type, skb->ip_summed);
2188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190/*
2191 * Invalidate hardware checksum when packet is to be mangled, and
2192 * complete checksum manually on outgoing path.
2193 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07002194int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Al Virod3bc23e2006-11-14 21:24:49 -08002196 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07002197 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Patrick McHardy84fa7932006-08-29 16:44:56 -07002199 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07002200 goto out_set_summed;
2201
2202 if (unlikely(skb_shinfo(skb)->gso_size)) {
Ben Hutchings36c92472012-01-17 07:57:56 +00002203 skb_warn_bad_offload(skb);
2204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206
Eric Dumazetcef401d2013-01-25 20:34:37 +00002207 /* Before computing a checksum, we should make sure no frag could
2208 * be modified by an external entity : checksum could be wrong.
2209 */
2210 if (skb_has_shared_frag(skb)) {
2211 ret = __skb_linearize(skb);
2212 if (ret)
2213 goto out;
2214 }
2215
Michał Mirosław55508d62010-12-14 15:24:08 +00002216 offset = skb_checksum_start_offset(skb);
Herbert Xua0308472007-10-15 01:47:15 -07002217 BUG_ON(offset >= skb_headlen(skb));
2218 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2219
2220 offset += skb->csum_offset;
2221 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2222
2223 if (skb_cloned(skb) &&
2224 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2226 if (ret)
2227 goto out;
2228 }
2229
Herbert Xua0308472007-10-15 01:47:15 -07002230 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07002231out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002233out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return ret;
2235}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002236EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002238__be16 skb_network_protocol(struct sk_buff *skb)
2239{
2240 __be16 type = skb->protocol;
David S. Miller61816592013-03-20 12:46:26 -04002241 int vlan_depth = ETH_HLEN;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002242
Pravin B Shelar19acc322013-05-07 20:41:07 +00002243 /* Tunnel gso handlers can set protocol to ethernet. */
2244 if (type == htons(ETH_P_TEB)) {
2245 struct ethhdr *eth;
2246
2247 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2248 return 0;
2249
2250 eth = (struct ethhdr *)skb_mac_header(skb);
2251 type = eth->h_proto;
2252 }
2253
Patrick McHardy8ad227f2013-04-19 02:04:31 +00002254 while (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002255 struct vlan_hdr *vh;
2256
2257 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
2258 return 0;
2259
2260 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
2261 type = vh->h_vlan_encapsulated_proto;
2262 vlan_depth += VLAN_HLEN;
2263 }
2264
2265 return type;
2266}
2267
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002268/**
2269 * skb_mac_gso_segment - mac layer segmentation handler.
2270 * @skb: buffer to segment
2271 * @features: features for the output path (see dev->features)
2272 */
2273struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2274 netdev_features_t features)
2275{
2276 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2277 struct packet_offload *ptype;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002278 __be16 type = skb_network_protocol(skb);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002279
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002280 if (unlikely(!type))
2281 return ERR_PTR(-EINVAL);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002282
2283 __skb_pull(skb, skb->mac_len);
2284
2285 rcu_read_lock();
2286 list_for_each_entry_rcu(ptype, &offload_base, list) {
2287 if (ptype->type == type && ptype->callbacks.gso_segment) {
2288 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
2289 int err;
2290
2291 err = ptype->callbacks.gso_send_check(skb);
2292 segs = ERR_PTR(err);
2293 if (err || skb_gso_ok(skb, features))
2294 break;
2295 __skb_push(skb, (skb->data -
2296 skb_network_header(skb)));
2297 }
2298 segs = ptype->callbacks.gso_segment(skb, features);
2299 break;
2300 }
2301 }
2302 rcu_read_unlock();
2303
2304 __skb_push(skb, skb->data - skb_mac_header(skb));
2305
2306 return segs;
2307}
2308EXPORT_SYMBOL(skb_mac_gso_segment);
2309
2310
Cong Wang12b00042013-02-05 16:36:38 +00002311/* openvswitch calls this on rx path, so we need a different check.
2312 */
2313static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
2314{
2315 if (tx_path)
2316 return skb->ip_summed != CHECKSUM_PARTIAL;
2317 else
2318 return skb->ip_summed == CHECKSUM_NONE;
2319}
2320
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002321/**
Cong Wang12b00042013-02-05 16:36:38 +00002322 * __skb_gso_segment - Perform segmentation on skb.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002323 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002324 * @features: features for the output path (see dev->features)
Cong Wang12b00042013-02-05 16:36:38 +00002325 * @tx_path: whether it is called in TX path
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002326 *
2327 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07002328 *
2329 * It may return NULL if the skb requires no segmentation. This is
2330 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002331 */
Cong Wang12b00042013-02-05 16:36:38 +00002332struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2333 netdev_features_t features, bool tx_path)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002334{
Cong Wang12b00042013-02-05 16:36:38 +00002335 if (unlikely(skb_needs_check(skb, tx_path))) {
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002336 int err;
2337
Ben Hutchings36c92472012-01-17 07:57:56 +00002338 skb_warn_bad_offload(skb);
Herbert Xu67fd1a72009-01-19 16:26:44 -08002339
Herbert Xua430a432006-07-08 13:34:56 -07002340 if (skb_header_cloned(skb) &&
2341 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
2342 return ERR_PTR(err);
2343 }
2344
Pravin B Shelar68c33162013-02-14 14:02:41 +00002345 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002346 skb_reset_mac_header(skb);
2347 skb_reset_mac_len(skb);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002348
Pravin B Shelar05e8ef42013-02-14 09:44:55 +00002349 return skb_mac_gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002350}
Cong Wang12b00042013-02-05 16:36:38 +00002351EXPORT_SYMBOL(__skb_gso_segment);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002352
Herbert Xufb286bb2005-11-10 13:01:24 -08002353/* Take action when hardware reception checksum errors are detected. */
2354#ifdef CONFIG_BUG
2355void netdev_rx_csum_fault(struct net_device *dev)
2356{
2357 if (net_ratelimit()) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00002358 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08002359 dump_stack();
2360 }
2361}
2362EXPORT_SYMBOL(netdev_rx_csum_fault);
2363#endif
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365/* Actually, we should eliminate this check as soon as we know, that:
2366 * 1. IOMMU is present and allows to map all the memory.
2367 * 2. No high memory really exists on this machine.
2368 */
2369
Eric Dumazet9092c652010-04-02 13:34:49 -07002370static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Herbert Xu3d3a8532006-06-27 13:33:10 -07002372#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 int i;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002374 if (!(dev->features & NETIF_F_HIGHDMA)) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002375 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2376 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2377 if (PageHighMem(skb_frag_page(frag)))
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002378 return 1;
Ian Campbellea2ab692011-08-22 23:44:58 +00002379 }
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002382 if (PCI_DMA_BUS_IS_PHYS) {
2383 struct device *pdev = dev->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Eric Dumazet9092c652010-04-02 13:34:49 -07002385 if (!pdev)
2386 return 0;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002387 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002388 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2389 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00002390 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2391 return 1;
2392 }
2393 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07002394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return 0;
2396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002398struct dev_gso_cb {
2399 void (*destructor)(struct sk_buff *skb);
2400};
2401
2402#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
2403
2404static void dev_gso_skb_destructor(struct sk_buff *skb)
2405{
2406 struct dev_gso_cb *cb;
2407
2408 do {
2409 struct sk_buff *nskb = skb->next;
2410
2411 skb->next = nskb->next;
2412 nskb->next = NULL;
2413 kfree_skb(nskb);
2414 } while (skb->next);
2415
2416 cb = DEV_GSO_CB(skb);
2417 if (cb->destructor)
2418 cb->destructor(skb);
2419}
2420
2421/**
2422 * dev_gso_segment - Perform emulated hardware segmentation on skb.
2423 * @skb: buffer to segment
Jesse Gross91ecb632011-01-09 06:23:33 +00002424 * @features: device features as applicable to this skb
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002425 *
2426 * This function segments the given skb and stores the list of segments
2427 * in skb->next.
2428 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002429static int dev_gso_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002430{
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002431 struct sk_buff *segs;
2432
Herbert Xu576a30e2006-06-27 13:22:38 -07002433 segs = skb_gso_segment(skb, features);
2434
2435 /* Verifying header integrity only. */
2436 if (!segs)
2437 return 0;
2438
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07002439 if (IS_ERR(segs))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002440 return PTR_ERR(segs);
2441
2442 skb->next = segs;
2443 DEV_GSO_CB(skb)->destructor = skb->destructor;
2444 skb->destructor = dev_gso_skb_destructor;
2445
2446 return 0;
2447}
2448
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002449static netdev_features_t harmonize_features(struct sk_buff *skb,
2450 __be16 protocol, netdev_features_t features)
Jesse Grossf01a5232011-01-09 06:23:31 +00002451{
Ed Cashinc0d680e2012-09-19 15:49:00 +00002452 if (skb->ip_summed != CHECKSUM_NONE &&
2453 !can_checksum_protocol(features, protocol)) {
Jesse Grossf01a5232011-01-09 06:23:31 +00002454 features &= ~NETIF_F_ALL_CSUM;
Jesse Grossf01a5232011-01-09 06:23:31 +00002455 } else if (illegal_highdma(skb->dev, skb)) {
2456 features &= ~NETIF_F_SG;
2457 }
2458
2459 return features;
2460}
2461
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002462netdev_features_t netif_skb_features(struct sk_buff *skb)
Jesse Gross58e998c2010-10-29 12:14:55 +00002463{
2464 __be16 protocol = skb->protocol;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002465 netdev_features_t features = skb->dev->features;
Jesse Gross58e998c2010-10-29 12:14:55 +00002466
Ben Hutchings30b678d2012-07-30 15:57:00 +00002467 if (skb_shinfo(skb)->gso_segs > skb->dev->gso_max_segs)
2468 features &= ~NETIF_F_GSO_MASK;
2469
Patrick McHardy8ad227f2013-04-19 02:04:31 +00002470 if (protocol == htons(ETH_P_8021Q) || protocol == htons(ETH_P_8021AD)) {
Jesse Gross58e998c2010-10-29 12:14:55 +00002471 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
2472 protocol = veh->h_vlan_encapsulated_proto;
Jesse Grossf01a5232011-01-09 06:23:31 +00002473 } else if (!vlan_tx_tag_present(skb)) {
2474 return harmonize_features(skb, protocol, features);
2475 }
Jesse Gross58e998c2010-10-29 12:14:55 +00002476
Patrick McHardy8ad227f2013-04-19 02:04:31 +00002477 features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_CTAG_TX |
2478 NETIF_F_HW_VLAN_STAG_TX);
Jesse Grossf01a5232011-01-09 06:23:31 +00002479
Patrick McHardy8ad227f2013-04-19 02:04:31 +00002480 if (protocol != htons(ETH_P_8021Q) && protocol != htons(ETH_P_8021AD)) {
Jesse Grossf01a5232011-01-09 06:23:31 +00002481 return harmonize_features(skb, protocol, features);
2482 } else {
2483 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
Patrick McHardy8ad227f2013-04-19 02:04:31 +00002484 NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
2485 NETIF_F_HW_VLAN_STAG_TX;
Jesse Grossf01a5232011-01-09 06:23:31 +00002486 return harmonize_features(skb, protocol, features);
2487 }
Jesse Gross58e998c2010-10-29 12:14:55 +00002488}
Jesse Grossf01a5232011-01-09 06:23:31 +00002489EXPORT_SYMBOL(netif_skb_features);
Jesse Gross58e998c2010-10-29 12:14:55 +00002490
John Fastabend6afff0c2010-06-16 14:18:12 +00002491/*
2492 * Returns true if either:
2493 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
Rami Rosend1a53df2012-08-27 23:39:24 +00002494 * 2. skb is fragmented and the device does not support SG.
John Fastabend6afff0c2010-06-16 14:18:12 +00002495 */
2496static inline int skb_needs_linearize(struct sk_buff *skb,
Patrick McHardy6708c9e2013-05-01 22:36:49 +00002497 netdev_features_t features)
John Fastabend6afff0c2010-06-16 14:18:12 +00002498{
Jesse Gross02932ce2011-01-09 06:23:34 +00002499 return skb_is_nonlinear(skb) &&
2500 ((skb_has_frag_list(skb) &&
2501 !(features & NETIF_F_FRAGLIST)) ||
Jesse Grosse1e78db2010-10-29 12:14:53 +00002502 (skb_shinfo(skb)->nr_frags &&
Jesse Gross02932ce2011-01-09 06:23:34 +00002503 !(features & NETIF_F_SG)));
John Fastabend6afff0c2010-06-16 14:18:12 +00002504}
2505
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002506int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2507 struct netdev_queue *txq)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002508{
Stephen Hemminger00829822008-11-20 20:14:53 -08002509 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy572a9d72009-11-10 06:14:14 +00002510 int rc = NETDEV_TX_OK;
Koki Sanagiec764bf2011-05-30 21:48:34 +00002511 unsigned int skb_len;
Stephen Hemminger00829822008-11-20 20:14:53 -08002512
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002513 if (likely(!skb->next)) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002514 netdev_features_t features;
Jesse Grossfc741212011-01-09 06:23:32 +00002515
Eric Dumazet93f154b2009-05-18 22:19:19 -07002516 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002517 * If device doesn't need skb->dst, release it right now while
Eric Dumazet93f154b2009-05-18 22:19:19 -07002518 * its hot in this cpu cache
2519 */
Eric Dumazetadf30902009-06-02 05:19:30 +00002520 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2521 skb_dst_drop(skb);
2522
Jesse Grossfc741212011-01-09 06:23:32 +00002523 features = netif_skb_features(skb);
2524
Jesse Gross7b9c6092010-10-20 13:56:04 +00002525 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002526 !vlan_hw_offload_capable(features, skb->vlan_proto)) {
2527 skb = __vlan_put_tag(skb, skb->vlan_proto,
2528 vlan_tx_tag_get(skb));
Jesse Gross7b9c6092010-10-20 13:56:04 +00002529 if (unlikely(!skb))
2530 goto out;
2531
2532 skb->vlan_tci = 0;
2533 }
2534
Alexander Duyckfc70fb62012-12-07 14:14:15 +00002535 /* If encapsulation offload request, verify we are testing
2536 * hardware encapsulation features instead of standard
2537 * features for the netdev
2538 */
2539 if (skb->encapsulation)
2540 features &= dev->hw_enc_features;
2541
Jesse Grossfc741212011-01-09 06:23:32 +00002542 if (netif_needs_gso(skb, features)) {
Jesse Gross91ecb632011-01-09 06:23:33 +00002543 if (unlikely(dev_gso_segment(skb, features)))
David S. Miller9ccb8972010-04-22 01:02:07 -07002544 goto out_kfree_skb;
2545 if (skb->next)
2546 goto gso;
John Fastabend6afff0c2010-06-16 14:18:12 +00002547 } else {
Jesse Gross02932ce2011-01-09 06:23:34 +00002548 if (skb_needs_linearize(skb, features) &&
John Fastabend6afff0c2010-06-16 14:18:12 +00002549 __skb_linearize(skb))
2550 goto out_kfree_skb;
2551
2552 /* If packet is not checksummed and device does not
2553 * support checksumming for this protocol, complete
2554 * checksumming here.
2555 */
2556 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Alexander Duyckfc70fb62012-12-07 14:14:15 +00002557 if (skb->encapsulation)
2558 skb_set_inner_transport_header(skb,
2559 skb_checksum_start_offset(skb));
2560 else
2561 skb_set_transport_header(skb,
2562 skb_checksum_start_offset(skb));
Jesse Gross03634662011-01-09 06:23:35 +00002563 if (!(features & NETIF_F_ALL_CSUM) &&
John Fastabend6afff0c2010-06-16 14:18:12 +00002564 skb_checksum_help(skb))
2565 goto out_kfree_skb;
2566 }
David S. Miller9ccb8972010-04-22 01:02:07 -07002567 }
2568
Eric Dumazetb40863c2012-09-18 20:44:49 +00002569 if (!list_empty(&ptype_all))
2570 dev_queue_xmit_nit(skb, dev);
2571
Koki Sanagiec764bf2011-05-30 21:48:34 +00002572 skb_len = skb->len;
Patrick Ohlyac45f602009-02-12 05:03:37 +00002573 rc = ops->ndo_start_xmit(skb, dev);
Koki Sanagiec764bf2011-05-30 21:48:34 +00002574 trace_net_dev_xmit(skb, rc, dev, skb_len);
Patrick McHardyec634fe2009-07-05 19:23:38 -07002575 if (rc == NETDEV_TX_OK)
Eric Dumazet08baf562009-05-25 22:58:01 -07002576 txq_trans_update(txq);
Patrick Ohlyac45f602009-02-12 05:03:37 +00002577 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002578 }
2579
Herbert Xu576a30e2006-06-27 13:22:38 -07002580gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002581 do {
2582 struct sk_buff *nskb = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002583
2584 skb->next = nskb->next;
2585 nskb->next = NULL;
Krishna Kumar068a2de2009-12-09 20:59:58 +00002586
Eric Dumazetb40863c2012-09-18 20:44:49 +00002587 if (!list_empty(&ptype_all))
2588 dev_queue_xmit_nit(nskb, dev);
2589
Koki Sanagiec764bf2011-05-30 21:48:34 +00002590 skb_len = nskb->len;
Stephen Hemminger00829822008-11-20 20:14:53 -08002591 rc = ops->ndo_start_xmit(nskb, dev);
Koki Sanagiec764bf2011-05-30 21:48:34 +00002592 trace_net_dev_xmit(nskb, rc, dev, skb_len);
Patrick McHardyec634fe2009-07-05 19:23:38 -07002593 if (unlikely(rc != NETDEV_TX_OK)) {
Patrick McHardy572a9d72009-11-10 06:14:14 +00002594 if (rc & ~NETDEV_TX_MASK)
2595 goto out_kfree_gso_skb;
Michael Chanf54d9e82006-06-25 23:57:04 -07002596 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002597 skb->next = nskb;
2598 return rc;
2599 }
Eric Dumazet08baf562009-05-25 22:58:01 -07002600 txq_trans_update(txq);
Tom Herbert734664982011-11-28 16:32:44 +00002601 if (unlikely(netif_xmit_stopped(txq) && skb->next))
Michael Chanf54d9e82006-06-25 23:57:04 -07002602 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002603 } while (skb->next);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002604
Patrick McHardy572a9d72009-11-10 06:14:14 +00002605out_kfree_gso_skb:
Sridhar Samudrala0c772152013-04-29 13:02:42 +00002606 if (likely(skb->next == NULL)) {
Patrick McHardy572a9d72009-11-10 06:14:14 +00002607 skb->destructor = DEV_GSO_CB(skb)->destructor;
Sridhar Samudrala0c772152013-04-29 13:02:42 +00002608 consume_skb(skb);
2609 return rc;
2610 }
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002611out_kfree_skb:
2612 kfree_skb(skb);
Jesse Gross7b9c6092010-10-20 13:56:04 +00002613out:
Patrick McHardy572a9d72009-11-10 06:14:14 +00002614 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002615}
2616
Eric Dumazet1def9232013-01-10 12:36:42 +00002617static void qdisc_pkt_len_init(struct sk_buff *skb)
2618{
2619 const struct skb_shared_info *shinfo = skb_shinfo(skb);
2620
2621 qdisc_skb_cb(skb)->pkt_len = skb->len;
2622
2623 /* To get more precise estimation of bytes sent on wire,
2624 * we add to pkt_len the headers size of all segments
2625 */
2626 if (shinfo->gso_size) {
Eric Dumazet757b8b12013-01-15 21:14:21 -08002627 unsigned int hdr_len;
Jason Wang15e5a032013-03-25 20:19:59 +00002628 u16 gso_segs = shinfo->gso_segs;
Eric Dumazet1def9232013-01-10 12:36:42 +00002629
Eric Dumazet757b8b12013-01-15 21:14:21 -08002630 /* mac layer + network layer */
2631 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
2632
2633 /* + transport layer */
Eric Dumazet1def9232013-01-10 12:36:42 +00002634 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
2635 hdr_len += tcp_hdrlen(skb);
2636 else
2637 hdr_len += sizeof(struct udphdr);
Jason Wang15e5a032013-03-25 20:19:59 +00002638
2639 if (shinfo->gso_type & SKB_GSO_DODGY)
2640 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
2641 shinfo->gso_size);
2642
2643 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
Eric Dumazet1def9232013-01-10 12:36:42 +00002644 }
2645}
2646
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002647static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2648 struct net_device *dev,
2649 struct netdev_queue *txq)
2650{
2651 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazeta2da5702011-01-20 03:48:19 +00002652 bool contended;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002653 int rc;
2654
Eric Dumazet1def9232013-01-10 12:36:42 +00002655 qdisc_pkt_len_init(skb);
Eric Dumazeta2da5702011-01-20 03:48:19 +00002656 qdisc_calculate_pkt_len(skb, q);
Eric Dumazet79640a42010-06-02 05:09:29 -07002657 /*
2658 * Heuristic to force contended enqueues to serialize on a
2659 * separate lock before trying to get qdisc main lock.
2660 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2661 * and dequeue packets faster.
2662 */
Eric Dumazeta2da5702011-01-20 03:48:19 +00002663 contended = qdisc_is_running(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07002664 if (unlikely(contended))
2665 spin_lock(&q->busylock);
2666
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002667 spin_lock(root_lock);
2668 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2669 kfree_skb(skb);
2670 rc = NET_XMIT_DROP;
2671 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07002672 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002673 /*
2674 * This is a work-conserving queue; there are no old skbs
2675 * waiting to be sent out; and the qdisc is not running -
2676 * xmit the skb directly.
2677 */
Eric Dumazet7fee2262010-05-11 23:19:48 +00002678 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2679 skb_dst_force(skb);
Eric Dumazetbfe0d022011-01-09 08:30:54 +00002680
Eric Dumazetbfe0d022011-01-09 08:30:54 +00002681 qdisc_bstats_update(q, skb);
2682
Eric Dumazet79640a42010-06-02 05:09:29 -07002683 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2684 if (unlikely(contended)) {
2685 spin_unlock(&q->busylock);
2686 contended = false;
2687 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002688 __qdisc_run(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07002689 } else
Eric Dumazetbc135b22010-06-02 03:23:51 -07002690 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002691
2692 rc = NET_XMIT_SUCCESS;
2693 } else {
Eric Dumazet7fee2262010-05-11 23:19:48 +00002694 skb_dst_force(skb);
Eric Dumazeta2da5702011-01-20 03:48:19 +00002695 rc = q->enqueue(skb, q) & NET_XMIT_MASK;
Eric Dumazet79640a42010-06-02 05:09:29 -07002696 if (qdisc_run_begin(q)) {
2697 if (unlikely(contended)) {
2698 spin_unlock(&q->busylock);
2699 contended = false;
2700 }
2701 __qdisc_run(q);
2702 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002703 }
2704 spin_unlock(root_lock);
Eric Dumazet79640a42010-06-02 05:09:29 -07002705 if (unlikely(contended))
2706 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002707 return rc;
2708}
2709
Neil Horman5bc14212011-11-22 05:10:51 +00002710#if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
2711static void skb_update_prio(struct sk_buff *skb)
2712{
Igor Maravic6977a792011-11-25 07:44:54 +00002713 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
Neil Horman5bc14212011-11-22 05:10:51 +00002714
Eric Dumazet91c68ce2012-07-08 21:45:10 +00002715 if (!skb->priority && skb->sk && map) {
2716 unsigned int prioidx = skb->sk->sk_cgrp_prioidx;
2717
2718 if (prioidx < map->priomap_len)
2719 skb->priority = map->priomap[prioidx];
2720 }
Neil Horman5bc14212011-11-22 05:10:51 +00002721}
2722#else
2723#define skb_update_prio(skb)
2724#endif
2725
Eric Dumazet745e20f2010-09-29 13:23:09 -07002726static DEFINE_PER_CPU(int, xmit_recursion);
David S. Miller11a766c2010-10-25 12:51:55 -07002727#define RECURSION_LIMIT 10
Eric Dumazet745e20f2010-09-29 13:23:09 -07002728
Dave Jonesd29f7492008-07-22 14:09:06 -07002729/**
Michel Machado95603e22012-06-12 10:16:35 +00002730 * dev_loopback_xmit - loop back @skb
2731 * @skb: buffer to transmit
2732 */
2733int dev_loopback_xmit(struct sk_buff *skb)
2734{
2735 skb_reset_mac_header(skb);
2736 __skb_pull(skb, skb_network_offset(skb));
2737 skb->pkt_type = PACKET_LOOPBACK;
2738 skb->ip_summed = CHECKSUM_UNNECESSARY;
2739 WARN_ON(!skb_dst(skb));
2740 skb_dst_force(skb);
2741 netif_rx_ni(skb);
2742 return 0;
2743}
2744EXPORT_SYMBOL(dev_loopback_xmit);
2745
2746/**
Dave Jonesd29f7492008-07-22 14:09:06 -07002747 * dev_queue_xmit - transmit a buffer
2748 * @skb: buffer to transmit
2749 *
2750 * Queue a buffer for transmission to a network device. The caller must
2751 * have set the device and priority and built the buffer before calling
2752 * this function. The function can be called from an interrupt.
2753 *
2754 * A negative errno code is returned on a failure. A success does not
2755 * guarantee the frame will be transmitted as it may be dropped due
2756 * to congestion or traffic shaping.
2757 *
2758 * -----------------------------------------------------------------------------------
2759 * I notice this method can also return errors from the queue disciplines,
2760 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2761 * be positive.
2762 *
2763 * Regardless of the return value, the skb is consumed, so it is currently
2764 * difficult to retry a send to this method. (You can bump the ref count
2765 * before sending to hold a reference for retry if you are careful.)
2766 *
2767 * When calling this method, interrupts MUST be enabled. This is because
2768 * the BH enable code must have IRQs enabled so that it will not deadlock.
2769 * --BLG
2770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771int dev_queue_xmit(struct sk_buff *skb)
2772{
2773 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07002774 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct Qdisc *q;
2776 int rc = -ENOMEM;
2777
Eric Dumazet6d1ccff2013-02-05 20:22:20 +00002778 skb_reset_mac_header(skb);
2779
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002780 /* Disable soft irqs for various locks below. Also
2781 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002783 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Neil Horman5bc14212011-11-22 05:10:51 +00002785 skb_update_prio(skb);
2786
Amerigo Wang8c4c49d2012-09-17 20:16:31 +00002787 txq = netdev_pick_tx(dev, skb);
Paul E. McKenneya898def2010-02-22 17:04:49 -08002788 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07002789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790#ifdef CONFIG_NET_CLS_ACT
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002791 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792#endif
Koki Sanagicf66ba52010-08-23 18:45:02 +09002793 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002795 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07002796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798
2799 /* The device has no queue. Common case for software devices:
2800 loopback, all the sorts of tunnels...
2801
Herbert Xu932ff272006-06-09 12:20:56 -07002802 Really, it is unlikely that netif_tx_lock protection is necessary
2803 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 counters.)
2805 However, it is possible, that they rely on protection
2806 made by us here.
2807
2808 Check this and shot the lock. It is not prone from deadlocks.
2809 Either shot noqueue qdisc, it is even simpler 8)
2810 */
2811 if (dev->flags & IFF_UP) {
2812 int cpu = smp_processor_id(); /* ok because BHs are off */
2813
David S. Millerc773e842008-07-08 23:13:53 -07002814 if (txq->xmit_lock_owner != cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Eric Dumazet745e20f2010-09-29 13:23:09 -07002816 if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
2817 goto recursion_alert;
2818
David S. Millerc773e842008-07-08 23:13:53 -07002819 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Tom Herbert734664982011-11-28 16:32:44 +00002821 if (!netif_xmit_stopped(txq)) {
Eric Dumazet745e20f2010-09-29 13:23:09 -07002822 __this_cpu_inc(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00002823 rc = dev_hard_start_xmit(skb, dev, txq);
Eric Dumazet745e20f2010-09-29 13:23:09 -07002824 __this_cpu_dec(xmit_recursion);
Patrick McHardy572a9d72009-11-10 06:14:14 +00002825 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07002826 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 goto out;
2828 }
2829 }
David S. Millerc773e842008-07-08 23:13:53 -07002830 HARD_TX_UNLOCK(dev, txq);
Joe Perchese87cc472012-05-13 21:56:26 +00002831 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
2832 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 } else {
2834 /* Recursion is detected! It is possible,
Eric Dumazet745e20f2010-09-29 13:23:09 -07002835 * unfortunately
2836 */
2837recursion_alert:
Joe Perchese87cc472012-05-13 21:56:26 +00002838 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
2839 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841 }
2842
2843 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07002844 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 kfree_skb(skb);
2847 return rc;
2848out:
Herbert Xud4828d82006-06-22 02:28:18 -07002849 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return rc;
2851}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002852EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854
2855/*=======================================================================
2856 Receiver routines
2857 =======================================================================*/
2858
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07002859int netdev_max_backlog __read_mostly = 1000;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00002860EXPORT_SYMBOL(netdev_max_backlog);
2861
Eric Dumazet3b098e22010-05-15 23:57:10 -07002862int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07002863int netdev_budget __read_mostly = 300;
2864int weight_p __read_mostly = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07002866/* Called with irq disabled */
2867static inline void ____napi_schedule(struct softnet_data *sd,
2868 struct napi_struct *napi)
2869{
2870 list_add_tail(&napi->poll_list, &sd->poll_list);
2871 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2872}
2873
Eric Dumazetdf334542010-03-24 19:13:54 +00002874#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07002875
2876/* One global table that all flow-based protocols share. */
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00002877struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07002878EXPORT_SYMBOL(rps_sock_flow_table);
2879
Ingo Molnarc5905af2012-02-24 08:31:31 +01002880struct static_key rps_needed __read_mostly;
Eric Dumazetadc93002011-11-17 03:13:26 +00002881
Ben Hutchingsc4454772011-01-19 11:03:53 +00002882static struct rps_dev_flow *
2883set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2884 struct rps_dev_flow *rflow, u16 next_cpu)
2885{
Ben Hutchings09994d12011-10-03 04:42:46 +00002886 if (next_cpu != RPS_NO_CPU) {
Ben Hutchingsc4454772011-01-19 11:03:53 +00002887#ifdef CONFIG_RFS_ACCEL
2888 struct netdev_rx_queue *rxqueue;
2889 struct rps_dev_flow_table *flow_table;
2890 struct rps_dev_flow *old_rflow;
2891 u32 flow_id;
2892 u16 rxq_index;
2893 int rc;
2894
2895 /* Should we steer this flow to a different hardware queue? */
Ben Hutchings69a19ee2011-02-15 20:32:04 +00002896 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
2897 !(dev->features & NETIF_F_NTUPLE))
Ben Hutchingsc4454772011-01-19 11:03:53 +00002898 goto out;
2899 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
2900 if (rxq_index == skb_get_rx_queue(skb))
2901 goto out;
2902
2903 rxqueue = dev->_rx + rxq_index;
2904 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2905 if (!flow_table)
2906 goto out;
2907 flow_id = skb->rxhash & flow_table->mask;
2908 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
2909 rxq_index, flow_id);
2910 if (rc < 0)
2911 goto out;
2912 old_rflow = rflow;
2913 rflow = &flow_table->flows[flow_id];
Ben Hutchingsc4454772011-01-19 11:03:53 +00002914 rflow->filter = rc;
2915 if (old_rflow->filter == rflow->filter)
2916 old_rflow->filter = RPS_NO_FILTER;
2917 out:
2918#endif
2919 rflow->last_qtail =
Ben Hutchings09994d12011-10-03 04:42:46 +00002920 per_cpu(softnet_data, next_cpu).input_queue_head;
Ben Hutchingsc4454772011-01-19 11:03:53 +00002921 }
2922
Ben Hutchings09994d12011-10-03 04:42:46 +00002923 rflow->cpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00002924 return rflow;
2925}
2926
Tom Herbert0a9627f2010-03-16 08:03:29 +00002927/*
2928 * get_rps_cpu is called from netif_receive_skb and returns the target
2929 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002930 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00002931 */
Tom Herbertfec5e652010-04-16 16:01:27 -07002932static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2933 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00002934{
Tom Herbert0a9627f2010-03-16 08:03:29 +00002935 struct netdev_rx_queue *rxqueue;
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00002936 struct rps_map *map;
Tom Herbertfec5e652010-04-16 16:01:27 -07002937 struct rps_dev_flow_table *flow_table;
2938 struct rps_sock_flow_table *sock_flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002939 int cpu = -1;
Tom Herbertfec5e652010-04-16 16:01:27 -07002940 u16 tcpu;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002941
Tom Herbert0a9627f2010-03-16 08:03:29 +00002942 if (skb_rx_queue_recorded(skb)) {
2943 u16 index = skb_get_rx_queue(skb);
Ben Hutchings62fe0b42010-09-27 08:24:33 +00002944 if (unlikely(index >= dev->real_num_rx_queues)) {
2945 WARN_ONCE(dev->real_num_rx_queues > 1,
2946 "%s received packet on queue %u, but number "
2947 "of RX queues is %u\n",
2948 dev->name, index, dev->real_num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002949 goto done;
2950 }
2951 rxqueue = dev->_rx + index;
2952 } else
2953 rxqueue = dev->_rx;
2954
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +00002955 map = rcu_dereference(rxqueue->rps_map);
2956 if (map) {
Tom Herbert85875232011-01-31 16:23:42 -08002957 if (map->len == 1 &&
Eric Dumazet33d480c2011-08-11 19:30:52 +00002958 !rcu_access_pointer(rxqueue->rps_flow_table)) {
Changli Gao6febfca2010-09-03 23:12:37 +00002959 tcpu = map->cpus[0];
2960 if (cpu_online(tcpu))
2961 cpu = tcpu;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002962 goto done;
Eric Dumazetb249dcb2010-04-19 21:56:38 +00002963 }
Eric Dumazet33d480c2011-08-11 19:30:52 +00002964 } else if (!rcu_access_pointer(rxqueue->rps_flow_table)) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00002965 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002966 }
2967
Changli Gao2d47b452010-08-17 19:00:56 +00002968 skb_reset_network_header(skb);
Krishna Kumarbfb564e2010-08-04 06:15:52 +00002969 if (!skb_get_rxhash(skb))
Tom Herbert0a9627f2010-03-16 08:03:29 +00002970 goto done;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002971
Tom Herbertfec5e652010-04-16 16:01:27 -07002972 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2973 sock_flow_table = rcu_dereference(rps_sock_flow_table);
2974 if (flow_table && sock_flow_table) {
2975 u16 next_cpu;
2976 struct rps_dev_flow *rflow;
2977
2978 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
2979 tcpu = rflow->cpu;
2980
2981 next_cpu = sock_flow_table->ents[skb->rxhash &
2982 sock_flow_table->mask];
2983
2984 /*
2985 * If the desired CPU (where last recvmsg was done) is
2986 * different from current CPU (one in the rx-queue flow
2987 * table entry), switch if one of the following holds:
2988 * - Current CPU is unset (equal to RPS_NO_CPU).
2989 * - Current CPU is offline.
2990 * - The current CPU's queue tail has advanced beyond the
2991 * last packet that was enqueued using this table entry.
2992 * This guarantees that all previous packets for the flow
2993 * have been dequeued, thus preserving in order delivery.
2994 */
2995 if (unlikely(tcpu != next_cpu) &&
2996 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
2997 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
Tom Herbertbaefa312012-11-16 09:04:15 +00002998 rflow->last_qtail)) >= 0)) {
2999 tcpu = next_cpu;
Ben Hutchingsc4454772011-01-19 11:03:53 +00003000 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
Tom Herbertbaefa312012-11-16 09:04:15 +00003001 }
Ben Hutchingsc4454772011-01-19 11:03:53 +00003002
Tom Herbertfec5e652010-04-16 16:01:27 -07003003 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
3004 *rflowp = rflow;
3005 cpu = tcpu;
3006 goto done;
3007 }
3008 }
3009
Tom Herbert0a9627f2010-03-16 08:03:29 +00003010 if (map) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003011 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
Tom Herbert0a9627f2010-03-16 08:03:29 +00003012
3013 if (cpu_online(tcpu)) {
3014 cpu = tcpu;
3015 goto done;
3016 }
3017 }
3018
3019done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00003020 return cpu;
3021}
3022
Ben Hutchingsc4454772011-01-19 11:03:53 +00003023#ifdef CONFIG_RFS_ACCEL
3024
3025/**
3026 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3027 * @dev: Device on which the filter was set
3028 * @rxq_index: RX queue index
3029 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3030 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3031 *
3032 * Drivers that implement ndo_rx_flow_steer() should periodically call
3033 * this function for each installed filter and remove the filters for
3034 * which it returns %true.
3035 */
3036bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3037 u32 flow_id, u16 filter_id)
3038{
3039 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3040 struct rps_dev_flow_table *flow_table;
3041 struct rps_dev_flow *rflow;
3042 bool expire = true;
3043 int cpu;
3044
3045 rcu_read_lock();
3046 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3047 if (flow_table && flow_id <= flow_table->mask) {
3048 rflow = &flow_table->flows[flow_id];
3049 cpu = ACCESS_ONCE(rflow->cpu);
3050 if (rflow->filter == filter_id && cpu != RPS_NO_CPU &&
3051 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3052 rflow->last_qtail) <
3053 (int)(10 * flow_table->mask)))
3054 expire = false;
3055 }
3056 rcu_read_unlock();
3057 return expire;
3058}
3059EXPORT_SYMBOL(rps_may_expire_flow);
3060
3061#endif /* CONFIG_RFS_ACCEL */
3062
Tom Herbert0a9627f2010-03-16 08:03:29 +00003063/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003064static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003065{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003066 struct softnet_data *sd = data;
3067
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003068 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00003069 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003070}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003071
Tom Herbertfec5e652010-04-16 16:01:27 -07003072#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00003073
3074/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003075 * Check if this softnet_data structure is another cpu one
3076 * If yes, queue it to our IPI list and return 1
3077 * If no, return 0
3078 */
3079static int rps_ipi_queued(struct softnet_data *sd)
3080{
3081#ifdef CONFIG_RPS
3082 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
3083
3084 if (sd != mysd) {
3085 sd->rps_ipi_next = mysd->rps_ipi_list;
3086 mysd->rps_ipi_list = sd;
3087
3088 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3089 return 1;
3090 }
3091#endif /* CONFIG_RPS */
3092 return 0;
3093}
3094
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003095#ifdef CONFIG_NET_FLOW_LIMIT
3096int netdev_flow_limit_table_len __read_mostly = (1 << 12);
3097#endif
3098
3099static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
3100{
3101#ifdef CONFIG_NET_FLOW_LIMIT
3102 struct sd_flow_limit *fl;
3103 struct softnet_data *sd;
3104 unsigned int old_flow, new_flow;
3105
3106 if (qlen < (netdev_max_backlog >> 1))
3107 return false;
3108
3109 sd = &__get_cpu_var(softnet_data);
3110
3111 rcu_read_lock();
3112 fl = rcu_dereference(sd->flow_limit);
3113 if (fl) {
3114 new_flow = skb_get_rxhash(skb) & (fl->num_buckets - 1);
3115 old_flow = fl->history[fl->history_head];
3116 fl->history[fl->history_head] = new_flow;
3117
3118 fl->history_head++;
3119 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
3120
3121 if (likely(fl->buckets[old_flow]))
3122 fl->buckets[old_flow]--;
3123
3124 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
3125 fl->count++;
3126 rcu_read_unlock();
3127 return true;
3128 }
3129 }
3130 rcu_read_unlock();
3131#endif
3132 return false;
3133}
3134
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003135/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00003136 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3137 * queue (may be a remote CPU queue).
3138 */
Tom Herbertfec5e652010-04-16 16:01:27 -07003139static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3140 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00003141{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003142 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003143 unsigned long flags;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003144 unsigned int qlen;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003145
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003146 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003147
3148 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003149
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003150 rps_lock(sd);
Willem de Bruijn99bbc702013-05-20 04:02:32 +00003151 qlen = skb_queue_len(&sd->input_pkt_queue);
3152 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
Changli Gao6e7676c2010-04-27 15:07:33 -07003153 if (skb_queue_len(&sd->input_pkt_queue)) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00003154enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003155 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003156 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003157 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00003158 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003159 return NET_RX_SUCCESS;
3160 }
3161
Eric Dumazetebda37c22010-05-06 23:51:21 +00003162 /* Schedule NAPI for backlog device
3163 * We can use non atomic operation since we own the queue lock
3164 */
3165 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003166 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003167 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003168 }
3169 goto enqueue;
3170 }
3171
Changli Gaodee42872010-05-02 05:42:16 +00003172 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003173 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003174
Tom Herbert0a9627f2010-03-16 08:03:29 +00003175 local_irq_restore(flags);
3176
Eric Dumazetcaf586e2010-09-30 21:06:55 +00003177 atomic_long_inc(&skb->dev->rx_dropped);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003178 kfree_skb(skb);
3179 return NET_RX_DROP;
3180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182/**
3183 * netif_rx - post buffer to the network code
3184 * @skb: buffer to post
3185 *
3186 * This function receives a packet from a device driver and queues it for
3187 * the upper (protocol) levels to process. It always succeeds. The buffer
3188 * may be dropped during processing for congestion control or by the
3189 * protocol layers.
3190 *
3191 * return values:
3192 * NET_RX_SUCCESS (no congestion)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 * NET_RX_DROP (packet was dropped)
3194 *
3195 */
3196
3197int netif_rx(struct sk_buff *skb)
3198{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003199 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 /* if netpoll wants it, pretend we never saw it */
3202 if (netpoll_rx(skb))
3203 return NET_RX_DROP;
3204
Eric Dumazet588f0332011-11-15 04:12:55 +00003205 net_timestamp_check(netdev_tstamp_prequeue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Koki Sanagicf66ba52010-08-23 18:45:02 +09003207 trace_netif_rx(skb);
Eric Dumazetdf334542010-03-24 19:13:54 +00003208#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01003209 if (static_key_false(&rps_needed)) {
Tom Herbertfec5e652010-04-16 16:01:27 -07003210 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003211 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Changli Gaocece1942010-08-07 20:35:43 -07003213 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003214 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07003215
3216 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003217 if (cpu < 0)
3218 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07003219
3220 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3221
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003222 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07003223 preempt_enable();
Eric Dumazetadc93002011-11-17 03:13:26 +00003224 } else
3225#endif
Tom Herbertfec5e652010-04-16 16:01:27 -07003226 {
3227 unsigned int qtail;
3228 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
3229 put_cpu();
3230 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07003231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003233EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235int netif_rx_ni(struct sk_buff *skb)
3236{
3237 int err;
3238
3239 preempt_disable();
3240 err = netif_rx(skb);
3241 if (local_softirq_pending())
3242 do_softirq();
3243 preempt_enable();
3244
3245 return err;
3246}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247EXPORT_SYMBOL(netif_rx_ni);
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249static void net_tx_action(struct softirq_action *h)
3250{
3251 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3252
3253 if (sd->completion_queue) {
3254 struct sk_buff *clist;
3255
3256 local_irq_disable();
3257 clist = sd->completion_queue;
3258 sd->completion_queue = NULL;
3259 local_irq_enable();
3260
3261 while (clist) {
3262 struct sk_buff *skb = clist;
3263 clist = clist->next;
3264
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003265 WARN_ON(atomic_read(&skb->users));
Koki Sanagi07dc22e2010-08-23 18:46:12 +09003266 trace_kfree_skb(skb, net_tx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 __kfree_skb(skb);
3268 }
3269 }
3270
3271 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07003272 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 local_irq_disable();
3275 head = sd->output_queue;
3276 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00003277 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 local_irq_enable();
3279
3280 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07003281 struct Qdisc *q = head;
3282 spinlock_t *root_lock;
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 head = head->next_sched;
3285
David S. Miller5fb66222008-08-02 20:02:43 -07003286 root_lock = qdisc_lock(q);
David S. Miller37437bb2008-07-16 02:15:04 -07003287 if (spin_trylock(root_lock)) {
Jarek Poplawskidef82a12008-08-17 21:54:43 -07003288 smp_mb__before_clear_bit();
3289 clear_bit(__QDISC_STATE_SCHED,
3290 &q->state);
David S. Miller37437bb2008-07-16 02:15:04 -07003291 qdisc_run(q);
3292 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 } else {
David S. Miller195648b2008-08-19 04:00:36 -07003294 if (!test_bit(__QDISC_STATE_DEACTIVATED,
Jarek Poplawskie8a83e12008-09-07 18:41:21 -07003295 &q->state)) {
David S. Miller195648b2008-08-19 04:00:36 -07003296 __netif_reschedule(q);
Jarek Poplawskie8a83e12008-09-07 18:41:21 -07003297 } else {
3298 smp_mb__before_clear_bit();
3299 clear_bit(__QDISC_STATE_SCHED,
3300 &q->state);
3301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303 }
3304 }
3305}
3306
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003307#if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
3308 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
Michał Mirosławda678292009-06-05 05:35:28 +00003309/* This hook is defined here for ATM LANE */
3310int (*br_fdb_test_addr_hook)(struct net_device *dev,
3311 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07003312EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00003313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315#ifdef CONFIG_NET_CLS_ACT
3316/* TODO: Maybe we should just force sch_ingress to be compiled in
3317 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
3318 * a compare and 2 stores extra right now if we dont have it on
3319 * but have CONFIG_NET_CLS_ACT
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003320 * NOTE: This doesn't stop any functionality; if you dont have
3321 * the ingress scheduler, you just can't add policies on ingress.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 *
3323 */
Eric Dumazet24824a02010-10-02 06:11:55 +00003324static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 struct net_device *dev = skb->dev;
Herbert Xuf697c3e2007-10-14 00:38:47 -07003327 u32 ttl = G_TC_RTTL(skb->tc_verd);
David S. Miller555353c2008-07-08 17:33:13 -07003328 int result = TC_ACT_OK;
3329 struct Qdisc *q;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003330
Stephen Hemmingerde384832010-08-01 00:33:23 -07003331 if (unlikely(MAX_RED_LOOP < ttl++)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003332 net_warn_ratelimited("Redir loop detected Dropping packet (%d->%d)\n",
3333 skb->skb_iif, dev->ifindex);
Herbert Xuf697c3e2007-10-14 00:38:47 -07003334 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336
Herbert Xuf697c3e2007-10-14 00:38:47 -07003337 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
3338 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
3339
David S. Miller83874002008-07-17 00:53:03 -07003340 q = rxq->qdisc;
David S. Miller8d50b532008-07-30 02:37:46 -07003341 if (q != &noop_qdisc) {
David S. Miller83874002008-07-17 00:53:03 -07003342 spin_lock(qdisc_lock(q));
David S. Millera9312ae2008-08-17 21:51:03 -07003343 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
3344 result = qdisc_enqueue_root(skb, q);
David S. Miller83874002008-07-17 00:53:03 -07003345 spin_unlock(qdisc_lock(q));
3346 }
Herbert Xuf697c3e2007-10-14 00:38:47 -07003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return result;
3349}
Herbert Xuf697c3e2007-10-14 00:38:47 -07003350
3351static inline struct sk_buff *handle_ing(struct sk_buff *skb,
3352 struct packet_type **pt_prev,
3353 int *ret, struct net_device *orig_dev)
3354{
Eric Dumazet24824a02010-10-02 06:11:55 +00003355 struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
3356
3357 if (!rxq || rxq->qdisc == &noop_qdisc)
Herbert Xuf697c3e2007-10-14 00:38:47 -07003358 goto out;
3359
3360 if (*pt_prev) {
3361 *ret = deliver_skb(skb, *pt_prev, orig_dev);
3362 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07003363 }
3364
Eric Dumazet24824a02010-10-02 06:11:55 +00003365 switch (ing_filter(skb, rxq)) {
Herbert Xuf697c3e2007-10-14 00:38:47 -07003366 case TC_ACT_SHOT:
3367 case TC_ACT_STOLEN:
3368 kfree_skb(skb);
3369 return NULL;
3370 }
3371
3372out:
3373 skb->tc_verd = 0;
3374 return skb;
3375}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376#endif
3377
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003378/**
3379 * netdev_rx_handler_register - register receive handler
3380 * @dev: device to register a handler for
3381 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00003382 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003383 *
3384 * Register a receive hander for a device. This handler will then be
3385 * called from __netif_receive_skb. A negative errno code is returned
3386 * on a failure.
3387 *
3388 * The caller must hold the rtnl_mutex.
Jiri Pirko8a4eb572011-03-12 03:14:39 +00003389 *
3390 * For a general description of rx_handler, see enum rx_handler_result.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003391 */
3392int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00003393 rx_handler_func_t *rx_handler,
3394 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003395{
3396 ASSERT_RTNL();
3397
3398 if (dev->rx_handler)
3399 return -EBUSY;
3400
Eric Dumazet00cfec32013-03-29 03:01:22 +00003401 /* Note: rx_handler_data must be set before rx_handler */
Jiri Pirko93e2c322010-06-10 03:34:59 +00003402 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003403 rcu_assign_pointer(dev->rx_handler, rx_handler);
3404
3405 return 0;
3406}
3407EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
3408
3409/**
3410 * netdev_rx_handler_unregister - unregister receive handler
3411 * @dev: device to unregister a handler from
3412 *
Kusanagi Kouichi166ec362013-03-18 02:59:52 +00003413 * Unregister a receive handler from a device.
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003414 *
3415 * The caller must hold the rtnl_mutex.
3416 */
3417void netdev_rx_handler_unregister(struct net_device *dev)
3418{
3419
3420 ASSERT_RTNL();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003421 RCU_INIT_POINTER(dev->rx_handler, NULL);
Eric Dumazet00cfec32013-03-29 03:01:22 +00003422 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
3423 * section has a guarantee to see a non NULL rx_handler_data
3424 * as well.
3425 */
3426 synchronize_net();
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003427 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003428}
3429EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
3430
Mel Gormanb4b9e352012-07-31 16:44:26 -07003431/*
3432 * Limit the use of PFMEMALLOC reserves to those protocols that implement
3433 * the special handling of PFMEMALLOC skbs.
3434 */
3435static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
3436{
3437 switch (skb->protocol) {
3438 case __constant_htons(ETH_P_ARP):
3439 case __constant_htons(ETH_P_IP):
3440 case __constant_htons(ETH_P_IPV6):
3441 case __constant_htons(ETH_P_8021Q):
Patrick McHardy8ad227f2013-04-19 02:04:31 +00003442 case __constant_htons(ETH_P_8021AD):
Mel Gormanb4b9e352012-07-31 16:44:26 -07003443 return true;
3444 default:
3445 return false;
3446 }
3447}
3448
David S. Miller9754e292013-02-14 15:57:38 -05003449static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
3451 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003452 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07003453 struct net_device *orig_dev;
David S. Miller63d8ea72011-02-28 10:48:59 -08003454 struct net_device *null_or_dev;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00003455 bool deliver_exact = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 int ret = NET_RX_DROP;
Al Viro252e3342006-11-14 20:48:11 -08003457 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Eric Dumazet588f0332011-11-15 04:12:55 +00003459 net_timestamp_check(!netdev_tstamp_prequeue, skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07003460
Koki Sanagicf66ba52010-08-23 18:45:02 +09003461 trace_netif_receive_skb(skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -08003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /* if we've gotten here through NAPI, check netpoll */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003464 if (netpoll_receive_skb(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07003465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07003467 orig_dev = skb->dev;
Jiri Pirko1765a572011-02-12 06:48:36 +00003468
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07003469 skb_reset_network_header(skb);
Eric Dumazetfda55ec2013-01-07 09:28:21 +00003470 if (!skb_transport_header_was_set(skb))
3471 skb_reset_transport_header(skb);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00003472 skb_reset_mac_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 pt_prev = NULL;
3475
3476 rcu_read_lock();
3477
David S. Miller63d8ea72011-02-28 10:48:59 -08003478another_round:
David S. Millerb6858172012-07-23 16:27:54 -07003479 skb->skb_iif = skb->dev->ifindex;
David S. Miller63d8ea72011-02-28 10:48:59 -08003480
3481 __this_cpu_inc(softnet_data.processed);
3482
Patrick McHardy8ad227f2013-04-19 02:04:31 +00003483 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
3484 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
Jiri Pirkobcc6d472011-04-07 19:48:33 +00003485 skb = vlan_untag(skb);
3486 if (unlikely(!skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07003487 goto unlock;
Jiri Pirkobcc6d472011-04-07 19:48:33 +00003488 }
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490#ifdef CONFIG_NET_CLS_ACT
3491 if (skb->tc_verd & TC_NCLS) {
3492 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
3493 goto ncls;
3494 }
3495#endif
3496
David S. Miller9754e292013-02-14 15:57:38 -05003497 if (pfmemalloc)
Mel Gormanb4b9e352012-07-31 16:44:26 -07003498 goto skip_taps;
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 list_for_each_entry_rcu(ptype, &ptype_all, list) {
David S. Miller63d8ea72011-02-28 10:48:59 -08003501 if (!ptype->dev || ptype->dev == skb->dev) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003502 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07003503 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 pt_prev = ptype;
3505 }
3506 }
3507
Mel Gormanb4b9e352012-07-31 16:44:26 -07003508skip_taps:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509#ifdef CONFIG_NET_CLS_ACT
Herbert Xuf697c3e2007-10-14 00:38:47 -07003510 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
3511 if (!skb)
Mel Gormanb4b9e352012-07-31 16:44:26 -07003512 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513ncls:
3514#endif
3515
David S. Miller9754e292013-02-14 15:57:38 -05003516 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07003517 goto drop;
3518
John Fastabend24257172011-10-10 09:16:41 +00003519 if (vlan_tx_tag_present(skb)) {
3520 if (pt_prev) {
3521 ret = deliver_skb(skb, pt_prev, orig_dev);
3522 pt_prev = NULL;
3523 }
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00003524 if (vlan_do_receive(&skb))
John Fastabend24257172011-10-10 09:16:41 +00003525 goto another_round;
3526 else if (unlikely(!skb))
Mel Gormanb4b9e352012-07-31 16:44:26 -07003527 goto unlock;
John Fastabend24257172011-10-10 09:16:41 +00003528 }
3529
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00003530 rx_handler = rcu_dereference(skb->dev->rx_handler);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003531 if (rx_handler) {
3532 if (pt_prev) {
3533 ret = deliver_skb(skb, pt_prev, orig_dev);
3534 pt_prev = NULL;
3535 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +00003536 switch (rx_handler(&skb)) {
3537 case RX_HANDLER_CONSUMED:
Cristian Bercaru3bc1b1a2013-03-08 07:03:38 +00003538 ret = NET_RX_SUCCESS;
Mel Gormanb4b9e352012-07-31 16:44:26 -07003539 goto unlock;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00003540 case RX_HANDLER_ANOTHER:
David S. Miller63d8ea72011-02-28 10:48:59 -08003541 goto another_round;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00003542 case RX_HANDLER_EXACT:
3543 deliver_exact = true;
3544 case RX_HANDLER_PASS:
3545 break;
3546 default:
3547 BUG();
3548 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00003549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +00003551 if (vlan_tx_nonzero_tag_present(skb))
3552 skb->pkt_type = PACKET_OTHERHOST;
3553
David S. Miller63d8ea72011-02-28 10:48:59 -08003554 /* deliver only exact match when indicated */
Jiri Pirko8a4eb572011-03-12 03:14:39 +00003555 null_or_dev = deliver_exact ? skb->dev : NULL;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00003556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 type = skb->protocol;
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08003558 list_for_each_entry_rcu(ptype,
3559 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
David S. Miller63d8ea72011-02-28 10:48:59 -08003560 if (ptype->type == type &&
Jiri Pirkoe3f48d32011-02-28 20:26:31 +00003561 (ptype->dev == null_or_dev || ptype->dev == skb->dev ||
3562 ptype->dev == orig_dev)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003563 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07003564 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 pt_prev = ptype;
3566 }
3567 }
3568
3569 if (pt_prev) {
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00003570 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
Michael S. Tsirkin0e698bf2012-09-15 22:44:16 +00003571 goto drop;
Michael S. Tsirkin1080e512012-07-20 09:23:17 +00003572 else
3573 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 } else {
Mel Gormanb4b9e352012-07-31 16:44:26 -07003575drop:
Eric Dumazetcaf586e2010-09-30 21:06:55 +00003576 atomic_long_inc(&skb->dev->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 kfree_skb(skb);
3578 /* Jamal, now you will not able to escape explaining
3579 * me how you were going to use this. :-)
3580 */
3581 ret = NET_RX_DROP;
3582 }
3583
Mel Gormanb4b9e352012-07-31 16:44:26 -07003584unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 rcu_read_unlock();
Mel Gormanb4b9e352012-07-31 16:44:26 -07003586out:
David S. Miller9754e292013-02-14 15:57:38 -05003587 return ret;
3588}
3589
3590static int __netif_receive_skb(struct sk_buff *skb)
3591{
3592 int ret;
3593
3594 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
3595 unsigned long pflags = current->flags;
3596
3597 /*
3598 * PFMEMALLOC skbs are special, they should
3599 * - be delivered to SOCK_MEMALLOC sockets only
3600 * - stay away from userspace
3601 * - have bounded memory usage
3602 *
3603 * Use PF_MEMALLOC as this saves us from propagating the allocation
3604 * context down to all allocation sites.
3605 */
3606 current->flags |= PF_MEMALLOC;
3607 ret = __netif_receive_skb_core(skb, true);
3608 tsk_restore_flags(current, pflags, PF_MEMALLOC);
3609 } else
3610 ret = __netif_receive_skb_core(skb, false);
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return ret;
3613}
Tom Herbert0a9627f2010-03-16 08:03:29 +00003614
3615/**
3616 * netif_receive_skb - process receive buffer from network
3617 * @skb: buffer to process
3618 *
3619 * netif_receive_skb() is the main receive data processing function.
3620 * It always succeeds. The buffer may be dropped during processing
3621 * for congestion control or by the protocol layers.
3622 *
3623 * This function may only be called from softirq context and interrupts
3624 * should be enabled.
3625 *
3626 * Return values (usually ignored):
3627 * NET_RX_SUCCESS: no congestion
3628 * NET_RX_DROP: packet was dropped
3629 */
3630int netif_receive_skb(struct sk_buff *skb)
3631{
Eric Dumazet588f0332011-11-15 04:12:55 +00003632 net_timestamp_check(netdev_tstamp_prequeue, skb);
Eric Dumazet3b098e22010-05-15 23:57:10 -07003633
Richard Cochranc1f19b52010-07-17 08:49:36 +00003634 if (skb_defer_rx_timestamp(skb))
3635 return NET_RX_SUCCESS;
3636
Eric Dumazetdf334542010-03-24 19:13:54 +00003637#ifdef CONFIG_RPS
Ingo Molnarc5905af2012-02-24 08:31:31 +01003638 if (static_key_false(&rps_needed)) {
Eric Dumazet3b098e22010-05-15 23:57:10 -07003639 struct rps_dev_flow voidflow, *rflow = &voidflow;
3640 int cpu, ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00003641
Eric Dumazet3b098e22010-05-15 23:57:10 -07003642 rcu_read_lock();
Tom Herbert0a9627f2010-03-16 08:03:29 +00003643
Eric Dumazet3b098e22010-05-15 23:57:10 -07003644 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07003645
Eric Dumazet3b098e22010-05-15 23:57:10 -07003646 if (cpu >= 0) {
3647 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3648 rcu_read_unlock();
Eric Dumazetadc93002011-11-17 03:13:26 +00003649 return ret;
Eric Dumazet3b098e22010-05-15 23:57:10 -07003650 }
Eric Dumazetadc93002011-11-17 03:13:26 +00003651 rcu_read_unlock();
Tom Herbertfec5e652010-04-16 16:01:27 -07003652 }
Tom Herbert1e94d722010-03-18 17:45:44 -07003653#endif
Eric Dumazetadc93002011-11-17 03:13:26 +00003654 return __netif_receive_skb(skb);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003655}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003656EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Eric Dumazet88751272010-04-19 05:07:33 +00003658/* Network device is going away, flush any packets still pending
3659 * Called with irqs disabled.
3660 */
Changli Gao152102c2010-03-30 20:16:22 +00003661static void flush_backlog(void *arg)
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003662{
Changli Gao152102c2010-03-30 20:16:22 +00003663 struct net_device *dev = arg;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003664 struct softnet_data *sd = &__get_cpu_var(softnet_data);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003665 struct sk_buff *skb, *tmp;
3666
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003667 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07003668 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003669 if (skb->dev == dev) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003670 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003671 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003672 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003673 }
Changli Gao6e7676c2010-04-27 15:07:33 -07003674 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003675 rps_unlock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07003676
3677 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3678 if (skb->dev == dev) {
3679 __skb_unlink(skb, &sd->process_queue);
3680 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003681 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07003682 }
3683 }
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003684}
3685
Herbert Xud565b0a2008-12-15 23:38:52 -08003686static int napi_gro_complete(struct sk_buff *skb)
3687{
Vlad Yasevich22061d82012-11-15 08:49:11 +00003688 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08003689 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00003690 struct list_head *head = &offload_base;
Herbert Xud565b0a2008-12-15 23:38:52 -08003691 int err = -ENOENT;
3692
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003693 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
3694
Herbert Xufc59f9a2009-04-14 15:11:06 -07003695 if (NAPI_GRO_CB(skb)->count == 1) {
3696 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003697 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07003698 }
Herbert Xud565b0a2008-12-15 23:38:52 -08003699
3700 rcu_read_lock();
3701 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00003702 if (ptype->type != type || !ptype->callbacks.gro_complete)
Herbert Xud565b0a2008-12-15 23:38:52 -08003703 continue;
3704
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00003705 err = ptype->callbacks.gro_complete(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003706 break;
3707 }
3708 rcu_read_unlock();
3709
3710 if (err) {
3711 WARN_ON(&ptype->list == head);
3712 kfree_skb(skb);
3713 return NET_RX_SUCCESS;
3714 }
3715
3716out:
Herbert Xud565b0a2008-12-15 23:38:52 -08003717 return netif_receive_skb(skb);
3718}
3719
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00003720/* napi->gro_list contains packets ordered by age.
3721 * youngest packets at the head of it.
3722 * Complete skbs in reverse order to reduce latencies.
3723 */
3724void napi_gro_flush(struct napi_struct *napi, bool flush_old)
Herbert Xud565b0a2008-12-15 23:38:52 -08003725{
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00003726 struct sk_buff *skb, *prev = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08003727
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00003728 /* scan list and build reverse chain */
3729 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
3730 skb->prev = prev;
3731 prev = skb;
Herbert Xud565b0a2008-12-15 23:38:52 -08003732 }
3733
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00003734 for (skb = prev; skb; skb = prev) {
3735 skb->next = NULL;
3736
3737 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
3738 return;
3739
3740 prev = skb->prev;
3741 napi_gro_complete(skb);
3742 napi->gro_count--;
3743 }
3744
Herbert Xud565b0a2008-12-15 23:38:52 -08003745 napi->gro_list = NULL;
3746}
Eric Dumazet86cac582010-08-31 18:25:32 +00003747EXPORT_SYMBOL(napi_gro_flush);
Herbert Xud565b0a2008-12-15 23:38:52 -08003748
Eric Dumazet89c5fa32012-12-10 13:28:16 +00003749static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
3750{
3751 struct sk_buff *p;
3752 unsigned int maclen = skb->dev->hard_header_len;
3753
3754 for (p = napi->gro_list; p; p = p->next) {
3755 unsigned long diffs;
3756
3757 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
3758 diffs |= p->vlan_tci ^ skb->vlan_tci;
3759 if (maclen == ETH_HLEN)
3760 diffs |= compare_ether_header(skb_mac_header(p),
3761 skb_gro_mac_header(skb));
3762 else if (!diffs)
3763 diffs = memcmp(skb_mac_header(p),
3764 skb_gro_mac_header(skb),
3765 maclen);
3766 NAPI_GRO_CB(p)->same_flow = !diffs;
3767 NAPI_GRO_CB(p)->flush = 0;
3768 }
3769}
3770
Rami Rosenbb728822012-11-28 21:55:25 +00003771static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08003772{
3773 struct sk_buff **pp = NULL;
Vlad Yasevich22061d82012-11-15 08:49:11 +00003774 struct packet_offload *ptype;
Herbert Xud565b0a2008-12-15 23:38:52 -08003775 __be16 type = skb->protocol;
Vlad Yasevich22061d82012-11-15 08:49:11 +00003776 struct list_head *head = &offload_base;
Herbert Xu0da2afd52008-12-26 14:57:42 -08003777 int same_flow;
Ben Hutchings5b252f02009-10-29 07:17:09 +00003778 enum gro_result ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08003779
Jarek Poplawskice9e76c2010-08-05 01:19:11 +00003780 if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
Herbert Xud565b0a2008-12-15 23:38:52 -08003781 goto normal;
3782
David S. Miller21dc3302010-08-23 00:13:46 -07003783 if (skb_is_gso(skb) || skb_has_frag_list(skb))
Herbert Xuf17f5c92009-01-14 14:36:12 -08003784 goto normal;
3785
Eric Dumazet89c5fa32012-12-10 13:28:16 +00003786 gro_list_prepare(napi, skb);
3787
Herbert Xud565b0a2008-12-15 23:38:52 -08003788 rcu_read_lock();
3789 list_for_each_entry_rcu(ptype, head, list) {
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00003790 if (ptype->type != type || !ptype->callbacks.gro_receive)
Herbert Xud565b0a2008-12-15 23:38:52 -08003791 continue;
3792
Herbert Xu86911732009-01-29 14:19:50 +00003793 skb_set_network_header(skb, skb_gro_offset(skb));
Eric Dumazetefd94502013-02-14 17:31:48 +00003794 skb_reset_mac_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003795 NAPI_GRO_CB(skb)->same_flow = 0;
3796 NAPI_GRO_CB(skb)->flush = 0;
Herbert Xu5d38a072009-01-04 16:13:40 -08003797 NAPI_GRO_CB(skb)->free = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003798
Vlad Yasevichf191a1d2012-11-15 08:49:23 +00003799 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003800 break;
3801 }
3802 rcu_read_unlock();
3803
3804 if (&ptype->list == head)
3805 goto normal;
3806
Herbert Xu0da2afd52008-12-26 14:57:42 -08003807 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003808 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08003809
Herbert Xud565b0a2008-12-15 23:38:52 -08003810 if (pp) {
3811 struct sk_buff *nskb = *pp;
3812
3813 *pp = nskb->next;
3814 nskb->next = NULL;
3815 napi_gro_complete(nskb);
Herbert Xu4ae55442009-02-08 18:00:36 +00003816 napi->gro_count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08003817 }
3818
Herbert Xu0da2afd52008-12-26 14:57:42 -08003819 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08003820 goto ok;
3821
Herbert Xu4ae55442009-02-08 18:00:36 +00003822 if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
Herbert Xud565b0a2008-12-15 23:38:52 -08003823 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08003824
Herbert Xu4ae55442009-02-08 18:00:36 +00003825 napi->gro_count++;
Herbert Xud565b0a2008-12-15 23:38:52 -08003826 NAPI_GRO_CB(skb)->count = 1;
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00003827 NAPI_GRO_CB(skb)->age = jiffies;
Herbert Xu86911732009-01-29 14:19:50 +00003828 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003829 skb->next = napi->gro_list;
3830 napi->gro_list = skb;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003831 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08003832
Herbert Xuad0f9902009-02-01 01:24:55 -08003833pull:
Herbert Xucb189782009-05-26 18:50:31 +00003834 if (skb_headlen(skb) < skb_gro_offset(skb)) {
3835 int grow = skb_gro_offset(skb) - skb_headlen(skb);
3836
3837 BUG_ON(skb->end - skb->tail < grow);
3838
3839 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
3840
3841 skb->tail += grow;
3842 skb->data_len -= grow;
3843
3844 skb_shinfo(skb)->frags[0].page_offset += grow;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003845 skb_frag_size_sub(&skb_shinfo(skb)->frags[0], grow);
Herbert Xucb189782009-05-26 18:50:31 +00003846
Eric Dumazet9e903e02011-10-18 21:00:24 +00003847 if (unlikely(!skb_frag_size(&skb_shinfo(skb)->frags[0]))) {
Ian Campbellea2ab692011-08-22 23:44:58 +00003848 skb_frag_unref(skb, 0);
Herbert Xucb189782009-05-26 18:50:31 +00003849 memmove(skb_shinfo(skb)->frags,
3850 skb_shinfo(skb)->frags + 1,
Jarek Poplawskie5093ae2010-08-11 02:02:10 +00003851 --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
Herbert Xucb189782009-05-26 18:50:31 +00003852 }
Herbert Xuad0f9902009-02-01 01:24:55 -08003853 }
3854
Herbert Xud565b0a2008-12-15 23:38:52 -08003855ok:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003856 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08003857
3858normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08003859 ret = GRO_NORMAL;
3860 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08003861}
Herbert Xu96e93ea2009-01-06 10:49:34 -08003862
Herbert Xu96e93ea2009-01-06 10:49:34 -08003863
Rami Rosenbb728822012-11-28 21:55:25 +00003864static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08003865{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003866 switch (ret) {
3867 case GRO_NORMAL:
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003868 if (netif_receive_skb(skb))
3869 ret = GRO_DROP;
3870 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08003871
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003872 case GRO_DROP:
Herbert Xu5d38a072009-01-04 16:13:40 -08003873 kfree_skb(skb);
3874 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00003875
Eric Dumazetdaa86542012-04-19 07:07:40 +00003876 case GRO_MERGED_FREE:
Eric Dumazetd7e88832012-04-30 08:10:34 +00003877 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
3878 kmem_cache_free(skbuff_head_cache, skb);
3879 else
3880 __kfree_skb(skb);
Eric Dumazetdaa86542012-04-19 07:07:40 +00003881 break;
3882
Ben Hutchings5b252f02009-10-29 07:17:09 +00003883 case GRO_HELD:
3884 case GRO_MERGED:
3885 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08003886 }
3887
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003888 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003889}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003890
Eric Dumazetca07e432012-10-06 22:28:06 +00003891static void skb_gro_reset_offset(struct sk_buff *skb)
Herbert Xu78a478d2009-05-26 18:50:21 +00003892{
Eric Dumazetca07e432012-10-06 22:28:06 +00003893 const struct skb_shared_info *pinfo = skb_shinfo(skb);
3894 const skb_frag_t *frag0 = &pinfo->frags[0];
3895
Herbert Xu78a478d2009-05-26 18:50:21 +00003896 NAPI_GRO_CB(skb)->data_offset = 0;
3897 NAPI_GRO_CB(skb)->frag0 = NULL;
Herbert Xu74895942009-05-26 18:50:27 +00003898 NAPI_GRO_CB(skb)->frag0_len = 0;
Herbert Xu78a478d2009-05-26 18:50:21 +00003899
Simon Hormanced14f62013-05-28 20:34:25 +00003900 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
Eric Dumazetca07e432012-10-06 22:28:06 +00003901 pinfo->nr_frags &&
3902 !PageHighMem(skb_frag_page(frag0))) {
3903 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
3904 NAPI_GRO_CB(skb)->frag0_len = skb_frag_size(frag0);
Herbert Xu74895942009-05-26 18:50:27 +00003905 }
Herbert Xu78a478d2009-05-26 18:50:21 +00003906}
Herbert Xu78a478d2009-05-26 18:50:21 +00003907
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003908gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003909{
Herbert Xu86911732009-01-29 14:19:50 +00003910 skb_gro_reset_offset(skb);
3911
Eric Dumazet89c5fa32012-12-10 13:28:16 +00003912 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003913}
3914EXPORT_SYMBOL(napi_gro_receive);
3915
stephen hemmingerd0c2b0d2010-10-19 07:12:10 +00003916static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08003917{
Herbert Xu96e93ea2009-01-06 10:49:34 -08003918 __skb_pull(skb, skb_headlen(skb));
Eric Dumazet2a2a4592012-03-21 06:58:03 +00003919 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
3920 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
Jesse Gross3701e512010-10-20 13:56:06 +00003921 skb->vlan_tci = 0;
Herbert Xu66c46d72011-01-29 20:44:54 -08003922 skb->dev = napi->dev;
Andy Gospodarek6d152e22011-02-02 14:53:25 -08003923 skb->skb_iif = 0;
Herbert Xu96e93ea2009-01-06 10:49:34 -08003924
3925 napi->skb = skb;
3926}
Herbert Xu96e93ea2009-01-06 10:49:34 -08003927
Herbert Xu76620aa2009-04-16 02:02:07 -07003928struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08003929{
Herbert Xu5d38a072009-01-04 16:13:40 -08003930 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08003931
3932 if (!skb) {
Eric Dumazet89d71a62009-10-13 05:34:20 +00003933 skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
3934 if (skb)
3935 napi->skb = skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08003936 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08003937 return skb;
3938}
Herbert Xu76620aa2009-04-16 02:02:07 -07003939EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08003940
Rami Rosenbb728822012-11-28 21:55:25 +00003941static gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003942 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003943{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003944 switch (ret) {
3945 case GRO_NORMAL:
Herbert Xu86911732009-01-29 14:19:50 +00003946 case GRO_HELD:
Ajit Khapardee76b69c2010-02-16 20:25:43 +00003947 skb->protocol = eth_type_trans(skb, skb->dev);
Herbert Xu86911732009-01-29 14:19:50 +00003948
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003949 if (ret == GRO_HELD)
3950 skb_gro_pull(skb, -ETH_HLEN);
3951 else if (netif_receive_skb(skb))
3952 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00003953 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003954
3955 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003956 case GRO_MERGED_FREE:
3957 napi_reuse_skb(napi, skb);
3958 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00003959
3960 case GRO_MERGED:
3961 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003962 }
3963
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003964 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003965}
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003966
Eric Dumazet4adb9c42012-05-18 20:49:06 +00003967static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08003968{
Herbert Xu76620aa2009-04-16 02:02:07 -07003969 struct sk_buff *skb = napi->skb;
3970 struct ethhdr *eth;
Herbert Xua5b1cf22009-05-26 18:50:28 +00003971 unsigned int hlen;
3972 unsigned int off;
Herbert Xu76620aa2009-04-16 02:02:07 -07003973
3974 napi->skb = NULL;
3975
3976 skb_reset_mac_header(skb);
3977 skb_gro_reset_offset(skb);
3978
Herbert Xua5b1cf22009-05-26 18:50:28 +00003979 off = skb_gro_offset(skb);
3980 hlen = off + sizeof(*eth);
3981 eth = skb_gro_header_fast(skb, off);
3982 if (skb_gro_header_hard(skb, hlen)) {
3983 eth = skb_gro_header_slow(skb, hlen, off);
3984 if (unlikely(!eth)) {
3985 napi_reuse_skb(napi, skb);
3986 skb = NULL;
3987 goto out;
3988 }
Herbert Xu76620aa2009-04-16 02:02:07 -07003989 }
3990
3991 skb_gro_pull(skb, sizeof(*eth));
3992
3993 /*
3994 * This works because the only protocols we care about don't require
3995 * special handling. We'll fix it up properly at the end.
3996 */
3997 skb->protocol = eth->h_proto;
3998
3999out:
4000 return skb;
4001}
Herbert Xu76620aa2009-04-16 02:02:07 -07004002
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004003gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07004004{
4005 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08004006
4007 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07004008 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08004009
Eric Dumazet89c5fa32012-12-10 13:28:16 +00004010 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
Herbert Xu5d38a072009-01-04 16:13:40 -08004011}
4012EXPORT_SYMBOL(napi_gro_frags);
4013
Eric Dumazete326bed2010-04-22 00:22:45 -07004014/*
4015 * net_rps_action sends any pending IPI's for rps.
4016 * Note: called with local irq disabled, but exits with local irq enabled.
4017 */
4018static void net_rps_action_and_irq_enable(struct softnet_data *sd)
4019{
4020#ifdef CONFIG_RPS
4021 struct softnet_data *remsd = sd->rps_ipi_list;
4022
4023 if (remsd) {
4024 sd->rps_ipi_list = NULL;
4025
4026 local_irq_enable();
4027
4028 /* Send pending IPI's to kick RPS processing on remote cpus. */
4029 while (remsd) {
4030 struct softnet_data *next = remsd->rps_ipi_next;
4031
4032 if (cpu_online(remsd->cpu))
4033 __smp_call_function_single(remsd->cpu,
4034 &remsd->csd, 0);
4035 remsd = next;
4036 }
4037 } else
4038#endif
4039 local_irq_enable();
4040}
4041
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004042static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 int work = 0;
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004045 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Eric Dumazete326bed2010-04-22 00:22:45 -07004047#ifdef CONFIG_RPS
4048 /* Check if we have pending ipi, its better to send them now,
4049 * not waiting net_rx_action() end.
4050 */
4051 if (sd->rps_ipi_list) {
4052 local_irq_disable();
4053 net_rps_action_and_irq_enable(sd);
4054 }
4055#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004056 napi->weight = weight_p;
Changli Gao6e7676c2010-04-27 15:07:33 -07004057 local_irq_disable();
4058 while (work < quota) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 struct sk_buff *skb;
Changli Gao6e7676c2010-04-27 15:07:33 -07004060 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Changli Gao6e7676c2010-04-27 15:07:33 -07004062 while ((skb = __skb_dequeue(&sd->process_queue))) {
Eric Dumazete4008272010-04-05 15:42:39 -07004063 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07004064 __netif_receive_skb(skb);
Changli Gao6e7676c2010-04-27 15:07:33 -07004065 local_irq_disable();
Tom Herbert76cc8b12010-05-20 18:37:59 +00004066 input_queue_head_incr(sd);
4067 if (++work >= quota) {
4068 local_irq_enable();
4069 return work;
4070 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Changli Gao6e7676c2010-04-27 15:07:33 -07004073 rps_lock(sd);
4074 qlen = skb_queue_len(&sd->input_pkt_queue);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004075 if (qlen)
Changli Gao6e7676c2010-04-27 15:07:33 -07004076 skb_queue_splice_tail_init(&sd->input_pkt_queue,
4077 &sd->process_queue);
Tom Herbert76cc8b12010-05-20 18:37:59 +00004078
Changli Gao6e7676c2010-04-27 15:07:33 -07004079 if (qlen < quota - work) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004080 /*
4081 * Inline a custom version of __napi_complete().
4082 * only current cpu owns and manipulates this napi,
4083 * and NAPI_STATE_SCHED is the only possible flag set on backlog.
4084 * we can use a plain write instead of clear_bit(),
4085 * and we dont need an smp_mb() memory barrier.
4086 */
4087 list_del(&napi->poll_list);
4088 napi->state = 0;
4089
Changli Gao6e7676c2010-04-27 15:07:33 -07004090 quota = work + qlen;
4091 }
4092 rps_unlock(sd);
4093 }
4094 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004096 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004099/**
4100 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07004101 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004102 *
4103 * The entry's receive function will be scheduled to run
4104 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08004105void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004106{
4107 unsigned long flags;
4108
4109 local_irq_save(flags);
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07004110 ____napi_schedule(&__get_cpu_var(softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004111 local_irq_restore(flags);
4112}
4113EXPORT_SYMBOL(__napi_schedule);
4114
Herbert Xud565b0a2008-12-15 23:38:52 -08004115void __napi_complete(struct napi_struct *n)
4116{
4117 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
4118 BUG_ON(n->gro_list);
4119
4120 list_del(&n->poll_list);
4121 smp_mb__before_clear_bit();
4122 clear_bit(NAPI_STATE_SCHED, &n->state);
4123}
4124EXPORT_SYMBOL(__napi_complete);
4125
4126void napi_complete(struct napi_struct *n)
4127{
4128 unsigned long flags;
4129
4130 /*
4131 * don't let napi dequeue from the cpu poll list
4132 * just in case its running on a different cpu
4133 */
4134 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
4135 return;
4136
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004137 napi_gro_flush(n, false);
Herbert Xud565b0a2008-12-15 23:38:52 -08004138 local_irq_save(flags);
4139 __napi_complete(n);
4140 local_irq_restore(flags);
4141}
4142EXPORT_SYMBOL(napi_complete);
4143
Eliezer Tamiraf12fa62013-06-10 11:39:41 +03004144/* must be called under rcu_read_lock(), as we dont take a reference */
4145struct napi_struct *napi_by_id(unsigned int napi_id)
4146{
4147 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
4148 struct napi_struct *napi;
4149
4150 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
4151 if (napi->napi_id == napi_id)
4152 return napi;
4153
4154 return NULL;
4155}
4156EXPORT_SYMBOL_GPL(napi_by_id);
4157
4158void napi_hash_add(struct napi_struct *napi)
4159{
4160 if (!test_and_set_bit(NAPI_STATE_HASHED, &napi->state)) {
4161
4162 spin_lock(&napi_hash_lock);
4163
4164 /* 0 is not a valid id, we also skip an id that is taken
4165 * we expect both events to be extremely rare
4166 */
4167 napi->napi_id = 0;
4168 while (!napi->napi_id) {
4169 napi->napi_id = ++napi_gen_id;
4170 if (napi_by_id(napi->napi_id))
4171 napi->napi_id = 0;
4172 }
4173
4174 hlist_add_head_rcu(&napi->napi_hash_node,
4175 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
4176
4177 spin_unlock(&napi_hash_lock);
4178 }
4179}
4180EXPORT_SYMBOL_GPL(napi_hash_add);
4181
4182/* Warning : caller is responsible to make sure rcu grace period
4183 * is respected before freeing memory containing @napi
4184 */
4185void napi_hash_del(struct napi_struct *napi)
4186{
4187 spin_lock(&napi_hash_lock);
4188
4189 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state))
4190 hlist_del_rcu(&napi->napi_hash_node);
4191
4192 spin_unlock(&napi_hash_lock);
4193}
4194EXPORT_SYMBOL_GPL(napi_hash_del);
4195
Herbert Xud565b0a2008-12-15 23:38:52 -08004196void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
4197 int (*poll)(struct napi_struct *, int), int weight)
4198{
4199 INIT_LIST_HEAD(&napi->poll_list);
Herbert Xu4ae55442009-02-08 18:00:36 +00004200 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004201 napi->gro_list = NULL;
Herbert Xu5d38a072009-01-04 16:13:40 -08004202 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08004203 napi->poll = poll;
Eric Dumazet82dc3c62013-03-05 15:57:22 +00004204 if (weight > NAPI_POLL_WEIGHT)
4205 pr_err_once("netif_napi_add() called with weight %d on device %s\n",
4206 weight, dev->name);
Herbert Xud565b0a2008-12-15 23:38:52 -08004207 napi->weight = weight;
4208 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08004209 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08004210#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08004211 spin_lock_init(&napi->poll_lock);
4212 napi->poll_owner = -1;
4213#endif
4214 set_bit(NAPI_STATE_SCHED, &napi->state);
4215}
4216EXPORT_SYMBOL(netif_napi_add);
4217
4218void netif_napi_del(struct napi_struct *napi)
4219{
4220 struct sk_buff *skb, *next;
4221
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08004222 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07004223 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08004224
4225 for (skb = napi->gro_list; skb; skb = next) {
4226 next = skb->next;
4227 skb->next = NULL;
4228 kfree_skb(skb);
4229 }
4230
4231 napi->gro_list = NULL;
Herbert Xu4ae55442009-02-08 18:00:36 +00004232 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08004233}
4234EXPORT_SYMBOL(netif_napi_del);
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236static void net_rx_action(struct softirq_action *h)
4237{
Eric Dumazete326bed2010-04-22 00:22:45 -07004238 struct softnet_data *sd = &__get_cpu_var(softnet_data);
Stephen Hemminger24f8b232008-11-03 17:14:38 -08004239 unsigned long time_limit = jiffies + 2;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07004240 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07004241 void *have;
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 local_irq_disable();
4244
Eric Dumazete326bed2010-04-22 00:22:45 -07004245 while (!list_empty(&sd->poll_list)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004246 struct napi_struct *n;
4247 int work, weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004249 /* If softirq window is exhuasted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08004250 * Allow this to run for 2 jiffies since which will allow
4251 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004252 */
Eric Dumazetd1f41b62013-03-05 07:15:13 +00004253 if (unlikely(budget <= 0 || time_after_eq(jiffies, time_limit)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 goto softnet_break;
4255
4256 local_irq_enable();
4257
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004258 /* Even though interrupts have been re-enabled, this
4259 * access is safe because interrupts can only add new
4260 * entries to the tail of this list, and only ->poll()
4261 * calls can remove this head entry from the list.
4262 */
Eric Dumazete326bed2010-04-22 00:22:45 -07004263 n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004265 have = netpoll_poll_lock(n);
4266
4267 weight = n->weight;
4268
David S. Miller0a7606c2007-10-29 21:28:47 -07004269 /* This NAPI_STATE_SCHED test is for avoiding a race
4270 * with netpoll's poll_napi(). Only the entity which
4271 * obtains the lock and sees NAPI_STATE_SCHED set will
4272 * actually make the ->poll() call. Therefore we avoid
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004273 * accidentally calling ->poll() when NAPI is not scheduled.
David S. Miller0a7606c2007-10-29 21:28:47 -07004274 */
4275 work = 0;
Neil Horman4ea7e382009-05-21 07:36:08 +00004276 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
David S. Miller0a7606c2007-10-29 21:28:47 -07004277 work = n->poll(n, weight);
Neil Horman4ea7e382009-05-21 07:36:08 +00004278 trace_napi_poll(n);
4279 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004280
4281 WARN_ON_ONCE(work > weight);
4282
4283 budget -= work;
4284
4285 local_irq_disable();
4286
4287 /* Drivers must not modify the NAPI state if they
4288 * consume the entire weight. In such cases this code
4289 * still "owns" the NAPI instance and therefore can
4290 * move the instance around on the list at-will.
4291 */
David S. Millerfed17f32008-01-07 21:00:40 -08004292 if (unlikely(work == weight)) {
Herbert Xuff780cd2009-06-26 19:27:04 -07004293 if (unlikely(napi_disable_pending(n))) {
4294 local_irq_enable();
4295 napi_complete(n);
4296 local_irq_disable();
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004297 } else {
4298 if (n->gro_list) {
4299 /* flush too old packets
4300 * If HZ < 1000, flush all packets.
4301 */
4302 local_irq_enable();
4303 napi_gro_flush(n, HZ >= 1000);
4304 local_irq_disable();
4305 }
Eric Dumazete326bed2010-04-22 00:22:45 -07004306 list_move_tail(&n->poll_list, &sd->poll_list);
Eric Dumazet2e71a6f2012-10-06 08:08:49 +00004307 }
David S. Millerfed17f32008-01-07 21:00:40 -08004308 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004309
4310 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
4312out:
Eric Dumazete326bed2010-04-22 00:22:45 -07004313 net_rps_action_and_irq_enable(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00004314
Chris Leechdb217332006-06-17 21:24:58 -07004315#ifdef CONFIG_NET_DMA
4316 /*
4317 * There may not be any more sk_buffs coming right now, so push
4318 * any pending DMA copies to hardware
4319 */
Dan Williams2ba05622009-01-06 11:38:14 -07004320 dma_issue_pending_all();
Chris Leechdb217332006-06-17 21:24:58 -07004321#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return;
4324
4325softnet_break:
Changli Gaodee42872010-05-02 05:42:16 +00004326 sd->time_squeeze++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4328 goto out;
4329}
4330
Jiri Pirko9ff162a2013-01-03 22:48:49 +00004331struct netdev_upper {
4332 struct net_device *dev;
4333 bool master;
4334 struct list_head list;
4335 struct rcu_head rcu;
4336 struct list_head search_list;
4337};
4338
4339static void __append_search_uppers(struct list_head *search_list,
4340 struct net_device *dev)
4341{
4342 struct netdev_upper *upper;
4343
4344 list_for_each_entry(upper, &dev->upper_dev_list, list) {
4345 /* check if this upper is not already in search list */
4346 if (list_empty(&upper->search_list))
4347 list_add_tail(&upper->search_list, search_list);
4348 }
4349}
4350
4351static bool __netdev_search_upper_dev(struct net_device *dev,
4352 struct net_device *upper_dev)
4353{
4354 LIST_HEAD(search_list);
4355 struct netdev_upper *upper;
4356 struct netdev_upper *tmp;
4357 bool ret = false;
4358
4359 __append_search_uppers(&search_list, dev);
4360 list_for_each_entry(upper, &search_list, search_list) {
4361 if (upper->dev == upper_dev) {
4362 ret = true;
4363 break;
4364 }
4365 __append_search_uppers(&search_list, upper->dev);
4366 }
4367 list_for_each_entry_safe(upper, tmp, &search_list, search_list)
4368 INIT_LIST_HEAD(&upper->search_list);
4369 return ret;
4370}
4371
4372static struct netdev_upper *__netdev_find_upper(struct net_device *dev,
4373 struct net_device *upper_dev)
4374{
4375 struct netdev_upper *upper;
4376
4377 list_for_each_entry(upper, &dev->upper_dev_list, list) {
4378 if (upper->dev == upper_dev)
4379 return upper;
4380 }
4381 return NULL;
4382}
4383
4384/**
4385 * netdev_has_upper_dev - Check if device is linked to an upper device
4386 * @dev: device
4387 * @upper_dev: upper device to check
4388 *
4389 * Find out if a device is linked to specified upper device and return true
4390 * in case it is. Note that this checks only immediate upper device,
4391 * not through a complete stack of devices. The caller must hold the RTNL lock.
4392 */
4393bool netdev_has_upper_dev(struct net_device *dev,
4394 struct net_device *upper_dev)
4395{
4396 ASSERT_RTNL();
4397
4398 return __netdev_find_upper(dev, upper_dev);
4399}
4400EXPORT_SYMBOL(netdev_has_upper_dev);
4401
4402/**
4403 * netdev_has_any_upper_dev - Check if device is linked to some device
4404 * @dev: device
4405 *
4406 * Find out if a device is linked to an upper device and return true in case
4407 * it is. The caller must hold the RTNL lock.
4408 */
4409bool netdev_has_any_upper_dev(struct net_device *dev)
4410{
4411 ASSERT_RTNL();
4412
4413 return !list_empty(&dev->upper_dev_list);
4414}
4415EXPORT_SYMBOL(netdev_has_any_upper_dev);
4416
4417/**
4418 * netdev_master_upper_dev_get - Get master upper device
4419 * @dev: device
4420 *
4421 * Find a master upper device and return pointer to it or NULL in case
4422 * it's not there. The caller must hold the RTNL lock.
4423 */
4424struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4425{
4426 struct netdev_upper *upper;
4427
4428 ASSERT_RTNL();
4429
4430 if (list_empty(&dev->upper_dev_list))
4431 return NULL;
4432
4433 upper = list_first_entry(&dev->upper_dev_list,
4434 struct netdev_upper, list);
4435 if (likely(upper->master))
4436 return upper->dev;
4437 return NULL;
4438}
4439EXPORT_SYMBOL(netdev_master_upper_dev_get);
4440
4441/**
4442 * netdev_master_upper_dev_get_rcu - Get master upper device
4443 * @dev: device
4444 *
4445 * Find a master upper device and return pointer to it or NULL in case
4446 * it's not there. The caller must hold the RCU read lock.
4447 */
4448struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
4449{
4450 struct netdev_upper *upper;
4451
4452 upper = list_first_or_null_rcu(&dev->upper_dev_list,
4453 struct netdev_upper, list);
4454 if (upper && likely(upper->master))
4455 return upper->dev;
4456 return NULL;
4457}
4458EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
4459
4460static int __netdev_upper_dev_link(struct net_device *dev,
4461 struct net_device *upper_dev, bool master)
4462{
4463 struct netdev_upper *upper;
4464
4465 ASSERT_RTNL();
4466
4467 if (dev == upper_dev)
4468 return -EBUSY;
4469
4470 /* To prevent loops, check if dev is not upper device to upper_dev. */
4471 if (__netdev_search_upper_dev(upper_dev, dev))
4472 return -EBUSY;
4473
4474 if (__netdev_find_upper(dev, upper_dev))
4475 return -EEXIST;
4476
4477 if (master && netdev_master_upper_dev_get(dev))
4478 return -EBUSY;
4479
4480 upper = kmalloc(sizeof(*upper), GFP_KERNEL);
4481 if (!upper)
4482 return -ENOMEM;
4483
4484 upper->dev = upper_dev;
4485 upper->master = master;
4486 INIT_LIST_HEAD(&upper->search_list);
4487
4488 /* Ensure that master upper link is always the first item in list. */
4489 if (master)
4490 list_add_rcu(&upper->list, &dev->upper_dev_list);
4491 else
4492 list_add_tail_rcu(&upper->list, &dev->upper_dev_list);
4493 dev_hold(upper_dev);
Jiri Pirko42e52bf2013-05-25 04:12:10 +00004494 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00004495 return 0;
4496}
4497
4498/**
4499 * netdev_upper_dev_link - Add a link to the upper device
4500 * @dev: device
4501 * @upper_dev: new upper device
4502 *
4503 * Adds a link to device which is upper to this one. The caller must hold
4504 * the RTNL lock. On a failure a negative errno code is returned.
4505 * On success the reference counts are adjusted and the function
4506 * returns zero.
4507 */
4508int netdev_upper_dev_link(struct net_device *dev,
4509 struct net_device *upper_dev)
4510{
4511 return __netdev_upper_dev_link(dev, upper_dev, false);
4512}
4513EXPORT_SYMBOL(netdev_upper_dev_link);
4514
4515/**
4516 * netdev_master_upper_dev_link - Add a master link to the upper device
4517 * @dev: device
4518 * @upper_dev: new upper device
4519 *
4520 * Adds a link to device which is upper to this one. In this case, only
4521 * one master upper device can be linked, although other non-master devices
4522 * might be linked as well. The caller must hold the RTNL lock.
4523 * On a failure a negative errno code is returned. On success the reference
4524 * counts are adjusted and the function returns zero.
4525 */
4526int netdev_master_upper_dev_link(struct net_device *dev,
4527 struct net_device *upper_dev)
4528{
4529 return __netdev_upper_dev_link(dev, upper_dev, true);
4530}
4531EXPORT_SYMBOL(netdev_master_upper_dev_link);
4532
4533/**
4534 * netdev_upper_dev_unlink - Removes a link to upper device
4535 * @dev: device
4536 * @upper_dev: new upper device
4537 *
4538 * Removes a link to device which is upper to this one. The caller must hold
4539 * the RTNL lock.
4540 */
4541void netdev_upper_dev_unlink(struct net_device *dev,
4542 struct net_device *upper_dev)
4543{
4544 struct netdev_upper *upper;
4545
4546 ASSERT_RTNL();
4547
4548 upper = __netdev_find_upper(dev, upper_dev);
4549 if (!upper)
4550 return;
4551 list_del_rcu(&upper->list);
4552 dev_put(upper_dev);
4553 kfree_rcu(upper, rcu);
Jiri Pirko42e52bf2013-05-25 04:12:10 +00004554 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00004555}
4556EXPORT_SYMBOL(netdev_upper_dev_unlink);
4557
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004558static void dev_change_rx_flags(struct net_device *dev, int flags)
4559{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004560 const struct net_device_ops *ops = dev->netdev_ops;
4561
4562 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
4563 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004564}
4565
Wang Chendad9b332008-06-18 01:48:28 -07004566static int __dev_set_promiscuity(struct net_device *dev, int inc)
Patrick McHardy4417da62007-06-27 01:28:10 -07004567{
Eric Dumazetb536db92011-11-30 21:42:26 +00004568 unsigned int old_flags = dev->flags;
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06004569 kuid_t uid;
4570 kgid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07004571
Patrick McHardy24023452007-07-14 18:51:31 -07004572 ASSERT_RTNL();
4573
Wang Chendad9b332008-06-18 01:48:28 -07004574 dev->flags |= IFF_PROMISC;
4575 dev->promiscuity += inc;
4576 if (dev->promiscuity == 0) {
4577 /*
4578 * Avoid overflow.
4579 * If inc causes overflow, untouch promisc and return error.
4580 */
4581 if (inc < 0)
4582 dev->flags &= ~IFF_PROMISC;
4583 else {
4584 dev->promiscuity -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00004585 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
4586 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07004587 return -EOVERFLOW;
4588 }
4589 }
Patrick McHardy4417da62007-06-27 01:28:10 -07004590 if (dev->flags != old_flags) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00004591 pr_info("device %s %s promiscuous mode\n",
4592 dev->name,
4593 dev->flags & IFF_PROMISC ? "entered" : "left");
David Howells8192b0c2008-11-14 10:39:10 +11004594 if (audit_enabled) {
4595 current_uid_gid(&uid, &gid);
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05004596 audit_log(current->audit_context, GFP_ATOMIC,
4597 AUDIT_ANOM_PROMISCUOUS,
4598 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
4599 dev->name, (dev->flags & IFF_PROMISC),
4600 (old_flags & IFF_PROMISC),
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07004601 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric W. Biedermand04a48b2012-05-23 17:01:57 -06004602 from_kuid(&init_user_ns, uid),
4603 from_kgid(&init_user_ns, gid),
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05004604 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11004605 }
Patrick McHardy24023452007-07-14 18:51:31 -07004606
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004607 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07004608 }
Wang Chendad9b332008-06-18 01:48:28 -07004609 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07004610}
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612/**
4613 * dev_set_promiscuity - update promiscuity count on a device
4614 * @dev: device
4615 * @inc: modifier
4616 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07004617 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 * remains above zero the interface remains promiscuous. Once it hits zero
4619 * the device reverts back to normal filtering operation. A negative inc
4620 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07004621 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
Wang Chendad9b332008-06-18 01:48:28 -07004623int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Eric Dumazetb536db92011-11-30 21:42:26 +00004625 unsigned int old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07004626 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Wang Chendad9b332008-06-18 01:48:28 -07004628 err = __dev_set_promiscuity(dev, inc);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07004629 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07004630 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07004631 if (dev->flags != old_flags)
4632 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07004633 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004635EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637/**
4638 * dev_set_allmulti - update allmulti count on a device
4639 * @dev: device
4640 * @inc: modifier
4641 *
4642 * Add or remove reception of all multicast frames to a device. While the
4643 * count in the device remains above zero the interface remains listening
4644 * to all interfaces. Once it hits zero the device reverts back to normal
4645 * filtering operation. A negative @inc value is used to drop the counter
4646 * when releasing a resource needing all multicasts.
Wang Chendad9b332008-06-18 01:48:28 -07004647 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 */
4649
Wang Chendad9b332008-06-18 01:48:28 -07004650int dev_set_allmulti(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Eric Dumazetb536db92011-11-30 21:42:26 +00004652 unsigned int old_flags = dev->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
Patrick McHardy24023452007-07-14 18:51:31 -07004654 ASSERT_RTNL();
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07004657 dev->allmulti += inc;
4658 if (dev->allmulti == 0) {
4659 /*
4660 * Avoid overflow.
4661 * If inc causes overflow, untouch allmulti and return error.
4662 */
4663 if (inc < 0)
4664 dev->flags &= ~IFF_ALLMULTI;
4665 else {
4666 dev->allmulti -= inc;
Joe Perches7b6cd1c2012-02-01 10:54:43 +00004667 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
4668 dev->name);
Wang Chendad9b332008-06-18 01:48:28 -07004669 return -EOVERFLOW;
4670 }
4671 }
Patrick McHardy24023452007-07-14 18:51:31 -07004672 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004673 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07004674 dev_set_rx_mode(dev);
Patrick McHardy24023452007-07-14 18:51:31 -07004675 }
Wang Chendad9b332008-06-18 01:48:28 -07004676 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07004677}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004678EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07004679
4680/*
4681 * Upload unicast and multicast address lists to device and
4682 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08004683 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07004684 * are present.
4685 */
4686void __dev_set_rx_mode(struct net_device *dev)
4687{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004688 const struct net_device_ops *ops = dev->netdev_ops;
4689
Patrick McHardy4417da62007-06-27 01:28:10 -07004690 /* dev_open will call this function so the list will stay sane. */
4691 if (!(dev->flags&IFF_UP))
4692 return;
4693
4694 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09004695 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07004696
Jiri Pirko01789342011-08-16 06:29:00 +00004697 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
Patrick McHardy4417da62007-06-27 01:28:10 -07004698 /* Unicast addresses changes may only happen under the rtnl,
4699 * therefore calling __dev_set_promiscuity here is safe.
4700 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08004701 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Patrick McHardy4417da62007-06-27 01:28:10 -07004702 __dev_set_promiscuity(dev, 1);
Joe Perches2d348d12011-07-25 16:17:35 -07004703 dev->uc_promisc = true;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08004704 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Patrick McHardy4417da62007-06-27 01:28:10 -07004705 __dev_set_promiscuity(dev, -1);
Joe Perches2d348d12011-07-25 16:17:35 -07004706 dev->uc_promisc = false;
Patrick McHardy4417da62007-06-27 01:28:10 -07004707 }
Patrick McHardy4417da62007-06-27 01:28:10 -07004708 }
Jiri Pirko01789342011-08-16 06:29:00 +00004709
4710 if (ops->ndo_set_rx_mode)
4711 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07004712}
4713
4714void dev_set_rx_mode(struct net_device *dev)
4715{
David S. Millerb9e40852008-07-15 00:15:08 -07004716 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07004717 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07004718 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004721/**
4722 * dev_get_flags - get flags reported to userspace
4723 * @dev: device
4724 *
4725 * Get the combination of flag bits exported through APIs to userspace.
4726 */
Eric Dumazet95c96172012-04-15 05:58:06 +00004727unsigned int dev_get_flags(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
Eric Dumazet95c96172012-04-15 05:58:06 +00004729 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
4731 flags = (dev->flags & ~(IFF_PROMISC |
4732 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08004733 IFF_RUNNING |
4734 IFF_LOWER_UP |
4735 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 (dev->gflags & (IFF_PROMISC |
4737 IFF_ALLMULTI));
4738
Stefan Rompfb00055a2006-03-20 17:09:11 -08004739 if (netif_running(dev)) {
4740 if (netif_oper_up(dev))
4741 flags |= IFF_RUNNING;
4742 if (netif_carrier_ok(dev))
4743 flags |= IFF_LOWER_UP;
4744 if (netif_dormant(dev))
4745 flags |= IFF_DORMANT;
4746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 return flags;
4749}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004750EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Patrick McHardybd380812010-02-26 06:34:53 +00004752int __dev_change_flags(struct net_device *dev, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
Eric Dumazetb536db92011-11-30 21:42:26 +00004754 unsigned int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00004755 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Patrick McHardy24023452007-07-14 18:51:31 -07004757 ASSERT_RTNL();
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /*
4760 * Set the flags on our device.
4761 */
4762
4763 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
4764 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
4765 IFF_AUTOMEDIA)) |
4766 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
4767 IFF_ALLMULTI));
4768
4769 /*
4770 * Load in the correct multicast list now the flags have changed.
4771 */
4772
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004773 if ((old_flags ^ flags) & IFF_MULTICAST)
4774 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07004775
Patrick McHardy4417da62007-06-27 01:28:10 -07004776 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
4778 /*
4779 * Have we downed the interface. We handle IFF_UP ourselves
4780 * according to user attempts to set it, rather than blindly
4781 * setting it.
4782 */
4783
4784 ret = 0;
4785 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
Patrick McHardybd380812010-02-26 06:34:53 +00004786 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 if (!ret)
Patrick McHardy4417da62007-06-27 01:28:10 -07004789 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004793 int inc = (flags & IFF_PROMISC) ? 1 : -1;
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 dev->gflags ^= IFF_PROMISC;
4796 dev_set_promiscuity(dev, inc);
4797 }
4798
4799 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
4800 is important. Some (broken) drivers set IFF_PROMISC, when
4801 IFF_ALLMULTI is requested not asking us and not reporting.
4802 */
4803 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004804 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 dev->gflags ^= IFF_ALLMULTI;
4807 dev_set_allmulti(dev, inc);
4808 }
4809
Patrick McHardybd380812010-02-26 06:34:53 +00004810 return ret;
4811}
4812
4813void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
4814{
4815 unsigned int changes = dev->flags ^ old_flags;
4816
4817 if (changes & IFF_UP) {
4818 if (dev->flags & IFF_UP)
4819 call_netdevice_notifiers(NETDEV_UP, dev);
4820 else
4821 call_netdevice_notifiers(NETDEV_DOWN, dev);
4822 }
4823
4824 if (dev->flags & IFF_UP &&
Jiri Pirkobe9efd32013-05-28 01:30:22 +00004825 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
4826 struct netdev_notifier_change_info change_info;
4827
4828 change_info.flags_changed = changes;
4829 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
4830 &change_info.info);
4831 }
Patrick McHardybd380812010-02-26 06:34:53 +00004832}
4833
4834/**
4835 * dev_change_flags - change device settings
4836 * @dev: device
4837 * @flags: device state flags
4838 *
4839 * Change settings on device based state flags. The flags are
4840 * in the userspace exported format.
4841 */
Eric Dumazetb536db92011-11-30 21:42:26 +00004842int dev_change_flags(struct net_device *dev, unsigned int flags)
Patrick McHardybd380812010-02-26 06:34:53 +00004843{
Eric Dumazetb536db92011-11-30 21:42:26 +00004844 int ret;
4845 unsigned int changes, old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00004846
4847 ret = __dev_change_flags(dev, flags);
4848 if (ret < 0)
4849 return ret;
4850
4851 changes = old_flags ^ dev->flags;
Thomas Graf7c355f52007-06-05 16:03:03 -07004852 if (changes)
4853 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Patrick McHardybd380812010-02-26 06:34:53 +00004855 __dev_notify_flags(dev, old_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return ret;
4857}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004858EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004860/**
4861 * dev_set_mtu - Change maximum transfer unit
4862 * @dev: device
4863 * @new_mtu: new transfer unit
4864 *
4865 * Change the maximum transfer size of the network device.
4866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867int dev_set_mtu(struct net_device *dev, int new_mtu)
4868{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004869 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 int err;
4871
4872 if (new_mtu == dev->mtu)
4873 return 0;
4874
4875 /* MTU must be positive. */
4876 if (new_mtu < 0)
4877 return -EINVAL;
4878
4879 if (!netif_device_present(dev))
4880 return -ENODEV;
4881
4882 err = 0;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004883 if (ops->ndo_change_mtu)
4884 err = ops->ndo_change_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 else
4886 dev->mtu = new_mtu;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004887
Jiri Pirkoe3d8fab2012-12-03 01:16:32 +00004888 if (!err)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07004889 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return err;
4891}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004892EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004894/**
Vlad Dogarucbda10f2011-01-13 23:38:30 +00004895 * dev_set_group - Change group this device belongs to
4896 * @dev: device
4897 * @new_group: group this device should belong to
4898 */
4899void dev_set_group(struct net_device *dev, int new_group)
4900{
4901 dev->group = new_group;
4902}
4903EXPORT_SYMBOL(dev_set_group);
4904
4905/**
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004906 * dev_set_mac_address - Change Media Access Control Address
4907 * @dev: device
4908 * @sa: new address
4909 *
4910 * Change the hardware (MAC) address of the device
4911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
4913{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004914 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 int err;
4916
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004917 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 return -EOPNOTSUPP;
4919 if (sa->sa_family != dev->type)
4920 return -EINVAL;
4921 if (!netif_device_present(dev))
4922 return -ENODEV;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004923 err = ops->ndo_set_mac_address(dev, sa);
Jiri Pirkof6521512013-01-01 03:30:14 +00004924 if (err)
4925 return err;
Jiri Pirkofbdeca22013-01-01 03:30:16 +00004926 dev->addr_assign_type = NET_ADDR_SET;
Jiri Pirkof6521512013-01-01 03:30:14 +00004927 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04004928 add_device_randomness(dev->dev_addr, dev->addr_len);
Jiri Pirkof6521512013-01-01 03:30:14 +00004929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004931EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Jiri Pirko4bf84c32012-12-27 23:49:37 +00004933/**
4934 * dev_change_carrier - Change device carrier
4935 * @dev: device
Randy Dunlap691b3b72013-03-04 12:32:43 +00004936 * @new_carrier: new value
Jiri Pirko4bf84c32012-12-27 23:49:37 +00004937 *
4938 * Change device carrier
4939 */
4940int dev_change_carrier(struct net_device *dev, bool new_carrier)
4941{
4942 const struct net_device_ops *ops = dev->netdev_ops;
4943
4944 if (!ops->ndo_change_carrier)
4945 return -EOPNOTSUPP;
4946 if (!netif_device_present(dev))
4947 return -ENODEV;
4948 return ops->ndo_change_carrier(dev, new_carrier);
4949}
4950EXPORT_SYMBOL(dev_change_carrier);
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952/**
4953 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07004954 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 *
4956 * Returns a suitable unique value for a new device interface
4957 * number. The caller must hold the rtnl semaphore or the
4958 * dev_base_lock to be sure it remains unique.
4959 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07004960static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00004962 int ifindex = net->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 for (;;) {
4964 if (++ifindex <= 0)
4965 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004966 if (!__dev_get_by_index(net, ifindex))
Pavel Emelyanovaa79e662012-08-08 21:53:19 +00004967 return net->ifindex = ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 }
4969}
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08004972static LIST_HEAD(net_todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Stephen Hemminger6f05f622007-03-08 20:46:03 -08004974static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 list_add_tail(&dev->todo_list, &net_todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977}
4978
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004979static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004980{
Krishna Kumare93737b2009-12-08 22:26:02 +00004981 struct net_device *dev, *tmp;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004982
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004983 BUG_ON(dev_boot_phase);
4984 ASSERT_RTNL();
4985
Krishna Kumare93737b2009-12-08 22:26:02 +00004986 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004987 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00004988 * for initialization unwind. Remove those
4989 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004990 */
4991 if (dev->reg_state == NETREG_UNINITIALIZED) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00004992 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
4993 dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004994
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004995 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00004996 list_del(&dev->unreg_list);
4997 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004998 }
Eric Dumazet449f4542011-05-19 12:24:16 +00004999 dev->dismantle = true;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005000 BUG_ON(dev->reg_state != NETREG_REGISTERED);
Octavian Purdila44345722010-12-13 12:44:07 +00005001 }
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005002
Octavian Purdila44345722010-12-13 12:44:07 +00005003 /* If device is running, close it first. */
5004 dev_close_many(head);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005005
Octavian Purdila44345722010-12-13 12:44:07 +00005006 list_for_each_entry(dev, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005007 /* And unlink it from device chain. */
5008 unlist_netdevice(dev);
5009
5010 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005011 }
5012
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005013 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005014
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005015 list_for_each_entry(dev, head, unreg_list) {
5016 /* Shutdown queueing discipline. */
5017 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005018
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005019
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005020 /* Notify protocols, that we are about to destroy
5021 this device. They should clean all the things.
5022 */
5023 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
5024
Patrick McHardya2835762010-02-26 06:34:51 +00005025 if (!dev->rtnl_link_ops ||
5026 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5027 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
5028
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005029 /*
5030 * Flush the unicast and multicast chains
5031 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00005032 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005033 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005034
5035 if (dev->netdev_ops->ndo_uninit)
5036 dev->netdev_ops->ndo_uninit(dev);
5037
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005038 /* Notifier chain MUST detach us all upper devices. */
5039 WARN_ON(netdev_has_any_upper_dev(dev));
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005040
5041 /* Remove entries from kobject tree */
5042 netdev_unregister_kobject(dev);
Alexander Duyck024e9672013-01-10 08:57:46 +00005043#ifdef CONFIG_XPS
5044 /* Remove XPS queueing entries */
5045 netif_reset_xps_queues_gt(dev, 0);
5046#endif
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005047 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005048
Eric W. Biederman850a5452011-10-13 22:25:23 +00005049 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005050
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00005051 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005052 dev_put(dev);
5053}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005054
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005055static void rollback_registered(struct net_device *dev)
5056{
5057 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005058
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005059 list_add(&dev->unreg_list, &single);
5060 rollback_registered_many(&single);
Eric Dumazetceaaec92011-02-17 22:59:19 +00005061 list_del(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005062}
5063
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005064static netdev_features_t netdev_fix_features(struct net_device *dev,
5065 netdev_features_t features)
Herbert Xub63365a2008-10-23 01:11:29 -07005066{
Michał Mirosław57422dc2011-01-22 12:14:12 +00005067 /* Fix illegal checksum combinations */
5068 if ((features & NETIF_F_HW_CSUM) &&
5069 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04005070 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
Michał Mirosław57422dc2011-01-22 12:14:12 +00005071 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5072 }
5073
Herbert Xub63365a2008-10-23 01:11:29 -07005074 /* TSO requires that SG is present as well. */
Ben Hutchingsea2d3682011-04-12 14:38:37 +00005075 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04005076 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
Ben Hutchingsea2d3682011-04-12 14:38:37 +00005077 features &= ~NETIF_F_ALL_TSO;
Herbert Xub63365a2008-10-23 01:11:29 -07005078 }
5079
Pravin B Shelarec5f0612013-03-07 09:28:01 +00005080 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
5081 !(features & NETIF_F_IP_CSUM)) {
5082 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
5083 features &= ~NETIF_F_TSO;
5084 features &= ~NETIF_F_TSO_ECN;
5085 }
5086
5087 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
5088 !(features & NETIF_F_IPV6_CSUM)) {
5089 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
5090 features &= ~NETIF_F_TSO6;
5091 }
5092
Ben Hutchings31d8b9e2011-04-12 14:47:15 +00005093 /* TSO ECN requires that TSO is present as well. */
5094 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
5095 features &= ~NETIF_F_TSO_ECN;
5096
Michał Mirosław212b5732011-02-15 16:59:16 +00005097 /* Software GSO depends on SG. */
5098 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04005099 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
Michał Mirosław212b5732011-02-15 16:59:16 +00005100 features &= ~NETIF_F_GSO;
5101 }
5102
Michał Mirosławacd11302011-01-24 15:45:15 -08005103 /* UFO needs SG and checksumming */
Herbert Xub63365a2008-10-23 01:11:29 -07005104 if (features & NETIF_F_UFO) {
Michał Mirosław79032642010-11-30 06:38:00 +00005105 /* maybe split UFO into V4 and V6? */
5106 if (!((features & NETIF_F_GEN_CSUM) ||
5107 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
5108 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04005109 netdev_dbg(dev,
Michał Mirosławacd11302011-01-24 15:45:15 -08005110 "Dropping NETIF_F_UFO since no checksum offload features.\n");
Herbert Xub63365a2008-10-23 01:11:29 -07005111 features &= ~NETIF_F_UFO;
5112 }
5113
5114 if (!(features & NETIF_F_SG)) {
Michał Mirosław6f404e42011-05-16 15:14:21 -04005115 netdev_dbg(dev,
Michał Mirosławacd11302011-01-24 15:45:15 -08005116 "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
Herbert Xub63365a2008-10-23 01:11:29 -07005117 features &= ~NETIF_F_UFO;
5118 }
5119 }
5120
5121 return features;
5122}
Herbert Xub63365a2008-10-23 01:11:29 -07005123
Michał Mirosław6cb6a272011-04-02 22:48:47 -07005124int __netdev_update_features(struct net_device *dev)
Michał Mirosław5455c692011-02-15 16:59:17 +00005125{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005126 netdev_features_t features;
Michał Mirosław5455c692011-02-15 16:59:17 +00005127 int err = 0;
5128
Michał Mirosław87267482011-04-12 09:56:38 +00005129 ASSERT_RTNL();
5130
Michał Mirosław5455c692011-02-15 16:59:17 +00005131 features = netdev_get_wanted_features(dev);
5132
5133 if (dev->netdev_ops->ndo_fix_features)
5134 features = dev->netdev_ops->ndo_fix_features(dev, features);
5135
5136 /* driver might be less strict about feature dependencies */
5137 features = netdev_fix_features(dev, features);
5138
5139 if (dev->features == features)
Michał Mirosław6cb6a272011-04-02 22:48:47 -07005140 return 0;
Michał Mirosław5455c692011-02-15 16:59:17 +00005141
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005142 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
5143 &dev->features, &features);
Michał Mirosław5455c692011-02-15 16:59:17 +00005144
5145 if (dev->netdev_ops->ndo_set_features)
5146 err = dev->netdev_ops->ndo_set_features(dev, features);
5147
Michał Mirosław6cb6a272011-04-02 22:48:47 -07005148 if (unlikely(err < 0)) {
Michał Mirosław5455c692011-02-15 16:59:17 +00005149 netdev_err(dev,
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005150 "set_features() failed (%d); wanted %pNF, left %pNF\n",
5151 err, &features, &dev->features);
Michał Mirosław6cb6a272011-04-02 22:48:47 -07005152 return -1;
5153 }
5154
5155 if (!err)
5156 dev->features = features;
5157
5158 return 1;
5159}
5160
Michał Mirosławafe12cc2011-05-07 03:22:17 +00005161/**
5162 * netdev_update_features - recalculate device features
5163 * @dev: the device to check
5164 *
5165 * Recalculate dev->features set and send notifications if it
5166 * has changed. Should be called after driver or hardware dependent
5167 * conditions might have changed that influence the features.
5168 */
Michał Mirosław6cb6a272011-04-02 22:48:47 -07005169void netdev_update_features(struct net_device *dev)
5170{
5171 if (__netdev_update_features(dev))
5172 netdev_features_change(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +00005173}
5174EXPORT_SYMBOL(netdev_update_features);
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176/**
Michał Mirosławafe12cc2011-05-07 03:22:17 +00005177 * netdev_change_features - recalculate device features
5178 * @dev: the device to check
5179 *
5180 * Recalculate dev->features set and send notifications even
5181 * if they have not changed. Should be called instead of
5182 * netdev_update_features() if also dev->vlan_features might
5183 * have changed to allow the changes to be propagated to stacked
5184 * VLAN devices.
5185 */
5186void netdev_change_features(struct net_device *dev)
5187{
5188 __netdev_update_features(dev);
5189 netdev_features_change(dev);
5190}
5191EXPORT_SYMBOL(netdev_change_features);
5192
5193/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08005194 * netif_stacked_transfer_operstate - transfer operstate
5195 * @rootdev: the root or lower level device to transfer state from
5196 * @dev: the device to transfer operstate to
5197 *
5198 * Transfer operational state from root to device. This is normally
5199 * called when a stacking relationship exists between the root
5200 * device and the device(a leaf device).
5201 */
5202void netif_stacked_transfer_operstate(const struct net_device *rootdev,
5203 struct net_device *dev)
5204{
5205 if (rootdev->operstate == IF_OPER_DORMANT)
5206 netif_dormant_on(dev);
5207 else
5208 netif_dormant_off(dev);
5209
5210 if (netif_carrier_ok(rootdev)) {
5211 if (!netif_carrier_ok(dev))
5212 netif_carrier_on(dev);
5213 } else {
5214 if (netif_carrier_ok(dev))
5215 netif_carrier_off(dev);
5216 }
5217}
5218EXPORT_SYMBOL(netif_stacked_transfer_operstate);
5219
Tom Herbertbf264142010-11-26 08:36:09 +00005220#ifdef CONFIG_RPS
Eric Dumazet1b4bf462010-09-23 17:26:35 +00005221static int netif_alloc_rx_queues(struct net_device *dev)
5222{
Eric Dumazet1b4bf462010-09-23 17:26:35 +00005223 unsigned int i, count = dev->num_rx_queues;
Tom Herbertbd25fa72010-10-18 18:00:16 +00005224 struct netdev_rx_queue *rx;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00005225
Tom Herbertbd25fa72010-10-18 18:00:16 +00005226 BUG_ON(count < 1);
Eric Dumazet1b4bf462010-09-23 17:26:35 +00005227
Tom Herbertbd25fa72010-10-18 18:00:16 +00005228 rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
Joe Perches62b59422013-02-04 16:48:16 +00005229 if (!rx)
Tom Herbertbd25fa72010-10-18 18:00:16 +00005230 return -ENOMEM;
Joe Perches62b59422013-02-04 16:48:16 +00005231
Tom Herbertbd25fa72010-10-18 18:00:16 +00005232 dev->_rx = rx;
5233
Tom Herbertbd25fa72010-10-18 18:00:16 +00005234 for (i = 0; i < count; i++)
Tom Herbertfe822242010-11-09 10:47:38 +00005235 rx[i].dev = dev;
Eric Dumazet1b4bf462010-09-23 17:26:35 +00005236 return 0;
5237}
Tom Herbertbf264142010-11-26 08:36:09 +00005238#endif
Eric Dumazet1b4bf462010-09-23 17:26:35 +00005239
Changli Gaoaa942102010-12-04 02:31:41 +00005240static void netdev_init_one_queue(struct net_device *dev,
5241 struct netdev_queue *queue, void *_unused)
5242{
5243 /* Initialize queue lock */
5244 spin_lock_init(&queue->_xmit_lock);
5245 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
5246 queue->xmit_lock_owner = -1;
Changli Gaob236da62010-12-14 03:09:15 +00005247 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
Changli Gaoaa942102010-12-04 02:31:41 +00005248 queue->dev = dev;
Tom Herbert114cf582011-11-28 16:33:09 +00005249#ifdef CONFIG_BQL
5250 dql_init(&queue->dql, HZ);
5251#endif
Changli Gaoaa942102010-12-04 02:31:41 +00005252}
5253
Eric Dumazet60877a32013-06-20 01:15:51 -07005254static void netif_free_tx_queues(struct net_device *dev)
5255{
5256 if (is_vmalloc_addr(dev->_tx))
5257 vfree(dev->_tx);
5258 else
5259 kfree(dev->_tx);
5260}
5261
Tom Herberte6484932010-10-18 18:04:39 +00005262static int netif_alloc_netdev_queues(struct net_device *dev)
5263{
5264 unsigned int count = dev->num_tx_queues;
5265 struct netdev_queue *tx;
Eric Dumazet60877a32013-06-20 01:15:51 -07005266 size_t sz = count * sizeof(*tx);
Tom Herberte6484932010-10-18 18:04:39 +00005267
Eric Dumazet60877a32013-06-20 01:15:51 -07005268 BUG_ON(count < 1 || count > 0xffff);
Tom Herberte6484932010-10-18 18:04:39 +00005269
Eric Dumazet60877a32013-06-20 01:15:51 -07005270 tx = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
5271 if (!tx) {
5272 tx = vzalloc(sz);
5273 if (!tx)
5274 return -ENOMEM;
5275 }
Tom Herberte6484932010-10-18 18:04:39 +00005276 dev->_tx = tx;
Tom Herbert1d24eb42010-11-21 13:17:27 +00005277
Tom Herberte6484932010-10-18 18:04:39 +00005278 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
5279 spin_lock_init(&dev->tx_global_lock);
Changli Gaoaa942102010-12-04 02:31:41 +00005280
5281 return 0;
Tom Herberte6484932010-10-18 18:04:39 +00005282}
5283
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08005284/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 * register_netdevice - register a network device
5286 * @dev: device to register
5287 *
5288 * Take a completed network device structure and add it to the kernel
5289 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
5290 * chain. 0 is returned on success. A negative errno code is returned
5291 * on a failure to set up the device, or if the name is a duplicate.
5292 *
5293 * Callers must hold the rtnl semaphore. You may want
5294 * register_netdev() instead of this.
5295 *
5296 * BUGS:
5297 * The locking appears insufficient to guarantee two parallel registers
5298 * will not get the same name.
5299 */
5300
5301int register_netdevice(struct net_device *dev)
5302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08005304 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 BUG_ON(dev_boot_phase);
5307 ASSERT_RTNL();
5308
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005309 might_sleep();
5310
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 /* When net_device's are persistent, this will be fatal. */
5312 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08005313 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
David S. Millerf1f28aa2008-07-15 00:08:33 -07005315 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07005316 netdev_set_addr_lockdep_class(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 dev->iflink = -1;
5319
Gao feng828de4f2012-09-13 20:58:27 +00005320 ret = dev_get_valid_name(net, dev, dev->name);
Peter Pan(潘卫平)0696c3a2011-05-12 15:46:56 +00005321 if (ret < 0)
5322 goto out;
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08005325 if (dev->netdev_ops->ndo_init) {
5326 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 if (ret) {
5328 if (ret > 0)
5329 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08005330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 }
5332 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005333
Patrick McHardyf6469682013-04-19 02:04:27 +00005334 if (((dev->hw_features | dev->features) &
5335 NETIF_F_HW_VLAN_CTAG_FILTER) &&
Michał Mirosławd2ed2732013-01-29 15:14:16 +00005336 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
5337 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
5338 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
5339 ret = -EINVAL;
5340 goto err_uninit;
5341 }
5342
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00005343 ret = -EBUSY;
5344 if (!dev->ifindex)
5345 dev->ifindex = dev_new_index(net);
5346 else if (__dev_get_by_index(net, dev->ifindex))
5347 goto err_uninit;
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 if (dev->iflink == -1)
5350 dev->iflink = dev->ifindex;
5351
Michał Mirosław5455c692011-02-15 16:59:17 +00005352 /* Transfer changeable features to wanted_features and enable
5353 * software offloads (GSO and GRO).
5354 */
5355 dev->hw_features |= NETIF_F_SOFT_FEATURES;
Michał Mirosław14d12322011-02-22 16:52:28 +00005356 dev->features |= NETIF_F_SOFT_FEATURES;
5357 dev->wanted_features = dev->features & dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07005359 /* Turn on no cache copy if HW is doing checksum */
Michał Mirosław34324dc2011-11-15 15:29:55 +00005360 if (!(dev->flags & IFF_LOOPBACK)) {
5361 dev->hw_features |= NETIF_F_NOCACHE_COPY;
5362 if (dev->features & NETIF_F_ALL_CSUM) {
5363 dev->wanted_features |= NETIF_F_NOCACHE_COPY;
5364 dev->features |= NETIF_F_NOCACHE_COPY;
5365 }
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07005366 }
5367
Michał Mirosław1180e7d2011-07-14 14:41:11 -07005368 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
Brandon Philips16c3ea72010-09-15 09:24:24 +00005369 */
Michał Mirosław1180e7d2011-07-14 14:41:11 -07005370 dev->vlan_features |= NETIF_F_HIGHDMA;
Brandon Philips16c3ea72010-09-15 09:24:24 +00005371
Pravin B Shelaree579672013-03-07 09:28:08 +00005372 /* Make NETIF_F_SG inheritable to tunnel devices.
5373 */
5374 dev->hw_enc_features |= NETIF_F_SG;
5375
Simon Horman0d89d202013-05-23 21:02:52 +00005376 /* Make NETIF_F_SG inheritable to MPLS.
5377 */
5378 dev->mpls_features |= NETIF_F_SG;
5379
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00005380 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
5381 ret = notifier_to_errno(ret);
5382 if (ret)
5383 goto err_uninit;
5384
Eric W. Biederman8b41d182007-09-26 22:02:53 -07005385 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005386 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005387 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005388 dev->reg_state = NETREG_REGISTERED;
5389
Michał Mirosław6cb6a272011-04-02 22:48:47 -07005390 __netdev_update_features(dev);
Michał Mirosław8e9b59b2011-02-22 16:52:28 +00005391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 /*
5393 * Default initial state at registry is that the
5394 * device is present.
5395 */
5396
5397 set_bit(__LINK_STATE_PRESENT, &dev->state);
5398
Ben Hutchings8f4cccb2012-08-20 22:16:51 +01005399 linkwatch_init_dev(dev);
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005403 list_netdevice(dev);
Theodore Ts'o7bf23572012-07-04 21:23:25 -04005404 add_device_randomness(dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Jiri Pirko948b3372013-01-08 01:38:25 +00005406 /* If the device has permanent device address, driver should
5407 * set dev_addr and also addr_assign_type should be set to
5408 * NET_ADDR_PERM (default value).
5409 */
5410 if (dev->addr_assign_type == NET_ADDR_PERM)
5411 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
5412
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07005414 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07005415 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005416 if (ret) {
5417 rollback_registered(dev);
5418 dev->reg_state = NETREG_UNREGISTERED;
5419 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00005420 /*
5421 * Prevent userspace races by waiting until the network
5422 * device is fully setup before sending notifications.
5423 */
Patrick McHardya2835762010-02-26 06:34:51 +00005424 if (!dev->rtnl_link_ops ||
5425 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5426 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428out:
5429 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005430
5431err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08005432 if (dev->netdev_ops->ndo_uninit)
5433 dev->netdev_ops->ndo_uninit(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005436EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
5438/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08005439 * init_dummy_netdev - init a dummy network device for NAPI
5440 * @dev: device to init
5441 *
5442 * This takes a network device structure and initialize the minimum
5443 * amount of fields so it can be used to schedule NAPI polls without
5444 * registering a full blown interface. This is to be used by drivers
5445 * that need to tie several hardware interfaces to a single NAPI
5446 * poll scheduler due to HW limitations.
5447 */
5448int init_dummy_netdev(struct net_device *dev)
5449{
5450 /* Clear everything. Note we don't initialize spinlocks
5451 * are they aren't supposed to be taken by any of the
5452 * NAPI code and this dummy netdev is supposed to be
5453 * only ever used for NAPI polls
5454 */
5455 memset(dev, 0, sizeof(struct net_device));
5456
5457 /* make sure we BUG if trying to hit standard
5458 * register/unregister code path
5459 */
5460 dev->reg_state = NETREG_DUMMY;
5461
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08005462 /* NAPI wants this */
5463 INIT_LIST_HEAD(&dev->napi_list);
5464
5465 /* a dummy interface is started by default */
5466 set_bit(__LINK_STATE_PRESENT, &dev->state);
5467 set_bit(__LINK_STATE_START, &dev->state);
5468
Eric Dumazet29b44332010-10-11 10:22:12 +00005469 /* Note : We dont allocate pcpu_refcnt for dummy devices,
5470 * because users of this 'device' dont need to change
5471 * its refcount.
5472 */
5473
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08005474 return 0;
5475}
5476EXPORT_SYMBOL_GPL(init_dummy_netdev);
5477
5478
5479/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 * register_netdev - register a network device
5481 * @dev: device to register
5482 *
5483 * Take a completed network device structure and add it to the kernel
5484 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
5485 * chain. 0 is returned on success. A negative errno code is returned
5486 * on a failure to set up the device, or if the name is a duplicate.
5487 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07005488 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 * and expands the device name if you passed a format string to
5490 * alloc_netdev.
5491 */
5492int register_netdev(struct net_device *dev)
5493{
5494 int err;
5495
5496 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 err = register_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 rtnl_unlock();
5499 return err;
5500}
5501EXPORT_SYMBOL(register_netdev);
5502
Eric Dumazet29b44332010-10-11 10:22:12 +00005503int netdev_refcnt_read(const struct net_device *dev)
5504{
5505 int i, refcnt = 0;
5506
5507 for_each_possible_cpu(i)
5508 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
5509 return refcnt;
5510}
5511EXPORT_SYMBOL(netdev_refcnt_read);
5512
Ben Hutchings2c530402012-07-10 10:55:09 +00005513/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 * netdev_wait_allrefs - wait until all references are gone.
Randy Dunlap3de7a372012-08-18 14:36:44 +00005515 * @dev: target net_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 *
5517 * This is called when unregistering network devices.
5518 *
5519 * Any protocol or device that holds a reference should register
5520 * for netdevice notification, and cleanup and put back the
5521 * reference if they receive an UNREGISTER event.
5522 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005523 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 */
5525static void netdev_wait_allrefs(struct net_device *dev)
5526{
5527 unsigned long rebroadcast_time, warning_time;
Eric Dumazet29b44332010-10-11 10:22:12 +00005528 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Eric Dumazete014deb2009-11-17 05:59:21 +00005530 linkwatch_forget_dev(dev);
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 rebroadcast_time = warning_time = jiffies;
Eric Dumazet29b44332010-10-11 10:22:12 +00005533 refcnt = netdev_refcnt_read(dev);
5534
5535 while (refcnt != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08005537 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07005540 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
Eric Dumazet748e2d92012-08-22 21:50:59 +00005542 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00005543 rcu_barrier();
Eric Dumazet748e2d92012-08-22 21:50:59 +00005544 rtnl_lock();
5545
Eric Dumazet0115e8e2012-08-22 17:19:46 +00005546 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
5548 &dev->state)) {
5549 /* We must not have linkwatch events
5550 * pending on unregister. If this
5551 * happens, we simply run the queue
5552 * unscheduled, resulting in a noop
5553 * for this device.
5554 */
5555 linkwatch_run_queue();
5556 }
5557
Stephen Hemminger6756ae42006-03-20 22:23:58 -08005558 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 rebroadcast_time = jiffies;
5561 }
5562
5563 msleep(250);
5564
Eric Dumazet29b44332010-10-11 10:22:12 +00005565 refcnt = netdev_refcnt_read(dev);
5566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 if (time_after(jiffies, warning_time + 10 * HZ)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00005568 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
5569 dev->name, refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 warning_time = jiffies;
5571 }
5572 }
5573}
5574
5575/* The sequence is:
5576 *
5577 * rtnl_lock();
5578 * ...
5579 * register_netdevice(x1);
5580 * register_netdevice(x2);
5581 * ...
5582 * unregister_netdevice(y1);
5583 * unregister_netdevice(y2);
5584 * ...
5585 * rtnl_unlock();
5586 * free_netdev(y1);
5587 * free_netdev(y2);
5588 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07005589 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005591 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 * without deadlocking with linkwatch via keventd.
5593 * 2) Since we run with the RTNL semaphore not held, we can sleep
5594 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07005595 *
5596 * We must not return until all unregister events added during
5597 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599void netdev_run_todo(void)
5600{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07005601 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07005604 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07005605
5606 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07005607
Eric Dumazet0115e8e2012-08-22 17:19:46 +00005608
5609 /* Wait for rcu callbacks to finish before next phase */
Eric W. Biederman850a5452011-10-13 22:25:23 +00005610 if (!list_empty(&list))
5611 rcu_barrier();
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 while (!list_empty(&list)) {
5614 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00005615 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 list_del(&dev->todo_list);
5617
Eric Dumazet748e2d92012-08-22 21:50:59 +00005618 rtnl_lock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00005619 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Eric Dumazet748e2d92012-08-22 21:50:59 +00005620 __rtnl_unlock();
Eric Dumazet0115e8e2012-08-22 17:19:46 +00005621
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005622 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00005623 pr_err("network todo '%s' but state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005625 dump_stack();
5626 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005628
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005629 dev->reg_state = NETREG_UNREGISTERED;
5630
Changli Gao152102c2010-03-30 20:16:22 +00005631 on_each_cpu(flush_backlog, dev, 1);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005632
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005633 netdev_wait_allrefs(dev);
5634
5635 /* paranoia */
Eric Dumazet29b44332010-10-11 10:22:12 +00005636 BUG_ON(netdev_refcnt_read(dev));
Eric Dumazet33d480c2011-08-11 19:30:52 +00005637 WARN_ON(rcu_access_pointer(dev->ip_ptr));
5638 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
Ilpo Järvinen547b7922008-07-25 21:43:18 -07005639 WARN_ON(dev->dn_ptr);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005640
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005641 if (dev->destructor)
5642 dev->destructor(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07005643
5644 /* Free network device */
5645 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647}
5648
Ben Hutchings3cfde792010-07-09 09:11:52 +00005649/* Convert net_device_stats to rtnl_link_stats64. They have the same
5650 * fields in the same order, with only the type differing.
5651 */
Eric Dumazet77a1abf2012-03-05 04:50:09 +00005652void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
5653 const struct net_device_stats *netdev_stats)
Ben Hutchings3cfde792010-07-09 09:11:52 +00005654{
5655#if BITS_PER_LONG == 64
Eric Dumazet77a1abf2012-03-05 04:50:09 +00005656 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
5657 memcpy(stats64, netdev_stats, sizeof(*stats64));
Ben Hutchings3cfde792010-07-09 09:11:52 +00005658#else
5659 size_t i, n = sizeof(*stats64) / sizeof(u64);
5660 const unsigned long *src = (const unsigned long *)netdev_stats;
5661 u64 *dst = (u64 *)stats64;
5662
5663 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
5664 sizeof(*stats64) / sizeof(u64));
5665 for (i = 0; i < n; i++)
5666 dst[i] = src[i];
5667#endif
5668}
Eric Dumazet77a1abf2012-03-05 04:50:09 +00005669EXPORT_SYMBOL(netdev_stats_to_stats64);
Ben Hutchings3cfde792010-07-09 09:11:52 +00005670
Eric Dumazetd83345a2009-11-16 03:36:51 +00005671/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005672 * dev_get_stats - get network device statistics
5673 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07005674 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005675 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00005676 * Get network statistics from device. Return @storage.
5677 * The device driver may provide its own method by setting
5678 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
5679 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005680 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00005681struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
5682 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00005683{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005684 const struct net_device_ops *ops = dev->netdev_ops;
5685
Eric Dumazet28172732010-07-07 14:58:56 -07005686 if (ops->ndo_get_stats64) {
5687 memset(storage, 0, sizeof(*storage));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00005688 ops->ndo_get_stats64(dev, storage);
5689 } else if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00005690 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazetcaf586e2010-09-30 21:06:55 +00005691 } else {
5692 netdev_stats_to_stats64(storage, &dev->stats);
Eric Dumazet28172732010-07-07 14:58:56 -07005693 }
Eric Dumazetcaf586e2010-09-30 21:06:55 +00005694 storage->rx_dropped += atomic_long_read(&dev->rx_dropped);
Eric Dumazet28172732010-07-07 14:58:56 -07005695 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07005696}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005697EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07005698
Eric Dumazet24824a02010-10-02 06:11:55 +00005699struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
David S. Millerdc2b4842008-07-08 17:18:23 -07005700{
Eric Dumazet24824a02010-10-02 06:11:55 +00005701 struct netdev_queue *queue = dev_ingress_queue(dev);
David S. Millerdc2b4842008-07-08 17:18:23 -07005702
Eric Dumazet24824a02010-10-02 06:11:55 +00005703#ifdef CONFIG_NET_CLS_ACT
5704 if (queue)
5705 return queue;
5706 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
5707 if (!queue)
5708 return NULL;
5709 netdev_init_one_queue(dev, queue, NULL);
Eric Dumazet24824a02010-10-02 06:11:55 +00005710 queue->qdisc = &noop_qdisc;
5711 queue->qdisc_sleeping = &noop_qdisc;
5712 rcu_assign_pointer(dev->ingress_queue, queue);
5713#endif
5714 return queue;
David S. Millerbb949fb2008-07-08 16:55:56 -07005715}
5716
Eric Dumazet2c60db02012-09-16 09:17:26 +00005717static const struct ethtool_ops default_ethtool_ops;
5718
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +00005719void netdev_set_default_ethtool_ops(struct net_device *dev,
5720 const struct ethtool_ops *ops)
5721{
5722 if (dev->ethtool_ops == &default_ethtool_ops)
5723 dev->ethtool_ops = ops;
5724}
5725EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
5726
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727/**
Tom Herbert36909ea2011-01-09 19:36:31 +00005728 * alloc_netdev_mqs - allocate network device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 * @sizeof_priv: size of private data to allocate space for
5730 * @name: device name format string
5731 * @setup: callback to initialize device
Tom Herbert36909ea2011-01-09 19:36:31 +00005732 * @txqs: the number of TX subqueues to allocate
5733 * @rxqs: the number of RX subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 *
5735 * Allocates a struct net_device with private data area for driver use
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07005736 * and performs basic initialization. Also allocates subquue structs
Tom Herbert36909ea2011-01-09 19:36:31 +00005737 * for each queue on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 */
Tom Herbert36909ea2011-01-09 19:36:31 +00005739struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
5740 void (*setup)(struct net_device *),
5741 unsigned int txqs, unsigned int rxqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 struct net_device *dev;
Stephen Hemminger79439862008-07-21 13:28:44 -07005744 size_t alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005745 struct net_device *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07005747 BUG_ON(strlen(name) >= sizeof(dev->name));
5748
Tom Herbert36909ea2011-01-09 19:36:31 +00005749 if (txqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00005750 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
Tom Herbert55513fb2010-10-18 17:55:58 +00005751 return NULL;
5752 }
5753
Tom Herbert36909ea2011-01-09 19:36:31 +00005754#ifdef CONFIG_RPS
5755 if (rxqs < 1) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00005756 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
Tom Herbert36909ea2011-01-09 19:36:31 +00005757 return NULL;
5758 }
5759#endif
5760
David S. Millerfd2ea0a2008-07-17 01:56:23 -07005761 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07005762 if (sizeof_priv) {
5763 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005764 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07005765 alloc_size += sizeof_priv;
5766 }
5767 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005768 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07005770 p = kzalloc(alloc_size, GFP_KERNEL);
Joe Perches62b59422013-02-04 16:48:16 +00005771 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005774 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005776
Eric Dumazet29b44332010-10-11 10:22:12 +00005777 dev->pcpu_refcnt = alloc_percpu(int);
5778 if (!dev->pcpu_refcnt)
Tom Herberte6484932010-10-18 18:04:39 +00005779 goto free_p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 if (dev_addr_init(dev))
Eric Dumazet29b44332010-10-11 10:22:12 +00005782 goto free_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
Jiri Pirko22bedad32010-04-01 21:22:57 +00005784 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00005785 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00005786
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005787 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07005789 dev->gso_max_size = GSO_MAX_SIZE;
Ben Hutchings30b678d2012-07-30 15:57:00 +00005790 dev->gso_max_segs = GSO_MAX_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
Herbert Xud565b0a2008-12-15 23:38:52 -08005792 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00005793 INIT_LIST_HEAD(&dev->unreg_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00005794 INIT_LIST_HEAD(&dev->link_watch_list);
Jiri Pirko9ff162a2013-01-03 22:48:49 +00005795 INIT_LIST_HEAD(&dev->upper_dev_list);
Eric Dumazet93f154b2009-05-18 22:19:19 -07005796 dev->priv_flags = IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 setup(dev);
David S. Miller8d3bdbd2011-02-08 15:02:50 -08005798
5799 dev->num_tx_queues = txqs;
5800 dev->real_num_tx_queues = txqs;
5801 if (netif_alloc_netdev_queues(dev))
5802 goto free_all;
5803
5804#ifdef CONFIG_RPS
5805 dev->num_rx_queues = rxqs;
5806 dev->real_num_rx_queues = rxqs;
5807 if (netif_alloc_rx_queues(dev))
5808 goto free_all;
5809#endif
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 strcpy(dev->name, name);
Vlad Dogarucbda10f2011-01-13 23:38:30 +00005812 dev->group = INIT_NETDEV_GROUP;
Eric Dumazet2c60db02012-09-16 09:17:26 +00005813 if (!dev->ethtool_ops)
5814 dev->ethtool_ops = &default_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005816
David S. Miller8d3bdbd2011-02-08 15:02:50 -08005817free_all:
5818 free_netdev(dev);
5819 return NULL;
5820
Eric Dumazet29b44332010-10-11 10:22:12 +00005821free_pcpu:
5822 free_percpu(dev->pcpu_refcnt);
Eric Dumazet60877a32013-06-20 01:15:51 -07005823 netif_free_tx_queues(dev);
Tom Herbertfe822242010-11-09 10:47:38 +00005824#ifdef CONFIG_RPS
5825 kfree(dev->_rx);
5826#endif
5827
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005828free_p:
5829 kfree(p);
5830 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831}
Tom Herbert36909ea2011-01-09 19:36:31 +00005832EXPORT_SYMBOL(alloc_netdev_mqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834/**
5835 * free_netdev - free network device
5836 * @dev: device
5837 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005838 * This function does the last stage of destroying an allocated device
5839 * interface. The reference to the device object is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 * If this is the last reference then it will be freed.
5841 */
5842void free_netdev(struct net_device *dev)
5843{
Herbert Xud565b0a2008-12-15 23:38:52 -08005844 struct napi_struct *p, *n;
5845
Denis V. Lunevf3005d72008-04-16 02:02:18 -07005846 release_net(dev_net(dev));
5847
Eric Dumazet60877a32013-06-20 01:15:51 -07005848 netif_free_tx_queues(dev);
Tom Herbertfe822242010-11-09 10:47:38 +00005849#ifdef CONFIG_RPS
5850 kfree(dev->_rx);
5851#endif
David S. Millere8a04642008-07-17 00:34:19 -07005852
Eric Dumazet33d480c2011-08-11 19:30:52 +00005853 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
Eric Dumazet24824a02010-10-02 06:11:55 +00005854
Jiri Pirkof001fde2009-05-05 02:48:28 +00005855 /* Flush device addresses */
5856 dev_addr_flush(dev);
5857
Herbert Xud565b0a2008-12-15 23:38:52 -08005858 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
5859 netif_napi_del(p);
5860
Eric Dumazet29b44332010-10-11 10:22:12 +00005861 free_percpu(dev->pcpu_refcnt);
5862 dev->pcpu_refcnt = NULL;
5863
Stephen Hemminger3041a062006-05-26 13:25:24 -07005864 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 if (dev->reg_state == NETREG_UNINITIALIZED) {
5866 kfree((char *)dev - dev->padded);
5867 return;
5868 }
5869
5870 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
5871 dev->reg_state = NETREG_RELEASED;
5872
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07005873 /* will free via device release */
5874 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005876EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005877
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07005878/**
5879 * synchronize_net - Synchronize with packet receive processing
5880 *
5881 * Wait for packets currently being received to be done.
5882 * Does not block later packets from starting.
5883 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005884void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885{
5886 might_sleep();
Eric Dumazetbe3fc412011-05-23 23:07:32 +00005887 if (rtnl_is_locked())
5888 synchronize_rcu_expedited();
5889 else
5890 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005892EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
5894/**
Eric Dumazet44a08732009-10-27 07:03:04 +00005895 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00005897 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08005898 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08005900 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00005901 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 *
5903 * Callers must hold the rtnl semaphore. You may want
5904 * unregister_netdev() instead of this.
5905 */
5906
Eric Dumazet44a08732009-10-27 07:03:04 +00005907void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908{
Herbert Xua6620712007-12-12 19:21:56 -08005909 ASSERT_RTNL();
5910
Eric Dumazet44a08732009-10-27 07:03:04 +00005911 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00005912 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00005913 } else {
5914 rollback_registered(dev);
5915 /* Finish processing unregister after unlock */
5916 net_set_todo(dev);
5917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918}
Eric Dumazet44a08732009-10-27 07:03:04 +00005919EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
5921/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005922 * unregister_netdevice_many - unregister many devices
5923 * @head: list of devices
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005924 */
5925void unregister_netdevice_many(struct list_head *head)
5926{
5927 struct net_device *dev;
5928
5929 if (!list_empty(head)) {
5930 rollback_registered_many(head);
5931 list_for_each_entry(dev, head, unreg_list)
5932 net_set_todo(dev);
5933 }
5934}
Eric Dumazet63c80992009-10-27 07:06:49 +00005935EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005936
5937/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 * unregister_netdev - remove device from the kernel
5939 * @dev: device
5940 *
5941 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08005942 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 *
5944 * This is just a wrapper for unregister_netdevice that takes
5945 * the rtnl semaphore. In general you want to use this and not
5946 * unregister_netdevice.
5947 */
5948void unregister_netdev(struct net_device *dev)
5949{
5950 rtnl_lock();
5951 unregister_netdevice(dev);
5952 rtnl_unlock();
5953}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954EXPORT_SYMBOL(unregister_netdev);
5955
Eric W. Biedermance286d32007-09-12 13:53:49 +02005956/**
5957 * dev_change_net_namespace - move device to different nethost namespace
5958 * @dev: device
5959 * @net: network namespace
5960 * @pat: If not NULL name pattern to try if the current device name
5961 * is already taken in the destination network namespace.
5962 *
5963 * This function shuts down a device interface and moves it
5964 * to a new network namespace. On success 0 is returned, on
5965 * a failure a netagive errno code is returned.
5966 *
5967 * Callers must hold the rtnl semaphore.
5968 */
5969
5970int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
5971{
Eric W. Biedermance286d32007-09-12 13:53:49 +02005972 int err;
5973
5974 ASSERT_RTNL();
5975
5976 /* Don't allow namespace local devices to be moved. */
5977 err = -EINVAL;
5978 if (dev->features & NETIF_F_NETNS_LOCAL)
5979 goto out;
5980
5981 /* Ensure the device has been registrered */
Eric W. Biedermance286d32007-09-12 13:53:49 +02005982 if (dev->reg_state != NETREG_REGISTERED)
5983 goto out;
5984
5985 /* Get out if there is nothing todo */
5986 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09005987 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02005988 goto out;
5989
5990 /* Pick the destination device name, and ensure
5991 * we can use it in the destination network namespace.
5992 */
5993 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00005994 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02005995 /* We get here if we can't use the current device name */
5996 if (!pat)
5997 goto out;
Gao feng828de4f2012-09-13 20:58:27 +00005998 if (dev_get_valid_name(net, dev, pat) < 0)
Eric W. Biedermance286d32007-09-12 13:53:49 +02005999 goto out;
6000 }
6001
6002 /*
6003 * And now a mini version of register_netdevice unregister_netdevice.
6004 */
6005
6006 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07006007 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006008
6009 /* And unlink it from device chain */
6010 err = -ENODEV;
6011 unlist_netdevice(dev);
6012
6013 synchronize_net();
6014
6015 /* Shutdown queueing discipline. */
6016 dev_shutdown(dev);
6017
6018 /* Notify protocols, that we are about to destroy
6019 this device. They should clean all the things.
David Lamparter3b27e102010-09-17 03:22:19 +00006020
6021 Note that dev->reg_state stays at NETREG_REGISTERED.
6022 This is wanted because this way 8021q and macvlan know
6023 the device is just moving and can keep their slaves up.
Eric W. Biedermance286d32007-09-12 13:53:49 +02006024 */
6025 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Gao feng6549dd42012-08-23 15:36:55 +00006026 rcu_barrier();
6027 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
Eric W. Biedermand2237d32011-10-21 06:24:20 +00006028 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006029
6030 /*
6031 * Flush the unicast and multicast chains
6032 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00006033 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00006034 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006035
Serge Hallyn4e66ae22012-12-03 16:17:12 +00006036 /* Send a netdev-removed uevent to the old namespace */
6037 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
6038
Eric W. Biedermance286d32007-09-12 13:53:49 +02006039 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006040 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006041
Eric W. Biedermance286d32007-09-12 13:53:49 +02006042 /* If there is an ifindex conflict assign a new one */
6043 if (__dev_get_by_index(net, dev->ifindex)) {
6044 int iflink = (dev->iflink == dev->ifindex);
6045 dev->ifindex = dev_new_index(net);
6046 if (iflink)
6047 dev->iflink = dev->ifindex;
6048 }
6049
Serge Hallyn4e66ae22012-12-03 16:17:12 +00006050 /* Send a netdev-add uevent to the new namespace */
6051 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
6052
Eric W. Biederman8b41d182007-09-26 22:02:53 -07006053 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07006054 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07006055 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006056
6057 /* Add the device back in the hashes */
6058 list_netdevice(dev);
6059
6060 /* Notify protocols, that a new device appeared. */
6061 call_netdevice_notifiers(NETDEV_REGISTER, dev);
6062
Eric W. Biedermand90a9092009-12-12 22:11:15 +00006063 /*
6064 * Prevent userspace races by waiting until the network
6065 * device is fully setup before sending notifications.
6066 */
6067 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
6068
Eric W. Biedermance286d32007-09-12 13:53:49 +02006069 synchronize_net();
6070 err = 0;
6071out:
6072 return err;
6073}
Johannes Berg463d0182009-07-14 00:33:35 +02006074EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076static int dev_cpu_callback(struct notifier_block *nfb,
6077 unsigned long action,
6078 void *ocpu)
6079{
6080 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 struct sk_buff *skb;
6082 unsigned int cpu, oldcpu = (unsigned long)ocpu;
6083 struct softnet_data *sd, *oldsd;
6084
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006085 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return NOTIFY_OK;
6087
6088 local_irq_disable();
6089 cpu = smp_processor_id();
6090 sd = &per_cpu(softnet_data, cpu);
6091 oldsd = &per_cpu(softnet_data, oldcpu);
6092
6093 /* Find end of our completion_queue. */
6094 list_skb = &sd->completion_queue;
6095 while (*list_skb)
6096 list_skb = &(*list_skb)->next;
6097 /* Append completion queue from offline CPU. */
6098 *list_skb = oldsd->completion_queue;
6099 oldsd->completion_queue = NULL;
6100
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00006102 if (oldsd->output_queue) {
6103 *sd->output_queue_tailp = oldsd->output_queue;
6104 sd->output_queue_tailp = oldsd->output_queue_tailp;
6105 oldsd->output_queue = NULL;
6106 oldsd->output_queue_tailp = &oldsd->output_queue;
6107 }
Heiko Carstens264524d2011-06-06 20:50:03 +00006108 /* Append NAPI poll list from offline CPU. */
6109 if (!list_empty(&oldsd->poll_list)) {
6110 list_splice_init(&oldsd->poll_list, &sd->poll_list);
6111 raise_softirq_irqoff(NET_RX_SOFTIRQ);
6112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
6114 raise_softirq_irqoff(NET_TX_SOFTIRQ);
6115 local_irq_enable();
6116
6117 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00006118 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
6119 netif_rx(skb);
6120 input_queue_head_incr(oldsd);
6121 }
Tom Herbertfec5e652010-04-16 16:01:27 -07006122 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 netif_rx(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00006124 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07006125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
6127 return NOTIFY_OK;
6128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
6130
Herbert Xu7f353bf2007-08-10 15:47:58 -07006131/**
Herbert Xub63365a2008-10-23 01:11:29 -07006132 * netdev_increment_features - increment feature set by one
6133 * @all: current feature set
6134 * @one: new feature set
6135 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07006136 *
6137 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07006138 * @one to the master device with current feature set @all. Will not
6139 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07006140 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006141netdev_features_t netdev_increment_features(netdev_features_t all,
6142 netdev_features_t one, netdev_features_t mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07006143{
Michał Mirosław1742f182011-04-22 06:31:16 +00006144 if (mask & NETIF_F_GEN_CSUM)
6145 mask |= NETIF_F_ALL_CSUM;
6146 mask |= NETIF_F_VLAN_CHALLENGED;
6147
6148 all |= one & (NETIF_F_ONE_FOR_ALL|NETIF_F_ALL_CSUM) & mask;
6149 all &= one | ~NETIF_F_ALL_FOR_ALL;
6150
Michał Mirosław1742f182011-04-22 06:31:16 +00006151 /* If one device supports hw checksumming, set for all. */
6152 if (all & NETIF_F_GEN_CSUM)
6153 all &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_GEN_CSUM);
Herbert Xu7f353bf2007-08-10 15:47:58 -07006154
6155 return all;
6156}
Herbert Xub63365a2008-10-23 01:11:29 -07006157EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07006158
Baruch Siach430f03c2013-06-02 20:43:55 +00006159static struct hlist_head * __net_init netdev_create_hash(void)
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07006160{
6161 int i;
6162 struct hlist_head *hash;
6163
6164 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
6165 if (hash != NULL)
6166 for (i = 0; i < NETDEV_HASHENTRIES; i++)
6167 INIT_HLIST_HEAD(&hash[i]);
6168
6169 return hash;
6170}
6171
Eric W. Biederman881d9662007-09-17 11:56:21 -07006172/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07006173static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07006174{
Rustad, Mark D734b6542012-07-18 09:06:07 +00006175 if (net != &init_net)
6176 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07006177
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07006178 net->dev_name_head = netdev_create_hash();
6179 if (net->dev_name_head == NULL)
6180 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07006181
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07006182 net->dev_index_head = netdev_create_hash();
6183 if (net->dev_index_head == NULL)
6184 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07006185
6186 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07006187
6188err_idx:
6189 kfree(net->dev_name_head);
6190err_name:
6191 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07006192}
6193
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006194/**
6195 * netdev_drivername - network driver for the device
6196 * @dev: network device
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07006197 *
6198 * Determine network driver for device.
6199 */
David S. Miller3019de12011-06-06 16:41:33 -07006200const char *netdev_drivername(const struct net_device *dev)
Arjan van de Ven6579e572008-07-21 13:31:48 -07006201{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07006202 const struct device_driver *driver;
6203 const struct device *parent;
David S. Miller3019de12011-06-06 16:41:33 -07006204 const char *empty = "";
Arjan van de Ven6579e572008-07-21 13:31:48 -07006205
6206 parent = dev->dev.parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07006207 if (!parent)
David S. Miller3019de12011-06-06 16:41:33 -07006208 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07006209
6210 driver = parent->driver;
6211 if (driver && driver->name)
David S. Miller3019de12011-06-06 16:41:33 -07006212 return driver->name;
6213 return empty;
Arjan van de Ven6579e572008-07-21 13:31:48 -07006214}
6215
Joe Perchesb004ff42012-09-12 20:12:19 -07006216static int __netdev_printk(const char *level, const struct net_device *dev,
Joe Perches256df2f2010-06-27 01:02:35 +00006217 struct va_format *vaf)
6218{
6219 int r;
6220
Joe Perchesb004ff42012-09-12 20:12:19 -07006221 if (dev && dev->dev.parent) {
Joe Perches666f3552012-09-12 20:14:11 -07006222 r = dev_printk_emit(level[1] - '0',
6223 dev->dev.parent,
6224 "%s %s %s: %pV",
6225 dev_driver_string(dev->dev.parent),
6226 dev_name(dev->dev.parent),
6227 netdev_name(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07006228 } else if (dev) {
Joe Perches256df2f2010-06-27 01:02:35 +00006229 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07006230 } else {
Joe Perches256df2f2010-06-27 01:02:35 +00006231 r = printk("%s(NULL net_device): %pV", level, vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07006232 }
Joe Perches256df2f2010-06-27 01:02:35 +00006233
6234 return r;
6235}
6236
6237int netdev_printk(const char *level, const struct net_device *dev,
6238 const char *format, ...)
6239{
6240 struct va_format vaf;
6241 va_list args;
6242 int r;
6243
6244 va_start(args, format);
6245
6246 vaf.fmt = format;
6247 vaf.va = &args;
6248
6249 r = __netdev_printk(level, dev, &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -07006250
Joe Perches256df2f2010-06-27 01:02:35 +00006251 va_end(args);
6252
6253 return r;
6254}
6255EXPORT_SYMBOL(netdev_printk);
6256
6257#define define_netdev_printk_level(func, level) \
6258int func(const struct net_device *dev, const char *fmt, ...) \
6259{ \
6260 int r; \
6261 struct va_format vaf; \
6262 va_list args; \
6263 \
6264 va_start(args, fmt); \
6265 \
6266 vaf.fmt = fmt; \
6267 vaf.va = &args; \
6268 \
6269 r = __netdev_printk(level, dev, &vaf); \
Joe Perchesb004ff42012-09-12 20:12:19 -07006270 \
Joe Perches256df2f2010-06-27 01:02:35 +00006271 va_end(args); \
6272 \
6273 return r; \
6274} \
6275EXPORT_SYMBOL(func);
6276
6277define_netdev_printk_level(netdev_emerg, KERN_EMERG);
6278define_netdev_printk_level(netdev_alert, KERN_ALERT);
6279define_netdev_printk_level(netdev_crit, KERN_CRIT);
6280define_netdev_printk_level(netdev_err, KERN_ERR);
6281define_netdev_printk_level(netdev_warn, KERN_WARNING);
6282define_netdev_printk_level(netdev_notice, KERN_NOTICE);
6283define_netdev_printk_level(netdev_info, KERN_INFO);
6284
Pavel Emelyanov46650792007-10-08 20:38:39 -07006285static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07006286{
6287 kfree(net->dev_name_head);
6288 kfree(net->dev_index_head);
6289}
6290
Denis V. Lunev022cbae2007-11-13 03:23:50 -08006291static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07006292 .init = netdev_init,
6293 .exit = netdev_exit,
6294};
6295
Pavel Emelyanov46650792007-10-08 20:38:39 -07006296static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02006297{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00006298 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02006299 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00006300 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02006301 * initial network namespace
6302 */
6303 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00006304 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02006305 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07006306 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02006307
6308 /* Ignore unmoveable devices (i.e. loopback) */
6309 if (dev->features & NETIF_F_NETNS_LOCAL)
6310 continue;
6311
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00006312 /* Leave virtual devices for the generic cleanup */
6313 if (dev->rtnl_link_ops)
6314 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08006315
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006316 /* Push remaining network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07006317 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
6318 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02006319 if (err) {
Joe Perches7b6cd1c2012-02-01 10:54:43 +00006320 pr_emerg("%s: failed to move %s to init_net: %d\n",
6321 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07006322 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02006323 }
6324 }
6325 rtnl_unlock();
6326}
6327
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00006328static void __net_exit default_device_exit_batch(struct list_head *net_list)
6329{
6330 /* At exit all network devices most be removed from a network
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04006331 * namespace. Do this in the reverse order of registration.
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00006332 * Do this across as many network namespaces as possible to
6333 * improve batching efficiency.
6334 */
6335 struct net_device *dev;
6336 struct net *net;
6337 LIST_HEAD(dev_kill_list);
6338
6339 rtnl_lock();
6340 list_for_each_entry(net, net_list, exit_list) {
6341 for_each_netdev_reverse(net, dev) {
6342 if (dev->rtnl_link_ops)
6343 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
6344 else
6345 unregister_netdevice_queue(dev, &dev_kill_list);
6346 }
6347 }
6348 unregister_netdevice_many(&dev_kill_list);
Eric Dumazetceaaec92011-02-17 22:59:19 +00006349 list_del(&dev_kill_list);
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00006350 rtnl_unlock();
6351}
6352
Denis V. Lunev022cbae2007-11-13 03:23:50 -08006353static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02006354 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00006355 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02006356};
6357
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358/*
6359 * Initialize the DEV module. At boot time this walks the device list and
6360 * unhooks any devices that fail to initialise (normally hardware not
6361 * present) and leaves us with a valid list of present and active devices.
6362 *
6363 */
6364
6365/*
6366 * This is called single threaded during boot, so no need
6367 * to take the rtnl semaphore.
6368 */
6369static int __init net_dev_init(void)
6370{
6371 int i, rc = -ENOMEM;
6372
6373 BUG_ON(!dev_boot_phase);
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 if (dev_proc_init())
6376 goto out;
6377
Eric W. Biederman8b41d182007-09-26 22:02:53 -07006378 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 goto out;
6380
6381 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08006382 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 INIT_LIST_HEAD(&ptype_base[i]);
6384
Vlad Yasevich62532da2012-11-15 08:49:10 +00006385 INIT_LIST_HEAD(&offload_base);
6386
Eric W. Biederman881d9662007-09-17 11:56:21 -07006387 if (register_pernet_subsys(&netdev_net_ops))
6388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
6390 /*
6391 * Initialise the packet receive queues.
6392 */
6393
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07006394 for_each_possible_cpu(i) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006395 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Changli Gaodee42872010-05-02 05:42:16 +00006397 memset(sd, 0, sizeof(*sd));
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006398 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006399 skb_queue_head_init(&sd->process_queue);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006400 sd->completion_queue = NULL;
6401 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00006402 sd->output_queue = NULL;
6403 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00006404#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006405 sd->csd.func = rps_trigger_softirq;
6406 sd->csd.info = sd;
6407 sd->csd.flags = 0;
6408 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07006409#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00006410
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006411 sd->backlog.poll = process_backlog;
6412 sd->backlog.weight = weight_p;
6413 sd->backlog.gro_list = NULL;
6414 sd->backlog.gro_count = 0;
Willem de Bruijn99bbc702013-05-20 04:02:32 +00006415
6416#ifdef CONFIG_NET_FLOW_LIMIT
6417 sd->flow_limit = NULL;
6418#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 }
6420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 dev_boot_phase = 0;
6422
Eric W. Biederman505d4f72008-11-07 22:54:20 -08006423 /* The loopback device is special if any other network devices
6424 * is present in a network namespace the loopback device must
6425 * be present. Since we now dynamically allocate and free the
6426 * loopback device ensure this invariant is maintained by
6427 * keeping the loopback device as the first device on the
6428 * list of network devices. Ensuring the loopback devices
6429 * is the first device that appears and the last network device
6430 * that disappears.
6431 */
6432 if (register_pernet_device(&loopback_net_ops))
6433 goto out;
6434
6435 if (register_pernet_device(&default_device_ops))
6436 goto out;
6437
Carlos R. Mafra962cf362008-05-15 11:15:37 -03006438 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
6439 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
6441 hotcpu_notifier(dev_cpu_callback, 0);
6442 dst_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 rc = 0;
6444out:
6445 return rc;
6446}
6447
6448subsys_initcall(net_dev_init);