blob: 2308cce480483c0df4c93ea61b21b98b8fb8754d [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>
76#include <asm/system.h>
77#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080078#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
stephen hemminger08e98972009-11-10 07:20:34 +000082#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090083#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/sched.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080085#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/string.h>
87#include <linux/mm.h>
88#include <linux/socket.h>
89#include <linux/sockios.h>
90#include <linux/errno.h>
91#include <linux/interrupt.h>
92#include <linux/if_ether.h>
93#include <linux/netdevice.h>
94#include <linux/etherdevice.h>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070095#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/notifier.h>
97#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020098#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <net/sock.h>
100#include <linux/rtnetlink.h>
101#include <linux/proc_fs.h>
102#include <linux/seq_file.h>
103#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/dst.h>
105#include <net/pkt_sched.h>
106#include <net/checksum.h>
Arnd Bergmann44540962009-11-26 06:07:08 +0000107#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#include <linux/highmem.h>
109#include <linux/init.h>
110#include <linux/kmod.h>
111#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <linux/netpoll.h>
113#include <linux/rcupdate.h>
114#include <linux/delay.h>
Johannes Berg295f4a12007-04-26 20:43:56 -0700115#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500118#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700119#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700120#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700121#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700122#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700123#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700124#include <linux/ip.h>
Alexander Duyckad55dca2008-09-20 22:05:50 -0700125#include <net/ip.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700126#include <linux/ipv6.h>
127#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700128#include <linux/jhash.h>
129#include <linux/random.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -0700130#include <trace/events/napi.h>
Koki Sanagicf66ba52010-08-23 18:45:02 +0900131#include <trace/events/net.h>
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900132#include <trace/events/skb.h>
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +0000133#include <linux/pci.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 -0700143/*
144 * The list of packet types we will receive (as opposed to discard)
145 * and the routines to invoke.
146 *
147 * Why 16. Because with 16 the only overlap we get on a hash of the
148 * low nibble of the protocol value is RARP/SNAP/X.25.
149 *
150 * NOTE: That is no longer true with the addition of VLAN tags. Not
151 * sure which should go first, but I bet it won't make much
152 * difference if we are running VLANs. The good news is that
153 * this protocol won't be in the list unless compiled in, so
Stephen Hemminger3041a062006-05-26 13:25:24 -0700154 * the average user (w/out VLANs) will not be adversely affected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * --BLG
156 *
157 * 0800 IP
158 * 8100 802.1Q VLAN
159 * 0001 802.3
160 * 0002 AX.25
161 * 0004 802.2
162 * 8035 RARP
163 * 0005 SNAP
164 * 0805 X.25
165 * 0806 ARP
166 * 8137 IPX
167 * 0009 Localtalk
168 * 86DD IPv6
169 */
170
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800171#define PTYPE_HASH_SIZE (16)
172#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static DEFINE_SPINLOCK(ptype_lock);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800175static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -0700176static struct list_head ptype_all __read_mostly; /* Taps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700179 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * semaphore.
181 *
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800182 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
184 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700185 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * actual updates. This allows pure readers to access the list even
187 * while a writer is preparing to update it.
188 *
189 * To put it another way, dev_base_lock is held for writing only to
190 * protect against pure readers; the rtnl semaphore provides the
191 * protection against other writers.
192 *
193 * See, for example usages, register_netdevice() and
194 * unregister_netdevice(), which must be called with the rtnl
195 * semaphore held.
196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197DEFINE_RWLOCK(dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198EXPORT_SYMBOL(dev_base_lock);
199
Eric W. Biederman881d9662007-09-17 11:56:21 -0700200static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
stephen hemminger08e98972009-11-10 07:20:34 +0000203 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Eric W. Biederman881d9662007-09-17 11:56:21 -0700206static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700208 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000211static inline void rps_lock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000212{
213#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000214 spin_lock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000215#endif
216}
217
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000218static inline void rps_unlock(struct softnet_data *sd)
Changli Gao152102c2010-03-30 20:16:22 +0000219{
220#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +0000221 spin_unlock(&sd->input_pkt_queue.lock);
Changli Gao152102c2010-03-30 20:16:22 +0000222#endif
223}
224
Eric W. Biedermance286d32007-09-12 13:53:49 +0200225/* Device list insertion */
226static int list_netdevice(struct net_device *dev)
227{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900228 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200229
230 ASSERT_RTNL();
231
232 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800233 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
Eric Dumazet72c95282009-10-30 07:11:27 +0000234 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000235 hlist_add_head_rcu(&dev->index_hlist,
236 dev_index_hash(net, dev->ifindex));
Eric W. Biedermance286d32007-09-12 13:53:49 +0200237 write_unlock_bh(&dev_base_lock);
238 return 0;
239}
240
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000241/* Device list removal
242 * caller must respect a RCU grace period before freeing/reusing dev
243 */
Eric W. Biedermance286d32007-09-12 13:53:49 +0200244static void unlist_netdevice(struct net_device *dev)
245{
246 ASSERT_RTNL();
247
248 /* Unlink dev from the device chain */
249 write_lock_bh(&dev_base_lock);
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800250 list_del_rcu(&dev->dev_list);
Eric Dumazet72c95282009-10-30 07:11:27 +0000251 hlist_del_rcu(&dev->name_hlist);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000252 hlist_del_rcu(&dev->index_hlist);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200253 write_unlock_bh(&dev_base_lock);
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
257 * Our notifier list
258 */
259
Alan Sternf07d5b92006-05-09 15:23:03 -0700260static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/*
263 * Device drivers call our routines to queue packets here. We empty the
264 * queue in the local softnet handler.
265 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700266
Eric Dumazet9958da02010-04-17 04:17:02 +0000267DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700268EXPORT_PER_CPU_SYMBOL(softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
David S. Millercf508b12008-07-22 14:16:42 -0700270#ifdef CONFIG_LOCKDEP
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700271/*
David S. Millerc773e842008-07-08 23:13:53 -0700272 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700273 * according to dev->type
274 */
275static const unsigned short netdev_lock_type[] =
276 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
277 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
278 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
279 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
280 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
281 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
282 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
283 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
284 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
285 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
286 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
287 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
288 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
Rémi Denis-Courmont2d91d782008-12-17 15:47:29 -0800289 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET,
Dmitry Eremin-Solenikov929122c2009-08-14 20:00:20 +0400290 ARPHRD_PHONET_PIPE, ARPHRD_IEEE802154,
Sergey Lapinfcb94e42009-06-08 12:18:47 +0000291 ARPHRD_VOID, ARPHRD_NONE};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700292
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700293static const char *const netdev_lock_name[] =
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700294 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
295 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
296 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
297 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
298 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
299 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
300 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
301 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
302 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
303 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
304 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
305 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
306 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
Rémi Denis-Courmont2d91d782008-12-17 15:47:29 -0800307 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET",
Dmitry Eremin-Solenikov929122c2009-08-14 20:00:20 +0400308 "_xmit_PHONET_PIPE", "_xmit_IEEE802154",
Sergey Lapinfcb94e42009-06-08 12:18:47 +0000309 "_xmit_VOID", "_xmit_NONE"};
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700310
311static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
David S. Millercf508b12008-07-22 14:16:42 -0700312static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700313
314static inline unsigned short netdev_lock_pos(unsigned short dev_type)
315{
316 int i;
317
318 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
319 if (netdev_lock_type[i] == dev_type)
320 return i;
321 /* the last key is used by default */
322 return ARRAY_SIZE(netdev_lock_type) - 1;
323}
324
David S. Millercf508b12008-07-22 14:16:42 -0700325static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
326 unsigned short dev_type)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700327{
328 int i;
329
330 i = netdev_lock_pos(dev_type);
331 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
332 netdev_lock_name[i]);
333}
David S. Millercf508b12008-07-22 14:16:42 -0700334
335static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
336{
337 int i;
338
339 i = netdev_lock_pos(dev->type);
340 lockdep_set_class_and_name(&dev->addr_list_lock,
341 &netdev_addr_lock_key[i],
342 netdev_lock_name[i]);
343}
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700344#else
David S. Millercf508b12008-07-22 14:16:42 -0700345static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
346 unsigned short dev_type)
347{
348}
349static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700350{
351}
352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354/*******************************************************************************
355
356 Protocol management and registration routines
357
358*******************************************************************************/
359
360/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * Add a protocol ID to the list. Now that the input handler is
362 * smarter we can dispense with all the messy stuff that used to be
363 * here.
364 *
365 * BEWARE!!! Protocol handlers, mangling input packets,
366 * MUST BE last in hash buckets and checking protocol handlers
367 * MUST start from promiscuous ptype_all chain in net_bh.
368 * It is true now, do not change it.
369 * Explanation follows: if protocol handler, mangling packet, will
370 * be the first on list, it is not able to sense, that packet
371 * is cloned and should be copied-on-write, so that it will
372 * change it and subsequent readers will get broken packet.
373 * --ANK (980803)
374 */
375
376/**
377 * dev_add_pack - add packet handler
378 * @pt: packet type declaration
379 *
380 * Add a protocol handler to the networking stack. The passed &packet_type
381 * is linked into kernel lists and may not be freed until it has been
382 * removed from the kernel lists.
383 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900384 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * guarantee all CPU's that are in middle of receiving packets
386 * will see the new packet type (until the next received packet).
387 */
388
389void dev_add_pack(struct packet_type *pt)
390{
391 int hash;
392
393 spin_lock_bh(&ptype_lock);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700394 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 list_add_rcu(&pt->list, &ptype_all);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700396 else {
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800397 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 list_add_rcu(&pt->list, &ptype_base[hash]);
399 }
400 spin_unlock_bh(&ptype_lock);
401}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700402EXPORT_SYMBOL(dev_add_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/**
405 * __dev_remove_pack - remove packet handler
406 * @pt: packet type declaration
407 *
408 * Remove a protocol handler that was previously added to the kernel
409 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
410 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900411 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 *
413 * The packet type might still be in use by receivers
414 * and must not be freed until after all the CPU's have gone
415 * through a quiescent state.
416 */
417void __dev_remove_pack(struct packet_type *pt)
418{
419 struct list_head *head;
420 struct packet_type *pt1;
421
422 spin_lock_bh(&ptype_lock);
423
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700424 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 head = &ptype_all;
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700426 else
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800427 head = &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 list_for_each_entry(pt1, head, list) {
430 if (pt == pt1) {
431 list_del_rcu(&pt->list);
432 goto out;
433 }
434 }
435
436 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
437out:
438 spin_unlock_bh(&ptype_lock);
439}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700440EXPORT_SYMBOL(__dev_remove_pack);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/**
443 * dev_remove_pack - remove packet handler
444 * @pt: packet type declaration
445 *
446 * Remove a protocol handler that was previously added to the kernel
447 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
448 * from the kernel lists and can be freed or reused once this function
449 * returns.
450 *
451 * This call sleeps to guarantee that no CPU is looking at the packet
452 * type after return.
453 */
454void dev_remove_pack(struct packet_type *pt)
455{
456 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 synchronize_net();
459}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700460EXPORT_SYMBOL(dev_remove_pack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462/******************************************************************************
463
464 Device Boot-time Settings Routines
465
466*******************************************************************************/
467
468/* Boot time configuration table */
469static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
470
471/**
472 * netdev_boot_setup_add - add new setup entry
473 * @name: name of the device
474 * @map: configured settings for the device
475 *
476 * Adds new setup entry to the dev_boot_setup list. The function
477 * returns 0 on error and 1 on success. This is a generic routine to
478 * all netdevices.
479 */
480static int netdev_boot_setup_add(char *name, struct ifmap *map)
481{
482 struct netdev_boot_setup *s;
483 int i;
484
485 s = dev_boot_setup;
486 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
487 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
488 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700489 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 memcpy(&s[i].map, map, sizeof(s[i].map));
491 break;
492 }
493 }
494
495 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
496}
497
498/**
499 * netdev_boot_setup_check - check boot time settings
500 * @dev: the netdevice
501 *
502 * Check boot time settings for the device.
503 * The found settings are set for the device to be used
504 * later in the device probing.
505 * Returns 0 if no settings found, 1 if they are.
506 */
507int netdev_boot_setup_check(struct net_device *dev)
508{
509 struct netdev_boot_setup *s = dev_boot_setup;
510 int i;
511
512 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
513 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700514 !strcmp(dev->name, s[i].name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 dev->irq = s[i].map.irq;
516 dev->base_addr = s[i].map.base_addr;
517 dev->mem_start = s[i].map.mem_start;
518 dev->mem_end = s[i].map.mem_end;
519 return 1;
520 }
521 }
522 return 0;
523}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700524EXPORT_SYMBOL(netdev_boot_setup_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526
527/**
528 * netdev_boot_base - get address from boot time settings
529 * @prefix: prefix for network device
530 * @unit: id for network device
531 *
532 * Check boot time settings for the base address of device.
533 * The found settings are set for the device to be used
534 * later in the device probing.
535 * Returns 0 if no settings found.
536 */
537unsigned long netdev_boot_base(const char *prefix, int unit)
538{
539 const struct netdev_boot_setup *s = dev_boot_setup;
540 char name[IFNAMSIZ];
541 int i;
542
543 sprintf(name, "%s%d", prefix, unit);
544
545 /*
546 * If device already registered then return base of 1
547 * to indicate not to probe for this interface
548 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700549 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 1;
551
552 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
553 if (!strcmp(name, s[i].name))
554 return s[i].map.base_addr;
555 return 0;
556}
557
558/*
559 * Saves at boot time configured settings for any netdevice.
560 */
561int __init netdev_boot_setup(char *str)
562{
563 int ints[5];
564 struct ifmap map;
565
566 str = get_options(str, ARRAY_SIZE(ints), ints);
567 if (!str || !*str)
568 return 0;
569
570 /* Save settings */
571 memset(&map, 0, sizeof(map));
572 if (ints[0] > 0)
573 map.irq = ints[1];
574 if (ints[0] > 1)
575 map.base_addr = ints[2];
576 if (ints[0] > 2)
577 map.mem_start = ints[3];
578 if (ints[0] > 3)
579 map.mem_end = ints[4];
580
581 /* Add new entry to the list */
582 return netdev_boot_setup_add(str, &map);
583}
584
585__setup("netdev=", netdev_boot_setup);
586
587/*******************************************************************************
588
589 Device Interface Subroutines
590
591*******************************************************************************/
592
593/**
594 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700595 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * @name: name to find
597 *
598 * Find an interface by name. Must be called under RTNL semaphore
599 * or @dev_base_lock. If the name is found a pointer to the device
600 * is returned. If the name is not found then %NULL is returned. The
601 * reference counters are not incremented so the caller must be
602 * careful with locks.
603 */
604
Eric W. Biederman881d9662007-09-17 11:56:21 -0700605struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct hlist_node *p;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700608 struct net_device *dev;
609 struct hlist_head *head = dev_name_hash(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700611 hlist_for_each_entry(dev, p, head, name_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!strncmp(dev->name, name, IFNAMSIZ))
613 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return NULL;
616}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700617EXPORT_SYMBOL(__dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619/**
Eric Dumazet72c95282009-10-30 07:11:27 +0000620 * dev_get_by_name_rcu - find a device by its name
621 * @net: the applicable net namespace
622 * @name: name to find
623 *
624 * Find an interface by name.
625 * If the name is found a pointer to the device is returned.
626 * If the name is not found then %NULL is returned.
627 * The reference counters are not incremented so the caller must be
628 * careful with locks. The caller must hold RCU lock.
629 */
630
631struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
632{
633 struct hlist_node *p;
634 struct net_device *dev;
635 struct hlist_head *head = dev_name_hash(net, name);
636
637 hlist_for_each_entry_rcu(dev, p, head, name_hlist)
638 if (!strncmp(dev->name, name, IFNAMSIZ))
639 return dev;
640
641 return NULL;
642}
643EXPORT_SYMBOL(dev_get_by_name_rcu);
644
645/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700647 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * @name: name to find
649 *
650 * Find an interface by name. This can be called from any
651 * context and does its own locking. The returned handle has
652 * the usage count incremented and the caller must use dev_put() to
653 * release it when it is no longer needed. %NULL is returned if no
654 * matching device is found.
655 */
656
Eric W. Biederman881d9662007-09-17 11:56:21 -0700657struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct net_device *dev;
660
Eric Dumazet72c95282009-10-30 07:11:27 +0000661 rcu_read_lock();
662 dev = dev_get_by_name_rcu(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (dev)
664 dev_hold(dev);
Eric Dumazet72c95282009-10-30 07:11:27 +0000665 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return dev;
667}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700668EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/**
671 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700672 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * @ifindex: index of device
674 *
675 * Search for an interface by index. Returns %NULL if the device
676 * is not found or a pointer to the device. The device has not
677 * had its reference counter increased so the caller must be careful
678 * about locking. The caller must hold either the RTNL semaphore
679 * or @dev_base_lock.
680 */
681
Eric W. Biederman881d9662007-09-17 11:56:21 -0700682struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct hlist_node *p;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700685 struct net_device *dev;
686 struct hlist_head *head = dev_index_hash(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700688 hlist_for_each_entry(dev, p, head, index_hlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (dev->ifindex == ifindex)
690 return dev;
Eric Dumazet0bd8d532009-10-30 01:40:11 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return NULL;
693}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700694EXPORT_SYMBOL(__dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000696/**
697 * dev_get_by_index_rcu - find a device by its ifindex
698 * @net: the applicable net namespace
699 * @ifindex: index of device
700 *
701 * Search for an interface by index. Returns %NULL if the device
702 * is not found or a pointer to the device. The device has not
703 * had its reference counter increased so the caller must be careful
704 * about locking. The caller must hold RCU lock.
705 */
706
707struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
708{
709 struct hlist_node *p;
710 struct net_device *dev;
711 struct hlist_head *head = dev_index_hash(net, ifindex);
712
713 hlist_for_each_entry_rcu(dev, p, head, index_hlist)
714 if (dev->ifindex == ifindex)
715 return dev;
716
717 return NULL;
718}
719EXPORT_SYMBOL(dev_get_by_index_rcu);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722/**
723 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700724 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * @ifindex: index of device
726 *
727 * Search for an interface by index. Returns NULL if the device
728 * is not found or a pointer to the device. The device returned has
729 * had a reference added and the pointer is safe until the user calls
730 * dev_put to indicate they have finished with it.
731 */
732
Eric W. Biederman881d9662007-09-17 11:56:21 -0700733struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct net_device *dev;
736
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000737 rcu_read_lock();
738 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (dev)
740 dev_hold(dev);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +0000741 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return dev;
743}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700744EXPORT_SYMBOL(dev_get_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746/**
747 * dev_getbyhwaddr - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700748 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * @type: media type of device
750 * @ha: hardware address
751 *
752 * Search for an interface by MAC address. Returns NULL if the device
753 * is not found or a pointer to the device. The caller must hold the
754 * rtnl semaphore. The returned device has not had its ref count increased
755 * and the caller must therefore be careful about locking
756 *
757 * BUGS:
758 * If the API was consistent this would be __dev_get_by_hwaddr
759 */
760
Eric W. Biederman881d9662007-09-17 11:56:21 -0700761struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct net_device *dev;
764
765 ASSERT_RTNL();
766
Denis V. Lunev81103a52007-12-12 10:47:38 -0800767 for_each_netdev(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (dev->type == type &&
769 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700770 return dev;
771
772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300774EXPORT_SYMBOL(dev_getbyhwaddr);
775
Eric W. Biederman881d9662007-09-17 11:56:21 -0700776struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700777{
778 struct net_device *dev;
779
780 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700781 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700782 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700783 return dev;
784
785 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700786}
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700787EXPORT_SYMBOL(__dev_getfirstbyhwtype);
788
Eric W. Biederman881d9662007-09-17 11:56:21 -0700789struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000791 struct net_device *dev, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Eric Dumazet99fe3c32010-03-18 11:27:25 +0000793 rcu_read_lock();
794 for_each_netdev_rcu(net, dev)
795 if (dev->type == type) {
796 dev_hold(dev);
797 ret = dev;
798 break;
799 }
800 rcu_read_unlock();
801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803EXPORT_SYMBOL(dev_getfirstbyhwtype);
804
805/**
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000806 * dev_get_by_flags_rcu - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700807 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * @if_flags: IFF_* values
809 * @mask: bitmask of bits in if_flags to check
810 *
811 * Search for any interface with the given flags. Returns NULL if a device
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000812 * is not found or a pointer to the device. Must be called inside
813 * rcu_read_lock(), and result refcount is unchanged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
815
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000816struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700817 unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700819 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Pavel Emelianov7562f872007-05-03 15:13:45 -0700821 ret = NULL;
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800822 for_each_netdev_rcu(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (((dev->flags ^ if_flags) & mask) == 0) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700824 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
826 }
827 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000830EXPORT_SYMBOL(dev_get_by_flags_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832/**
833 * dev_valid_name - check if name is okay for network device
834 * @name: name string
835 *
836 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700837 * to allow sysfs to work. We also disallow any kind of
838 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Mitch Williamsc2373ee2005-11-09 10:34:45 -0800840int dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700842 if (*name == '\0')
843 return 0;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -0700844 if (strlen(name) >= IFNAMSIZ)
845 return 0;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700846 if (!strcmp(name, ".") || !strcmp(name, ".."))
847 return 0;
848
849 while (*name) {
850 if (*name == '/' || isspace(*name))
851 return 0;
852 name++;
853 }
854 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700856EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200859 * __dev_alloc_name - allocate a name for a device
860 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200862 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *
864 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700865 * id. It scans list of devices to build up a free map, then chooses
866 * the first empty slot. The caller must hold the dev_base or rtnl lock
867 * while allocating the name and adding the device in order to avoid
868 * duplicates.
869 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
870 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200873static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 const char *p;
877 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700878 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct net_device *d;
880
881 p = strnchr(name, IFNAMSIZ-1, '%');
882 if (p) {
883 /*
884 * Verify the string as this thing may have come from
885 * the user. There must be either one "%d" and no other "%"
886 * characters.
887 */
888 if (p[1] != 'd' || strchr(p + 2, '%'))
889 return -EINVAL;
890
891 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700892 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!inuse)
894 return -ENOMEM;
895
Eric W. Biederman881d9662007-09-17 11:56:21 -0700896 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (!sscanf(d->name, name, &i))
898 continue;
899 if (i < 0 || i >= max_netdevices)
900 continue;
901
902 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200903 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (!strncmp(buf, d->name, IFNAMSIZ))
905 set_bit(i, inuse);
906 }
907
908 i = find_first_zero_bit(inuse, max_netdevices);
909 free_page((unsigned long) inuse);
910 }
911
Octavian Purdilad9031022009-11-18 02:36:59 +0000912 if (buf != name)
913 snprintf(buf, IFNAMSIZ, name, i);
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200914 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* It is possible to run out of possible slots
918 * when the name is long and there isn't enough space left
919 * for the digits, or if all bits are used.
920 */
921 return -ENFILE;
922}
923
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200924/**
925 * dev_alloc_name - allocate a name for a device
926 * @dev: device
927 * @name: name format string
928 *
929 * Passed a format string - eg "lt%d" it will try and find a suitable
930 * id. It scans list of devices to build up a free map, then chooses
931 * the first empty slot. The caller must hold the dev_base or rtnl lock
932 * while allocating the name and adding the device in order to avoid
933 * duplicates.
934 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
935 * Returns the number of the unit assigned or a negative errno code.
936 */
937
938int dev_alloc_name(struct net_device *dev, const char *name)
939{
940 char buf[IFNAMSIZ];
941 struct net *net;
942 int ret;
943
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900944 BUG_ON(!dev_net(dev));
945 net = dev_net(dev);
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200946 ret = __dev_alloc_name(net, name, buf);
947 if (ret >= 0)
948 strlcpy(dev->name, buf, IFNAMSIZ);
949 return ret;
950}
Eric Dumazetd1b19df2009-09-03 01:29:39 -0700951EXPORT_SYMBOL(dev_alloc_name);
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200952
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +0000953static int dev_get_valid_name(struct net_device *dev, const char *name, bool fmt)
Octavian Purdilad9031022009-11-18 02:36:59 +0000954{
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +0000955 struct net *net;
956
957 BUG_ON(!dev_net(dev));
958 net = dev_net(dev);
959
Octavian Purdilad9031022009-11-18 02:36:59 +0000960 if (!dev_valid_name(name))
961 return -EINVAL;
962
963 if (fmt && strchr(name, '%'))
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +0000964 return dev_alloc_name(dev, name);
Octavian Purdilad9031022009-11-18 02:36:59 +0000965 else if (__dev_get_by_name(net, name))
966 return -EEXIST;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +0000967 else if (dev->name != name)
968 strlcpy(dev->name, name, IFNAMSIZ);
Octavian Purdilad9031022009-11-18 02:36:59 +0000969
970 return 0;
971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973/**
974 * dev_change_name - change name of a device
975 * @dev: device
976 * @newname: name (or format string) must be at least IFNAMSIZ
977 *
978 * Change name of a device, can pass format strings "eth%d".
979 * for wildcarding.
980 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -0700981int dev_change_name(struct net_device *dev, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Herbert Xufcc5a032007-07-30 17:03:38 -0700983 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -0700985 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700986 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900989 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900991 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (dev->flags & IFF_UP)
993 return -EBUSY;
994
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -0700995 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
996 return 0;
997
Herbert Xufcc5a032007-07-30 17:03:38 -0700998 memcpy(oldname, dev->name, IFNAMSIZ);
999
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00001000 err = dev_get_valid_name(dev, newname, 1);
Octavian Purdilad9031022009-11-18 02:36:59 +00001001 if (err < 0)
1002 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Herbert Xufcc5a032007-07-30 17:03:38 -07001004rollback:
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07001005 ret = device_rename(&dev->dev, dev->name);
1006 if (ret) {
1007 memcpy(dev->name, oldname, IFNAMSIZ);
1008 return ret;
Stephen Hemmingerdcc99772008-05-14 22:33:38 -07001009 }
Herbert Xu7f988ea2007-07-30 16:35:46 -07001010
1011 write_lock_bh(&dev_base_lock);
Eric W. Biederman92749822007-04-03 00:07:30 -06001012 hlist_del(&dev->name_hlist);
Eric Dumazet72c95282009-10-30 07:11:27 +00001013 write_unlock_bh(&dev_base_lock);
1014
1015 synchronize_rcu();
1016
1017 write_lock_bh(&dev_base_lock);
1018 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -07001019 write_unlock_bh(&dev_base_lock);
1020
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001021 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001022 ret = notifier_to_errno(ret);
1023
1024 if (ret) {
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001025 /* err >= 0 after dev_alloc_name() or stores the first errno */
1026 if (err >= 0) {
Herbert Xufcc5a032007-07-30 17:03:38 -07001027 err = ret;
1028 memcpy(dev->name, oldname, IFNAMSIZ);
1029 goto rollback;
Eric Dumazet91e9c07b2009-11-15 23:30:24 +00001030 } else {
1031 printk(KERN_ERR
1032 "%s: name change rollback failed: %d.\n",
1033 dev->name, ret);
Herbert Xufcc5a032007-07-30 17:03:38 -07001034 }
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 return err;
1038}
1039
1040/**
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001041 * dev_set_alias - change ifalias of a device
1042 * @dev: device
1043 * @alias: name up to IFALIASZ
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07001044 * @len: limit of bytes to copy from info
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001045 *
1046 * Set ifalias for a device,
1047 */
1048int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1049{
1050 ASSERT_RTNL();
1051
1052 if (len >= IFALIASZ)
1053 return -EINVAL;
1054
Oliver Hartkopp96ca4a22008-09-23 21:23:19 -07001055 if (!len) {
1056 if (dev->ifalias) {
1057 kfree(dev->ifalias);
1058 dev->ifalias = NULL;
1059 }
1060 return 0;
1061 }
1062
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001063 dev->ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001064 if (!dev->ifalias)
1065 return -ENOMEM;
1066
1067 strlcpy(dev->ifalias, alias, len+1);
1068 return len;
1069}
1070
1071
1072/**
Stephen Hemminger3041a062006-05-26 13:25:24 -07001073 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001074 * @dev: device to cause notification
1075 *
1076 * Called to indicate a device has changed features.
1077 */
1078void netdev_features_change(struct net_device *dev)
1079{
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001080 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001081}
1082EXPORT_SYMBOL(netdev_features_change);
1083
1084/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * netdev_state_change - device changes state
1086 * @dev: device to cause notification
1087 *
1088 * Called to indicate a device has changed state. This function calls
1089 * the notifier chains for netdev_chain and sends a NEWLINK message
1090 * to the routing socket.
1091 */
1092void netdev_state_change(struct net_device *dev)
1093{
1094 if (dev->flags & IFF_UP) {
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001095 call_netdevice_notifiers(NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1097 }
1098}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001099EXPORT_SYMBOL(netdev_state_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001101int netdev_bonding_change(struct net_device *dev, unsigned long event)
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001102{
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001103 return call_netdevice_notifiers(event, dev);
Or Gerlitzc1da4ac2008-06-13 18:12:00 -07001104}
1105EXPORT_SYMBOL(netdev_bonding_change);
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/**
1108 * dev_load - load a network module
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001109 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * @name: name of interface
1111 *
1112 * If a network interface is not present and the process has suitable
1113 * privileges this function loads the module. If module loading is not
1114 * available in this kernel then it becomes a nop.
1115 */
1116
Eric W. Biederman881d9662007-09-17 11:56:21 -07001117void dev_load(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001119 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Eric Dumazet72c95282009-10-30 07:11:27 +00001121 rcu_read_lock();
1122 dev = dev_get_by_name_rcu(net, name);
1123 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Eric Parisa8f80e82009-08-13 09:44:51 -04001125 if (!dev && capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 request_module("%s", name);
1127}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001128EXPORT_SYMBOL(dev_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Patrick McHardybd380812010-02-26 06:34:53 +00001130static int __dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001132 const struct net_device_ops *ops = dev->netdev_ops;
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001133 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001135 ASSERT_RTNL();
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * Is it even present?
1139 */
1140 if (!netif_device_present(dev))
1141 return -ENODEV;
1142
Johannes Berg3b8bcfd2009-05-30 01:39:53 +02001143 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1144 ret = notifier_to_errno(ret);
1145 if (ret)
1146 return ret;
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Call device private open method
1150 */
1151 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001152
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001153 if (ops->ndo_validate_addr)
1154 ret = ops->ndo_validate_addr(dev);
Jeff Garzikbada3392007-10-23 20:19:37 -07001155
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001156 if (!ret && ops->ndo_open)
1157 ret = ops->ndo_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001159 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * If it went open OK then:
1161 */
1162
Jeff Garzikbada3392007-10-23 20:19:37 -07001163 if (ret)
1164 clear_bit(__LINK_STATE_START, &dev->state);
1165 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /*
1167 * Set the flags.
1168 */
1169 dev->flags |= IFF_UP;
1170
1171 /*
Dan Williams649274d2009-01-11 00:20:39 -08001172 * Enable NET_DMA
1173 */
David S. Millerb4bd07c2009-02-06 22:06:43 -08001174 net_dmaengine_get();
Dan Williams649274d2009-01-11 00:20:39 -08001175
1176 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * Initialize multicasting status
1178 */
Patrick McHardy4417da62007-06-27 01:28:10 -07001179 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
1182 * Wakeup transmit queue engine
1183 */
1184 dev_activate(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return ret;
1188}
Patrick McHardybd380812010-02-26 06:34:53 +00001189
1190/**
1191 * dev_open - prepare an interface for use.
1192 * @dev: device to open
1193 *
1194 * Takes a device from down to up state. The device's private open
1195 * function is invoked and then the multicast lists are loaded. Finally
1196 * the device is moved into the up state and a %NETDEV_UP message is
1197 * sent to the netdev notifier chain.
1198 *
1199 * Calling this function on an active interface is a nop. On a failure
1200 * a negative errno code is returned.
1201 */
1202int dev_open(struct net_device *dev)
1203{
1204 int ret;
1205
1206 /*
1207 * Is it already up?
1208 */
1209 if (dev->flags & IFF_UP)
1210 return 0;
1211
1212 /*
1213 * Open device
1214 */
1215 ret = __dev_open(dev);
1216 if (ret < 0)
1217 return ret;
1218
1219 /*
1220 * ... and announce new interface.
1221 */
1222 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1223 call_netdevice_notifiers(NETDEV_UP, dev);
1224
1225 return ret;
1226}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001227EXPORT_SYMBOL(dev_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Patrick McHardybd380812010-02-26 06:34:53 +00001229static int __dev_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001231 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardybd380812010-02-26 06:34:53 +00001232
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001233 ASSERT_RTNL();
David S. Miller9d5010d2007-09-12 14:33:25 +02001234 might_sleep();
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * Tell people we are going down, so that they can
1238 * prepare to death, when device is still operating.
1239 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001240 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 clear_bit(__LINK_STATE_START, &dev->state);
1243
1244 /* Synchronize to scheduled poll. We cannot touch poll list,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001245 * it can be even on different cpu. So just clear netif_running().
1246 *
1247 * dev->stop() will invoke napi_disable() on all of it's
1248 * napi_struct instances on this device.
1249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 smp_mb__after_clear_bit(); /* Commit netif_running(). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001252 dev_deactivate(dev);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Call the device specific close. This cannot fail.
1256 * Only if device is UP
1257 *
1258 * We allow it to be called even after a DETACH hot-plug
1259 * event.
1260 */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001261 if (ops->ndo_stop)
1262 ops->ndo_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /*
1265 * Device is now down.
1266 */
1267
1268 dev->flags &= ~IFF_UP;
1269
1270 /*
Dan Williams649274d2009-01-11 00:20:39 -08001271 * Shutdown NET_DMA
1272 */
David S. Millerb4bd07c2009-02-06 22:06:43 -08001273 net_dmaengine_put();
Dan Williams649274d2009-01-11 00:20:39 -08001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276}
Patrick McHardybd380812010-02-26 06:34:53 +00001277
1278/**
1279 * dev_close - shutdown an interface.
1280 * @dev: device to shutdown
1281 *
1282 * This function moves an active device into down state. A
1283 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1284 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1285 * chain.
1286 */
1287int dev_close(struct net_device *dev)
1288{
1289 if (!(dev->flags & IFF_UP))
1290 return 0;
1291
1292 __dev_close(dev);
1293
1294 /*
1295 * Tell people we are down
1296 */
1297 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1298 call_netdevice_notifiers(NETDEV_DOWN, dev);
1299
1300 return 0;
1301}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001302EXPORT_SYMBOL(dev_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001305/**
1306 * dev_disable_lro - disable Large Receive Offload on a device
1307 * @dev: device
1308 *
1309 * Disable Large Receive Offload (LRO) on a net device. Must be
1310 * called under RTNL. This is needed if received packets may be
1311 * forwarded to another interface.
1312 */
1313void dev_disable_lro(struct net_device *dev)
1314{
1315 if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
1316 dev->ethtool_ops->set_flags) {
1317 u32 flags = dev->ethtool_ops->get_flags(dev);
1318 if (flags & ETH_FLAG_LRO) {
1319 flags &= ~ETH_FLAG_LRO;
1320 dev->ethtool_ops->set_flags(dev, flags);
1321 }
1322 }
1323 WARN_ON(dev->features & NETIF_F_LRO);
1324}
1325EXPORT_SYMBOL(dev_disable_lro);
1326
1327
Eric W. Biederman881d9662007-09-17 11:56:21 -07001328static int dev_boot_phase = 1;
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330/*
1331 * Device change register/unregister. These are not inline or static
1332 * as we export them to the world.
1333 */
1334
1335/**
1336 * register_netdevice_notifier - register a network notifier block
1337 * @nb: notifier
1338 *
1339 * Register a notifier to be called when network device events occur.
1340 * The notifier passed is linked into the kernel structures and must
1341 * not be reused until it has been unregistered. A negative errno code
1342 * is returned on a failure.
1343 *
1344 * When registered all registration and up events are replayed
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001345 * to the new notifier to allow device to have a race free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * view of the network device list.
1347 */
1348
1349int register_netdevice_notifier(struct notifier_block *nb)
1350{
1351 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001352 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001353 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int err;
1355
1356 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001357 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001358 if (err)
1359 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001360 if (dev_boot_phase)
1361 goto unlock;
1362 for_each_net(net) {
1363 for_each_netdev(net, dev) {
1364 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1365 err = notifier_to_errno(err);
1366 if (err)
1367 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric W. Biederman881d9662007-09-17 11:56:21 -07001369 if (!(dev->flags & IFF_UP))
1370 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001371
Eric W. Biederman881d9662007-09-17 11:56:21 -07001372 nb->notifier_call(nb, NETDEV_UP, dev);
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001375
1376unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 rtnl_unlock();
1378 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001379
1380rollback:
1381 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001382 for_each_net(net) {
1383 for_each_netdev(net, dev) {
1384 if (dev == last)
1385 break;
Herbert Xufcc5a032007-07-30 17:03:38 -07001386
Eric W. Biederman881d9662007-09-17 11:56:21 -07001387 if (dev->flags & IFF_UP) {
1388 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1389 nb->notifier_call(nb, NETDEV_DOWN, dev);
1390 }
1391 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00001392 nb->notifier_call(nb, NETDEV_UNREGISTER_BATCH, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001393 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001394 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001395
1396 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001397 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001399EXPORT_SYMBOL(register_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401/**
1402 * unregister_netdevice_notifier - unregister a network notifier block
1403 * @nb: notifier
1404 *
1405 * Unregister a notifier previously registered by
1406 * register_netdevice_notifier(). The notifier is unlinked into the
1407 * kernel structures and may then be reused. A negative errno code
1408 * is returned on a failure.
1409 */
1410
1411int unregister_netdevice_notifier(struct notifier_block *nb)
1412{
Herbert Xu9f514952006-03-25 01:24:25 -08001413 int err;
1414
1415 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001416 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xu9f514952006-03-25 01:24:25 -08001417 rtnl_unlock();
1418 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001420EXPORT_SYMBOL(unregister_netdevice_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422/**
1423 * call_netdevice_notifiers - call all network notifier blocks
1424 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001425 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *
1427 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001428 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001431int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Jiri Pirkoab930472010-04-20 01:45:37 -07001433 ASSERT_RTNL();
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001434 return raw_notifier_call_chain(&netdev_chain, val, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437/* When > 0 there are consumers of rx skb time stamps */
1438static atomic_t netstamp_needed = ATOMIC_INIT(0);
1439
1440void net_enable_timestamp(void)
1441{
1442 atomic_inc(&netstamp_needed);
1443}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001444EXPORT_SYMBOL(net_enable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446void net_disable_timestamp(void)
1447{
1448 atomic_dec(&netstamp_needed);
1449}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001450EXPORT_SYMBOL(net_disable_timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Eric Dumazet3b098e22010-05-15 23:57:10 -07001452static inline void net_timestamp_set(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 if (atomic_read(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001455 __net_timestamp(skb);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001456 else
1457 skb->tstamp.tv64 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Eric Dumazet3b098e22010-05-15 23:57:10 -07001460static inline void net_timestamp_check(struct sk_buff *skb)
1461{
1462 if (!skb->tstamp.tv64 && atomic_read(&netstamp_needed))
1463 __net_timestamp(skb);
1464}
1465
Arnd Bergmann44540962009-11-26 06:07:08 +00001466/**
1467 * dev_forward_skb - loopback an skb to another netif
1468 *
1469 * @dev: destination network device
1470 * @skb: buffer to forward
1471 *
1472 * return values:
1473 * NET_RX_SUCCESS (no congestion)
Eric Dumazet6ec82562010-05-06 00:53:53 -07001474 * NET_RX_DROP (packet was dropped, but freed)
Arnd Bergmann44540962009-11-26 06:07:08 +00001475 *
1476 * dev_forward_skb can be used for injecting an skb from the
1477 * start_xmit function of one device into the receive queue
1478 * of another device.
1479 *
1480 * The receiving device may be in another namespace, so
1481 * we have to clear all information in the skb that could
1482 * impact namespace isolation.
1483 */
1484int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1485{
1486 skb_orphan(skb);
Ben Greearc736eef2010-07-22 09:54:47 +00001487 nf_reset(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001488
Eric Dumazet6ec82562010-05-06 00:53:53 -07001489 if (!(dev->flags & IFF_UP) ||
1490 (skb->len > (dev->mtu + dev->hard_header_len))) {
1491 kfree_skb(skb);
Arnd Bergmann44540962009-11-26 06:07:08 +00001492 return NET_RX_DROP;
Eric Dumazet6ec82562010-05-06 00:53:53 -07001493 }
Arnd Bergmann8a83a002010-01-30 12:23:03 +00001494 skb_set_dev(skb, dev);
Arnd Bergmann44540962009-11-26 06:07:08 +00001495 skb->tstamp.tv64 = 0;
1496 skb->pkt_type = PACKET_HOST;
1497 skb->protocol = eth_type_trans(skb, dev);
Arnd Bergmann44540962009-11-26 06:07:08 +00001498 return netif_rx(skb);
1499}
1500EXPORT_SYMBOL_GPL(dev_forward_skb);
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/*
1503 * Support routine. Sends outgoing frames to any network
1504 * taps currently in use.
1505 */
1506
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001507static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct packet_type *ptype;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001510
Jarek Poplawski8caf1532009-04-17 10:08:49 +00001511#ifdef CONFIG_NET_CLS_ACT
1512 if (!(skb->tstamp.tv64 && (G_TC_FROM(skb->tc_verd) & AT_INGRESS)))
Eric Dumazet3b098e22010-05-15 23:57:10 -07001513 net_timestamp_set(skb);
Jarek Poplawski8caf1532009-04-17 10:08:49 +00001514#else
Eric Dumazet3b098e22010-05-15 23:57:10 -07001515 net_timestamp_set(skb);
Jarek Poplawski8caf1532009-04-17 10:08:49 +00001516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 rcu_read_lock();
1519 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1520 /* Never send packets back to the socket
1521 * they originated from - MvS (miquels@drinkel.ow.org)
1522 */
1523 if ((ptype->dev == dev || !ptype->dev) &&
1524 (ptype->af_packet_priv == NULL ||
1525 (struct sock *)ptype->af_packet_priv != skb->sk)) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001526 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (!skb2)
1528 break;
1529
1530 /* skb->nh should be correctly
1531 set by sender, so that the second statement is
1532 just protection against buggy protocols.
1533 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001534 skb_reset_mac_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001536 if (skb_network_header(skb2) < skb2->data ||
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001537 skb2->network_header > skb2->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (net_ratelimit())
1539 printk(KERN_CRIT "protocol %04x is "
1540 "buggy, dev %s\n",
Sebastian Andrzej Siewior70777d02010-06-30 10:39:19 -07001541 ntohs(skb2->protocol),
1542 dev->name);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001543 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001546 skb2->transport_header = skb2->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 skb2->pkt_type = PACKET_OUTGOING;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001548 ptype->func(skb2, skb->dev, ptype, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 }
1551 rcu_read_unlock();
1552}
1553
John Fastabendf0796d52010-07-01 13:21:57 +00001554/*
1555 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
1556 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
1557 */
1558void netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
1559{
1560 unsigned int real_num = dev->real_num_tx_queues;
1561
1562 if (unlikely(txq > dev->num_tx_queues))
1563 ;
1564 else if (txq > real_num)
1565 dev->real_num_tx_queues = txq;
1566 else if (txq < real_num) {
1567 dev->real_num_tx_queues = txq;
1568 qdisc_reset_all_tx_gt(dev, txq);
1569 }
1570}
1571EXPORT_SYMBOL(netif_set_real_num_tx_queues);
Denis Vlasenko56079432006-03-29 15:57:29 -08001572
Jarek Poplawskidef82a12008-08-17 21:54:43 -07001573static inline void __netif_reschedule(struct Qdisc *q)
1574{
1575 struct softnet_data *sd;
1576 unsigned long flags;
1577
1578 local_irq_save(flags);
1579 sd = &__get_cpu_var(softnet_data);
Changli Gaoa9cbd582010-04-26 23:06:24 +00001580 q->next_sched = NULL;
1581 *sd->output_queue_tailp = q;
1582 sd->output_queue_tailp = &q->next_sched;
Jarek Poplawskidef82a12008-08-17 21:54:43 -07001583 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1584 local_irq_restore(flags);
1585}
1586
David S. Miller37437bb2008-07-16 02:15:04 -07001587void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08001588{
Jarek Poplawskidef82a12008-08-17 21:54:43 -07001589 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
1590 __netif_reschedule(q);
Denis Vlasenko56079432006-03-29 15:57:29 -08001591}
1592EXPORT_SYMBOL(__netif_schedule);
1593
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001594void dev_kfree_skb_irq(struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08001595{
David S. Miller3578b0c2010-08-03 00:24:04 -07001596 if (atomic_dec_and_test(&skb->users)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001597 struct softnet_data *sd;
1598 unsigned long flags;
Denis Vlasenko56079432006-03-29 15:57:29 -08001599
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001600 local_irq_save(flags);
1601 sd = &__get_cpu_var(softnet_data);
1602 skb->next = sd->completion_queue;
1603 sd->completion_queue = skb;
1604 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1605 local_irq_restore(flags);
1606 }
Denis Vlasenko56079432006-03-29 15:57:29 -08001607}
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001608EXPORT_SYMBOL(dev_kfree_skb_irq);
Denis Vlasenko56079432006-03-29 15:57:29 -08001609
1610void dev_kfree_skb_any(struct sk_buff *skb)
1611{
1612 if (in_irq() || irqs_disabled())
1613 dev_kfree_skb_irq(skb);
1614 else
1615 dev_kfree_skb(skb);
1616}
1617EXPORT_SYMBOL(dev_kfree_skb_any);
1618
1619
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001620/**
1621 * netif_device_detach - mark device as removed
1622 * @dev: network device
1623 *
1624 * Mark device as removed from system and therefore no longer available.
1625 */
Denis Vlasenko56079432006-03-29 15:57:29 -08001626void netif_device_detach(struct net_device *dev)
1627{
1628 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1629 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00001630 netif_tx_stop_all_queues(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08001631 }
1632}
1633EXPORT_SYMBOL(netif_device_detach);
1634
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001635/**
1636 * netif_device_attach - mark device as attached
1637 * @dev: network device
1638 *
1639 * Mark device as attached from system and restart if needed.
1640 */
Denis Vlasenko56079432006-03-29 15:57:29 -08001641void netif_device_attach(struct net_device *dev)
1642{
1643 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1644 netif_running(dev)) {
Alexander Duyckd5431032009-04-08 13:15:22 +00001645 netif_tx_wake_all_queues(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001646 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08001647 }
1648}
1649EXPORT_SYMBOL(netif_device_attach);
1650
Ben Hutchings6de329e2008-06-16 17:02:28 -07001651static bool can_checksum_protocol(unsigned long features, __be16 protocol)
1652{
1653 return ((features & NETIF_F_GEN_CSUM) ||
1654 ((features & NETIF_F_IP_CSUM) &&
1655 protocol == htons(ETH_P_IP)) ||
1656 ((features & NETIF_F_IPV6_CSUM) &&
Yi Zou1c8dbcf2009-02-27 14:06:54 -08001657 protocol == htons(ETH_P_IPV6)) ||
1658 ((features & NETIF_F_FCOE_CRC) &&
1659 protocol == htons(ETH_P_FCOE)));
Ben Hutchings6de329e2008-06-16 17:02:28 -07001660}
1661
1662static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
1663{
1664 if (can_checksum_protocol(dev->features, skb->protocol))
1665 return true;
1666
1667 if (skb->protocol == htons(ETH_P_8021Q)) {
1668 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1669 if (can_checksum_protocol(dev->features & dev->vlan_features,
1670 veh->h_vlan_encapsulated_proto))
1671 return true;
1672 }
1673
1674 return false;
1675}
Denis Vlasenko56079432006-03-29 15:57:29 -08001676
Arnd Bergmann8a83a002010-01-30 12:23:03 +00001677/**
1678 * skb_dev_set -- assign a new device to a buffer
1679 * @skb: buffer for the new device
1680 * @dev: network device
1681 *
1682 * If an skb is owned by a device already, we have to reset
1683 * all data private to the namespace a device belongs to
1684 * before assigning it a new device.
1685 */
1686#ifdef CONFIG_NET_NS
1687void skb_set_dev(struct sk_buff *skb, struct net_device *dev)
1688{
1689 skb_dst_drop(skb);
1690 if (skb->dev && !net_eq(dev_net(skb->dev), dev_net(dev))) {
1691 secpath_reset(skb);
1692 nf_reset(skb);
1693 skb_init_secmark(skb);
1694 skb->mark = 0;
1695 skb->priority = 0;
1696 skb->nf_trace = 0;
1697 skb->ipvs_property = 0;
1698#ifdef CONFIG_NET_SCHED
1699 skb->tc_index = 0;
1700#endif
1701 }
1702 skb->dev = dev;
1703}
1704EXPORT_SYMBOL(skb_set_dev);
1705#endif /* CONFIG_NET_NS */
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
1708 * Invalidate hardware checksum when packet is to be mangled, and
1709 * complete checksum manually on outgoing path.
1710 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001711int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Al Virod3bc23e2006-11-14 21:24:49 -08001713 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07001714 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Patrick McHardy84fa7932006-08-29 16:44:56 -07001716 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07001717 goto out_set_summed;
1718
1719 if (unlikely(skb_shinfo(skb)->gso_size)) {
Herbert Xua430a432006-07-08 13:34:56 -07001720 /* Let GSO fix up the checksum. */
1721 goto out_set_summed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
Herbert Xua0308472007-10-15 01:47:15 -07001724 offset = skb->csum_start - skb_headroom(skb);
1725 BUG_ON(offset >= skb_headlen(skb));
1726 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1727
1728 offset += skb->csum_offset;
1729 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1730
1731 if (skb_cloned(skb) &&
1732 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1734 if (ret)
1735 goto out;
1736 }
1737
Herbert Xua0308472007-10-15 01:47:15 -07001738 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07001739out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001741out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return ret;
1743}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07001744EXPORT_SYMBOL(skb_checksum_help);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001746/**
1747 * skb_gso_segment - Perform segmentation on skb.
1748 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001749 * @features: features for the output path (see dev->features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001750 *
1751 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07001752 *
1753 * It may return NULL if the skb requires no segmentation. This is
1754 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001755 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001756struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001757{
1758 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1759 struct packet_type *ptype;
Al Viro252e33462006-11-14 20:48:11 -08001760 __be16 type = skb->protocol;
Herbert Xua430a432006-07-08 13:34:56 -07001761 int err;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001762
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001763 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001764 skb->mac_len = skb->network_header - skb->mac_header;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001765 __skb_pull(skb, skb->mac_len);
1766
Herbert Xu67fd1a72009-01-19 16:26:44 -08001767 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1768 struct net_device *dev = skb->dev;
1769 struct ethtool_drvinfo info = {};
1770
1771 if (dev && dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
1772 dev->ethtool_ops->get_drvinfo(dev, &info);
1773
1774 WARN(1, "%s: caps=(0x%lx, 0x%lx) len=%d data_len=%d "
1775 "ip_summed=%d",
1776 info.driver, dev ? dev->features : 0L,
1777 skb->sk ? skb->sk->sk_route_caps : 0L,
1778 skb->len, skb->data_len, skb->ip_summed);
1779
Herbert Xua430a432006-07-08 13:34:56 -07001780 if (skb_header_cloned(skb) &&
1781 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1782 return ERR_PTR(err);
1783 }
1784
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001785 rcu_read_lock();
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08001786 list_for_each_entry_rcu(ptype,
1787 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001788 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001789 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001790 err = ptype->gso_send_check(skb);
1791 segs = ERR_PTR(err);
1792 if (err || skb_gso_ok(skb, features))
1793 break;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001794 __skb_push(skb, (skb->data -
1795 skb_network_header(skb)));
Herbert Xua430a432006-07-08 13:34:56 -07001796 }
Herbert Xu576a30e2006-06-27 13:22:38 -07001797 segs = ptype->gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001798 break;
1799 }
1800 }
1801 rcu_read_unlock();
1802
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001803 __skb_push(skb, skb->data - skb_mac_header(skb));
Herbert Xu576a30e2006-06-27 13:22:38 -07001804
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001805 return segs;
1806}
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001807EXPORT_SYMBOL(skb_gso_segment);
1808
Herbert Xufb286bb2005-11-10 13:01:24 -08001809/* Take action when hardware reception checksum errors are detected. */
1810#ifdef CONFIG_BUG
1811void netdev_rx_csum_fault(struct net_device *dev)
1812{
1813 if (net_ratelimit()) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001814 printk(KERN_ERR "%s: hw csum failure.\n",
Stephen Hemminger246a4212005-12-08 15:21:39 -08001815 dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08001816 dump_stack();
1817 }
1818}
1819EXPORT_SYMBOL(netdev_rx_csum_fault);
1820#endif
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822/* Actually, we should eliminate this check as soon as we know, that:
1823 * 1. IOMMU is present and allows to map all the memory.
1824 * 2. No high memory really exists on this machine.
1825 */
1826
Eric Dumazet9092c652010-04-02 13:34:49 -07001827static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Herbert Xu3d3a8532006-06-27 13:33:10 -07001829#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 int i;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00001831 if (!(dev->features & NETIF_F_HIGHDMA)) {
1832 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1833 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1834 return 1;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00001837 if (PCI_DMA_BUS_IS_PHYS) {
1838 struct device *pdev = dev->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Eric Dumazet9092c652010-04-02 13:34:49 -07001840 if (!pdev)
1841 return 0;
FUJITA Tomonori5acbbd42010-03-30 22:35:50 +00001842 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1843 dma_addr_t addr = page_to_phys(skb_shinfo(skb)->frags[i].page);
1844 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
1845 return 1;
1846 }
1847 }
Herbert Xu3d3a8532006-06-27 13:33:10 -07001848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return 0;
1850}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001852struct dev_gso_cb {
1853 void (*destructor)(struct sk_buff *skb);
1854};
1855
1856#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1857
1858static void dev_gso_skb_destructor(struct sk_buff *skb)
1859{
1860 struct dev_gso_cb *cb;
1861
1862 do {
1863 struct sk_buff *nskb = skb->next;
1864
1865 skb->next = nskb->next;
1866 nskb->next = NULL;
1867 kfree_skb(nskb);
1868 } while (skb->next);
1869
1870 cb = DEV_GSO_CB(skb);
1871 if (cb->destructor)
1872 cb->destructor(skb);
1873}
1874
1875/**
1876 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1877 * @skb: buffer to segment
1878 *
1879 * This function segments the given skb and stores the list of segments
1880 * in skb->next.
1881 */
1882static int dev_gso_segment(struct sk_buff *skb)
1883{
1884 struct net_device *dev = skb->dev;
1885 struct sk_buff *segs;
Herbert Xu576a30e2006-06-27 13:22:38 -07001886 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1887 NETIF_F_SG : 0);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001888
Herbert Xu576a30e2006-06-27 13:22:38 -07001889 segs = skb_gso_segment(skb, features);
1890
1891 /* Verifying header integrity only. */
1892 if (!segs)
1893 return 0;
1894
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001895 if (IS_ERR(segs))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001896 return PTR_ERR(segs);
1897
1898 skb->next = segs;
1899 DEV_GSO_CB(skb)->destructor = skb->destructor;
1900 skb->destructor = dev_gso_skb_destructor;
1901
1902 return 0;
1903}
1904
Eric Dumazetfc6055a2010-04-16 12:18:22 +00001905/*
1906 * Try to orphan skb early, right before transmission by the device.
1907 * We cannot orphan skb if tx timestamp is requested, since
1908 * drivers need to call skb_tstamp_tx() to send the timestamp.
1909 */
1910static inline void skb_orphan_try(struct sk_buff *skb)
1911{
Eric Dumazet87fd3082010-07-13 05:24:20 +00001912 struct sock *sk = skb->sk;
1913
1914 if (sk && !skb_tx(skb)->flags) {
1915 /* skb_tx_hash() wont be able to get sk.
1916 * We copy sk_hash into skb->rxhash
1917 */
1918 if (!skb->rxhash)
1919 skb->rxhash = sk->sk_hash;
Eric Dumazetfc6055a2010-04-16 12:18:22 +00001920 skb_orphan(skb);
Eric Dumazet87fd3082010-07-13 05:24:20 +00001921 }
Eric Dumazetfc6055a2010-04-16 12:18:22 +00001922}
1923
John Fastabend6afff0c2010-06-16 14:18:12 +00001924/*
1925 * Returns true if either:
1926 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
1927 * 2. skb is fragmented and the device does not support SG, or if
1928 * at least one of fragments is in highmem and device does not
1929 * support DMA from it.
1930 */
1931static inline int skb_needs_linearize(struct sk_buff *skb,
1932 struct net_device *dev)
1933{
1934 return skb_is_nonlinear(skb) &&
1935 ((skb_has_frags(skb) && !(dev->features & NETIF_F_FRAGLIST)) ||
1936 (skb_shinfo(skb)->nr_frags && (!(dev->features & NETIF_F_SG) ||
1937 illegal_highdma(dev, skb))));
1938}
1939
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001940int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
1941 struct netdev_queue *txq)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001942{
Stephen Hemminger00829822008-11-20 20:14:53 -08001943 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy572a9d72009-11-10 06:14:14 +00001944 int rc = NETDEV_TX_OK;
Stephen Hemminger00829822008-11-20 20:14:53 -08001945
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001946 if (likely(!skb->next)) {
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -07001947 if (!list_empty(&ptype_all))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001948 dev_queue_xmit_nit(skb, dev);
1949
Eric Dumazet93f154b2009-05-18 22:19:19 -07001950 /*
1951 * If device doesnt need skb->dst, release it right now while
1952 * its hot in this cpu cache
1953 */
Eric Dumazetadf30902009-06-02 05:19:30 +00001954 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
1955 skb_dst_drop(skb);
1956
Eric Dumazetfc6055a2010-04-16 12:18:22 +00001957 skb_orphan_try(skb);
David S. Miller9ccb8972010-04-22 01:02:07 -07001958
1959 if (netif_needs_gso(dev, skb)) {
1960 if (unlikely(dev_gso_segment(skb)))
1961 goto out_kfree_skb;
1962 if (skb->next)
1963 goto gso;
John Fastabend6afff0c2010-06-16 14:18:12 +00001964 } else {
1965 if (skb_needs_linearize(skb, dev) &&
1966 __skb_linearize(skb))
1967 goto out_kfree_skb;
1968
1969 /* If packet is not checksummed and device does not
1970 * support checksumming for this protocol, complete
1971 * checksumming here.
1972 */
1973 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1974 skb_set_transport_header(skb, skb->csum_start -
1975 skb_headroom(skb));
1976 if (!dev_can_checksum(dev, skb) &&
1977 skb_checksum_help(skb))
1978 goto out_kfree_skb;
1979 }
David S. Miller9ccb8972010-04-22 01:02:07 -07001980 }
1981
Patrick Ohlyac45f602009-02-12 05:03:37 +00001982 rc = ops->ndo_start_xmit(skb, dev);
Koki Sanagicf66ba52010-08-23 18:45:02 +09001983 trace_net_dev_xmit(skb, rc);
Patrick McHardyec634fe2009-07-05 19:23:38 -07001984 if (rc == NETDEV_TX_OK)
Eric Dumazet08baf562009-05-25 22:58:01 -07001985 txq_trans_update(txq);
Patrick Ohlyac45f602009-02-12 05:03:37 +00001986 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001987 }
1988
Herbert Xu576a30e2006-06-27 13:22:38 -07001989gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001990 do {
1991 struct sk_buff *nskb = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001992
1993 skb->next = nskb->next;
1994 nskb->next = NULL;
Krishna Kumar068a2de2009-12-09 20:59:58 +00001995
1996 /*
1997 * If device doesnt need nskb->dst, release it right now while
1998 * its hot in this cpu cache
1999 */
2000 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2001 skb_dst_drop(nskb);
2002
Stephen Hemminger00829822008-11-20 20:14:53 -08002003 rc = ops->ndo_start_xmit(nskb, dev);
Koki Sanagicf66ba52010-08-23 18:45:02 +09002004 trace_net_dev_xmit(nskb, rc);
Patrick McHardyec634fe2009-07-05 19:23:38 -07002005 if (unlikely(rc != NETDEV_TX_OK)) {
Patrick McHardy572a9d72009-11-10 06:14:14 +00002006 if (rc & ~NETDEV_TX_MASK)
2007 goto out_kfree_gso_skb;
Michael Chanf54d9e82006-06-25 23:57:04 -07002008 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002009 skb->next = nskb;
2010 return rc;
2011 }
Eric Dumazet08baf562009-05-25 22:58:01 -07002012 txq_trans_update(txq);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002013 if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
Michael Chanf54d9e82006-06-25 23:57:04 -07002014 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002015 } while (skb->next);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002016
Patrick McHardy572a9d72009-11-10 06:14:14 +00002017out_kfree_gso_skb:
2018 if (likely(skb->next == NULL))
2019 skb->destructor = DEV_GSO_CB(skb)->destructor;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002020out_kfree_skb:
2021 kfree_skb(skb);
Patrick McHardy572a9d72009-11-10 06:14:14 +00002022 return rc;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07002023}
2024
Tom Herbert0a9627f2010-03-16 08:03:29 +00002025static u32 hashrnd __read_mostly;
David S. Millerb6b2fed2008-07-21 09:48:06 -07002026
Stephen Hemminger92477442009-03-21 13:39:26 -07002027u16 skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb)
David S. Miller8f0f2222008-07-15 03:47:03 -07002028{
David S. Miller70192982009-01-27 16:34:47 -08002029 u32 hash;
David S. Millerb6b2fed2008-07-21 09:48:06 -07002030
David S. Miller513de112009-05-03 14:43:10 -07002031 if (skb_rx_queue_recorded(skb)) {
2032 hash = skb_get_rx_queue(skb);
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002033 while (unlikely(hash >= dev->real_num_tx_queues))
David S. Miller513de112009-05-03 14:43:10 -07002034 hash -= dev->real_num_tx_queues;
2035 return hash;
2036 }
Eric Dumazetec581f62009-05-01 09:05:06 -07002037
2038 if (skb->sk && skb->sk->sk_hash)
David S. Miller70192982009-01-27 16:34:47 -08002039 hash = skb->sk->sk_hash;
Eric Dumazetec581f62009-05-01 09:05:06 -07002040 else
Eric Dumazet87fd3082010-07-13 05:24:20 +00002041 hash = (__force u16) skb->protocol ^ skb->rxhash;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002042 hash = jhash_1word(hash, hashrnd);
David S. Millerd5a9e242009-01-27 16:22:11 -08002043
David S. Millerb6b2fed2008-07-21 09:48:06 -07002044 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
David S. Miller8f0f2222008-07-15 03:47:03 -07002045}
Stephen Hemminger92477442009-03-21 13:39:26 -07002046EXPORT_SYMBOL(skb_tx_hash);
David S. Miller8f0f2222008-07-15 03:47:03 -07002047
Eric Dumazeted046422009-11-13 21:54:04 +00002048static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
2049{
2050 if (unlikely(queue_index >= dev->real_num_tx_queues)) {
2051 if (net_ratelimit()) {
Eric Dumazet7a161ea2010-04-08 21:26:13 +00002052 pr_warning("%s selects TX queue %d, but "
2053 "real number of TX queues is %d\n",
2054 dev->name, queue_index, dev->real_num_tx_queues);
Eric Dumazeted046422009-11-13 21:54:04 +00002055 }
2056 return 0;
2057 }
2058 return queue_index;
2059}
2060
David S. Millere8a04642008-07-17 00:34:19 -07002061static struct netdev_queue *dev_pick_tx(struct net_device *dev,
2062 struct sk_buff *skb)
2063{
Tom Herbertb0f77d02010-07-14 20:50:29 -07002064 int queue_index;
Krishna Kumara4ee3ce2009-10-19 23:50:07 +00002065 struct sock *sk = skb->sk;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002066
Tom Herbertb0f77d02010-07-14 20:50:29 -07002067 queue_index = sk_tx_queue_get(sk);
2068 if (queue_index < 0) {
Krishna Kumara4ee3ce2009-10-19 23:50:07 +00002069 const struct net_device_ops *ops = dev->netdev_ops;
2070
2071 if (ops->ndo_select_queue) {
2072 queue_index = ops->ndo_select_queue(dev, skb);
Eric Dumazeted046422009-11-13 21:54:04 +00002073 queue_index = dev_cap_txqueue(dev, queue_index);
Krishna Kumara4ee3ce2009-10-19 23:50:07 +00002074 } else {
2075 queue_index = 0;
2076 if (dev->real_num_tx_queues > 1)
2077 queue_index = skb_tx_hash(dev, skb);
2078
Eric Dumazet8728c542010-04-11 21:18:17 +00002079 if (sk) {
David S. Miller87eb3672010-04-21 01:14:25 -07002080 struct dst_entry *dst = rcu_dereference_check(sk->sk_dst_cache, 1);
Eric Dumazet8728c542010-04-11 21:18:17 +00002081
2082 if (dst && skb_dst(skb) == dst)
2083 sk_tx_queue_set(sk, queue_index);
2084 }
Krishna Kumara4ee3ce2009-10-19 23:50:07 +00002085 }
2086 }
David S. Millereae792b2008-07-15 03:03:33 -07002087
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002088 skb_set_queue_mapping(skb, queue_index);
2089 return netdev_get_tx_queue(dev, queue_index);
David S. Millere8a04642008-07-17 00:34:19 -07002090}
2091
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002092static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2093 struct net_device *dev,
2094 struct netdev_queue *txq)
2095{
2096 spinlock_t *root_lock = qdisc_lock(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07002097 bool contended = qdisc_is_running(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002098 int rc;
2099
Eric Dumazet79640a42010-06-02 05:09:29 -07002100 /*
2101 * Heuristic to force contended enqueues to serialize on a
2102 * separate lock before trying to get qdisc main lock.
2103 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2104 * and dequeue packets faster.
2105 */
2106 if (unlikely(contended))
2107 spin_lock(&q->busylock);
2108
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002109 spin_lock(root_lock);
2110 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2111 kfree_skb(skb);
2112 rc = NET_XMIT_DROP;
2113 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
Eric Dumazetbc135b22010-06-02 03:23:51 -07002114 qdisc_run_begin(q)) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002115 /*
2116 * This is a work-conserving queue; there are no old skbs
2117 * waiting to be sent out; and the qdisc is not running -
2118 * xmit the skb directly.
2119 */
Eric Dumazet7fee2262010-05-11 23:19:48 +00002120 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2121 skb_dst_force(skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002122 __qdisc_update_bstats(q, skb->len);
Eric Dumazet79640a42010-06-02 05:09:29 -07002123 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2124 if (unlikely(contended)) {
2125 spin_unlock(&q->busylock);
2126 contended = false;
2127 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002128 __qdisc_run(q);
Eric Dumazet79640a42010-06-02 05:09:29 -07002129 } else
Eric Dumazetbc135b22010-06-02 03:23:51 -07002130 qdisc_run_end(q);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002131
2132 rc = NET_XMIT_SUCCESS;
2133 } else {
Eric Dumazet7fee2262010-05-11 23:19:48 +00002134 skb_dst_force(skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002135 rc = qdisc_enqueue_root(skb, q);
Eric Dumazet79640a42010-06-02 05:09:29 -07002136 if (qdisc_run_begin(q)) {
2137 if (unlikely(contended)) {
2138 spin_unlock(&q->busylock);
2139 contended = false;
2140 }
2141 __qdisc_run(q);
2142 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002143 }
2144 spin_unlock(root_lock);
Eric Dumazet79640a42010-06-02 05:09:29 -07002145 if (unlikely(contended))
2146 spin_unlock(&q->busylock);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002147 return rc;
2148}
2149
Dave Jonesd29f7492008-07-22 14:09:06 -07002150/**
2151 * dev_queue_xmit - transmit a buffer
2152 * @skb: buffer to transmit
2153 *
2154 * Queue a buffer for transmission to a network device. The caller must
2155 * have set the device and priority and built the buffer before calling
2156 * this function. The function can be called from an interrupt.
2157 *
2158 * A negative errno code is returned on a failure. A success does not
2159 * guarantee the frame will be transmitted as it may be dropped due
2160 * to congestion or traffic shaping.
2161 *
2162 * -----------------------------------------------------------------------------------
2163 * I notice this method can also return errors from the queue disciplines,
2164 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2165 * be positive.
2166 *
2167 * Regardless of the return value, the skb is consumed, so it is currently
2168 * difficult to retry a send to this method. (You can bump the ref count
2169 * before sending to hold a reference for retry if you are careful.)
2170 *
2171 * When calling this method, interrupts MUST be enabled. This is because
2172 * the BH enable code must have IRQs enabled so that it will not deadlock.
2173 * --BLG
2174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175int dev_queue_xmit(struct sk_buff *skb)
2176{
2177 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07002178 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 struct Qdisc *q;
2180 int rc = -ENOMEM;
2181
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002182 /* Disable soft irqs for various locks below. Also
2183 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002185 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
David S. Millereae792b2008-07-15 03:03:33 -07002187 txq = dev_pick_tx(dev, skb);
Paul E. McKenneya898def2010-02-22 17:04:49 -08002188 q = rcu_dereference_bh(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifdef CONFIG_NET_CLS_ACT
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002191 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192#endif
Koki Sanagicf66ba52010-08-23 18:45:02 +09002193 trace_net_dev_queue(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (q->enqueue) {
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +00002195 rc = __dev_xmit_skb(skb, q, dev, txq);
David S. Miller37437bb2008-07-16 02:15:04 -07002196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
2199 /* The device has no queue. Common case for software devices:
2200 loopback, all the sorts of tunnels...
2201
Herbert Xu932ff272006-06-09 12:20:56 -07002202 Really, it is unlikely that netif_tx_lock protection is necessary
2203 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 counters.)
2205 However, it is possible, that they rely on protection
2206 made by us here.
2207
2208 Check this and shot the lock. It is not prone from deadlocks.
2209 Either shot noqueue qdisc, it is even simpler 8)
2210 */
2211 if (dev->flags & IFF_UP) {
2212 int cpu = smp_processor_id(); /* ok because BHs are off */
2213
David S. Millerc773e842008-07-08 23:13:53 -07002214 if (txq->xmit_lock_owner != cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
David S. Millerc773e842008-07-08 23:13:53 -07002216 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002218 if (!netif_tx_queue_stopped(txq)) {
Patrick McHardy572a9d72009-11-10 06:14:14 +00002219 rc = dev_hard_start_xmit(skb, dev, txq);
2220 if (dev_xmit_complete(rc)) {
David S. Millerc773e842008-07-08 23:13:53 -07002221 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto out;
2223 }
2224 }
David S. Millerc773e842008-07-08 23:13:53 -07002225 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (net_ratelimit())
2227 printk(KERN_CRIT "Virtual device %s asks to "
2228 "queue packet!\n", dev->name);
2229 } else {
2230 /* Recursion is detected! It is possible,
2231 * unfortunately */
2232 if (net_ratelimit())
2233 printk(KERN_CRIT "Dead loop on virtual device "
2234 "%s, fix it urgently!\n", dev->name);
2235 }
2236 }
2237
2238 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07002239 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 kfree_skb(skb);
2242 return rc;
2243out:
Herbert Xud4828d82006-06-22 02:28:18 -07002244 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return rc;
2246}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002247EXPORT_SYMBOL(dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249
2250/*=======================================================================
2251 Receiver routines
2252 =======================================================================*/
2253
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07002254int netdev_max_backlog __read_mostly = 1000;
Eric Dumazet3b098e22010-05-15 23:57:10 -07002255int netdev_tstamp_prequeue __read_mostly = 1;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07002256int netdev_budget __read_mostly = 300;
2257int weight_p __read_mostly = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07002259/* Called with irq disabled */
2260static inline void ____napi_schedule(struct softnet_data *sd,
2261 struct napi_struct *napi)
2262{
2263 list_add_tail(&napi->poll_list, &sd->poll_list);
2264 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2265}
2266
Eric Dumazetdf334542010-03-24 19:13:54 +00002267#ifdef CONFIG_RPS
Tom Herbertfec5e652010-04-16 16:01:27 -07002268
2269/* One global table that all flow-based protocols share. */
Eric Dumazet8770acf2010-04-17 00:54:36 -07002270struct rps_sock_flow_table *rps_sock_flow_table __read_mostly;
Tom Herbertfec5e652010-04-16 16:01:27 -07002271EXPORT_SYMBOL(rps_sock_flow_table);
2272
Tom Herbert0a9627f2010-03-16 08:03:29 +00002273/*
2274 * get_rps_cpu is called from netif_receive_skb and returns the target
2275 * CPU from the RPS map of the receiving queue for a given skb.
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002276 * rcu_read_lock must be held on entry.
Tom Herbert0a9627f2010-03-16 08:03:29 +00002277 */
Tom Herbertfec5e652010-04-16 16:01:27 -07002278static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2279 struct rps_dev_flow **rflowp)
Tom Herbert0a9627f2010-03-16 08:03:29 +00002280{
2281 struct ipv6hdr *ip6;
2282 struct iphdr *ip;
2283 struct netdev_rx_queue *rxqueue;
2284 struct rps_map *map;
Tom Herbertfec5e652010-04-16 16:01:27 -07002285 struct rps_dev_flow_table *flow_table;
2286 struct rps_sock_flow_table *sock_flow_table;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002287 int cpu = -1;
2288 u8 ip_proto;
Tom Herbertfec5e652010-04-16 16:01:27 -07002289 u16 tcpu;
Changli Gao8c52d502010-04-24 22:50:10 -07002290 u32 addr1, addr2, ihl;
2291 union {
2292 u32 v32;
2293 u16 v16[2];
2294 } ports;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002295
Tom Herbert0a9627f2010-03-16 08:03:29 +00002296 if (skb_rx_queue_recorded(skb)) {
2297 u16 index = skb_get_rx_queue(skb);
2298 if (unlikely(index >= dev->num_rx_queues)) {
Tim Gardner08c801f2010-06-08 17:51:27 -06002299 WARN_ONCE(dev->num_rx_queues > 1, "%s received packet "
2300 "on queue %u, but number of RX queues is %u\n",
2301 dev->name, index, dev->num_rx_queues);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002302 goto done;
2303 }
2304 rxqueue = dev->_rx + index;
2305 } else
2306 rxqueue = dev->_rx;
2307
Tom Herbertfec5e652010-04-16 16:01:27 -07002308 if (!rxqueue->rps_map && !rxqueue->rps_flow_table)
Tom Herbert0a9627f2010-03-16 08:03:29 +00002309 goto done;
2310
2311 if (skb->rxhash)
2312 goto got_hash; /* Skip hash computation on packet header */
2313
2314 switch (skb->protocol) {
2315 case __constant_htons(ETH_P_IP):
2316 if (!pskb_may_pull(skb, sizeof(*ip)))
2317 goto done;
2318
2319 ip = (struct iphdr *) skb->data;
2320 ip_proto = ip->protocol;
Eric Dumazetb249dcb2010-04-19 21:56:38 +00002321 addr1 = (__force u32) ip->saddr;
2322 addr2 = (__force u32) ip->daddr;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002323 ihl = ip->ihl;
2324 break;
2325 case __constant_htons(ETH_P_IPV6):
2326 if (!pskb_may_pull(skb, sizeof(*ip6)))
2327 goto done;
2328
2329 ip6 = (struct ipv6hdr *) skb->data;
2330 ip_proto = ip6->nexthdr;
Eric Dumazetb249dcb2010-04-19 21:56:38 +00002331 addr1 = (__force u32) ip6->saddr.s6_addr32[3];
2332 addr2 = (__force u32) ip6->daddr.s6_addr32[3];
Tom Herbert0a9627f2010-03-16 08:03:29 +00002333 ihl = (40 >> 2);
2334 break;
2335 default:
2336 goto done;
2337 }
Tom Herbert0a9627f2010-03-16 08:03:29 +00002338 switch (ip_proto) {
2339 case IPPROTO_TCP:
2340 case IPPROTO_UDP:
2341 case IPPROTO_DCCP:
2342 case IPPROTO_ESP:
2343 case IPPROTO_AH:
2344 case IPPROTO_SCTP:
2345 case IPPROTO_UDPLITE:
Eric Dumazetb249dcb2010-04-19 21:56:38 +00002346 if (pskb_may_pull(skb, (ihl * 4) + 4)) {
Changli Gao8c52d502010-04-24 22:50:10 -07002347 ports.v32 = * (__force u32 *) (skb->data + (ihl * 4));
2348 if (ports.v16[1] < ports.v16[0])
2349 swap(ports.v16[0], ports.v16[1]);
2350 break;
Eric Dumazetb249dcb2010-04-19 21:56:38 +00002351 }
Tom Herbert0a9627f2010-03-16 08:03:29 +00002352 default:
Changli Gao8c52d502010-04-24 22:50:10 -07002353 ports.v32 = 0;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002354 break;
2355 }
2356
Eric Dumazetb249dcb2010-04-19 21:56:38 +00002357 /* get a consistent hash (same value on both flow directions) */
2358 if (addr2 < addr1)
2359 swap(addr1, addr2);
Changli Gao8c52d502010-04-24 22:50:10 -07002360 skb->rxhash = jhash_3words(addr1, addr2, ports.v32, hashrnd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002361 if (!skb->rxhash)
2362 skb->rxhash = 1;
2363
2364got_hash:
Tom Herbertfec5e652010-04-16 16:01:27 -07002365 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2366 sock_flow_table = rcu_dereference(rps_sock_flow_table);
2367 if (flow_table && sock_flow_table) {
2368 u16 next_cpu;
2369 struct rps_dev_flow *rflow;
2370
2371 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
2372 tcpu = rflow->cpu;
2373
2374 next_cpu = sock_flow_table->ents[skb->rxhash &
2375 sock_flow_table->mask];
2376
2377 /*
2378 * If the desired CPU (where last recvmsg was done) is
2379 * different from current CPU (one in the rx-queue flow
2380 * table entry), switch if one of the following holds:
2381 * - Current CPU is unset (equal to RPS_NO_CPU).
2382 * - Current CPU is offline.
2383 * - The current CPU's queue tail has advanced beyond the
2384 * last packet that was enqueued using this table entry.
2385 * This guarantees that all previous packets for the flow
2386 * have been dequeued, thus preserving in order delivery.
2387 */
2388 if (unlikely(tcpu != next_cpu) &&
2389 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
2390 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
2391 rflow->last_qtail)) >= 0)) {
2392 tcpu = rflow->cpu = next_cpu;
2393 if (tcpu != RPS_NO_CPU)
2394 rflow->last_qtail = per_cpu(softnet_data,
2395 tcpu).input_queue_head;
2396 }
2397 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
2398 *rflowp = rflow;
2399 cpu = tcpu;
2400 goto done;
2401 }
2402 }
2403
Tom Herbert0a9627f2010-03-16 08:03:29 +00002404 map = rcu_dereference(rxqueue->rps_map);
2405 if (map) {
Tom Herbertfec5e652010-04-16 16:01:27 -07002406 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
Tom Herbert0a9627f2010-03-16 08:03:29 +00002407
2408 if (cpu_online(tcpu)) {
2409 cpu = tcpu;
2410 goto done;
2411 }
2412 }
2413
2414done:
Tom Herbert0a9627f2010-03-16 08:03:29 +00002415 return cpu;
2416}
2417
Tom Herbert0a9627f2010-03-16 08:03:29 +00002418/* Called from hardirq (IPI) context */
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002419static void rps_trigger_softirq(void *data)
Tom Herbert0a9627f2010-03-16 08:03:29 +00002420{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002421 struct softnet_data *sd = data;
2422
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07002423 ____napi_schedule(sd, &sd->backlog);
Changli Gaodee42872010-05-02 05:42:16 +00002424 sd->received_rps++;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002425}
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002426
Tom Herbertfec5e652010-04-16 16:01:27 -07002427#endif /* CONFIG_RPS */
Tom Herbert0a9627f2010-03-16 08:03:29 +00002428
2429/*
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002430 * Check if this softnet_data structure is another cpu one
2431 * If yes, queue it to our IPI list and return 1
2432 * If no, return 0
2433 */
2434static int rps_ipi_queued(struct softnet_data *sd)
2435{
2436#ifdef CONFIG_RPS
2437 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
2438
2439 if (sd != mysd) {
2440 sd->rps_ipi_next = mysd->rps_ipi_list;
2441 mysd->rps_ipi_list = sd;
2442
2443 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2444 return 1;
2445 }
2446#endif /* CONFIG_RPS */
2447 return 0;
2448}
2449
2450/*
Tom Herbert0a9627f2010-03-16 08:03:29 +00002451 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
2452 * queue (may be a remote CPU queue).
2453 */
Tom Herbertfec5e652010-04-16 16:01:27 -07002454static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
2455 unsigned int *qtail)
Tom Herbert0a9627f2010-03-16 08:03:29 +00002456{
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002457 struct softnet_data *sd;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002458 unsigned long flags;
2459
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002460 sd = &per_cpu(softnet_data, cpu);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002461
2462 local_irq_save(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002463
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002464 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07002465 if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
2466 if (skb_queue_len(&sd->input_pkt_queue)) {
Tom Herbert0a9627f2010-03-16 08:03:29 +00002467enqueue:
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002468 __skb_queue_tail(&sd->input_pkt_queue, skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00002469 input_queue_tail_incr_save(sd, qtail);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002470 rps_unlock(sd);
Changli Gao152102c2010-03-30 20:16:22 +00002471 local_irq_restore(flags);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002472 return NET_RX_SUCCESS;
2473 }
2474
Eric Dumazetebda37c22010-05-06 23:51:21 +00002475 /* Schedule NAPI for backlog device
2476 * We can use non atomic operation since we own the queue lock
2477 */
2478 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002479 if (!rps_ipi_queued(sd))
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07002480 ____napi_schedule(sd, &sd->backlog);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002481 }
2482 goto enqueue;
2483 }
2484
Changli Gaodee42872010-05-02 05:42:16 +00002485 sd->dropped++;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00002486 rps_unlock(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00002487
Tom Herbert0a9627f2010-03-16 08:03:29 +00002488 local_irq_restore(flags);
2489
2490 kfree_skb(skb);
2491 return NET_RX_DROP;
2492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/**
2495 * netif_rx - post buffer to the network code
2496 * @skb: buffer to post
2497 *
2498 * This function receives a packet from a device driver and queues it for
2499 * the upper (protocol) levels to process. It always succeeds. The buffer
2500 * may be dropped during processing for congestion control or by the
2501 * protocol layers.
2502 *
2503 * return values:
2504 * NET_RX_SUCCESS (no congestion)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 * NET_RX_DROP (packet was dropped)
2506 *
2507 */
2508
2509int netif_rx(struct sk_buff *skb)
2510{
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002511 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 /* if netpoll wants it, pretend we never saw it */
2514 if (netpoll_rx(skb))
2515 return NET_RX_DROP;
2516
Eric Dumazet3b098e22010-05-15 23:57:10 -07002517 if (netdev_tstamp_prequeue)
2518 net_timestamp_check(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Koki Sanagicf66ba52010-08-23 18:45:02 +09002520 trace_netif_rx(skb);
Eric Dumazetdf334542010-03-24 19:13:54 +00002521#ifdef CONFIG_RPS
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002522 {
Tom Herbertfec5e652010-04-16 16:01:27 -07002523 struct rps_dev_flow voidflow, *rflow = &voidflow;
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002524 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Changli Gaocece1942010-08-07 20:35:43 -07002526 preempt_disable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002527 rcu_read_lock();
Tom Herbertfec5e652010-04-16 16:01:27 -07002528
2529 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002530 if (cpu < 0)
2531 cpu = smp_processor_id();
Tom Herbertfec5e652010-04-16 16:01:27 -07002532
2533 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2534
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002535 rcu_read_unlock();
Changli Gaocece1942010-08-07 20:35:43 -07002536 preempt_enable();
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002537 }
2538#else
Tom Herbertfec5e652010-04-16 16:01:27 -07002539 {
2540 unsigned int qtail;
2541 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
2542 put_cpu();
2543 }
Eric Dumazetb0e28f12010-04-15 00:14:07 -07002544#endif
2545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002547EXPORT_SYMBOL(netif_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549int netif_rx_ni(struct sk_buff *skb)
2550{
2551 int err;
2552
2553 preempt_disable();
2554 err = netif_rx(skb);
2555 if (local_softirq_pending())
2556 do_softirq();
2557 preempt_enable();
2558
2559 return err;
2560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561EXPORT_SYMBOL(netif_rx_ni);
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static void net_tx_action(struct softirq_action *h)
2564{
2565 struct softnet_data *sd = &__get_cpu_var(softnet_data);
2566
2567 if (sd->completion_queue) {
2568 struct sk_buff *clist;
2569
2570 local_irq_disable();
2571 clist = sd->completion_queue;
2572 sd->completion_queue = NULL;
2573 local_irq_enable();
2574
2575 while (clist) {
2576 struct sk_buff *skb = clist;
2577 clist = clist->next;
2578
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002579 WARN_ON(atomic_read(&skb->users));
Koki Sanagi07dc22e2010-08-23 18:46:12 +09002580 trace_kfree_skb(skb, net_tx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 __kfree_skb(skb);
2582 }
2583 }
2584
2585 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07002586 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 local_irq_disable();
2589 head = sd->output_queue;
2590 sd->output_queue = NULL;
Changli Gaoa9cbd582010-04-26 23:06:24 +00002591 sd->output_queue_tailp = &sd->output_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 local_irq_enable();
2593
2594 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07002595 struct Qdisc *q = head;
2596 spinlock_t *root_lock;
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 head = head->next_sched;
2599
David S. Miller5fb66222008-08-02 20:02:43 -07002600 root_lock = qdisc_lock(q);
David S. Miller37437bb2008-07-16 02:15:04 -07002601 if (spin_trylock(root_lock)) {
Jarek Poplawskidef82a12008-08-17 21:54:43 -07002602 smp_mb__before_clear_bit();
2603 clear_bit(__QDISC_STATE_SCHED,
2604 &q->state);
David S. Miller37437bb2008-07-16 02:15:04 -07002605 qdisc_run(q);
2606 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 } else {
David S. Miller195648b2008-08-19 04:00:36 -07002608 if (!test_bit(__QDISC_STATE_DEACTIVATED,
Jarek Poplawskie8a83e12008-09-07 18:41:21 -07002609 &q->state)) {
David S. Miller195648b2008-08-19 04:00:36 -07002610 __netif_reschedule(q);
Jarek Poplawskie8a83e12008-09-07 18:41:21 -07002611 } else {
2612 smp_mb__before_clear_bit();
2613 clear_bit(__QDISC_STATE_SCHED,
2614 &q->state);
2615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 }
2618 }
2619}
2620
Stephen Hemminger6f05f622007-03-08 20:46:03 -08002621static inline int deliver_skb(struct sk_buff *skb,
2622 struct packet_type *pt_prev,
2623 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 atomic_inc(&skb->users);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002626 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002629#if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
2630 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
Michał Mirosławda678292009-06-05 05:35:28 +00002631/* This hook is defined here for ATM LANE */
2632int (*br_fdb_test_addr_hook)(struct net_device *dev,
2633 unsigned char *addr) __read_mostly;
Stephen Hemminger4fb019a2009-09-11 11:50:08 -07002634EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
Michał Mirosławda678292009-06-05 05:35:28 +00002635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637#ifdef CONFIG_NET_CLS_ACT
2638/* TODO: Maybe we should just force sch_ingress to be compiled in
2639 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2640 * a compare and 2 stores extra right now if we dont have it on
2641 * but have CONFIG_NET_CLS_ACT
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002642 * NOTE: This doesnt stop any functionality; if you dont have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 * the ingress scheduler, you just cant add policies on ingress.
2644 *
2645 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002646static int ing_filter(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct net_device *dev = skb->dev;
Herbert Xuf697c3e2007-10-14 00:38:47 -07002649 u32 ttl = G_TC_RTTL(skb->tc_verd);
David S. Miller555353c2008-07-08 17:33:13 -07002650 struct netdev_queue *rxq;
2651 int result = TC_ACT_OK;
2652 struct Qdisc *q;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002653
Stephen Hemmingerde384832010-08-01 00:33:23 -07002654 if (unlikely(MAX_RED_LOOP < ttl++)) {
2655 if (net_ratelimit())
2656 pr_warning( "Redir loop detected Dropping packet (%d->%d)\n",
2657 skb->skb_iif, dev->ifindex);
Herbert Xuf697c3e2007-10-14 00:38:47 -07002658 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660
Herbert Xuf697c3e2007-10-14 00:38:47 -07002661 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
2662 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
2663
David S. Miller555353c2008-07-08 17:33:13 -07002664 rxq = &dev->rx_queue;
2665
David S. Miller83874002008-07-17 00:53:03 -07002666 q = rxq->qdisc;
David S. Miller8d50b532008-07-30 02:37:46 -07002667 if (q != &noop_qdisc) {
David S. Miller83874002008-07-17 00:53:03 -07002668 spin_lock(qdisc_lock(q));
David S. Millera9312ae2008-08-17 21:51:03 -07002669 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
2670 result = qdisc_enqueue_root(skb, q);
David S. Miller83874002008-07-17 00:53:03 -07002671 spin_unlock(qdisc_lock(q));
2672 }
Herbert Xuf697c3e2007-10-14 00:38:47 -07002673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return result;
2675}
Herbert Xuf697c3e2007-10-14 00:38:47 -07002676
2677static inline struct sk_buff *handle_ing(struct sk_buff *skb,
2678 struct packet_type **pt_prev,
2679 int *ret, struct net_device *orig_dev)
2680{
David S. Miller8d50b532008-07-30 02:37:46 -07002681 if (skb->dev->rx_queue.qdisc == &noop_qdisc)
Herbert Xuf697c3e2007-10-14 00:38:47 -07002682 goto out;
2683
2684 if (*pt_prev) {
2685 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2686 *pt_prev = NULL;
Herbert Xuf697c3e2007-10-14 00:38:47 -07002687 }
2688
2689 switch (ing_filter(skb)) {
2690 case TC_ACT_SHOT:
2691 case TC_ACT_STOLEN:
2692 kfree_skb(skb);
2693 return NULL;
2694 }
2695
2696out:
2697 skb->tc_verd = 0;
2698 return skb;
2699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700#endif
2701
Patrick McHardybc1d0412008-07-14 22:49:30 -07002702/*
2703 * netif_nit_deliver - deliver received packets to network taps
2704 * @skb: buffer
2705 *
2706 * This function is used to deliver incoming packets to network
2707 * taps. It should be used when the normal netif_receive_skb path
2708 * is bypassed, for example because of VLAN acceleration.
2709 */
2710void netif_nit_deliver(struct sk_buff *skb)
2711{
2712 struct packet_type *ptype;
2713
2714 if (list_empty(&ptype_all))
2715 return;
2716
2717 skb_reset_network_header(skb);
2718 skb_reset_transport_header(skb);
2719 skb->mac_len = skb->network_header - skb->mac_header;
2720
2721 rcu_read_lock();
2722 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2723 if (!ptype->dev || ptype->dev == skb->dev)
2724 deliver_skb(skb, ptype, skb->dev);
2725 }
2726 rcu_read_unlock();
2727}
2728
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002729/**
2730 * netdev_rx_handler_register - register receive handler
2731 * @dev: device to register a handler for
2732 * @rx_handler: receive handler to register
Jiri Pirko93e2c322010-06-10 03:34:59 +00002733 * @rx_handler_data: data pointer that is used by rx handler
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002734 *
2735 * Register a receive hander for a device. This handler will then be
2736 * called from __netif_receive_skb. A negative errno code is returned
2737 * on a failure.
2738 *
2739 * The caller must hold the rtnl_mutex.
2740 */
2741int netdev_rx_handler_register(struct net_device *dev,
Jiri Pirko93e2c322010-06-10 03:34:59 +00002742 rx_handler_func_t *rx_handler,
2743 void *rx_handler_data)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002744{
2745 ASSERT_RTNL();
2746
2747 if (dev->rx_handler)
2748 return -EBUSY;
2749
Jiri Pirko93e2c322010-06-10 03:34:59 +00002750 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002751 rcu_assign_pointer(dev->rx_handler, rx_handler);
2752
2753 return 0;
2754}
2755EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
2756
2757/**
2758 * netdev_rx_handler_unregister - unregister receive handler
2759 * @dev: device to unregister a handler from
2760 *
2761 * Unregister a receive hander from a device.
2762 *
2763 * The caller must hold the rtnl_mutex.
2764 */
2765void netdev_rx_handler_unregister(struct net_device *dev)
2766{
2767
2768 ASSERT_RTNL();
2769 rcu_assign_pointer(dev->rx_handler, NULL);
Jiri Pirko93e2c322010-06-10 03:34:59 +00002770 rcu_assign_pointer(dev->rx_handler_data, NULL);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002771}
2772EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
2773
Eric Dumazetacbbc072010-04-11 06:56:11 +00002774static inline void skb_bond_set_mac_by_master(struct sk_buff *skb,
2775 struct net_device *master)
2776{
2777 if (skb->pkt_type == PACKET_HOST) {
2778 u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
2779
2780 memcpy(dest, master->dev_addr, ETH_ALEN);
2781 }
2782}
2783
2784/* On bonding slaves other than the currently active slave, suppress
2785 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
2786 * ARP on active-backup slaves with arp_validate enabled.
2787 */
2788int __skb_bond_should_drop(struct sk_buff *skb, struct net_device *master)
2789{
2790 struct net_device *dev = skb->dev;
2791
2792 if (master->priv_flags & IFF_MASTER_ARPMON)
2793 dev->last_rx = jiffies;
2794
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002795 if ((master->priv_flags & IFF_MASTER_ALB) &&
2796 (master->priv_flags & IFF_BRIDGE_PORT)) {
Eric Dumazetacbbc072010-04-11 06:56:11 +00002797 /* Do address unmangle. The local destination address
2798 * will be always the one master has. Provides the right
2799 * functionality in a bridge.
2800 */
2801 skb_bond_set_mac_by_master(skb, master);
2802 }
2803
2804 if (dev->priv_flags & IFF_SLAVE_INACTIVE) {
2805 if ((dev->priv_flags & IFF_SLAVE_NEEDARP) &&
2806 skb->protocol == __cpu_to_be16(ETH_P_ARP))
2807 return 0;
2808
2809 if (master->priv_flags & IFF_MASTER_ALB) {
2810 if (skb->pkt_type != PACKET_BROADCAST &&
2811 skb->pkt_type != PACKET_MULTICAST)
2812 return 0;
2813 }
2814 if (master->priv_flags & IFF_MASTER_8023AD &&
2815 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
2816 return 0;
2817
2818 return 1;
2819 }
2820 return 0;
2821}
2822EXPORT_SYMBOL(__skb_bond_should_drop);
2823
Eric Dumazet10f744d2010-03-28 23:07:20 -07002824static int __netif_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 struct packet_type *ptype, *pt_prev;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002827 rx_handler_func_t *rx_handler;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002828 struct net_device *orig_dev;
Eric Dumazet0641e4f2010-03-18 21:16:45 -07002829 struct net_device *master;
Joe Eykholt0d7a3682008-07-02 18:22:01 -07002830 struct net_device *null_or_orig;
John Fastabend2df4a0f2010-05-12 21:31:11 +00002831 struct net_device *orig_or_bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08002833 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Eric Dumazet3b098e22010-05-15 23:57:10 -07002835 if (!netdev_tstamp_prequeue)
2836 net_timestamp_check(skb);
Eric Dumazet81bbb3d2009-09-30 16:42:42 -07002837
Koki Sanagicf66ba52010-08-23 18:45:02 +09002838 trace_netif_receive_skb(skb);
Eric Dumazet05423b22009-10-26 18:40:35 -07002839 if (vlan_tx_tag_present(skb) && vlan_hwaccel_do_receive(skb))
Patrick McHardy9b22ea52008-11-04 14:49:57 -08002840 return NET_RX_SUCCESS;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 /* if we've gotten here through NAPI, check netpoll */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002843 if (netpoll_receive_skb(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return NET_RX_DROP;
2845
Eric Dumazet8964be42009-11-20 15:35:04 -08002846 if (!skb->skb_iif)
2847 skb->skb_iif = skb->dev->ifindex;
David S. Miller86e65da2005-08-09 19:36:29 -07002848
John Fastabend597a2642010-06-03 09:30:11 +00002849 /*
2850 * bonding note: skbs received on inactive slaves should only
2851 * be delivered to pkt handlers that are exact matches. Also
2852 * the deliver_no_wcard flag will be set. If packet handlers
2853 * are sensitive to duplicate packets these skbs will need to
2854 * be dropped at the handler. The vlan accel path may have
2855 * already set the deliver_no_wcard flag.
2856 */
Joe Eykholt0d7a3682008-07-02 18:22:01 -07002857 null_or_orig = NULL;
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07002858 orig_dev = skb->dev;
Eric Dumazet0641e4f2010-03-18 21:16:45 -07002859 master = ACCESS_ONCE(orig_dev->master);
John Fastabend597a2642010-06-03 09:30:11 +00002860 if (skb->deliver_no_wcard)
2861 null_or_orig = orig_dev;
2862 else if (master) {
2863 if (skb_bond_should_drop(skb, master)) {
2864 skb->deliver_no_wcard = 1;
Joe Eykholt0d7a3682008-07-02 18:22:01 -07002865 null_or_orig = orig_dev; /* deliver only exact match */
John Fastabend597a2642010-06-03 09:30:11 +00002866 } else
Eric Dumazet0641e4f2010-03-18 21:16:45 -07002867 skb->dev = master;
Joe Eykholtcc9bd5c2008-07-02 18:22:00 -07002868 }
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002869
Eric Dumazet27f39c73e2010-05-19 22:07:23 +00002870 __this_cpu_inc(softnet_data.processed);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07002871 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002872 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002873 skb->mac_len = skb->network_header - skb->mac_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 pt_prev = NULL;
2876
2877 rcu_read_lock();
2878
2879#ifdef CONFIG_NET_CLS_ACT
2880 if (skb->tc_verd & TC_NCLS) {
2881 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2882 goto ncls;
2883 }
2884#endif
2885
2886 list_for_each_entry_rcu(ptype, &ptype_all, list) {
Joe Eykholtf9823072008-07-02 18:22:02 -07002887 if (ptype->dev == null_or_orig || ptype->dev == skb->dev ||
2888 ptype->dev == orig_dev) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002889 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002890 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 pt_prev = ptype;
2892 }
2893 }
2894
2895#ifdef CONFIG_NET_CLS_ACT
Herbert Xuf697c3e2007-10-14 00:38:47 -07002896 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
2897 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899ncls:
2900#endif
2901
Jiri Pirkoab95bfe2010-06-01 21:52:08 +00002902 /* Handle special case of bridge or macvlan */
2903 rx_handler = rcu_dereference(skb->dev->rx_handler);
2904 if (rx_handler) {
2905 if (pt_prev) {
2906 ret = deliver_skb(skb, pt_prev, orig_dev);
2907 pt_prev = NULL;
2908 }
2909 skb = rx_handler(skb);
2910 if (!skb)
2911 goto out;
2912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002914 /*
2915 * Make sure frames received on VLAN interfaces stacked on
2916 * bonding interfaces still make their way to any base bonding
2917 * device that may have registered for a specific ptype. The
2918 * handler may have to adjust skb->dev and orig_dev.
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002919 */
John Fastabend2df4a0f2010-05-12 21:31:11 +00002920 orig_or_bond = orig_dev;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002921 if ((skb->dev->priv_flags & IFF_802_1Q_VLAN) &&
2922 (vlan_dev_real_dev(skb->dev)->priv_flags & IFF_BONDING)) {
John Fastabend2df4a0f2010-05-12 21:31:11 +00002923 orig_or_bond = vlan_dev_real_dev(skb->dev);
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002924 }
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 type = skb->protocol;
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002927 list_for_each_entry_rcu(ptype,
2928 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002929 if (ptype->type == type && (ptype->dev == null_or_orig ||
Andy Gospodarekca8d9ea2010-01-06 12:56:37 +00002930 ptype->dev == skb->dev || ptype->dev == orig_dev ||
John Fastabend2df4a0f2010-05-12 21:31:11 +00002931 ptype->dev == orig_or_bond)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002932 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002933 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 pt_prev = ptype;
2935 }
2936 }
2937
2938 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002939 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 } else {
2941 kfree_skb(skb);
2942 /* Jamal, now you will not able to escape explaining
2943 * me how you were going to use this. :-)
2944 */
2945 ret = NET_RX_DROP;
2946 }
2947
2948out:
2949 rcu_read_unlock();
2950 return ret;
2951}
Tom Herbert0a9627f2010-03-16 08:03:29 +00002952
2953/**
2954 * netif_receive_skb - process receive buffer from network
2955 * @skb: buffer to process
2956 *
2957 * netif_receive_skb() is the main receive data processing function.
2958 * It always succeeds. The buffer may be dropped during processing
2959 * for congestion control or by the protocol layers.
2960 *
2961 * This function may only be called from softirq context and interrupts
2962 * should be enabled.
2963 *
2964 * Return values (usually ignored):
2965 * NET_RX_SUCCESS: no congestion
2966 * NET_RX_DROP: packet was dropped
2967 */
2968int netif_receive_skb(struct sk_buff *skb)
2969{
Eric Dumazet3b098e22010-05-15 23:57:10 -07002970 if (netdev_tstamp_prequeue)
2971 net_timestamp_check(skb);
2972
Richard Cochranc1f19b52010-07-17 08:49:36 +00002973 if (skb_defer_rx_timestamp(skb))
2974 return NET_RX_SUCCESS;
2975
Eric Dumazetdf334542010-03-24 19:13:54 +00002976#ifdef CONFIG_RPS
Eric Dumazet3b098e22010-05-15 23:57:10 -07002977 {
2978 struct rps_dev_flow voidflow, *rflow = &voidflow;
2979 int cpu, ret;
Tom Herbert0a9627f2010-03-16 08:03:29 +00002980
Eric Dumazet3b098e22010-05-15 23:57:10 -07002981 rcu_read_lock();
Tom Herbert0a9627f2010-03-16 08:03:29 +00002982
Eric Dumazet3b098e22010-05-15 23:57:10 -07002983 cpu = get_rps_cpu(skb->dev, skb, &rflow);
Tom Herbertfec5e652010-04-16 16:01:27 -07002984
Eric Dumazet3b098e22010-05-15 23:57:10 -07002985 if (cpu >= 0) {
2986 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2987 rcu_read_unlock();
2988 } else {
2989 rcu_read_unlock();
2990 ret = __netif_receive_skb(skb);
2991 }
2992
2993 return ret;
Tom Herbertfec5e652010-04-16 16:01:27 -07002994 }
Tom Herbert1e94d722010-03-18 17:45:44 -07002995#else
2996 return __netif_receive_skb(skb);
2997#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00002998}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07002999EXPORT_SYMBOL(netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Eric Dumazet88751272010-04-19 05:07:33 +00003001/* Network device is going away, flush any packets still pending
3002 * Called with irqs disabled.
3003 */
Changli Gao152102c2010-03-30 20:16:22 +00003004static void flush_backlog(void *arg)
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003005{
Changli Gao152102c2010-03-30 20:16:22 +00003006 struct net_device *dev = arg;
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003007 struct softnet_data *sd = &__get_cpu_var(softnet_data);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003008 struct sk_buff *skb, *tmp;
3009
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003010 rps_lock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07003011 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003012 if (skb->dev == dev) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003013 __skb_unlink(skb, &sd->input_pkt_queue);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003014 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003015 input_queue_head_incr(sd);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003016 }
Changli Gao6e7676c2010-04-27 15:07:33 -07003017 }
Eric Dumazete36fa2f2010-04-19 21:17:14 +00003018 rps_unlock(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07003019
3020 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3021 if (skb->dev == dev) {
3022 __skb_unlink(skb, &sd->process_queue);
3023 kfree_skb(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003024 input_queue_head_incr(sd);
Changli Gao6e7676c2010-04-27 15:07:33 -07003025 }
3026 }
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07003027}
3028
Herbert Xud565b0a2008-12-15 23:38:52 -08003029static int napi_gro_complete(struct sk_buff *skb)
3030{
3031 struct packet_type *ptype;
3032 __be16 type = skb->protocol;
3033 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
3034 int err = -ENOENT;
3035
Herbert Xufc59f9a2009-04-14 15:11:06 -07003036 if (NAPI_GRO_CB(skb)->count == 1) {
3037 skb_shinfo(skb)->gso_size = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003038 goto out;
Herbert Xufc59f9a2009-04-14 15:11:06 -07003039 }
Herbert Xud565b0a2008-12-15 23:38:52 -08003040
3041 rcu_read_lock();
3042 list_for_each_entry_rcu(ptype, head, list) {
3043 if (ptype->type != type || ptype->dev || !ptype->gro_complete)
3044 continue;
3045
3046 err = ptype->gro_complete(skb);
3047 break;
3048 }
3049 rcu_read_unlock();
3050
3051 if (err) {
3052 WARN_ON(&ptype->list == head);
3053 kfree_skb(skb);
3054 return NET_RX_SUCCESS;
3055 }
3056
3057out:
Herbert Xud565b0a2008-12-15 23:38:52 -08003058 return netif_receive_skb(skb);
3059}
3060
David S. Miller11380a42010-01-19 13:46:10 -08003061static void napi_gro_flush(struct napi_struct *napi)
Herbert Xud565b0a2008-12-15 23:38:52 -08003062{
3063 struct sk_buff *skb, *next;
3064
3065 for (skb = napi->gro_list; skb; skb = next) {
3066 next = skb->next;
3067 skb->next = NULL;
3068 napi_gro_complete(skb);
3069 }
3070
Herbert Xu4ae55442009-02-08 18:00:36 +00003071 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003072 napi->gro_list = NULL;
3073}
Herbert Xud565b0a2008-12-15 23:38:52 -08003074
Ben Hutchings5b252f02009-10-29 07:17:09 +00003075enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xud565b0a2008-12-15 23:38:52 -08003076{
3077 struct sk_buff **pp = NULL;
3078 struct packet_type *ptype;
3079 __be16 type = skb->protocol;
3080 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
Herbert Xu0da2afd52008-12-26 14:57:42 -08003081 int same_flow;
Herbert Xud565b0a2008-12-15 23:38:52 -08003082 int mac_len;
Ben Hutchings5b252f02009-10-29 07:17:09 +00003083 enum gro_result ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08003084
Jarek Poplawskice9e76c2010-08-05 01:19:11 +00003085 if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
Herbert Xud565b0a2008-12-15 23:38:52 -08003086 goto normal;
3087
David S. Miller4cf704f2009-06-09 00:18:51 -07003088 if (skb_is_gso(skb) || skb_has_frags(skb))
Herbert Xuf17f5c92009-01-14 14:36:12 -08003089 goto normal;
3090
Herbert Xud565b0a2008-12-15 23:38:52 -08003091 rcu_read_lock();
3092 list_for_each_entry_rcu(ptype, head, list) {
Herbert Xud565b0a2008-12-15 23:38:52 -08003093 if (ptype->type != type || ptype->dev || !ptype->gro_receive)
3094 continue;
3095
Herbert Xu86911732009-01-29 14:19:50 +00003096 skb_set_network_header(skb, skb_gro_offset(skb));
Herbert Xud565b0a2008-12-15 23:38:52 -08003097 mac_len = skb->network_header - skb->mac_header;
3098 skb->mac_len = mac_len;
3099 NAPI_GRO_CB(skb)->same_flow = 0;
3100 NAPI_GRO_CB(skb)->flush = 0;
Herbert Xu5d38a072009-01-04 16:13:40 -08003101 NAPI_GRO_CB(skb)->free = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003102
Herbert Xud565b0a2008-12-15 23:38:52 -08003103 pp = ptype->gro_receive(&napi->gro_list, skb);
3104 break;
3105 }
3106 rcu_read_unlock();
3107
3108 if (&ptype->list == head)
3109 goto normal;
3110
Herbert Xu0da2afd52008-12-26 14:57:42 -08003111 same_flow = NAPI_GRO_CB(skb)->same_flow;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003112 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
Herbert Xu0da2afd52008-12-26 14:57:42 -08003113
Herbert Xud565b0a2008-12-15 23:38:52 -08003114 if (pp) {
3115 struct sk_buff *nskb = *pp;
3116
3117 *pp = nskb->next;
3118 nskb->next = NULL;
3119 napi_gro_complete(nskb);
Herbert Xu4ae55442009-02-08 18:00:36 +00003120 napi->gro_count--;
Herbert Xud565b0a2008-12-15 23:38:52 -08003121 }
3122
Herbert Xu0da2afd52008-12-26 14:57:42 -08003123 if (same_flow)
Herbert Xud565b0a2008-12-15 23:38:52 -08003124 goto ok;
3125
Herbert Xu4ae55442009-02-08 18:00:36 +00003126 if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
Herbert Xud565b0a2008-12-15 23:38:52 -08003127 goto normal;
Herbert Xud565b0a2008-12-15 23:38:52 -08003128
Herbert Xu4ae55442009-02-08 18:00:36 +00003129 napi->gro_count++;
Herbert Xud565b0a2008-12-15 23:38:52 -08003130 NAPI_GRO_CB(skb)->count = 1;
Herbert Xu86911732009-01-29 14:19:50 +00003131 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003132 skb->next = napi->gro_list;
3133 napi->gro_list = skb;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003134 ret = GRO_HELD;
Herbert Xud565b0a2008-12-15 23:38:52 -08003135
Herbert Xuad0f9902009-02-01 01:24:55 -08003136pull:
Herbert Xucb189782009-05-26 18:50:31 +00003137 if (skb_headlen(skb) < skb_gro_offset(skb)) {
3138 int grow = skb_gro_offset(skb) - skb_headlen(skb);
3139
3140 BUG_ON(skb->end - skb->tail < grow);
3141
3142 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
3143
3144 skb->tail += grow;
3145 skb->data_len -= grow;
3146
3147 skb_shinfo(skb)->frags[0].page_offset += grow;
3148 skb_shinfo(skb)->frags[0].size -= grow;
3149
3150 if (unlikely(!skb_shinfo(skb)->frags[0].size)) {
3151 put_page(skb_shinfo(skb)->frags[0].page);
3152 memmove(skb_shinfo(skb)->frags,
3153 skb_shinfo(skb)->frags + 1,
Jarek Poplawskie5093ae2010-08-11 02:02:10 +00003154 --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
Herbert Xucb189782009-05-26 18:50:31 +00003155 }
Herbert Xuad0f9902009-02-01 01:24:55 -08003156 }
3157
Herbert Xud565b0a2008-12-15 23:38:52 -08003158ok:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003159 return ret;
Herbert Xud565b0a2008-12-15 23:38:52 -08003160
3161normal:
Herbert Xuad0f9902009-02-01 01:24:55 -08003162 ret = GRO_NORMAL;
3163 goto pull;
Herbert Xu5d38a072009-01-04 16:13:40 -08003164}
Herbert Xu96e93ea2009-01-06 10:49:34 -08003165EXPORT_SYMBOL(dev_gro_receive);
3166
Ben Hutchings5b252f02009-10-29 07:17:09 +00003167static gro_result_t
3168__napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu96e93ea2009-01-06 10:49:34 -08003169{
3170 struct sk_buff *p;
3171
3172 for (p = napi->gro_list; p; p = p->next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08003173 NAPI_GRO_CB(p)->same_flow =
3174 (p->dev == skb->dev) &&
3175 !compare_ether_header(skb_mac_header(p),
3176 skb_gro_mac_header(skb));
Herbert Xu96e93ea2009-01-06 10:49:34 -08003177 NAPI_GRO_CB(p)->flush = 0;
3178 }
3179
3180 return dev_gro_receive(napi, skb);
3181}
Herbert Xu5d38a072009-01-04 16:13:40 -08003182
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003183gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
Herbert Xu5d38a072009-01-04 16:13:40 -08003184{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003185 switch (ret) {
3186 case GRO_NORMAL:
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003187 if (netif_receive_skb(skb))
3188 ret = GRO_DROP;
3189 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08003190
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003191 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003192 case GRO_MERGED_FREE:
Herbert Xu5d38a072009-01-04 16:13:40 -08003193 kfree_skb(skb);
3194 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00003195
3196 case GRO_HELD:
3197 case GRO_MERGED:
3198 break;
Herbert Xu5d38a072009-01-04 16:13:40 -08003199 }
3200
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003201 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003202}
3203EXPORT_SYMBOL(napi_skb_finish);
3204
Herbert Xu78a478d2009-05-26 18:50:21 +00003205void skb_gro_reset_offset(struct sk_buff *skb)
3206{
3207 NAPI_GRO_CB(skb)->data_offset = 0;
3208 NAPI_GRO_CB(skb)->frag0 = NULL;
Herbert Xu74895942009-05-26 18:50:27 +00003209 NAPI_GRO_CB(skb)->frag0_len = 0;
Herbert Xu78a478d2009-05-26 18:50:21 +00003210
Herbert Xu78d3fd02009-05-26 18:50:23 +00003211 if (skb->mac_header == skb->tail &&
Herbert Xu74895942009-05-26 18:50:27 +00003212 !PageHighMem(skb_shinfo(skb)->frags[0].page)) {
Herbert Xu78a478d2009-05-26 18:50:21 +00003213 NAPI_GRO_CB(skb)->frag0 =
3214 page_address(skb_shinfo(skb)->frags[0].page) +
3215 skb_shinfo(skb)->frags[0].page_offset;
Herbert Xu74895942009-05-26 18:50:27 +00003216 NAPI_GRO_CB(skb)->frag0_len = skb_shinfo(skb)->frags[0].size;
3217 }
Herbert Xu78a478d2009-05-26 18:50:21 +00003218}
3219EXPORT_SYMBOL(skb_gro_reset_offset);
3220
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003221gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003222{
Herbert Xu86911732009-01-29 14:19:50 +00003223 skb_gro_reset_offset(skb);
3224
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003225 return napi_skb_finish(__napi_gro_receive(napi, skb), skb);
Herbert Xud565b0a2008-12-15 23:38:52 -08003226}
3227EXPORT_SYMBOL(napi_gro_receive);
3228
Herbert Xu96e93ea2009-01-06 10:49:34 -08003229void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
3230{
Herbert Xu96e93ea2009-01-06 10:49:34 -08003231 __skb_pull(skb, skb_headlen(skb));
3232 skb_reserve(skb, NET_IP_ALIGN - skb_headroom(skb));
3233
3234 napi->skb = skb;
3235}
3236EXPORT_SYMBOL(napi_reuse_skb);
3237
Herbert Xu76620aa2009-04-16 02:02:07 -07003238struct sk_buff *napi_get_frags(struct napi_struct *napi)
Herbert Xu5d38a072009-01-04 16:13:40 -08003239{
Herbert Xu5d38a072009-01-04 16:13:40 -08003240 struct sk_buff *skb = napi->skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08003241
3242 if (!skb) {
Eric Dumazet89d71a62009-10-13 05:34:20 +00003243 skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
3244 if (skb)
3245 napi->skb = skb;
Herbert Xu5d38a072009-01-04 16:13:40 -08003246 }
Herbert Xu96e93ea2009-01-06 10:49:34 -08003247 return skb;
3248}
Herbert Xu76620aa2009-04-16 02:02:07 -07003249EXPORT_SYMBOL(napi_get_frags);
Herbert Xu96e93ea2009-01-06 10:49:34 -08003250
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003251gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
3252 gro_result_t ret)
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003253{
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003254 switch (ret) {
3255 case GRO_NORMAL:
Herbert Xu86911732009-01-29 14:19:50 +00003256 case GRO_HELD:
Ajit Khapardee76b69c2010-02-16 20:25:43 +00003257 skb->protocol = eth_type_trans(skb, skb->dev);
Herbert Xu86911732009-01-29 14:19:50 +00003258
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003259 if (ret == GRO_HELD)
3260 skb_gro_pull(skb, -ETH_HLEN);
3261 else if (netif_receive_skb(skb))
3262 ret = GRO_DROP;
Herbert Xu86911732009-01-29 14:19:50 +00003263 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003264
3265 case GRO_DROP:
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003266 case GRO_MERGED_FREE:
3267 napi_reuse_skb(napi, skb);
3268 break;
Ben Hutchings5b252f02009-10-29 07:17:09 +00003269
3270 case GRO_MERGED:
3271 break;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003272 }
3273
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003274 return ret;
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003275}
3276EXPORT_SYMBOL(napi_frags_finish);
3277
Herbert Xu76620aa2009-04-16 02:02:07 -07003278struct sk_buff *napi_frags_skb(struct napi_struct *napi)
Herbert Xu96e93ea2009-01-06 10:49:34 -08003279{
Herbert Xu76620aa2009-04-16 02:02:07 -07003280 struct sk_buff *skb = napi->skb;
3281 struct ethhdr *eth;
Herbert Xua5b1cf22009-05-26 18:50:28 +00003282 unsigned int hlen;
3283 unsigned int off;
Herbert Xu76620aa2009-04-16 02:02:07 -07003284
3285 napi->skb = NULL;
3286
3287 skb_reset_mac_header(skb);
3288 skb_gro_reset_offset(skb);
3289
Herbert Xua5b1cf22009-05-26 18:50:28 +00003290 off = skb_gro_offset(skb);
3291 hlen = off + sizeof(*eth);
3292 eth = skb_gro_header_fast(skb, off);
3293 if (skb_gro_header_hard(skb, hlen)) {
3294 eth = skb_gro_header_slow(skb, hlen, off);
3295 if (unlikely(!eth)) {
3296 napi_reuse_skb(napi, skb);
3297 skb = NULL;
3298 goto out;
3299 }
Herbert Xu76620aa2009-04-16 02:02:07 -07003300 }
3301
3302 skb_gro_pull(skb, sizeof(*eth));
3303
3304 /*
3305 * This works because the only protocols we care about don't require
3306 * special handling. We'll fix it up properly at the end.
3307 */
3308 skb->protocol = eth->h_proto;
3309
3310out:
3311 return skb;
3312}
3313EXPORT_SYMBOL(napi_frags_skb);
3314
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003315gro_result_t napi_gro_frags(struct napi_struct *napi)
Herbert Xu76620aa2009-04-16 02:02:07 -07003316{
3317 struct sk_buff *skb = napi_frags_skb(napi);
Herbert Xu96e93ea2009-01-06 10:49:34 -08003318
3319 if (!skb)
Ben Hutchingsc7c4b3b2009-10-29 21:36:53 -07003320 return GRO_DROP;
Herbert Xu96e93ea2009-01-06 10:49:34 -08003321
Herbert Xu5d0d9be2009-01-29 14:19:48 +00003322 return napi_frags_finish(napi, skb, __napi_gro_receive(napi, skb));
Herbert Xu5d38a072009-01-04 16:13:40 -08003323}
3324EXPORT_SYMBOL(napi_gro_frags);
3325
Eric Dumazete326bed2010-04-22 00:22:45 -07003326/*
3327 * net_rps_action sends any pending IPI's for rps.
3328 * Note: called with local irq disabled, but exits with local irq enabled.
3329 */
3330static void net_rps_action_and_irq_enable(struct softnet_data *sd)
3331{
3332#ifdef CONFIG_RPS
3333 struct softnet_data *remsd = sd->rps_ipi_list;
3334
3335 if (remsd) {
3336 sd->rps_ipi_list = NULL;
3337
3338 local_irq_enable();
3339
3340 /* Send pending IPI's to kick RPS processing on remote cpus. */
3341 while (remsd) {
3342 struct softnet_data *next = remsd->rps_ipi_next;
3343
3344 if (cpu_online(remsd->cpu))
3345 __smp_call_function_single(remsd->cpu,
3346 &remsd->csd, 0);
3347 remsd = next;
3348 }
3349 } else
3350#endif
3351 local_irq_enable();
3352}
3353
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003354static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 int work = 0;
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003357 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Eric Dumazete326bed2010-04-22 00:22:45 -07003359#ifdef CONFIG_RPS
3360 /* Check if we have pending ipi, its better to send them now,
3361 * not waiting net_rx_action() end.
3362 */
3363 if (sd->rps_ipi_list) {
3364 local_irq_disable();
3365 net_rps_action_and_irq_enable(sd);
3366 }
3367#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003368 napi->weight = weight_p;
Changli Gao6e7676c2010-04-27 15:07:33 -07003369 local_irq_disable();
3370 while (work < quota) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 struct sk_buff *skb;
Changli Gao6e7676c2010-04-27 15:07:33 -07003372 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Changli Gao6e7676c2010-04-27 15:07:33 -07003374 while ((skb = __skb_dequeue(&sd->process_queue))) {
Eric Dumazete4008272010-04-05 15:42:39 -07003375 local_irq_enable();
Changli Gao6e7676c2010-04-27 15:07:33 -07003376 __netif_receive_skb(skb);
Changli Gao6e7676c2010-04-27 15:07:33 -07003377 local_irq_disable();
Tom Herbert76cc8b12010-05-20 18:37:59 +00003378 input_queue_head_incr(sd);
3379 if (++work >= quota) {
3380 local_irq_enable();
3381 return work;
3382 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Changli Gao6e7676c2010-04-27 15:07:33 -07003385 rps_lock(sd);
3386 qlen = skb_queue_len(&sd->input_pkt_queue);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003387 if (qlen)
Changli Gao6e7676c2010-04-27 15:07:33 -07003388 skb_queue_splice_tail_init(&sd->input_pkt_queue,
3389 &sd->process_queue);
Tom Herbert76cc8b12010-05-20 18:37:59 +00003390
Changli Gao6e7676c2010-04-27 15:07:33 -07003391 if (qlen < quota - work) {
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003392 /*
3393 * Inline a custom version of __napi_complete().
3394 * only current cpu owns and manipulates this napi,
3395 * and NAPI_STATE_SCHED is the only possible flag set on backlog.
3396 * we can use a plain write instead of clear_bit(),
3397 * and we dont need an smp_mb() memory barrier.
3398 */
3399 list_del(&napi->poll_list);
3400 napi->state = 0;
3401
Changli Gao6e7676c2010-04-27 15:07:33 -07003402 quota = work + qlen;
3403 }
3404 rps_unlock(sd);
3405 }
3406 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003408 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003411/**
3412 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07003413 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003414 *
3415 * The entry's receive function will be scheduled to run
3416 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08003417void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003418{
3419 unsigned long flags;
3420
3421 local_irq_save(flags);
Eric Dumazeteecfd7c2010-05-06 22:07:48 -07003422 ____napi_schedule(&__get_cpu_var(softnet_data), n);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003423 local_irq_restore(flags);
3424}
3425EXPORT_SYMBOL(__napi_schedule);
3426
Herbert Xud565b0a2008-12-15 23:38:52 -08003427void __napi_complete(struct napi_struct *n)
3428{
3429 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
3430 BUG_ON(n->gro_list);
3431
3432 list_del(&n->poll_list);
3433 smp_mb__before_clear_bit();
3434 clear_bit(NAPI_STATE_SCHED, &n->state);
3435}
3436EXPORT_SYMBOL(__napi_complete);
3437
3438void napi_complete(struct napi_struct *n)
3439{
3440 unsigned long flags;
3441
3442 /*
3443 * don't let napi dequeue from the cpu poll list
3444 * just in case its running on a different cpu
3445 */
3446 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
3447 return;
3448
3449 napi_gro_flush(n);
3450 local_irq_save(flags);
3451 __napi_complete(n);
3452 local_irq_restore(flags);
3453}
3454EXPORT_SYMBOL(napi_complete);
3455
3456void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
3457 int (*poll)(struct napi_struct *, int), int weight)
3458{
3459 INIT_LIST_HEAD(&napi->poll_list);
Herbert Xu4ae55442009-02-08 18:00:36 +00003460 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003461 napi->gro_list = NULL;
Herbert Xu5d38a072009-01-04 16:13:40 -08003462 napi->skb = NULL;
Herbert Xud565b0a2008-12-15 23:38:52 -08003463 napi->poll = poll;
3464 napi->weight = weight;
3465 list_add(&napi->dev_list, &dev->napi_list);
Herbert Xud565b0a2008-12-15 23:38:52 -08003466 napi->dev = dev;
Herbert Xu5d38a072009-01-04 16:13:40 -08003467#ifdef CONFIG_NETPOLL
Herbert Xud565b0a2008-12-15 23:38:52 -08003468 spin_lock_init(&napi->poll_lock);
3469 napi->poll_owner = -1;
3470#endif
3471 set_bit(NAPI_STATE_SCHED, &napi->state);
3472}
3473EXPORT_SYMBOL(netif_napi_add);
3474
3475void netif_napi_del(struct napi_struct *napi)
3476{
3477 struct sk_buff *skb, *next;
3478
Peter P Waskiewicz Jrd7b06632008-12-26 01:35:35 -08003479 list_del_init(&napi->dev_list);
Herbert Xu76620aa2009-04-16 02:02:07 -07003480 napi_free_frags(napi);
Herbert Xud565b0a2008-12-15 23:38:52 -08003481
3482 for (skb = napi->gro_list; skb; skb = next) {
3483 next = skb->next;
3484 skb->next = NULL;
3485 kfree_skb(skb);
3486 }
3487
3488 napi->gro_list = NULL;
Herbert Xu4ae55442009-02-08 18:00:36 +00003489 napi->gro_count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08003490}
3491EXPORT_SYMBOL(netif_napi_del);
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static void net_rx_action(struct softirq_action *h)
3494{
Eric Dumazete326bed2010-04-22 00:22:45 -07003495 struct softnet_data *sd = &__get_cpu_var(softnet_data);
Stephen Hemminger24f8b232008-11-03 17:14:38 -08003496 unsigned long time_limit = jiffies + 2;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07003497 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07003498 void *have;
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 local_irq_disable();
3501
Eric Dumazete326bed2010-04-22 00:22:45 -07003502 while (!list_empty(&sd->poll_list)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003503 struct napi_struct *n;
3504 int work, weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003506 /* If softirq window is exhuasted then punt.
Stephen Hemminger24f8b232008-11-03 17:14:38 -08003507 * Allow this to run for 2 jiffies since which will allow
3508 * an average latency of 1.5/HZ.
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003509 */
Stephen Hemminger24f8b232008-11-03 17:14:38 -08003510 if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 goto softnet_break;
3512
3513 local_irq_enable();
3514
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003515 /* Even though interrupts have been re-enabled, this
3516 * access is safe because interrupts can only add new
3517 * entries to the tail of this list, and only ->poll()
3518 * calls can remove this head entry from the list.
3519 */
Eric Dumazete326bed2010-04-22 00:22:45 -07003520 n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003522 have = netpoll_poll_lock(n);
3523
3524 weight = n->weight;
3525
David S. Miller0a7606c2007-10-29 21:28:47 -07003526 /* This NAPI_STATE_SCHED test is for avoiding a race
3527 * with netpoll's poll_napi(). Only the entity which
3528 * obtains the lock and sees NAPI_STATE_SCHED set will
3529 * actually make the ->poll() call. Therefore we avoid
3530 * accidently calling ->poll() when NAPI is not scheduled.
3531 */
3532 work = 0;
Neil Horman4ea7e382009-05-21 07:36:08 +00003533 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
David S. Miller0a7606c2007-10-29 21:28:47 -07003534 work = n->poll(n, weight);
Neil Horman4ea7e382009-05-21 07:36:08 +00003535 trace_napi_poll(n);
3536 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003537
3538 WARN_ON_ONCE(work > weight);
3539
3540 budget -= work;
3541
3542 local_irq_disable();
3543
3544 /* Drivers must not modify the NAPI state if they
3545 * consume the entire weight. In such cases this code
3546 * still "owns" the NAPI instance and therefore can
3547 * move the instance around on the list at-will.
3548 */
David S. Millerfed17f32008-01-07 21:00:40 -08003549 if (unlikely(work == weight)) {
Herbert Xuff780cd2009-06-26 19:27:04 -07003550 if (unlikely(napi_disable_pending(n))) {
3551 local_irq_enable();
3552 napi_complete(n);
3553 local_irq_disable();
3554 } else
Eric Dumazete326bed2010-04-22 00:22:45 -07003555 list_move_tail(&n->poll_list, &sd->poll_list);
David S. Millerfed17f32008-01-07 21:00:40 -08003556 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003557
3558 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560out:
Eric Dumazete326bed2010-04-22 00:22:45 -07003561 net_rps_action_and_irq_enable(sd);
Tom Herbert0a9627f2010-03-16 08:03:29 +00003562
Chris Leechdb217332006-06-17 21:24:58 -07003563#ifdef CONFIG_NET_DMA
3564 /*
3565 * There may not be any more sk_buffs coming right now, so push
3566 * any pending DMA copies to hardware
3567 */
Dan Williams2ba05622009-01-06 11:38:14 -07003568 dma_issue_pending_all();
Chris Leechdb217332006-06-17 21:24:58 -07003569#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return;
3572
3573softnet_break:
Changli Gaodee42872010-05-02 05:42:16 +00003574 sd->time_squeeze++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3576 goto out;
3577}
3578
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003579static gifconf_func_t *gifconf_list[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581/**
3582 * register_gifconf - register a SIOCGIF handler
3583 * @family: Address family
3584 * @gifconf: Function handler
3585 *
3586 * Register protocol dependent address dumping routines. The handler
3587 * that is passed must not be freed or reused until it has been replaced
3588 * by another handler.
3589 */
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003590int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
3592 if (family >= NPROTO)
3593 return -EINVAL;
3594 gifconf_list[family] = gifconf;
3595 return 0;
3596}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07003597EXPORT_SYMBOL(register_gifconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599
3600/*
3601 * Map an interface index to its name (SIOCGIFNAME)
3602 */
3603
3604/*
3605 * We need this ioctl for efficient implementation of the
3606 * if_indextoname() function required by the IPv6 API. Without
3607 * it, we would have to search all the interfaces to find a
3608 * match. --pb
3609 */
3610
Eric W. Biederman881d9662007-09-17 11:56:21 -07003611static int dev_ifname(struct net *net, struct ifreq __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 struct net_device *dev;
3614 struct ifreq ifr;
3615
3616 /*
3617 * Fetch the caller's info block.
3618 */
3619
3620 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3621 return -EFAULT;
3622
Eric Dumazetfb699dfd2009-10-19 19:18:49 +00003623 rcu_read_lock();
3624 dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 if (!dev) {
Eric Dumazetfb699dfd2009-10-19 19:18:49 +00003626 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return -ENODEV;
3628 }
3629
3630 strcpy(ifr.ifr_name, dev->name);
Eric Dumazetfb699dfd2009-10-19 19:18:49 +00003631 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
3634 return -EFAULT;
3635 return 0;
3636}
3637
3638/*
3639 * Perform a SIOCGIFCONF call. This structure will change
3640 * size eventually, and there is nothing I can do about it.
3641 * Thus we will need a 'compatibility mode'.
3642 */
3643
Eric W. Biederman881d9662007-09-17 11:56:21 -07003644static int dev_ifconf(struct net *net, char __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 struct ifconf ifc;
3647 struct net_device *dev;
3648 char __user *pos;
3649 int len;
3650 int total;
3651 int i;
3652
3653 /*
3654 * Fetch the caller's info block.
3655 */
3656
3657 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
3658 return -EFAULT;
3659
3660 pos = ifc.ifc_buf;
3661 len = ifc.ifc_len;
3662
3663 /*
3664 * Loop over the interfaces, and write an info block for each.
3665 */
3666
3667 total = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003668 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 for (i = 0; i < NPROTO; i++) {
3670 if (gifconf_list[i]) {
3671 int done;
3672 if (!pos)
3673 done = gifconf_list[i](dev, NULL, 0);
3674 else
3675 done = gifconf_list[i](dev, pos + total,
3676 len - total);
3677 if (done < 0)
3678 return -EFAULT;
3679 total += done;
3680 }
3681 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 /*
3685 * All done. Write the updated control block back to the caller.
3686 */
3687 ifc.ifc_len = total;
3688
3689 /*
3690 * Both BSD and Solaris return 0 here, so we do too.
3691 */
3692 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
3693}
3694
3695#ifdef CONFIG_PROC_FS
3696/*
3697 * This is invoked by the /proc filesystem handler to display a device
3698 * in detail.
3699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700void *dev_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003701 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Denis V. Luneve372c412007-11-19 22:31:54 -08003703 struct net *net = seq_file_net(seq);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003704 loff_t off;
3705 struct net_device *dev;
3706
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003707 rcu_read_lock();
Pavel Emelianov7562f872007-05-03 15:13:45 -07003708 if (!*pos)
3709 return SEQ_START_TOKEN;
3710
3711 off = 1;
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003712 for_each_netdev_rcu(net, dev)
Pavel Emelianov7562f872007-05-03 15:13:45 -07003713 if (off++ == *pos)
3714 return dev;
3715
3716 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
3719void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3720{
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003721 struct net_device *dev = (v == SEQ_START_TOKEN) ?
3722 first_net_device(seq_file_net(seq)) :
3723 next_net_device((struct net_device *)v);
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 ++*pos;
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003726 return rcu_dereference(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
3729void dev_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003730 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Eric Dumazetc6d14c82009-11-04 05:43:23 -08003732 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
3735static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
3736{
Eric Dumazet28172732010-07-07 14:58:56 -07003737 struct rtnl_link_stats64 temp;
3738 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003740 seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
3741 "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
Rusty Russell5a1b5892007-04-28 21:04:03 -07003742 dev->name, stats->rx_bytes, stats->rx_packets,
3743 stats->rx_errors,
3744 stats->rx_dropped + stats->rx_missed_errors,
3745 stats->rx_fifo_errors,
3746 stats->rx_length_errors + stats->rx_over_errors +
3747 stats->rx_crc_errors + stats->rx_frame_errors,
3748 stats->rx_compressed, stats->multicast,
3749 stats->tx_bytes, stats->tx_packets,
3750 stats->tx_errors, stats->tx_dropped,
3751 stats->tx_fifo_errors, stats->collisions,
3752 stats->tx_carrier_errors +
3753 stats->tx_aborted_errors +
3754 stats->tx_window_errors +
3755 stats->tx_heartbeat_errors,
3756 stats->tx_compressed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
3759/*
3760 * Called from the PROCfs module. This now uses the new arbitrary sized
3761 * /proc/net interface to create /proc/net/dev
3762 */
3763static int dev_seq_show(struct seq_file *seq, void *v)
3764{
3765 if (v == SEQ_START_TOKEN)
3766 seq_puts(seq, "Inter-| Receive "
3767 " | Transmit\n"
3768 " face |bytes packets errs drop fifo frame "
3769 "compressed multicast|bytes packets errs "
3770 "drop fifo colls carrier compressed\n");
3771 else
3772 dev_seq_printf_stats(seq, v);
3773 return 0;
3774}
3775
Changli Gaodee42872010-05-02 05:42:16 +00003776static struct softnet_data *softnet_get_online(loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
Changli Gaodee42872010-05-02 05:42:16 +00003778 struct softnet_data *sd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Mike Travis0c0b0ac2008-05-02 16:43:08 -07003780 while (*pos < nr_cpu_ids)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003781 if (cpu_online(*pos)) {
Changli Gaodee42872010-05-02 05:42:16 +00003782 sd = &per_cpu(softnet_data, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 break;
3784 } else
3785 ++*pos;
Changli Gaodee42872010-05-02 05:42:16 +00003786 return sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
3789static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
3790{
3791 return softnet_get_online(pos);
3792}
3793
3794static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3795{
3796 ++*pos;
3797 return softnet_get_online(pos);
3798}
3799
3800static void softnet_seq_stop(struct seq_file *seq, void *v)
3801{
3802}
3803
3804static int softnet_seq_show(struct seq_file *seq, void *v)
3805{
Changli Gaodee42872010-05-02 05:42:16 +00003806 struct softnet_data *sd = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Tom Herbert0a9627f2010-03-16 08:03:29 +00003808 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Changli Gaodee42872010-05-02 05:42:16 +00003809 sd->processed, sd->dropped, sd->time_squeeze, 0,
Stephen Hemmingerc1ebcdb2005-06-23 20:08:59 -07003810 0, 0, 0, 0, /* was fastroute */
Changli Gaodee42872010-05-02 05:42:16 +00003811 sd->cpu_collision, sd->received_rps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return 0;
3813}
3814
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003815static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 .start = dev_seq_start,
3817 .next = dev_seq_next,
3818 .stop = dev_seq_stop,
3819 .show = dev_seq_show,
3820};
3821
3822static int dev_seq_open(struct inode *inode, struct file *file)
3823{
Denis V. Luneve372c412007-11-19 22:31:54 -08003824 return seq_open_net(inode, file, &dev_seq_ops,
3825 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
Arjan van de Ven9a321442007-02-12 00:55:35 -08003828static const struct file_operations dev_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 .owner = THIS_MODULE,
3830 .open = dev_seq_open,
3831 .read = seq_read,
3832 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003833 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834};
3835
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003836static const struct seq_operations softnet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 .start = softnet_seq_start,
3838 .next = softnet_seq_next,
3839 .stop = softnet_seq_stop,
3840 .show = softnet_seq_show,
3841};
3842
3843static int softnet_seq_open(struct inode *inode, struct file *file)
3844{
3845 return seq_open(file, &softnet_seq_ops);
3846}
3847
Arjan van de Ven9a321442007-02-12 00:55:35 -08003848static const struct file_operations softnet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 .owner = THIS_MODULE,
3850 .open = softnet_seq_open,
3851 .read = seq_read,
3852 .llseek = seq_lseek,
3853 .release = seq_release,
3854};
3855
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003856static void *ptype_get_idx(loff_t pos)
3857{
3858 struct packet_type *pt = NULL;
3859 loff_t i = 0;
3860 int t;
3861
3862 list_for_each_entry_rcu(pt, &ptype_all, list) {
3863 if (i == pos)
3864 return pt;
3865 ++i;
3866 }
3867
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08003868 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003869 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
3870 if (i == pos)
3871 return pt;
3872 ++i;
3873 }
3874 }
3875 return NULL;
3876}
3877
3878static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemminger72348a42008-01-21 02:27:29 -08003879 __acquires(RCU)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003880{
3881 rcu_read_lock();
3882 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
3883}
3884
3885static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3886{
3887 struct packet_type *pt;
3888 struct list_head *nxt;
3889 int hash;
3890
3891 ++*pos;
3892 if (v == SEQ_START_TOKEN)
3893 return ptype_get_idx(0);
3894
3895 pt = v;
3896 nxt = pt->list.next;
3897 if (pt->type == htons(ETH_P_ALL)) {
3898 if (nxt != &ptype_all)
3899 goto found;
3900 hash = 0;
3901 nxt = ptype_base[0].next;
3902 } else
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08003903 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003904
3905 while (nxt == &ptype_base[hash]) {
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08003906 if (++hash >= PTYPE_HASH_SIZE)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003907 return NULL;
3908 nxt = ptype_base[hash].next;
3909 }
3910found:
3911 return list_entry(nxt, struct packet_type, list);
3912}
3913
3914static void ptype_seq_stop(struct seq_file *seq, void *v)
Stephen Hemminger72348a42008-01-21 02:27:29 -08003915 __releases(RCU)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003916{
3917 rcu_read_unlock();
3918}
3919
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003920static int ptype_seq_show(struct seq_file *seq, void *v)
3921{
3922 struct packet_type *pt = v;
3923
3924 if (v == SEQ_START_TOKEN)
3925 seq_puts(seq, "Type Device Function\n");
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003926 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003927 if (pt->type == htons(ETH_P_ALL))
3928 seq_puts(seq, "ALL ");
3929 else
3930 seq_printf(seq, "%04x", ntohs(pt->type));
3931
Alexey Dobriyan908cd2d2008-11-16 19:50:35 -08003932 seq_printf(seq, " %-8s %pF\n",
3933 pt->dev ? pt->dev->name : "", pt->func);
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003934 }
3935
3936 return 0;
3937}
3938
3939static const struct seq_operations ptype_seq_ops = {
3940 .start = ptype_seq_start,
3941 .next = ptype_seq_next,
3942 .stop = ptype_seq_stop,
3943 .show = ptype_seq_show,
3944};
3945
3946static int ptype_seq_open(struct inode *inode, struct file *file)
3947{
Pavel Emelyanov2feb27d2008-03-24 14:57:45 -07003948 return seq_open_net(inode, file, &ptype_seq_ops,
3949 sizeof(struct seq_net_private));
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003950}
3951
3952static const struct file_operations ptype_seq_fops = {
3953 .owner = THIS_MODULE,
3954 .open = ptype_seq_open,
3955 .read = seq_read,
3956 .llseek = seq_lseek,
Pavel Emelyanov2feb27d2008-03-24 14:57:45 -07003957 .release = seq_release_net,
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003958};
3959
3960
Pavel Emelyanov46650792007-10-08 20:38:39 -07003961static int __net_init dev_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
3963 int rc = -ENOMEM;
3964
Eric W. Biederman881d9662007-09-17 11:56:21 -07003965 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 goto out;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003967 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 goto out_dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003969 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003970 goto out_softnet;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07003971
Eric W. Biederman881d9662007-09-17 11:56:21 -07003972 if (wext_proc_init(net))
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003973 goto out_ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 rc = 0;
3975out:
3976 return rc;
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003977out_ptype:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003978 proc_net_remove(net, "ptype");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979out_softnet:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003980 proc_net_remove(net, "softnet_stat");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981out_dev:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003982 proc_net_remove(net, "dev");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 goto out;
3984}
Eric W. Biederman881d9662007-09-17 11:56:21 -07003985
Pavel Emelyanov46650792007-10-08 20:38:39 -07003986static void __net_exit dev_proc_net_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003987{
3988 wext_proc_exit(net);
3989
3990 proc_net_remove(net, "ptype");
3991 proc_net_remove(net, "softnet_stat");
3992 proc_net_remove(net, "dev");
3993}
3994
Denis V. Lunev022cbae2007-11-13 03:23:50 -08003995static struct pernet_operations __net_initdata dev_proc_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07003996 .init = dev_proc_net_init,
3997 .exit = dev_proc_net_exit,
3998};
3999
4000static int __init dev_proc_init(void)
4001{
4002 return register_pernet_subsys(&dev_proc_ops);
4003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004#else
4005#define dev_proc_init() 0
4006#endif /* CONFIG_PROC_FS */
4007
4008
4009/**
4010 * netdev_set_master - set up master/slave pair
4011 * @slave: slave device
4012 * @master: new master device
4013 *
4014 * Changes the master device of the slave. Pass %NULL to break the
4015 * bonding. The caller must hold the RTNL semaphore. On a failure
4016 * a negative errno code is returned. On success the reference counts
4017 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
4018 * function returns zero.
4019 */
4020int netdev_set_master(struct net_device *slave, struct net_device *master)
4021{
4022 struct net_device *old = slave->master;
4023
4024 ASSERT_RTNL();
4025
4026 if (master) {
4027 if (old)
4028 return -EBUSY;
4029 dev_hold(master);
4030 }
4031
4032 slave->master = master;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004033
Eric Dumazet283f2fe2010-03-18 13:37:40 +00004034 if (old) {
4035 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 dev_put(old);
Eric Dumazet283f2fe2010-03-18 13:37:40 +00004037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 if (master)
4039 slave->flags |= IFF_SLAVE;
4040 else
4041 slave->flags &= ~IFF_SLAVE;
4042
4043 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
4044 return 0;
4045}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004046EXPORT_SYMBOL(netdev_set_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004048static void dev_change_rx_flags(struct net_device *dev, int flags)
4049{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004050 const struct net_device_ops *ops = dev->netdev_ops;
4051
4052 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
4053 ops->ndo_change_rx_flags(dev, flags);
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004054}
4055
Wang Chendad9b332008-06-18 01:48:28 -07004056static int __dev_set_promiscuity(struct net_device *dev, int inc)
Patrick McHardy4417da62007-06-27 01:28:10 -07004057{
4058 unsigned short old_flags = dev->flags;
David Howells8192b0c2008-11-14 10:39:10 +11004059 uid_t uid;
4060 gid_t gid;
Patrick McHardy4417da62007-06-27 01:28:10 -07004061
Patrick McHardy24023452007-07-14 18:51:31 -07004062 ASSERT_RTNL();
4063
Wang Chendad9b332008-06-18 01:48:28 -07004064 dev->flags |= IFF_PROMISC;
4065 dev->promiscuity += inc;
4066 if (dev->promiscuity == 0) {
4067 /*
4068 * Avoid overflow.
4069 * If inc causes overflow, untouch promisc and return error.
4070 */
4071 if (inc < 0)
4072 dev->flags &= ~IFF_PROMISC;
4073 else {
4074 dev->promiscuity -= inc;
4075 printk(KERN_WARNING "%s: promiscuity touches roof, "
4076 "set promiscuity failed, promiscuity feature "
4077 "of device might be broken.\n", dev->name);
4078 return -EOVERFLOW;
4079 }
4080 }
Patrick McHardy4417da62007-06-27 01:28:10 -07004081 if (dev->flags != old_flags) {
4082 printk(KERN_INFO "device %s %s promiscuous mode\n",
4083 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
4084 "left");
David Howells8192b0c2008-11-14 10:39:10 +11004085 if (audit_enabled) {
4086 current_uid_gid(&uid, &gid);
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05004087 audit_log(current->audit_context, GFP_ATOMIC,
4088 AUDIT_ANOM_PROMISCUOUS,
4089 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
4090 dev->name, (dev->flags & IFF_PROMISC),
4091 (old_flags & IFF_PROMISC),
4092 audit_get_loginuid(current),
David Howells8192b0c2008-11-14 10:39:10 +11004093 uid, gid,
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05004094 audit_get_sessionid(current));
David Howells8192b0c2008-11-14 10:39:10 +11004095 }
Patrick McHardy24023452007-07-14 18:51:31 -07004096
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004097 dev_change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07004098 }
Wang Chendad9b332008-06-18 01:48:28 -07004099 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07004100}
4101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102/**
4103 * dev_set_promiscuity - update promiscuity count on a device
4104 * @dev: device
4105 * @inc: modifier
4106 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07004107 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 * remains above zero the interface remains promiscuous. Once it hits zero
4109 * the device reverts back to normal filtering operation. A negative inc
4110 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07004111 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 */
Wang Chendad9b332008-06-18 01:48:28 -07004113int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 unsigned short old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07004116 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Wang Chendad9b332008-06-18 01:48:28 -07004118 err = __dev_set_promiscuity(dev, inc);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07004119 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07004120 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07004121 if (dev->flags != old_flags)
4122 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07004123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004125EXPORT_SYMBOL(dev_set_promiscuity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127/**
4128 * dev_set_allmulti - update allmulti count on a device
4129 * @dev: device
4130 * @inc: modifier
4131 *
4132 * Add or remove reception of all multicast frames to a device. While the
4133 * count in the device remains above zero the interface remains listening
4134 * to all interfaces. Once it hits zero the device reverts back to normal
4135 * filtering operation. A negative @inc value is used to drop the counter
4136 * when releasing a resource needing all multicasts.
Wang Chendad9b332008-06-18 01:48:28 -07004137 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 */
4139
Wang Chendad9b332008-06-18 01:48:28 -07004140int dev_set_allmulti(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
4142 unsigned short old_flags = dev->flags;
4143
Patrick McHardy24023452007-07-14 18:51:31 -07004144 ASSERT_RTNL();
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07004147 dev->allmulti += inc;
4148 if (dev->allmulti == 0) {
4149 /*
4150 * Avoid overflow.
4151 * If inc causes overflow, untouch allmulti and return error.
4152 */
4153 if (inc < 0)
4154 dev->flags &= ~IFF_ALLMULTI;
4155 else {
4156 dev->allmulti -= inc;
4157 printk(KERN_WARNING "%s: allmulti touches roof, "
4158 "set allmulti failed, allmulti feature of "
4159 "device might be broken.\n", dev->name);
4160 return -EOVERFLOW;
4161 }
4162 }
Patrick McHardy24023452007-07-14 18:51:31 -07004163 if (dev->flags ^ old_flags) {
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004164 dev_change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07004165 dev_set_rx_mode(dev);
Patrick McHardy24023452007-07-14 18:51:31 -07004166 }
Wang Chendad9b332008-06-18 01:48:28 -07004167 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07004168}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004169EXPORT_SYMBOL(dev_set_allmulti);
Patrick McHardy4417da62007-06-27 01:28:10 -07004170
4171/*
4172 * Upload unicast and multicast address lists to device and
4173 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08004174 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07004175 * are present.
4176 */
4177void __dev_set_rx_mode(struct net_device *dev)
4178{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004179 const struct net_device_ops *ops = dev->netdev_ops;
4180
Patrick McHardy4417da62007-06-27 01:28:10 -07004181 /* dev_open will call this function so the list will stay sane. */
4182 if (!(dev->flags&IFF_UP))
4183 return;
4184
4185 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09004186 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07004187
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004188 if (ops->ndo_set_rx_mode)
4189 ops->ndo_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07004190 else {
4191 /* Unicast addresses changes may only happen under the rtnl,
4192 * therefore calling __dev_set_promiscuity here is safe.
4193 */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08004194 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
Patrick McHardy4417da62007-06-27 01:28:10 -07004195 __dev_set_promiscuity(dev, 1);
4196 dev->uc_promisc = 1;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08004197 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
Patrick McHardy4417da62007-06-27 01:28:10 -07004198 __dev_set_promiscuity(dev, -1);
4199 dev->uc_promisc = 0;
4200 }
4201
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004202 if (ops->ndo_set_multicast_list)
4203 ops->ndo_set_multicast_list(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07004204 }
4205}
4206
4207void dev_set_rx_mode(struct net_device *dev)
4208{
David S. Millerb9e40852008-07-15 00:15:08 -07004209 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07004210 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07004211 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004214/**
4215 * dev_get_flags - get flags reported to userspace
4216 * @dev: device
4217 *
4218 * Get the combination of flag bits exported through APIs to userspace.
4219 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220unsigned dev_get_flags(const struct net_device *dev)
4221{
4222 unsigned flags;
4223
4224 flags = (dev->flags & ~(IFF_PROMISC |
4225 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08004226 IFF_RUNNING |
4227 IFF_LOWER_UP |
4228 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 (dev->gflags & (IFF_PROMISC |
4230 IFF_ALLMULTI));
4231
Stefan Rompfb00055a2006-03-20 17:09:11 -08004232 if (netif_running(dev)) {
4233 if (netif_oper_up(dev))
4234 flags |= IFF_RUNNING;
4235 if (netif_carrier_ok(dev))
4236 flags |= IFF_LOWER_UP;
4237 if (netif_dormant(dev))
4238 flags |= IFF_DORMANT;
4239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 return flags;
4242}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004243EXPORT_SYMBOL(dev_get_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Patrick McHardybd380812010-02-26 06:34:53 +00004245int __dev_change_flags(struct net_device *dev, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 int old_flags = dev->flags;
Patrick McHardybd380812010-02-26 06:34:53 +00004248 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Patrick McHardy24023452007-07-14 18:51:31 -07004250 ASSERT_RTNL();
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 /*
4253 * Set the flags on our device.
4254 */
4255
4256 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
4257 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
4258 IFF_AUTOMEDIA)) |
4259 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
4260 IFF_ALLMULTI));
4261
4262 /*
4263 * Load in the correct multicast list now the flags have changed.
4264 */
4265
Patrick McHardyb6c40d62008-10-07 15:26:48 -07004266 if ((old_flags ^ flags) & IFF_MULTICAST)
4267 dev_change_rx_flags(dev, IFF_MULTICAST);
Patrick McHardy24023452007-07-14 18:51:31 -07004268
Patrick McHardy4417da62007-06-27 01:28:10 -07004269 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
4271 /*
4272 * Have we downed the interface. We handle IFF_UP ourselves
4273 * according to user attempts to set it, rather than blindly
4274 * setting it.
4275 */
4276
4277 ret = 0;
4278 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
Patrick McHardybd380812010-02-26 06:34:53 +00004279 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281 if (!ret)
Patrick McHardy4417da62007-06-27 01:28:10 -07004282 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
4284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 if ((flags ^ dev->gflags) & IFF_PROMISC) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004286 int inc = (flags & IFF_PROMISC) ? 1 : -1;
4287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 dev->gflags ^= IFF_PROMISC;
4289 dev_set_promiscuity(dev, inc);
4290 }
4291
4292 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
4293 is important. Some (broken) drivers set IFF_PROMISC, when
4294 IFF_ALLMULTI is requested not asking us and not reporting.
4295 */
4296 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004297 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 dev->gflags ^= IFF_ALLMULTI;
4300 dev_set_allmulti(dev, inc);
4301 }
4302
Patrick McHardybd380812010-02-26 06:34:53 +00004303 return ret;
4304}
4305
4306void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
4307{
4308 unsigned int changes = dev->flags ^ old_flags;
4309
4310 if (changes & IFF_UP) {
4311 if (dev->flags & IFF_UP)
4312 call_netdevice_notifiers(NETDEV_UP, dev);
4313 else
4314 call_netdevice_notifiers(NETDEV_DOWN, dev);
4315 }
4316
4317 if (dev->flags & IFF_UP &&
4318 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
4319 call_netdevice_notifiers(NETDEV_CHANGE, dev);
4320}
4321
4322/**
4323 * dev_change_flags - change device settings
4324 * @dev: device
4325 * @flags: device state flags
4326 *
4327 * Change settings on device based state flags. The flags are
4328 * in the userspace exported format.
4329 */
4330int dev_change_flags(struct net_device *dev, unsigned flags)
4331{
4332 int ret, changes;
4333 int old_flags = dev->flags;
4334
4335 ret = __dev_change_flags(dev, flags);
4336 if (ret < 0)
4337 return ret;
4338
4339 changes = old_flags ^ dev->flags;
Thomas Graf7c355f52007-06-05 16:03:03 -07004340 if (changes)
4341 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Patrick McHardybd380812010-02-26 06:34:53 +00004343 __dev_notify_flags(dev, old_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return ret;
4345}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004346EXPORT_SYMBOL(dev_change_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004348/**
4349 * dev_set_mtu - Change maximum transfer unit
4350 * @dev: device
4351 * @new_mtu: new transfer unit
4352 *
4353 * Change the maximum transfer size of the network device.
4354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355int dev_set_mtu(struct net_device *dev, int new_mtu)
4356{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004357 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 int err;
4359
4360 if (new_mtu == dev->mtu)
4361 return 0;
4362
4363 /* MTU must be positive. */
4364 if (new_mtu < 0)
4365 return -EINVAL;
4366
4367 if (!netif_device_present(dev))
4368 return -ENODEV;
4369
4370 err = 0;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004371 if (ops->ndo_change_mtu)
4372 err = ops->ndo_change_mtu(dev, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 else
4374 dev->mtu = new_mtu;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (!err && dev->flags & IFF_UP)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07004377 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 return err;
4379}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004380EXPORT_SYMBOL(dev_set_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07004382/**
4383 * dev_set_mac_address - Change Media Access Control Address
4384 * @dev: device
4385 * @sa: new address
4386 *
4387 * Change the hardware (MAC) address of the device
4388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
4390{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004391 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 int err;
4393
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004394 if (!ops->ndo_set_mac_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 return -EOPNOTSUPP;
4396 if (sa->sa_family != dev->type)
4397 return -EINVAL;
4398 if (!netif_device_present(dev))
4399 return -ENODEV;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004400 err = ops->ndo_set_mac_address(dev, sa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 if (!err)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07004402 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return err;
4404}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004405EXPORT_SYMBOL(dev_set_mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
4407/*
Eric Dumazet3710bec2009-11-01 19:42:09 +00004408 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 */
Jeff Garzik14e3e072007-10-08 00:06:32 -07004410static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
4412 int err;
Eric Dumazet3710bec2009-11-01 19:42:09 +00004413 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 if (!dev)
4416 return -ENODEV;
4417
4418 switch (cmd) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004419 case SIOCGIFFLAGS: /* Get interface flags */
4420 ifr->ifr_flags = (short) dev_get_flags(dev);
4421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004423 case SIOCGIFMETRIC: /* Get the metric on the interface
4424 (currently unused) */
4425 ifr->ifr_metric = 0;
4426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004428 case SIOCGIFMTU: /* Get the MTU of a device */
4429 ifr->ifr_mtu = dev->mtu;
4430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004432 case SIOCGIFHWADDR:
4433 if (!dev->addr_len)
4434 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
4435 else
4436 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
4437 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
4438 ifr->ifr_hwaddr.sa_family = dev->type;
4439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004441 case SIOCGIFSLAVE:
4442 err = -EINVAL;
4443 break;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004444
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004445 case SIOCGIFMAP:
4446 ifr->ifr_map.mem_start = dev->mem_start;
4447 ifr->ifr_map.mem_end = dev->mem_end;
4448 ifr->ifr_map.base_addr = dev->base_addr;
4449 ifr->ifr_map.irq = dev->irq;
4450 ifr->ifr_map.dma = dev->dma;
4451 ifr->ifr_map.port = dev->if_port;
4452 return 0;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004453
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004454 case SIOCGIFINDEX:
4455 ifr->ifr_ifindex = dev->ifindex;
4456 return 0;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004457
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004458 case SIOCGIFTXQLEN:
4459 ifr->ifr_qlen = dev->tx_queue_len;
4460 return 0;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004461
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004462 default:
4463 /* dev_ioctl() should ensure this case
4464 * is never reached
4465 */
4466 WARN_ON(1);
4467 err = -EINVAL;
4468 break;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004469
4470 }
4471 return err;
4472}
4473
4474/*
4475 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
4476 */
4477static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
4478{
4479 int err;
4480 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Jarek Poplawski5f2f6da2008-12-22 19:35:28 -08004481 const struct net_device_ops *ops;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004482
4483 if (!dev)
4484 return -ENODEV;
4485
Jarek Poplawski5f2f6da2008-12-22 19:35:28 -08004486 ops = dev->netdev_ops;
4487
Jeff Garzik14e3e072007-10-08 00:06:32 -07004488 switch (cmd) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004489 case SIOCSIFFLAGS: /* Set interface flags */
4490 return dev_change_flags(dev, ifr->ifr_flags);
Jeff Garzik14e3e072007-10-08 00:06:32 -07004491
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004492 case SIOCSIFMETRIC: /* Set the metric on the interface
4493 (currently unused) */
4494 return -EOPNOTSUPP;
Jeff Garzik14e3e072007-10-08 00:06:32 -07004495
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004496 case SIOCSIFMTU: /* Set the MTU of a device */
4497 return dev_set_mtu(dev, ifr->ifr_mtu);
Jeff Garzik14e3e072007-10-08 00:06:32 -07004498
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004499 case SIOCSIFHWADDR:
4500 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004502 case SIOCSIFHWBROADCAST:
4503 if (ifr->ifr_hwaddr.sa_family != dev->type)
4504 return -EINVAL;
4505 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
4506 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
4507 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
4508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004510 case SIOCSIFMAP:
4511 if (ops->ndo_set_config) {
4512 if (!netif_device_present(dev))
4513 return -ENODEV;
4514 return ops->ndo_set_config(dev, &ifr->ifr_map);
4515 }
4516 return -EOPNOTSUPP;
4517
4518 case SIOCADDMULTI:
4519 if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
4520 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
4521 return -EINVAL;
4522 if (!netif_device_present(dev))
4523 return -ENODEV;
Jiri Pirko22bedad32010-04-01 21:22:57 +00004524 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004525
4526 case SIOCDELMULTI:
4527 if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
4528 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
4529 return -EINVAL;
4530 if (!netif_device_present(dev))
4531 return -ENODEV;
Jiri Pirko22bedad32010-04-01 21:22:57 +00004532 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004533
4534 case SIOCSIFTXQLEN:
4535 if (ifr->ifr_qlen < 0)
4536 return -EINVAL;
4537 dev->tx_queue_len = ifr->ifr_qlen;
4538 return 0;
4539
4540 case SIOCSIFNAME:
4541 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
4542 return dev_change_name(dev, ifr->ifr_newname);
4543
4544 /*
4545 * Unknown or private ioctl
4546 */
4547 default:
4548 if ((cmd >= SIOCDEVPRIVATE &&
4549 cmd <= SIOCDEVPRIVATE + 15) ||
4550 cmd == SIOCBONDENSLAVE ||
4551 cmd == SIOCBONDRELEASE ||
4552 cmd == SIOCBONDSETHWADDR ||
4553 cmd == SIOCBONDSLAVEINFOQUERY ||
4554 cmd == SIOCBONDINFOQUERY ||
4555 cmd == SIOCBONDCHANGEACTIVE ||
4556 cmd == SIOCGMIIPHY ||
4557 cmd == SIOCGMIIREG ||
4558 cmd == SIOCSMIIREG ||
4559 cmd == SIOCBRADDIF ||
4560 cmd == SIOCBRDELIF ||
4561 cmd == SIOCSHWTSTAMP ||
4562 cmd == SIOCWANDEV) {
4563 err = -EOPNOTSUPP;
4564 if (ops->ndo_do_ioctl) {
4565 if (netif_device_present(dev))
4566 err = ops->ndo_do_ioctl(dev, ifr, cmd);
4567 else
4568 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004570 } else
4571 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
4573 }
4574 return err;
4575}
4576
4577/*
4578 * This function handles all "interface"-type I/O control requests. The actual
4579 * 'doing' part of this is dev_ifsioc above.
4580 */
4581
4582/**
4583 * dev_ioctl - network device ioctl
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07004584 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 * @cmd: command to issue
4586 * @arg: pointer to a struct ifreq in user space
4587 *
4588 * Issue ioctl functions to devices. This is normally called by the
4589 * user space syscall interfaces but can sometimes be useful for
4590 * other purposes. The return value is the return from the syscall if
4591 * positive or a negative errno code on error.
4592 */
4593
Eric W. Biederman881d9662007-09-17 11:56:21 -07004594int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
4596 struct ifreq ifr;
4597 int ret;
4598 char *colon;
4599
4600 /* One special case: SIOCGIFCONF takes ifconf argument
4601 and requires shared lock, because it sleeps writing
4602 to user space.
4603 */
4604
4605 if (cmd == SIOCGIFCONF) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08004606 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07004607 ret = dev_ifconf(net, (char __user *) arg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08004608 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return ret;
4610 }
4611 if (cmd == SIOCGIFNAME)
Eric W. Biederman881d9662007-09-17 11:56:21 -07004612 return dev_ifname(net, (struct ifreq __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
4615 return -EFAULT;
4616
4617 ifr.ifr_name[IFNAMSIZ-1] = 0;
4618
4619 colon = strchr(ifr.ifr_name, ':');
4620 if (colon)
4621 *colon = 0;
4622
4623 /*
4624 * See which interface the caller is talking about.
4625 */
4626
4627 switch (cmd) {
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004628 /*
4629 * These ioctl calls:
4630 * - can be done by all.
4631 * - atomic and do not require locking.
4632 * - return a value
4633 */
4634 case SIOCGIFFLAGS:
4635 case SIOCGIFMETRIC:
4636 case SIOCGIFMTU:
4637 case SIOCGIFHWADDR:
4638 case SIOCGIFSLAVE:
4639 case SIOCGIFMAP:
4640 case SIOCGIFINDEX:
4641 case SIOCGIFTXQLEN:
4642 dev_load(net, ifr.ifr_name);
Eric Dumazet3710bec2009-11-01 19:42:09 +00004643 rcu_read_lock();
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004644 ret = dev_ifsioc_locked(net, &ifr, cmd);
Eric Dumazet3710bec2009-11-01 19:42:09 +00004645 rcu_read_unlock();
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004646 if (!ret) {
4647 if (colon)
4648 *colon = ':';
4649 if (copy_to_user(arg, &ifr,
4650 sizeof(struct ifreq)))
4651 ret = -EFAULT;
4652 }
4653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004655 case SIOCETHTOOL:
4656 dev_load(net, ifr.ifr_name);
4657 rtnl_lock();
4658 ret = dev_ethtool(net, &ifr);
4659 rtnl_unlock();
4660 if (!ret) {
4661 if (colon)
4662 *colon = ':';
4663 if (copy_to_user(arg, &ifr,
4664 sizeof(struct ifreq)))
4665 ret = -EFAULT;
4666 }
4667 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004669 /*
4670 * These ioctl calls:
4671 * - require superuser power.
4672 * - require strict serialization.
4673 * - return a value
4674 */
4675 case SIOCGMIIPHY:
4676 case SIOCGMIIREG:
4677 case SIOCSIFNAME:
4678 if (!capable(CAP_NET_ADMIN))
4679 return -EPERM;
4680 dev_load(net, ifr.ifr_name);
4681 rtnl_lock();
4682 ret = dev_ifsioc(net, &ifr, cmd);
4683 rtnl_unlock();
4684 if (!ret) {
4685 if (colon)
4686 *colon = ':';
4687 if (copy_to_user(arg, &ifr,
4688 sizeof(struct ifreq)))
4689 ret = -EFAULT;
4690 }
4691 return ret;
4692
4693 /*
4694 * These ioctl calls:
4695 * - require superuser power.
4696 * - require strict serialization.
4697 * - do not return a value
4698 */
4699 case SIOCSIFFLAGS:
4700 case SIOCSIFMETRIC:
4701 case SIOCSIFMTU:
4702 case SIOCSIFMAP:
4703 case SIOCSIFHWADDR:
4704 case SIOCSIFSLAVE:
4705 case SIOCADDMULTI:
4706 case SIOCDELMULTI:
4707 case SIOCSIFHWBROADCAST:
4708 case SIOCSIFTXQLEN:
4709 case SIOCSMIIREG:
4710 case SIOCBONDENSLAVE:
4711 case SIOCBONDRELEASE:
4712 case SIOCBONDSETHWADDR:
4713 case SIOCBONDCHANGEACTIVE:
4714 case SIOCBRADDIF:
4715 case SIOCBRDELIF:
4716 case SIOCSHWTSTAMP:
4717 if (!capable(CAP_NET_ADMIN))
4718 return -EPERM;
4719 /* fall through */
4720 case SIOCBONDSLAVEINFOQUERY:
4721 case SIOCBONDINFOQUERY:
4722 dev_load(net, ifr.ifr_name);
4723 rtnl_lock();
4724 ret = dev_ifsioc(net, &ifr, cmd);
4725 rtnl_unlock();
4726 return ret;
4727
4728 case SIOCGIFMEM:
4729 /* Get the per device memory space. We can add this but
4730 * currently do not support it */
4731 case SIOCSIFMEM:
4732 /* Set the per device memory buffer space.
4733 * Not applicable in our case */
4734 case SIOCSIFLINK:
4735 return -EINVAL;
4736
4737 /*
4738 * Unknown or private ioctl.
4739 */
4740 default:
4741 if (cmd == SIOCWANDEV ||
4742 (cmd >= SIOCDEVPRIVATE &&
4743 cmd <= SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07004744 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07004746 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 rtnl_unlock();
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004748 if (!ret && copy_to_user(arg, &ifr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 sizeof(struct ifreq)))
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004750 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 return ret;
Eric Dumazetd1b19df2009-09-03 01:29:39 -07004752 }
4753 /* Take care of Wireless Extensions */
4754 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
4755 return wext_handle_ioctl(net, &ifr, cmd, arg);
4756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 }
4758}
4759
4760
4761/**
4762 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07004763 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 *
4765 * Returns a suitable unique value for a new device interface
4766 * number. The caller must hold the rtnl semaphore or the
4767 * dev_base_lock to be sure it remains unique.
4768 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07004769static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
4771 static int ifindex;
4772 for (;;) {
4773 if (++ifindex <= 0)
4774 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004775 if (!__dev_get_by_index(net, ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 return ifindex;
4777 }
4778}
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780/* Delayed registration/unregisteration */
Denis Cheng3b5b34f2007-12-07 00:49:17 -08004781static LIST_HEAD(net_todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Stephen Hemminger6f05f622007-03-08 20:46:03 -08004783static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 list_add_tail(&dev->todo_list, &net_todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004788static void rollback_registered_many(struct list_head *head)
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004789{
Krishna Kumare93737b2009-12-08 22:26:02 +00004790 struct net_device *dev, *tmp;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004791
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004792 BUG_ON(dev_boot_phase);
4793 ASSERT_RTNL();
4794
Krishna Kumare93737b2009-12-08 22:26:02 +00004795 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004796 /* Some devices call without registering
Krishna Kumare93737b2009-12-08 22:26:02 +00004797 * for initialization unwind. Remove those
4798 * devices and proceed with the remaining.
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004799 */
4800 if (dev->reg_state == NETREG_UNINITIALIZED) {
4801 pr_debug("unregister_netdevice: device %s/%p never "
4802 "was registered\n", dev->name, dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004803
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004804 WARN_ON(1);
Krishna Kumare93737b2009-12-08 22:26:02 +00004805 list_del(&dev->unreg_list);
4806 continue;
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004807 }
4808
4809 BUG_ON(dev->reg_state != NETREG_REGISTERED);
4810
4811 /* If device is running, close it first. */
4812 dev_close(dev);
4813
4814 /* And unlink it from device chain. */
4815 unlist_netdevice(dev);
4816
4817 dev->reg_state = NETREG_UNREGISTERING;
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004818 }
4819
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004820 synchronize_net();
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004821
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004822 list_for_each_entry(dev, head, unreg_list) {
4823 /* Shutdown queueing discipline. */
4824 dev_shutdown(dev);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004825
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004826
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004827 /* Notify protocols, that we are about to destroy
4828 this device. They should clean all the things.
4829 */
4830 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
4831
Patrick McHardya2835762010-02-26 06:34:51 +00004832 if (!dev->rtnl_link_ops ||
4833 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
4834 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
4835
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004836 /*
4837 * Flush the unicast and multicast chains
4838 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00004839 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004840 dev_mc_flush(dev);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004841
4842 if (dev->netdev_ops->ndo_uninit)
4843 dev->netdev_ops->ndo_uninit(dev);
4844
4845 /* Notifier chain MUST detach us from master device. */
4846 WARN_ON(dev->master);
4847
4848 /* Remove entries from kobject tree */
4849 netdev_unregister_kobject(dev);
4850 }
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004851
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00004852 /* Process any work delayed until the end of the batch */
stephen hemmingere5e26d72010-02-24 14:01:38 +00004853 dev = list_first_entry(head, struct net_device, unreg_list);
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00004854 call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
4855
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004856 synchronize_net();
4857
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00004858 list_for_each_entry(dev, head, unreg_list)
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004859 dev_put(dev);
4860}
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004861
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004862static void rollback_registered(struct net_device *dev)
4863{
4864 LIST_HEAD(single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004865
Eric Dumazet9b5e3832009-10-27 07:04:19 +00004866 list_add(&dev->unreg_list, &single);
4867 rollback_registered_many(&single);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004868}
4869
David S. Millere8a04642008-07-17 00:34:19 -07004870static void __netdev_init_queue_locks_one(struct net_device *dev,
4871 struct netdev_queue *dev_queue,
4872 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004873{
4874 spin_lock_init(&dev_queue->_xmit_lock);
David S. Millercf508b12008-07-22 14:16:42 -07004875 netdev_set_xmit_lockdep_class(&dev_queue->_xmit_lock, dev->type);
David S. Millerc773e842008-07-08 23:13:53 -07004876 dev_queue->xmit_lock_owner = -1;
4877}
4878
4879static void netdev_init_queue_locks(struct net_device *dev)
4880{
David S. Millere8a04642008-07-17 00:34:19 -07004881 netdev_for_each_tx_queue(dev, __netdev_init_queue_locks_one, NULL);
4882 __netdev_init_queue_locks_one(dev, &dev->rx_queue, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004883}
4884
Herbert Xub63365a2008-10-23 01:11:29 -07004885unsigned long netdev_fix_features(unsigned long features, const char *name)
4886{
4887 /* Fix illegal SG+CSUM combinations. */
4888 if ((features & NETIF_F_SG) &&
4889 !(features & NETIF_F_ALL_CSUM)) {
4890 if (name)
4891 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no "
4892 "checksum feature.\n", name);
4893 features &= ~NETIF_F_SG;
4894 }
4895
4896 /* TSO requires that SG is present as well. */
4897 if ((features & NETIF_F_TSO) && !(features & NETIF_F_SG)) {
4898 if (name)
4899 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no "
4900 "SG feature.\n", name);
4901 features &= ~NETIF_F_TSO;
4902 }
4903
4904 if (features & NETIF_F_UFO) {
4905 if (!(features & NETIF_F_GEN_CSUM)) {
4906 if (name)
4907 printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
4908 "since no NETIF_F_HW_CSUM feature.\n",
4909 name);
4910 features &= ~NETIF_F_UFO;
4911 }
4912
4913 if (!(features & NETIF_F_SG)) {
4914 if (name)
4915 printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
4916 "since no NETIF_F_SG feature.\n", name);
4917 features &= ~NETIF_F_UFO;
4918 }
4919 }
4920
4921 return features;
4922}
4923EXPORT_SYMBOL(netdev_fix_features);
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925/**
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -08004926 * netif_stacked_transfer_operstate - transfer operstate
4927 * @rootdev: the root or lower level device to transfer state from
4928 * @dev: the device to transfer operstate to
4929 *
4930 * Transfer operational state from root to device. This is normally
4931 * called when a stacking relationship exists between the root
4932 * device and the device(a leaf device).
4933 */
4934void netif_stacked_transfer_operstate(const struct net_device *rootdev,
4935 struct net_device *dev)
4936{
4937 if (rootdev->operstate == IF_OPER_DORMANT)
4938 netif_dormant_on(dev);
4939 else
4940 netif_dormant_off(dev);
4941
4942 if (netif_carrier_ok(rootdev)) {
4943 if (!netif_carrier_ok(dev))
4944 netif_carrier_on(dev);
4945 } else {
4946 if (netif_carrier_ok(dev))
4947 netif_carrier_off(dev);
4948 }
4949}
4950EXPORT_SYMBOL(netif_stacked_transfer_operstate);
4951
4952/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 * register_netdevice - register a network device
4954 * @dev: device to register
4955 *
4956 * Take a completed network device structure and add it to the kernel
4957 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4958 * chain. 0 is returned on success. A negative errno code is returned
4959 * on a failure to set up the device, or if the name is a duplicate.
4960 *
4961 * Callers must hold the rtnl semaphore. You may want
4962 * register_netdev() instead of this.
4963 *
4964 * BUGS:
4965 * The locking appears insufficient to guarantee two parallel registers
4966 * will not get the same name.
4967 */
4968
4969int register_netdevice(struct net_device *dev)
4970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 int ret;
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004972 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 BUG_ON(dev_boot_phase);
4975 ASSERT_RTNL();
4976
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004977 might_sleep();
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 /* When net_device's are persistent, this will be fatal. */
4980 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08004981 BUG_ON(!net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
David S. Millerf1f28aa2008-07-15 00:08:33 -07004983 spin_lock_init(&dev->addr_list_lock);
David S. Millercf508b12008-07-22 14:16:42 -07004984 netdev_set_addr_lockdep_class(dev);
David S. Millerc773e842008-07-08 23:13:53 -07004985 netdev_init_queue_locks(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 dev->iflink = -1;
4988
Eric Dumazetdf334542010-03-24 19:13:54 +00004989#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +00004990 if (!dev->num_rx_queues) {
4991 /*
4992 * Allocate a single RX queue if driver never called
4993 * alloc_netdev_mq
4994 */
4995
4996 dev->_rx = kzalloc(sizeof(struct netdev_rx_queue), GFP_KERNEL);
4997 if (!dev->_rx) {
4998 ret = -ENOMEM;
4999 goto out;
5000 }
5001
5002 dev->_rx->first = dev->_rx;
5003 atomic_set(&dev->_rx->count, 1);
5004 dev->num_rx_queues = 1;
5005 }
Eric Dumazetdf334542010-03-24 19:13:54 +00005006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 /* Init, if this function is available */
Stephen Hemmingerd3147742008-11-19 21:32:24 -08005008 if (dev->netdev_ops->ndo_init) {
5009 ret = dev->netdev_ops->ndo_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 if (ret) {
5011 if (ret > 0)
5012 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08005013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
5015 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005016
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00005017 ret = dev_get_valid_name(dev, dev->name, 0);
Octavian Purdilad9031022009-11-18 02:36:59 +00005018 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005019 goto err_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Eric W. Biederman881d9662007-09-17 11:56:21 -07005021 dev->ifindex = dev_new_index(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 if (dev->iflink == -1)
5023 dev->iflink = dev->ifindex;
5024
Stephen Hemmingerd212f872007-06-27 00:47:37 -07005025 /* Fix illegal checksum combinations */
5026 if ((dev->features & NETIF_F_HW_CSUM) &&
5027 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5028 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
5029 dev->name);
5030 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5031 }
5032
5033 if ((dev->features & NETIF_F_NO_CSUM) &&
5034 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5035 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
5036 dev->name);
5037 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
5038 }
5039
Herbert Xub63365a2008-10-23 01:11:29 -07005040 dev->features = netdev_fix_features(dev->features, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Lennert Buytenheke5a4a722008-08-03 01:23:10 -07005042 /* Enable software GSO if SG is supported. */
5043 if (dev->features & NETIF_F_SG)
5044 dev->features |= NETIF_F_GSO;
5045
Johannes Berg7ffbe3f2009-10-02 05:15:27 +00005046 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
5047 ret = notifier_to_errno(ret);
5048 if (ret)
5049 goto err_uninit;
5050
Eric W. Biederman8b41d182007-09-26 22:02:53 -07005051 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005052 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005053 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005054 dev->reg_state = NETREG_REGISTERED;
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 /*
5057 * Default initial state at registry is that the
5058 * device is present.
5059 */
5060
5061 set_bit(__LINK_STATE_PRESENT, &dev->state);
5062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005065 list_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
5067 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07005068 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07005069 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07005070 if (ret) {
5071 rollback_registered(dev);
5072 dev->reg_state = NETREG_UNREGISTERED;
5073 }
Eric W. Biedermand90a9092009-12-12 22:11:15 +00005074 /*
5075 * Prevent userspace races by waiting until the network
5076 * device is fully setup before sending notifications.
5077 */
Patrick McHardya2835762010-02-26 06:34:51 +00005078 if (!dev->rtnl_link_ops ||
5079 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5080 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
5082out:
5083 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005084
5085err_uninit:
Stephen Hemmingerd3147742008-11-19 21:32:24 -08005086 if (dev->netdev_ops->ndo_uninit)
5087 dev->netdev_ops->ndo_uninit(dev);
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07005088 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005090EXPORT_SYMBOL(register_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
5092/**
Benjamin Herrenschmidt937f1ba2009-01-14 21:05:05 -08005093 * init_dummy_netdev - init a dummy network device for NAPI
5094 * @dev: device to init
5095 *
5096 * This takes a network device structure and initialize the minimum
5097 * amount of fields so it can be used to schedule NAPI polls without
5098 * registering a full blown interface. This is to be used by drivers
5099 * that need to tie several hardware interfaces to a single NAPI
5100 * poll scheduler due to HW limitations.
5101 */
5102int init_dummy_netdev(struct net_device *dev)
5103{
5104 /* Clear everything. Note we don't initialize spinlocks
5105 * are they aren't supposed to be taken by any of the
5106 * NAPI code and this dummy netdev is supposed to be
5107 * only ever used for NAPI polls
5108 */
5109 memset(dev, 0, sizeof(struct net_device));
5110
5111 /* make sure we BUG if trying to hit standard
5112 * register/unregister code path
5113 */
5114 dev->reg_state = NETREG_DUMMY;
5115
5116 /* initialize the ref count */
5117 atomic_set(&dev->refcnt, 1);
5118
5119 /* NAPI wants this */
5120 INIT_LIST_HEAD(&dev->napi_list);
5121
5122 /* a dummy interface is started by default */
5123 set_bit(__LINK_STATE_PRESENT, &dev->state);
5124 set_bit(__LINK_STATE_START, &dev->state);
5125
5126 return 0;
5127}
5128EXPORT_SYMBOL_GPL(init_dummy_netdev);
5129
5130
5131/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 * register_netdev - register a network device
5133 * @dev: device to register
5134 *
5135 * Take a completed network device structure and add it to the kernel
5136 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
5137 * chain. 0 is returned on success. A negative errno code is returned
5138 * on a failure to set up the device, or if the name is a duplicate.
5139 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07005140 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 * and expands the device name if you passed a format string to
5142 * alloc_netdev.
5143 */
5144int register_netdev(struct net_device *dev)
5145{
5146 int err;
5147
5148 rtnl_lock();
5149
5150 /*
5151 * If the name is a format string the caller wants us to do a
5152 * name allocation.
5153 */
5154 if (strchr(dev->name, '%')) {
5155 err = dev_alloc_name(dev, dev->name);
5156 if (err < 0)
5157 goto out;
5158 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 err = register_netdevice(dev);
5161out:
5162 rtnl_unlock();
5163 return err;
5164}
5165EXPORT_SYMBOL(register_netdev);
5166
5167/*
5168 * netdev_wait_allrefs - wait until all references are gone.
5169 *
5170 * This is called when unregistering network devices.
5171 *
5172 * Any protocol or device that holds a reference should register
5173 * for netdevice notification, and cleanup and put back the
5174 * reference if they receive an UNREGISTER event.
5175 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005176 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 */
5178static void netdev_wait_allrefs(struct net_device *dev)
5179{
5180 unsigned long rebroadcast_time, warning_time;
5181
Eric Dumazete014deb2009-11-17 05:59:21 +00005182 linkwatch_forget_dev(dev);
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 rebroadcast_time = warning_time = jiffies;
5185 while (atomic_read(&dev->refcnt) != 0) {
5186 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08005187 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
5189 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07005190 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00005191 /* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users
Octavian Purdila395264d2009-11-16 13:49:35 +00005192 * should have already handle it the first time */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
5194 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
5195 &dev->state)) {
5196 /* We must not have linkwatch events
5197 * pending on unregister. If this
5198 * happens, we simply run the queue
5199 * unscheduled, resulting in a noop
5200 * for this device.
5201 */
5202 linkwatch_run_queue();
5203 }
5204
Stephen Hemminger6756ae42006-03-20 22:23:58 -08005205 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
5207 rebroadcast_time = jiffies;
5208 }
5209
5210 msleep(250);
5211
5212 if (time_after(jiffies, warning_time + 10 * HZ)) {
5213 printk(KERN_EMERG "unregister_netdevice: "
5214 "waiting for %s to become free. Usage "
5215 "count = %d\n",
5216 dev->name, atomic_read(&dev->refcnt));
5217 warning_time = jiffies;
5218 }
5219 }
5220}
5221
5222/* The sequence is:
5223 *
5224 * rtnl_lock();
5225 * ...
5226 * register_netdevice(x1);
5227 * register_netdevice(x2);
5228 * ...
5229 * unregister_netdevice(y1);
5230 * unregister_netdevice(y2);
5231 * ...
5232 * rtnl_unlock();
5233 * free_netdev(y1);
5234 * free_netdev(y2);
5235 *
Herbert Xu58ec3b42008-10-07 15:50:03 -07005236 * We are invoked by rtnl_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005238 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 * without deadlocking with linkwatch via keventd.
5240 * 2) Since we run with the RTNL semaphore not held, we can sleep
5241 * safely in order to wait for the netdev refcnt to drop to zero.
Herbert Xu58ec3b42008-10-07 15:50:03 -07005242 *
5243 * We must not return until all unregister events added during
5244 * the interval the lock was held have been completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246void netdev_run_todo(void)
5247{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07005248 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 /* Snapshot list, allow later requests */
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07005251 list_replace_init(&net_todo_list, &list);
Herbert Xu58ec3b42008-10-07 15:50:03 -07005252
5253 __rtnl_unlock();
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07005254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 while (!list_empty(&list)) {
5256 struct net_device *dev
stephen hemmingere5e26d72010-02-24 14:01:38 +00005257 = list_first_entry(&list, struct net_device, todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 list_del(&dev->todo_list);
5259
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005260 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 printk(KERN_ERR "network todo '%s' but state %d\n",
5262 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005263 dump_stack();
5264 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005266
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005267 dev->reg_state = NETREG_UNREGISTERED;
5268
Changli Gao152102c2010-03-30 20:16:22 +00005269 on_each_cpu(flush_backlog, dev, 1);
Stephen Hemminger6e583ce2008-08-03 21:29:57 -07005270
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005271 netdev_wait_allrefs(dev);
5272
5273 /* paranoia */
5274 BUG_ON(atomic_read(&dev->refcnt));
Ilpo Järvinen547b7922008-07-25 21:43:18 -07005275 WARN_ON(dev->ip_ptr);
5276 WARN_ON(dev->ip6_ptr);
5277 WARN_ON(dev->dn_ptr);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005278
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07005279 if (dev->destructor)
5280 dev->destructor(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07005281
5282 /* Free network device */
5283 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285}
5286
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005287/**
Eric Dumazetd83345a2009-11-16 03:36:51 +00005288 * dev_txq_stats_fold - fold tx_queues stats
5289 * @dev: device to get statistics from
Ben Hutchings3cfde792010-07-09 09:11:52 +00005290 * @stats: struct rtnl_link_stats64 to hold results
Eric Dumazetd83345a2009-11-16 03:36:51 +00005291 */
5292void dev_txq_stats_fold(const struct net_device *dev,
Ben Hutchings3cfde792010-07-09 09:11:52 +00005293 struct rtnl_link_stats64 *stats)
Eric Dumazetd83345a2009-11-16 03:36:51 +00005294{
Eric Dumazetbd272902010-07-19 09:35:40 -07005295 u64 tx_bytes = 0, tx_packets = 0, tx_dropped = 0;
Eric Dumazetd83345a2009-11-16 03:36:51 +00005296 unsigned int i;
5297 struct netdev_queue *txq;
5298
5299 for (i = 0; i < dev->num_tx_queues; i++) {
5300 txq = netdev_get_tx_queue(dev, i);
Eric Dumazetbd272902010-07-19 09:35:40 -07005301 spin_lock_bh(&txq->_xmit_lock);
Eric Dumazetd83345a2009-11-16 03:36:51 +00005302 tx_bytes += txq->tx_bytes;
5303 tx_packets += txq->tx_packets;
5304 tx_dropped += txq->tx_dropped;
Eric Dumazetbd272902010-07-19 09:35:40 -07005305 spin_unlock_bh(&txq->_xmit_lock);
Eric Dumazetd83345a2009-11-16 03:36:51 +00005306 }
5307 if (tx_bytes || tx_packets || tx_dropped) {
5308 stats->tx_bytes = tx_bytes;
5309 stats->tx_packets = tx_packets;
5310 stats->tx_dropped = tx_dropped;
5311 }
5312}
5313EXPORT_SYMBOL(dev_txq_stats_fold);
5314
Ben Hutchings3cfde792010-07-09 09:11:52 +00005315/* Convert net_device_stats to rtnl_link_stats64. They have the same
5316 * fields in the same order, with only the type differing.
5317 */
5318static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
5319 const struct net_device_stats *netdev_stats)
5320{
5321#if BITS_PER_LONG == 64
5322 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
5323 memcpy(stats64, netdev_stats, sizeof(*stats64));
5324#else
5325 size_t i, n = sizeof(*stats64) / sizeof(u64);
5326 const unsigned long *src = (const unsigned long *)netdev_stats;
5327 u64 *dst = (u64 *)stats64;
5328
5329 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
5330 sizeof(*stats64) / sizeof(u64));
5331 for (i = 0; i < n; i++)
5332 dst[i] = src[i];
5333#endif
5334}
5335
Eric Dumazetd83345a2009-11-16 03:36:51 +00005336/**
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005337 * dev_get_stats - get network device statistics
5338 * @dev: device to get statistics from
Eric Dumazet28172732010-07-07 14:58:56 -07005339 * @storage: place to store stats
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005340 *
Ben Hutchingsd7753512010-07-09 09:12:41 +00005341 * Get network statistics from device. Return @storage.
5342 * The device driver may provide its own method by setting
5343 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
5344 * otherwise the internal statistics structure is used.
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005345 */
Ben Hutchingsd7753512010-07-09 09:12:41 +00005346struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
5347 struct rtnl_link_stats64 *storage)
Eric Dumazet7004bf22009-05-18 00:34:33 +00005348{
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005349 const struct net_device_ops *ops = dev->netdev_ops;
5350
Eric Dumazet28172732010-07-07 14:58:56 -07005351 if (ops->ndo_get_stats64) {
5352 memset(storage, 0, sizeof(*storage));
5353 return ops->ndo_get_stats64(dev, storage);
5354 }
5355 if (ops->ndo_get_stats) {
Ben Hutchings3cfde792010-07-09 09:11:52 +00005356 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
Eric Dumazet28172732010-07-07 14:58:56 -07005357 return storage;
5358 }
Ben Hutchings3cfde792010-07-09 09:11:52 +00005359 netdev_stats_to_stats64(storage, &dev->stats);
5360 dev_txq_stats_fold(dev, storage);
Eric Dumazet28172732010-07-07 14:58:56 -07005361 return storage;
Rusty Russellc45d2862007-03-28 14:29:08 -07005362}
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08005363EXPORT_SYMBOL(dev_get_stats);
Rusty Russellc45d2862007-03-28 14:29:08 -07005364
David S. Millerdc2b4842008-07-08 17:18:23 -07005365static void netdev_init_one_queue(struct net_device *dev,
David S. Millere8a04642008-07-17 00:34:19 -07005366 struct netdev_queue *queue,
5367 void *_unused)
David S. Millerdc2b4842008-07-08 17:18:23 -07005368{
David S. Millerdc2b4842008-07-08 17:18:23 -07005369 queue->dev = dev;
5370}
5371
David S. Millerbb949fb2008-07-08 16:55:56 -07005372static void netdev_init_queues(struct net_device *dev)
5373{
David S. Millere8a04642008-07-17 00:34:19 -07005374 netdev_init_one_queue(dev, &dev->rx_queue, NULL);
5375 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
David S. Millerc3f26a22008-07-31 16:58:50 -07005376 spin_lock_init(&dev->tx_global_lock);
David S. Millerbb949fb2008-07-08 16:55:56 -07005377}
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379/**
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07005380 * alloc_netdev_mq - allocate network device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 * @sizeof_priv: size of private data to allocate space for
5382 * @name: device name format string
5383 * @setup: callback to initialize device
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07005384 * @queue_count: the number of subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 *
5386 * Allocates a struct net_device with private data area for driver use
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07005387 * and performs basic initialization. Also allocates subquue structs
5388 * for each queue on the device at the end of the netdevice.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 */
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07005390struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
5391 void (*setup)(struct net_device *), unsigned int queue_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
David S. Millere8a04642008-07-17 00:34:19 -07005393 struct netdev_queue *tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 struct net_device *dev;
Stephen Hemminger79439862008-07-21 13:28:44 -07005395 size_t alloc_size;
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005396 struct net_device *p;
Eric Dumazetdf334542010-03-24 19:13:54 +00005397#ifdef CONFIG_RPS
5398 struct netdev_rx_queue *rx;
Tom Herbert0a9627f2010-03-16 08:03:29 +00005399 int i;
Eric Dumazetdf334542010-03-24 19:13:54 +00005400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07005402 BUG_ON(strlen(name) >= sizeof(dev->name));
5403
David S. Millerfd2ea0a2008-07-17 01:56:23 -07005404 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07005405 if (sizeof_priv) {
5406 /* ensure 32-byte alignment of private area */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005407 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07005408 alloc_size += sizeof_priv;
5409 }
5410 /* ensure 32-byte alignment of whole construct */
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005411 alloc_size += NETDEV_ALIGN - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07005413 p = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 if (!p) {
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07005415 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 return NULL;
5417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
Stephen Hemminger79439862008-07-21 13:28:44 -07005419 tx = kcalloc(queue_count, sizeof(struct netdev_queue), GFP_KERNEL);
David S. Millere8a04642008-07-17 00:34:19 -07005420 if (!tx) {
5421 printk(KERN_ERR "alloc_netdev: Unable to allocate "
5422 "tx qdiscs.\n");
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005423 goto free_p;
David S. Millere8a04642008-07-17 00:34:19 -07005424 }
5425
Eric Dumazetdf334542010-03-24 19:13:54 +00005426#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +00005427 rx = kcalloc(queue_count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
5428 if (!rx) {
5429 printk(KERN_ERR "alloc_netdev: Unable to allocate "
5430 "rx queues.\n");
5431 goto free_tx;
5432 }
5433
5434 atomic_set(&rx->count, queue_count);
5435
5436 /*
5437 * Set a pointer to first element in the array which holds the
5438 * reference count.
5439 */
5440 for (i = 0; i < queue_count; i++)
5441 rx[i].first = rx;
Eric Dumazetdf334542010-03-24 19:13:54 +00005442#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00005443
Eric Dumazet1ce8e7b2009-05-27 04:42:37 +00005444 dev = PTR_ALIGN(p, NETDEV_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 dev->padded = (char *)dev - (char *)p;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005446
5447 if (dev_addr_init(dev))
Tom Herbert0a9627f2010-03-16 08:03:29 +00005448 goto free_rx;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005449
Jiri Pirko22bedad32010-04-01 21:22:57 +00005450 dev_mc_init(dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +00005451 dev_uc_init(dev);
Jiri Pirkoccffad252009-05-22 23:22:17 +00005452
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005453 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
David S. Millere8a04642008-07-17 00:34:19 -07005455 dev->_tx = tx;
5456 dev->num_tx_queues = queue_count;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07005457 dev->real_num_tx_queues = queue_count;
David S. Millere8a04642008-07-17 00:34:19 -07005458
Eric Dumazetdf334542010-03-24 19:13:54 +00005459#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +00005460 dev->_rx = rx;
5461 dev->num_rx_queues = queue_count;
Eric Dumazetdf334542010-03-24 19:13:54 +00005462#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00005463
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07005464 dev->gso_max_size = GSO_MAX_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
David S. Millerbb949fb2008-07-08 16:55:56 -07005466 netdev_init_queues(dev);
5467
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08005468 INIT_LIST_HEAD(&dev->ethtool_ntuple_list.list);
5469 dev->ethtool_ntuple_list.count = 0;
Herbert Xud565b0a2008-12-15 23:38:52 -08005470 INIT_LIST_HEAD(&dev->napi_list);
Eric W. Biederman9fdce092009-10-30 14:51:13 +00005471 INIT_LIST_HEAD(&dev->unreg_list);
Eric Dumazete014deb2009-11-17 05:59:21 +00005472 INIT_LIST_HEAD(&dev->link_watch_list);
Eric Dumazet93f154b2009-05-18 22:19:19 -07005473 dev->priv_flags = IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 setup(dev);
5475 strcpy(dev->name, name);
5476 return dev;
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005477
Tom Herbert0a9627f2010-03-16 08:03:29 +00005478free_rx:
Eric Dumazetdf334542010-03-24 19:13:54 +00005479#ifdef CONFIG_RPS
Tom Herbert0a9627f2010-03-16 08:03:29 +00005480 kfree(rx);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005481free_tx:
Eric Dumazetdf334542010-03-24 19:13:54 +00005482#endif
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005483 kfree(tx);
Jiri Pirkoab9c73c2009-05-08 13:30:17 +00005484free_p:
5485 kfree(p);
5486 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07005488EXPORT_SYMBOL(alloc_netdev_mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490/**
5491 * free_netdev - free network device
5492 * @dev: device
5493 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005494 * This function does the last stage of destroying an allocated device
5495 * interface. The reference to the device object is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 * If this is the last reference then it will be freed.
5497 */
5498void free_netdev(struct net_device *dev)
5499{
Herbert Xud565b0a2008-12-15 23:38:52 -08005500 struct napi_struct *p, *n;
5501
Denis V. Lunevf3005d72008-04-16 02:02:18 -07005502 release_net(dev_net(dev));
5503
David S. Millere8a04642008-07-17 00:34:19 -07005504 kfree(dev->_tx);
5505
Jiri Pirkof001fde2009-05-05 02:48:28 +00005506 /* Flush device addresses */
5507 dev_addr_flush(dev);
5508
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08005509 /* Clear ethtool n-tuple list */
5510 ethtool_ntuple_flush(dev);
5511
Herbert Xud565b0a2008-12-15 23:38:52 -08005512 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
5513 netif_napi_del(p);
5514
Stephen Hemminger3041a062006-05-26 13:25:24 -07005515 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 if (dev->reg_state == NETREG_UNINITIALIZED) {
5517 kfree((char *)dev - dev->padded);
5518 return;
5519 }
5520
5521 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
5522 dev->reg_state = NETREG_RELEASED;
5523
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07005524 /* will free via device release */
5525 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005527EXPORT_SYMBOL(free_netdev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005528
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07005529/**
5530 * synchronize_net - Synchronize with packet receive processing
5531 *
5532 * Wait for packets currently being received to be done.
5533 * Does not block later packets from starting.
5534 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09005535void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
5537 might_sleep();
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005538 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539}
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005540EXPORT_SYMBOL(synchronize_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
5542/**
Eric Dumazet44a08732009-10-27 07:03:04 +00005543 * unregister_netdevice_queue - remove device from the kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 * @dev: device
Eric Dumazet44a08732009-10-27 07:03:04 +00005545 * @head: list
Jaswinder Singh Rajput6ebfbc02009-11-22 20:43:13 -08005546 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08005548 * from the kernel tables.
Eric Dumazet44a08732009-10-27 07:03:04 +00005549 * If head not NULL, device is queued to be unregistered later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 *
5551 * Callers must hold the rtnl semaphore. You may want
5552 * unregister_netdev() instead of this.
5553 */
5554
Eric Dumazet44a08732009-10-27 07:03:04 +00005555void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556{
Herbert Xua6620712007-12-12 19:21:56 -08005557 ASSERT_RTNL();
5558
Eric Dumazet44a08732009-10-27 07:03:04 +00005559 if (head) {
Eric W. Biederman9fdce092009-10-30 14:51:13 +00005560 list_move_tail(&dev->unreg_list, head);
Eric Dumazet44a08732009-10-27 07:03:04 +00005561 } else {
5562 rollback_registered(dev);
5563 /* Finish processing unregister after unlock */
5564 net_set_todo(dev);
5565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566}
Eric Dumazet44a08732009-10-27 07:03:04 +00005567EXPORT_SYMBOL(unregister_netdevice_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
5569/**
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005570 * unregister_netdevice_many - unregister many devices
5571 * @head: list of devices
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005572 */
5573void unregister_netdevice_many(struct list_head *head)
5574{
5575 struct net_device *dev;
5576
5577 if (!list_empty(head)) {
5578 rollback_registered_many(head);
5579 list_for_each_entry(dev, head, unreg_list)
5580 net_set_todo(dev);
5581 }
5582}
Eric Dumazet63c80992009-10-27 07:06:49 +00005583EXPORT_SYMBOL(unregister_netdevice_many);
Eric Dumazet9b5e3832009-10-27 07:04:19 +00005584
5585/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 * unregister_netdev - remove device from the kernel
5587 * @dev: device
5588 *
5589 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08005590 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 *
5592 * This is just a wrapper for unregister_netdevice that takes
5593 * the rtnl semaphore. In general you want to use this and not
5594 * unregister_netdevice.
5595 */
5596void unregister_netdev(struct net_device *dev)
5597{
5598 rtnl_lock();
5599 unregister_netdevice(dev);
5600 rtnl_unlock();
5601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602EXPORT_SYMBOL(unregister_netdev);
5603
Eric W. Biedermance286d32007-09-12 13:53:49 +02005604/**
5605 * dev_change_net_namespace - move device to different nethost namespace
5606 * @dev: device
5607 * @net: network namespace
5608 * @pat: If not NULL name pattern to try if the current device name
5609 * is already taken in the destination network namespace.
5610 *
5611 * This function shuts down a device interface and moves it
5612 * to a new network namespace. On success 0 is returned, on
5613 * a failure a netagive errno code is returned.
5614 *
5615 * Callers must hold the rtnl semaphore.
5616 */
5617
5618int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
5619{
Eric W. Biedermance286d32007-09-12 13:53:49 +02005620 int err;
5621
5622 ASSERT_RTNL();
5623
5624 /* Don't allow namespace local devices to be moved. */
5625 err = -EINVAL;
5626 if (dev->features & NETIF_F_NETNS_LOCAL)
5627 goto out;
5628
5629 /* Ensure the device has been registrered */
5630 err = -EINVAL;
5631 if (dev->reg_state != NETREG_REGISTERED)
5632 goto out;
5633
5634 /* Get out if there is nothing todo */
5635 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09005636 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02005637 goto out;
5638
5639 /* Pick the destination device name, and ensure
5640 * we can use it in the destination network namespace.
5641 */
5642 err = -EEXIST;
Octavian Purdilad9031022009-11-18 02:36:59 +00005643 if (__dev_get_by_name(net, dev->name)) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02005644 /* We get here if we can't use the current device name */
5645 if (!pat)
5646 goto out;
Daniel Lezcano8ce6cebc2010-05-19 10:12:19 +00005647 if (dev_get_valid_name(dev, pat, 1))
Eric W. Biedermance286d32007-09-12 13:53:49 +02005648 goto out;
5649 }
5650
5651 /*
5652 * And now a mini version of register_netdevice unregister_netdevice.
5653 */
5654
5655 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07005656 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005657
5658 /* And unlink it from device chain */
5659 err = -ENODEV;
5660 unlist_netdevice(dev);
5661
5662 synchronize_net();
5663
5664 /* Shutdown queueing discipline. */
5665 dev_shutdown(dev);
5666
5667 /* Notify protocols, that we are about to destroy
5668 this device. They should clean all the things.
5669 */
5670 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Eric W. Biedermana5ee1552009-11-29 15:45:58 +00005671 call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005672
5673 /*
5674 * Flush the unicast and multicast chains
5675 */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00005676 dev_uc_flush(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005677 dev_mc_flush(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005678
5679 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005680 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005681
Eric W. Biedermance286d32007-09-12 13:53:49 +02005682 /* If there is an ifindex conflict assign a new one */
5683 if (__dev_get_by_index(net, dev->ifindex)) {
5684 int iflink = (dev->iflink == dev->ifindex);
5685 dev->ifindex = dev_new_index(net);
5686 if (iflink)
5687 dev->iflink = dev->ifindex;
5688 }
5689
Eric W. Biederman8b41d182007-09-26 22:02:53 -07005690 /* Fixup kobjects */
Eric W. Biedermana1b3f592010-05-04 17:36:49 -07005691 err = device_rename(&dev->dev, dev->name);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07005692 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005693
5694 /* Add the device back in the hashes */
5695 list_netdevice(dev);
5696
5697 /* Notify protocols, that a new device appeared. */
5698 call_netdevice_notifiers(NETDEV_REGISTER, dev);
5699
Eric W. Biedermand90a9092009-12-12 22:11:15 +00005700 /*
5701 * Prevent userspace races by waiting until the network
5702 * device is fully setup before sending notifications.
5703 */
5704 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
5705
Eric W. Biedermance286d32007-09-12 13:53:49 +02005706 synchronize_net();
5707 err = 0;
5708out:
5709 return err;
5710}
Johannes Berg463d0182009-07-14 00:33:35 +02005711EXPORT_SYMBOL_GPL(dev_change_net_namespace);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713static int dev_cpu_callback(struct notifier_block *nfb,
5714 unsigned long action,
5715 void *ocpu)
5716{
5717 struct sk_buff **list_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 struct sk_buff *skb;
5719 unsigned int cpu, oldcpu = (unsigned long)ocpu;
5720 struct softnet_data *sd, *oldsd;
5721
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005722 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 return NOTIFY_OK;
5724
5725 local_irq_disable();
5726 cpu = smp_processor_id();
5727 sd = &per_cpu(softnet_data, cpu);
5728 oldsd = &per_cpu(softnet_data, oldcpu);
5729
5730 /* Find end of our completion_queue. */
5731 list_skb = &sd->completion_queue;
5732 while (*list_skb)
5733 list_skb = &(*list_skb)->next;
5734 /* Append completion queue from offline CPU. */
5735 *list_skb = oldsd->completion_queue;
5736 oldsd->completion_queue = NULL;
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 /* Append output queue from offline CPU. */
Changli Gaoa9cbd582010-04-26 23:06:24 +00005739 if (oldsd->output_queue) {
5740 *sd->output_queue_tailp = oldsd->output_queue;
5741 sd->output_queue_tailp = oldsd->output_queue_tailp;
5742 oldsd->output_queue = NULL;
5743 oldsd->output_queue_tailp = &oldsd->output_queue;
5744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746 raise_softirq_irqoff(NET_TX_SOFTIRQ);
5747 local_irq_enable();
5748
5749 /* Process offline CPU's input_pkt_queue */
Tom Herbert76cc8b12010-05-20 18:37:59 +00005750 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
5751 netif_rx(skb);
5752 input_queue_head_incr(oldsd);
5753 }
Tom Herbertfec5e652010-04-16 16:01:27 -07005754 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 netif_rx(skb);
Tom Herbert76cc8b12010-05-20 18:37:59 +00005756 input_queue_head_incr(oldsd);
Tom Herbertfec5e652010-04-16 16:01:27 -07005757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
5759 return NOTIFY_OK;
5760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762
Herbert Xu7f353bf2007-08-10 15:47:58 -07005763/**
Herbert Xub63365a2008-10-23 01:11:29 -07005764 * netdev_increment_features - increment feature set by one
5765 * @all: current feature set
5766 * @one: new feature set
5767 * @mask: mask feature set
Herbert Xu7f353bf2007-08-10 15:47:58 -07005768 *
5769 * Computes a new feature set after adding a device with feature set
Herbert Xub63365a2008-10-23 01:11:29 -07005770 * @one to the master device with current feature set @all. Will not
5771 * enable anything that is off in @mask. Returns the new feature set.
Herbert Xu7f353bf2007-08-10 15:47:58 -07005772 */
Herbert Xub63365a2008-10-23 01:11:29 -07005773unsigned long netdev_increment_features(unsigned long all, unsigned long one,
5774 unsigned long mask)
Herbert Xu7f353bf2007-08-10 15:47:58 -07005775{
Herbert Xub63365a2008-10-23 01:11:29 -07005776 /* If device needs checksumming, downgrade to it. */
Eric Dumazetd1b19df2009-09-03 01:29:39 -07005777 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
Herbert Xub63365a2008-10-23 01:11:29 -07005778 all ^= NETIF_F_NO_CSUM | (one & NETIF_F_ALL_CSUM);
5779 else if (mask & NETIF_F_ALL_CSUM) {
5780 /* If one device supports v4/v6 checksumming, set for all. */
5781 if (one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM) &&
5782 !(all & NETIF_F_GEN_CSUM)) {
5783 all &= ~NETIF_F_ALL_CSUM;
5784 all |= one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
5785 }
Herbert Xu7f353bf2007-08-10 15:47:58 -07005786
Herbert Xub63365a2008-10-23 01:11:29 -07005787 /* If one device supports hw checksumming, set for all. */
5788 if (one & NETIF_F_GEN_CSUM && !(all & NETIF_F_GEN_CSUM)) {
5789 all &= ~NETIF_F_ALL_CSUM;
5790 all |= NETIF_F_HW_CSUM;
5791 }
5792 }
Herbert Xu7f353bf2007-08-10 15:47:58 -07005793
Herbert Xub63365a2008-10-23 01:11:29 -07005794 one |= NETIF_F_ALL_CSUM;
Herbert Xu7f353bf2007-08-10 15:47:58 -07005795
Herbert Xub63365a2008-10-23 01:11:29 -07005796 one |= all & NETIF_F_ONE_FOR_ALL;
Sridhar Samudralad9f59502009-10-07 12:24:25 +00005797 all &= one | NETIF_F_LLTX | NETIF_F_GSO | NETIF_F_UFO;
Herbert Xub63365a2008-10-23 01:11:29 -07005798 all |= one & mask & NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07005799
5800 return all;
5801}
Herbert Xub63365a2008-10-23 01:11:29 -07005802EXPORT_SYMBOL(netdev_increment_features);
Herbert Xu7f353bf2007-08-10 15:47:58 -07005803
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07005804static struct hlist_head *netdev_create_hash(void)
5805{
5806 int i;
5807 struct hlist_head *hash;
5808
5809 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
5810 if (hash != NULL)
5811 for (i = 0; i < NETDEV_HASHENTRIES; i++)
5812 INIT_HLIST_HEAD(&hash[i]);
5813
5814 return hash;
5815}
5816
Eric W. Biederman881d9662007-09-17 11:56:21 -07005817/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07005818static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07005819{
Eric W. Biederman881d9662007-09-17 11:56:21 -07005820 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07005821
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07005822 net->dev_name_head = netdev_create_hash();
5823 if (net->dev_name_head == NULL)
5824 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07005825
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07005826 net->dev_index_head = netdev_create_hash();
5827 if (net->dev_index_head == NULL)
5828 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07005829
5830 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07005831
5832err_idx:
5833 kfree(net->dev_name_head);
5834err_name:
5835 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07005836}
5837
Stephen Hemmingerf0db2752008-09-30 02:23:58 -07005838/**
5839 * netdev_drivername - network driver for the device
5840 * @dev: network device
5841 * @buffer: buffer for resulting name
5842 * @len: size of buffer
5843 *
5844 * Determine network driver for device.
5845 */
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07005846char *netdev_drivername(const struct net_device *dev, char *buffer, int len)
Arjan van de Ven6579e572008-07-21 13:31:48 -07005847{
Stephen Hemmingercf04a4c72008-09-30 02:22:14 -07005848 const struct device_driver *driver;
5849 const struct device *parent;
Arjan van de Ven6579e572008-07-21 13:31:48 -07005850
5851 if (len <= 0 || !buffer)
5852 return buffer;
5853 buffer[0] = 0;
5854
5855 parent = dev->dev.parent;
5856
5857 if (!parent)
5858 return buffer;
5859
5860 driver = parent->driver;
5861 if (driver && driver->name)
5862 strlcpy(buffer, driver->name, len);
5863 return buffer;
5864}
5865
Joe Perches256df2f2010-06-27 01:02:35 +00005866static int __netdev_printk(const char *level, const struct net_device *dev,
5867 struct va_format *vaf)
5868{
5869 int r;
5870
5871 if (dev && dev->dev.parent)
5872 r = dev_printk(level, dev->dev.parent, "%s: %pV",
5873 netdev_name(dev), vaf);
5874 else if (dev)
5875 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
5876 else
5877 r = printk("%s(NULL net_device): %pV", level, vaf);
5878
5879 return r;
5880}
5881
5882int netdev_printk(const char *level, const struct net_device *dev,
5883 const char *format, ...)
5884{
5885 struct va_format vaf;
5886 va_list args;
5887 int r;
5888
5889 va_start(args, format);
5890
5891 vaf.fmt = format;
5892 vaf.va = &args;
5893
5894 r = __netdev_printk(level, dev, &vaf);
5895 va_end(args);
5896
5897 return r;
5898}
5899EXPORT_SYMBOL(netdev_printk);
5900
5901#define define_netdev_printk_level(func, level) \
5902int func(const struct net_device *dev, const char *fmt, ...) \
5903{ \
5904 int r; \
5905 struct va_format vaf; \
5906 va_list args; \
5907 \
5908 va_start(args, fmt); \
5909 \
5910 vaf.fmt = fmt; \
5911 vaf.va = &args; \
5912 \
5913 r = __netdev_printk(level, dev, &vaf); \
5914 va_end(args); \
5915 \
5916 return r; \
5917} \
5918EXPORT_SYMBOL(func);
5919
5920define_netdev_printk_level(netdev_emerg, KERN_EMERG);
5921define_netdev_printk_level(netdev_alert, KERN_ALERT);
5922define_netdev_printk_level(netdev_crit, KERN_CRIT);
5923define_netdev_printk_level(netdev_err, KERN_ERR);
5924define_netdev_printk_level(netdev_warn, KERN_WARNING);
5925define_netdev_printk_level(netdev_notice, KERN_NOTICE);
5926define_netdev_printk_level(netdev_info, KERN_INFO);
5927
Pavel Emelyanov46650792007-10-08 20:38:39 -07005928static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07005929{
5930 kfree(net->dev_name_head);
5931 kfree(net->dev_index_head);
5932}
5933
Denis V. Lunev022cbae2007-11-13 03:23:50 -08005934static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07005935 .init = netdev_init,
5936 .exit = netdev_exit,
5937};
5938
Pavel Emelyanov46650792007-10-08 20:38:39 -07005939static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02005940{
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00005941 struct net_device *dev, *aux;
Eric W. Biedermance286d32007-09-12 13:53:49 +02005942 /*
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00005943 * Push all migratable network devices back to the
Eric W. Biedermance286d32007-09-12 13:53:49 +02005944 * initial network namespace
5945 */
5946 rtnl_lock();
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00005947 for_each_netdev_safe(net, dev, aux) {
Eric W. Biedermance286d32007-09-12 13:53:49 +02005948 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07005949 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02005950
5951 /* Ignore unmoveable devices (i.e. loopback) */
5952 if (dev->features & NETIF_F_NETNS_LOCAL)
5953 continue;
5954
Eric W. Biedermane008b5f2009-11-29 22:25:30 +00005955 /* Leave virtual devices for the generic cleanup */
5956 if (dev->rtnl_link_ops)
5957 continue;
Eric W. Biedermand0c082c2008-11-05 15:59:38 -08005958
Eric W. Biedermance286d32007-09-12 13:53:49 +02005959 /* Push remaing network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07005960 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
5961 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02005962 if (err) {
Pavel Emelyanovaca51392008-05-08 01:24:25 -07005963 printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
Eric W. Biedermance286d32007-09-12 13:53:49 +02005964 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07005965 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02005966 }
5967 }
5968 rtnl_unlock();
5969}
5970
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00005971static void __net_exit default_device_exit_batch(struct list_head *net_list)
5972{
5973 /* At exit all network devices most be removed from a network
5974 * namespace. Do this in the reverse order of registeration.
5975 * Do this across as many network namespaces as possible to
5976 * improve batching efficiency.
5977 */
5978 struct net_device *dev;
5979 struct net *net;
5980 LIST_HEAD(dev_kill_list);
5981
5982 rtnl_lock();
5983 list_for_each_entry(net, net_list, exit_list) {
5984 for_each_netdev_reverse(net, dev) {
5985 if (dev->rtnl_link_ops)
5986 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
5987 else
5988 unregister_netdevice_queue(dev, &dev_kill_list);
5989 }
5990 }
5991 unregister_netdevice_many(&dev_kill_list);
5992 rtnl_unlock();
5993}
5994
Denis V. Lunev022cbae2007-11-13 03:23:50 -08005995static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02005996 .exit = default_device_exit,
Eric W. Biederman04dc7f6b2009-12-03 02:29:04 +00005997 .exit_batch = default_device_exit_batch,
Eric W. Biedermance286d32007-09-12 13:53:49 +02005998};
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000/*
6001 * Initialize the DEV module. At boot time this walks the device list and
6002 * unhooks any devices that fail to initialise (normally hardware not
6003 * present) and leaves us with a valid list of present and active devices.
6004 *
6005 */
6006
6007/*
6008 * This is called single threaded during boot, so no need
6009 * to take the rtnl semaphore.
6010 */
6011static int __init net_dev_init(void)
6012{
6013 int i, rc = -ENOMEM;
6014
6015 BUG_ON(!dev_boot_phase);
6016
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 if (dev_proc_init())
6018 goto out;
6019
Eric W. Biederman8b41d182007-09-26 22:02:53 -07006020 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 goto out;
6022
6023 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08006024 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 INIT_LIST_HEAD(&ptype_base[i]);
6026
Eric W. Biederman881d9662007-09-17 11:56:21 -07006027 if (register_pernet_subsys(&netdev_net_ops))
6028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
6030 /*
6031 * Initialise the packet receive queues.
6032 */
6033
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07006034 for_each_possible_cpu(i) {
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006035 struct softnet_data *sd = &per_cpu(softnet_data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Changli Gaodee42872010-05-02 05:42:16 +00006037 memset(sd, 0, sizeof(*sd));
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006038 skb_queue_head_init(&sd->input_pkt_queue);
Changli Gao6e7676c2010-04-27 15:07:33 -07006039 skb_queue_head_init(&sd->process_queue);
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006040 sd->completion_queue = NULL;
6041 INIT_LIST_HEAD(&sd->poll_list);
Changli Gaoa9cbd582010-04-26 23:06:24 +00006042 sd->output_queue = NULL;
6043 sd->output_queue_tailp = &sd->output_queue;
Eric Dumazetdf334542010-03-24 19:13:54 +00006044#ifdef CONFIG_RPS
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006045 sd->csd.func = rps_trigger_softirq;
6046 sd->csd.info = sd;
6047 sd->csd.flags = 0;
6048 sd->cpu = i;
Tom Herbert1e94d722010-03-18 17:45:44 -07006049#endif
Tom Herbert0a9627f2010-03-16 08:03:29 +00006050
Eric Dumazete36fa2f2010-04-19 21:17:14 +00006051 sd->backlog.poll = process_backlog;
6052 sd->backlog.weight = weight_p;
6053 sd->backlog.gro_list = NULL;
6054 sd->backlog.gro_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 }
6056
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 dev_boot_phase = 0;
6058
Eric W. Biederman505d4f72008-11-07 22:54:20 -08006059 /* The loopback device is special if any other network devices
6060 * is present in a network namespace the loopback device must
6061 * be present. Since we now dynamically allocate and free the
6062 * loopback device ensure this invariant is maintained by
6063 * keeping the loopback device as the first device on the
6064 * list of network devices. Ensuring the loopback devices
6065 * is the first device that appears and the last network device
6066 * that disappears.
6067 */
6068 if (register_pernet_device(&loopback_net_ops))
6069 goto out;
6070
6071 if (register_pernet_device(&default_device_ops))
6072 goto out;
6073
Carlos R. Mafra962cf362008-05-15 11:15:37 -03006074 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
6075 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
6077 hotcpu_notifier(dev_cpu_callback, 0);
6078 dst_init();
6079 dev_mcast_init();
6080 rc = 0;
6081out:
6082 return rc;
6083}
6084
6085subsys_initcall(net_dev_init);
6086
Krishna Kumare88721f2009-02-18 17:55:02 -08006087static int __init initialize_hashrnd(void)
6088{
Tom Herbert0a9627f2010-03-16 08:03:29 +00006089 get_random_bytes(&hashrnd, sizeof(hashrnd));
Krishna Kumare88721f2009-02-18 17:55:02 -08006090 return 0;
6091}
6092
6093late_initcall_sync(initialize_hashrnd);
6094