]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/dev.c
netdev: Add tracepoints to netdev layer
[net-next-2.6.git] / net / core / dev.c
CommitLineData
1da177e4
LT
1/*
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
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * 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>
4fc268d2 78#include <linux/capability.h>
1da177e4
LT
79#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
08e9897d 82#include <linux/hash.h>
5a0e3ad6 83#include <linux/slab.h>
1da177e4 84#include <linux/sched.h>
4a3e2f71 85#include <linux/mutex.h>
1da177e4
LT
86#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>
0187bdfb 95#include <linux/ethtool.h>
1da177e4
LT
96#include <linux/notifier.h>
97#include <linux/skbuff.h>
457c4cbc 98#include <net/net_namespace.h>
1da177e4
LT
99#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>
1da177e4
LT
104#include <net/dst.h>
105#include <net/pkt_sched.h>
106#include <net/checksum.h>
44540960 107#include <net/xfrm.h>
1da177e4
LT
108#include <linux/highmem.h>
109#include <linux/init.h>
110#include <linux/kmod.h>
111#include <linux/module.h>
1da177e4
LT
112#include <linux/netpoll.h>
113#include <linux/rcupdate.h>
114#include <linux/delay.h>
295f4a1f 115#include <net/wext.h>
1da177e4 116#include <net/iw_handler.h>
1da177e4 117#include <asm/current.h>
5bdb9886 118#include <linux/audit.h>
db217334 119#include <linux/dmaengine.h>
f6a78bfc 120#include <linux/err.h>
c7fa9d18 121#include <linux/ctype.h>
723e98b7 122#include <linux/if_arp.h>
6de329e2 123#include <linux/if_vlan.h>
8f0f2223 124#include <linux/ip.h>
ad55dcaf 125#include <net/ip.h>
8f0f2223
DM
126#include <linux/ipv6.h>
127#include <linux/in.h>
b6b2fed1
DM
128#include <linux/jhash.h>
129#include <linux/random.h>
9cbc1cb8 130#include <trace/events/napi.h>
cf66ba58 131#include <trace/events/net.h>
5acbbd42 132#include <linux/pci.h>
1da177e4 133
342709ef
PE
134#include "net-sysfs.h"
135
d565b0a1
HX
136/* Instead of increasing this, you should create a hash table. */
137#define MAX_GRO_SKBS 8
138
5d38a079
HX
139/* This should be increased if a protocol with a bigger head is added. */
140#define GRO_MAX_HEAD (MAX_HEADER + 128)
141
1da177e4
LT
142/*
143 * The list of packet types we will receive (as opposed to discard)
144 * and the routines to invoke.
145 *
146 * Why 16. Because with 16 the only overlap we get on a hash of the
147 * low nibble of the protocol value is RARP/SNAP/X.25.
148 *
149 * NOTE: That is no longer true with the addition of VLAN tags. Not
150 * sure which should go first, but I bet it won't make much
151 * difference if we are running VLANs. The good news is that
152 * this protocol won't be in the list unless compiled in, so
3041a069 153 * the average user (w/out VLANs) will not be adversely affected.
1da177e4
LT
154 * --BLG
155 *
156 * 0800 IP
157 * 8100 802.1Q VLAN
158 * 0001 802.3
159 * 0002 AX.25
160 * 0004 802.2
161 * 8035 RARP
162 * 0005 SNAP
163 * 0805 X.25
164 * 0806 ARP
165 * 8137 IPX
166 * 0009 Localtalk
167 * 86DD IPv6
168 */
169
82d8a867
PE
170#define PTYPE_HASH_SIZE (16)
171#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
172
1da177e4 173static DEFINE_SPINLOCK(ptype_lock);
82d8a867 174static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
6b2bedc3 175static struct list_head ptype_all __read_mostly; /* Taps */
1da177e4 176
1da177e4 177/*
7562f876 178 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
179 * semaphore.
180 *
c6d14c84 181 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
1da177e4
LT
182 *
183 * Writers must hold the rtnl semaphore while they loop through the
7562f876 184 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
185 * actual updates. This allows pure readers to access the list even
186 * while a writer is preparing to update it.
187 *
188 * To put it another way, dev_base_lock is held for writing only to
189 * protect against pure readers; the rtnl semaphore provides the
190 * protection against other writers.
191 *
192 * See, for example usages, register_netdevice() and
193 * unregister_netdevice(), which must be called with the rtnl
194 * semaphore held.
195 */
1da177e4 196DEFINE_RWLOCK(dev_base_lock);
1da177e4
LT
197EXPORT_SYMBOL(dev_base_lock);
198
881d966b 199static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
1da177e4
LT
200{
201 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
08e9897d 202 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
1da177e4
LT
203}
204
881d966b 205static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
1da177e4 206{
7c28bd0b 207 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
1da177e4
LT
208}
209
e36fa2f7 210static inline void rps_lock(struct softnet_data *sd)
152102c7
CG
211{
212#ifdef CONFIG_RPS
e36fa2f7 213 spin_lock(&sd->input_pkt_queue.lock);
152102c7
CG
214#endif
215}
216
e36fa2f7 217static inline void rps_unlock(struct softnet_data *sd)
152102c7
CG
218{
219#ifdef CONFIG_RPS
e36fa2f7 220 spin_unlock(&sd->input_pkt_queue.lock);
152102c7
CG
221#endif
222}
223
ce286d32
EB
224/* Device list insertion */
225static int list_netdevice(struct net_device *dev)
226{
c346dca1 227 struct net *net = dev_net(dev);
ce286d32
EB
228
229 ASSERT_RTNL();
230
231 write_lock_bh(&dev_base_lock);
c6d14c84 232 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
72c9528b 233 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
fb699dfd
ED
234 hlist_add_head_rcu(&dev->index_hlist,
235 dev_index_hash(net, dev->ifindex));
ce286d32
EB
236 write_unlock_bh(&dev_base_lock);
237 return 0;
238}
239
fb699dfd
ED
240/* Device list removal
241 * caller must respect a RCU grace period before freeing/reusing dev
242 */
ce286d32
EB
243static void unlist_netdevice(struct net_device *dev)
244{
245 ASSERT_RTNL();
246
247 /* Unlink dev from the device chain */
248 write_lock_bh(&dev_base_lock);
c6d14c84 249 list_del_rcu(&dev->dev_list);
72c9528b 250 hlist_del_rcu(&dev->name_hlist);
fb699dfd 251 hlist_del_rcu(&dev->index_hlist);
ce286d32
EB
252 write_unlock_bh(&dev_base_lock);
253}
254
1da177e4
LT
255/*
256 * Our notifier list
257 */
258
f07d5b94 259static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
260
261/*
262 * Device drivers call our routines to queue packets here. We empty the
263 * queue in the local softnet handler.
264 */
bea3348e 265
9958da05 266DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
d1b19dff 267EXPORT_PER_CPU_SYMBOL(softnet_data);
1da177e4 268
cf508b12 269#ifdef CONFIG_LOCKDEP
723e98b7 270/*
c773e847 271 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
723e98b7
JP
272 * according to dev->type
273 */
274static const unsigned short netdev_lock_type[] =
275 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
276 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
277 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
278 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
279 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
280 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
281 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
282 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
283 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
284 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
285 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
286 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
287 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
2d91d78b 288 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET,
929122cd 289 ARPHRD_PHONET_PIPE, ARPHRD_IEEE802154,
fcb94e42 290 ARPHRD_VOID, ARPHRD_NONE};
723e98b7 291
36cbd3dc 292static const char *const netdev_lock_name[] =
723e98b7
JP
293 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
294 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
295 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
296 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
297 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
298 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
299 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
300 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
301 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
302 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
303 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
304 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
305 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
2d91d78b 306 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET",
929122cd 307 "_xmit_PHONET_PIPE", "_xmit_IEEE802154",
fcb94e42 308 "_xmit_VOID", "_xmit_NONE"};
723e98b7
JP
309
310static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
cf508b12 311static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
723e98b7
JP
312
313static inline unsigned short netdev_lock_pos(unsigned short dev_type)
314{
315 int i;
316
317 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
318 if (netdev_lock_type[i] == dev_type)
319 return i;
320 /* the last key is used by default */
321 return ARRAY_SIZE(netdev_lock_type) - 1;
322}
323
cf508b12
DM
324static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
325 unsigned short dev_type)
723e98b7
JP
326{
327 int i;
328
329 i = netdev_lock_pos(dev_type);
330 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
331 netdev_lock_name[i]);
332}
cf508b12
DM
333
334static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
335{
336 int i;
337
338 i = netdev_lock_pos(dev->type);
339 lockdep_set_class_and_name(&dev->addr_list_lock,
340 &netdev_addr_lock_key[i],
341 netdev_lock_name[i]);
342}
723e98b7 343#else
cf508b12
DM
344static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
345 unsigned short dev_type)
346{
347}
348static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
723e98b7
JP
349{
350}
351#endif
1da177e4
LT
352
353/*******************************************************************************
354
355 Protocol management and registration routines
356
357*******************************************************************************/
358
1da177e4
LT
359/*
360 * Add a protocol ID to the list. Now that the input handler is
361 * smarter we can dispense with all the messy stuff that used to be
362 * here.
363 *
364 * BEWARE!!! Protocol handlers, mangling input packets,
365 * MUST BE last in hash buckets and checking protocol handlers
366 * MUST start from promiscuous ptype_all chain in net_bh.
367 * It is true now, do not change it.
368 * Explanation follows: if protocol handler, mangling packet, will
369 * be the first on list, it is not able to sense, that packet
370 * is cloned and should be copied-on-write, so that it will
371 * change it and subsequent readers will get broken packet.
372 * --ANK (980803)
373 */
374
375/**
376 * dev_add_pack - add packet handler
377 * @pt: packet type declaration
378 *
379 * Add a protocol handler to the networking stack. The passed &packet_type
380 * is linked into kernel lists and may not be freed until it has been
381 * removed from the kernel lists.
382 *
4ec93edb 383 * This call does not sleep therefore it can not
1da177e4
LT
384 * guarantee all CPU's that are in middle of receiving packets
385 * will see the new packet type (until the next received packet).
386 */
387
388void dev_add_pack(struct packet_type *pt)
389{
390 int hash;
391
392 spin_lock_bh(&ptype_lock);
9be9a6b9 393 if (pt->type == htons(ETH_P_ALL))
1da177e4 394 list_add_rcu(&pt->list, &ptype_all);
9be9a6b9 395 else {
82d8a867 396 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
1da177e4
LT
397 list_add_rcu(&pt->list, &ptype_base[hash]);
398 }
399 spin_unlock_bh(&ptype_lock);
400}
d1b19dff 401EXPORT_SYMBOL(dev_add_pack);
1da177e4 402
1da177e4
LT
403/**
404 * __dev_remove_pack - remove packet handler
405 * @pt: packet type declaration
406 *
407 * Remove a protocol handler that was previously added to the kernel
408 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
409 * from the kernel lists and can be freed or reused once this function
4ec93edb 410 * returns.
1da177e4
LT
411 *
412 * The packet type might still be in use by receivers
413 * and must not be freed until after all the CPU's have gone
414 * through a quiescent state.
415 */
416void __dev_remove_pack(struct packet_type *pt)
417{
418 struct list_head *head;
419 struct packet_type *pt1;
420
421 spin_lock_bh(&ptype_lock);
422
9be9a6b9 423 if (pt->type == htons(ETH_P_ALL))
1da177e4 424 head = &ptype_all;
9be9a6b9 425 else
82d8a867 426 head = &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
1da177e4
LT
427
428 list_for_each_entry(pt1, head, list) {
429 if (pt == pt1) {
430 list_del_rcu(&pt->list);
431 goto out;
432 }
433 }
434
435 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
436out:
437 spin_unlock_bh(&ptype_lock);
438}
d1b19dff
ED
439EXPORT_SYMBOL(__dev_remove_pack);
440
1da177e4
LT
441/**
442 * dev_remove_pack - remove packet handler
443 * @pt: packet type declaration
444 *
445 * Remove a protocol handler that was previously added to the kernel
446 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
447 * from the kernel lists and can be freed or reused once this function
448 * returns.
449 *
450 * This call sleeps to guarantee that no CPU is looking at the packet
451 * type after return.
452 */
453void dev_remove_pack(struct packet_type *pt)
454{
455 __dev_remove_pack(pt);
4ec93edb 456
1da177e4
LT
457 synchronize_net();
458}
d1b19dff 459EXPORT_SYMBOL(dev_remove_pack);
1da177e4
LT
460
461/******************************************************************************
462
463 Device Boot-time Settings Routines
464
465*******************************************************************************/
466
467/* Boot time configuration table */
468static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
469
470/**
471 * netdev_boot_setup_add - add new setup entry
472 * @name: name of the device
473 * @map: configured settings for the device
474 *
475 * Adds new setup entry to the dev_boot_setup list. The function
476 * returns 0 on error and 1 on success. This is a generic routine to
477 * all netdevices.
478 */
479static int netdev_boot_setup_add(char *name, struct ifmap *map)
480{
481 struct netdev_boot_setup *s;
482 int i;
483
484 s = dev_boot_setup;
485 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
486 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
487 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 488 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
489 memcpy(&s[i].map, map, sizeof(s[i].map));
490 break;
491 }
492 }
493
494 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
495}
496
497/**
498 * netdev_boot_setup_check - check boot time settings
499 * @dev: the netdevice
500 *
501 * Check boot time settings for the device.
502 * The found settings are set for the device to be used
503 * later in the device probing.
504 * Returns 0 if no settings found, 1 if they are.
505 */
506int netdev_boot_setup_check(struct net_device *dev)
507{
508 struct netdev_boot_setup *s = dev_boot_setup;
509 int i;
510
511 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
512 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 513 !strcmp(dev->name, s[i].name)) {
1da177e4
LT
514 dev->irq = s[i].map.irq;
515 dev->base_addr = s[i].map.base_addr;
516 dev->mem_start = s[i].map.mem_start;
517 dev->mem_end = s[i].map.mem_end;
518 return 1;
519 }
520 }
521 return 0;
522}
d1b19dff 523EXPORT_SYMBOL(netdev_boot_setup_check);
1da177e4
LT
524
525
526/**
527 * netdev_boot_base - get address from boot time settings
528 * @prefix: prefix for network device
529 * @unit: id for network device
530 *
531 * Check boot time settings for the base address of device.
532 * The found settings are set for the device to be used
533 * later in the device probing.
534 * Returns 0 if no settings found.
535 */
536unsigned long netdev_boot_base(const char *prefix, int unit)
537{
538 const struct netdev_boot_setup *s = dev_boot_setup;
539 char name[IFNAMSIZ];
540 int i;
541
542 sprintf(name, "%s%d", prefix, unit);
543
544 /*
545 * If device already registered then return base of 1
546 * to indicate not to probe for this interface
547 */
881d966b 548 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
549 return 1;
550
551 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
552 if (!strcmp(name, s[i].name))
553 return s[i].map.base_addr;
554 return 0;
555}
556
557/*
558 * Saves at boot time configured settings for any netdevice.
559 */
560int __init netdev_boot_setup(char *str)
561{
562 int ints[5];
563 struct ifmap map;
564
565 str = get_options(str, ARRAY_SIZE(ints), ints);
566 if (!str || !*str)
567 return 0;
568
569 /* Save settings */
570 memset(&map, 0, sizeof(map));
571 if (ints[0] > 0)
572 map.irq = ints[1];
573 if (ints[0] > 1)
574 map.base_addr = ints[2];
575 if (ints[0] > 2)
576 map.mem_start = ints[3];
577 if (ints[0] > 3)
578 map.mem_end = ints[4];
579
580 /* Add new entry to the list */
581 return netdev_boot_setup_add(str, &map);
582}
583
584__setup("netdev=", netdev_boot_setup);
585
586/*******************************************************************************
587
588 Device Interface Subroutines
589
590*******************************************************************************/
591
592/**
593 * __dev_get_by_name - find a device by its name
c4ea43c5 594 * @net: the applicable net namespace
1da177e4
LT
595 * @name: name to find
596 *
597 * Find an interface by name. Must be called under RTNL semaphore
598 * or @dev_base_lock. If the name is found a pointer to the device
599 * is returned. If the name is not found then %NULL is returned. The
600 * reference counters are not incremented so the caller must be
601 * careful with locks.
602 */
603
881d966b 604struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
605{
606 struct hlist_node *p;
0bd8d536
ED
607 struct net_device *dev;
608 struct hlist_head *head = dev_name_hash(net, name);
1da177e4 609
0bd8d536 610 hlist_for_each_entry(dev, p, head, name_hlist)
1da177e4
LT
611 if (!strncmp(dev->name, name, IFNAMSIZ))
612 return dev;
0bd8d536 613
1da177e4
LT
614 return NULL;
615}
d1b19dff 616EXPORT_SYMBOL(__dev_get_by_name);
1da177e4 617
72c9528b
ED
618/**
619 * dev_get_by_name_rcu - find a device by its name
620 * @net: the applicable net namespace
621 * @name: name to find
622 *
623 * Find an interface by name.
624 * If the name is found a pointer to the device is returned.
625 * If the name is not found then %NULL is returned.
626 * The reference counters are not incremented so the caller must be
627 * careful with locks. The caller must hold RCU lock.
628 */
629
630struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
631{
632 struct hlist_node *p;
633 struct net_device *dev;
634 struct hlist_head *head = dev_name_hash(net, name);
635
636 hlist_for_each_entry_rcu(dev, p, head, name_hlist)
637 if (!strncmp(dev->name, name, IFNAMSIZ))
638 return dev;
639
640 return NULL;
641}
642EXPORT_SYMBOL(dev_get_by_name_rcu);
643
1da177e4
LT
644/**
645 * dev_get_by_name - find a device by its name
c4ea43c5 646 * @net: the applicable net namespace
1da177e4
LT
647 * @name: name to find
648 *
649 * Find an interface by name. This can be called from any
650 * context and does its own locking. The returned handle has
651 * the usage count incremented and the caller must use dev_put() to
652 * release it when it is no longer needed. %NULL is returned if no
653 * matching device is found.
654 */
655
881d966b 656struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
657{
658 struct net_device *dev;
659
72c9528b
ED
660 rcu_read_lock();
661 dev = dev_get_by_name_rcu(net, name);
1da177e4
LT
662 if (dev)
663 dev_hold(dev);
72c9528b 664 rcu_read_unlock();
1da177e4
LT
665 return dev;
666}
d1b19dff 667EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
668
669/**
670 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 671 * @net: the applicable net namespace
1da177e4
LT
672 * @ifindex: index of device
673 *
674 * Search for an interface by index. Returns %NULL if the device
675 * is not found or a pointer to the device. The device has not
676 * had its reference counter increased so the caller must be careful
677 * about locking. The caller must hold either the RTNL semaphore
678 * or @dev_base_lock.
679 */
680
881d966b 681struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
682{
683 struct hlist_node *p;
0bd8d536
ED
684 struct net_device *dev;
685 struct hlist_head *head = dev_index_hash(net, ifindex);
1da177e4 686
0bd8d536 687 hlist_for_each_entry(dev, p, head, index_hlist)
1da177e4
LT
688 if (dev->ifindex == ifindex)
689 return dev;
0bd8d536 690
1da177e4
LT
691 return NULL;
692}
d1b19dff 693EXPORT_SYMBOL(__dev_get_by_index);
1da177e4 694
fb699dfd
ED
695/**
696 * dev_get_by_index_rcu - find a device by its ifindex
697 * @net: the applicable net namespace
698 * @ifindex: index of device
699 *
700 * Search for an interface by index. Returns %NULL if the device
701 * is not found or a pointer to the device. The device has not
702 * had its reference counter increased so the caller must be careful
703 * about locking. The caller must hold RCU lock.
704 */
705
706struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
707{
708 struct hlist_node *p;
709 struct net_device *dev;
710 struct hlist_head *head = dev_index_hash(net, ifindex);
711
712 hlist_for_each_entry_rcu(dev, p, head, index_hlist)
713 if (dev->ifindex == ifindex)
714 return dev;
715
716 return NULL;
717}
718EXPORT_SYMBOL(dev_get_by_index_rcu);
719
1da177e4
LT
720
721/**
722 * dev_get_by_index - find a device by its ifindex
c4ea43c5 723 * @net: the applicable net namespace
1da177e4
LT
724 * @ifindex: index of device
725 *
726 * Search for an interface by index. Returns NULL if the device
727 * is not found or a pointer to the device. The device returned has
728 * had a reference added and the pointer is safe until the user calls
729 * dev_put to indicate they have finished with it.
730 */
731
881d966b 732struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
733{
734 struct net_device *dev;
735
fb699dfd
ED
736 rcu_read_lock();
737 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
738 if (dev)
739 dev_hold(dev);
fb699dfd 740 rcu_read_unlock();
1da177e4
LT
741 return dev;
742}
d1b19dff 743EXPORT_SYMBOL(dev_get_by_index);
1da177e4
LT
744
745/**
746 * dev_getbyhwaddr - find a device by its hardware address
c4ea43c5 747 * @net: the applicable net namespace
1da177e4
LT
748 * @type: media type of device
749 * @ha: hardware address
750 *
751 * Search for an interface by MAC address. Returns NULL if the device
752 * is not found or a pointer to the device. The caller must hold the
753 * rtnl semaphore. The returned device has not had its ref count increased
754 * and the caller must therefore be careful about locking
755 *
756 * BUGS:
757 * If the API was consistent this would be __dev_get_by_hwaddr
758 */
759
881d966b 760struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
1da177e4
LT
761{
762 struct net_device *dev;
763
764 ASSERT_RTNL();
765
81103a52 766 for_each_netdev(net, dev)
1da177e4
LT
767 if (dev->type == type &&
768 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
769 return dev;
770
771 return NULL;
1da177e4 772}
cf309e3f
JF
773EXPORT_SYMBOL(dev_getbyhwaddr);
774
881d966b 775struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1da177e4
LT
776{
777 struct net_device *dev;
778
4e9cac2b 779 ASSERT_RTNL();
881d966b 780 for_each_netdev(net, dev)
4e9cac2b 781 if (dev->type == type)
7562f876
PE
782 return dev;
783
784 return NULL;
4e9cac2b 785}
4e9cac2b
PM
786EXPORT_SYMBOL(__dev_getfirstbyhwtype);
787
881d966b 788struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b 789{
99fe3c39 790 struct net_device *dev, *ret = NULL;
4e9cac2b 791
99fe3c39
ED
792 rcu_read_lock();
793 for_each_netdev_rcu(net, dev)
794 if (dev->type == type) {
795 dev_hold(dev);
796 ret = dev;
797 break;
798 }
799 rcu_read_unlock();
800 return ret;
1da177e4 801}
1da177e4
LT
802EXPORT_SYMBOL(dev_getfirstbyhwtype);
803
804/**
bb69ae04 805 * dev_get_by_flags_rcu - find any device with given flags
c4ea43c5 806 * @net: the applicable net namespace
1da177e4
LT
807 * @if_flags: IFF_* values
808 * @mask: bitmask of bits in if_flags to check
809 *
810 * Search for any interface with the given flags. Returns NULL if a device
bb69ae04
ED
811 * is not found or a pointer to the device. Must be called inside
812 * rcu_read_lock(), and result refcount is unchanged.
1da177e4
LT
813 */
814
bb69ae04 815struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
d1b19dff 816 unsigned short mask)
1da177e4 817{
7562f876 818 struct net_device *dev, *ret;
1da177e4 819
7562f876 820 ret = NULL;
c6d14c84 821 for_each_netdev_rcu(net, dev) {
1da177e4 822 if (((dev->flags ^ if_flags) & mask) == 0) {
7562f876 823 ret = dev;
1da177e4
LT
824 break;
825 }
826 }
7562f876 827 return ret;
1da177e4 828}
bb69ae04 829EXPORT_SYMBOL(dev_get_by_flags_rcu);
1da177e4
LT
830
831/**
832 * dev_valid_name - check if name is okay for network device
833 * @name: name string
834 *
835 * Network device names need to be valid file names to
c7fa9d18
DM
836 * to allow sysfs to work. We also disallow any kind of
837 * whitespace.
1da177e4 838 */
c2373ee9 839int dev_valid_name(const char *name)
1da177e4 840{
c7fa9d18
DM
841 if (*name == '\0')
842 return 0;
b6fe17d6
SH
843 if (strlen(name) >= IFNAMSIZ)
844 return 0;
c7fa9d18
DM
845 if (!strcmp(name, ".") || !strcmp(name, ".."))
846 return 0;
847
848 while (*name) {
849 if (*name == '/' || isspace(*name))
850 return 0;
851 name++;
852 }
853 return 1;
1da177e4 854}
d1b19dff 855EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
856
857/**
b267b179
EB
858 * __dev_alloc_name - allocate a name for a device
859 * @net: network namespace to allocate the device name in
1da177e4 860 * @name: name format string
b267b179 861 * @buf: scratch buffer and result name string
1da177e4
LT
862 *
863 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
864 * id. It scans list of devices to build up a free map, then chooses
865 * the first empty slot. The caller must hold the dev_base or rtnl lock
866 * while allocating the name and adding the device in order to avoid
867 * duplicates.
868 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
869 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
870 */
871
b267b179 872static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
873{
874 int i = 0;
1da177e4
LT
875 const char *p;
876 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 877 unsigned long *inuse;
1da177e4
LT
878 struct net_device *d;
879
880 p = strnchr(name, IFNAMSIZ-1, '%');
881 if (p) {
882 /*
883 * Verify the string as this thing may have come from
884 * the user. There must be either one "%d" and no other "%"
885 * characters.
886 */
887 if (p[1] != 'd' || strchr(p + 2, '%'))
888 return -EINVAL;
889
890 /* Use one page as a bit array of possible slots */
cfcabdcc 891 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
892 if (!inuse)
893 return -ENOMEM;
894
881d966b 895 for_each_netdev(net, d) {
1da177e4
LT
896 if (!sscanf(d->name, name, &i))
897 continue;
898 if (i < 0 || i >= max_netdevices)
899 continue;
900
901 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 902 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
903 if (!strncmp(buf, d->name, IFNAMSIZ))
904 set_bit(i, inuse);
905 }
906
907 i = find_first_zero_bit(inuse, max_netdevices);
908 free_page((unsigned long) inuse);
909 }
910
d9031024
OP
911 if (buf != name)
912 snprintf(buf, IFNAMSIZ, name, i);
b267b179 913 if (!__dev_get_by_name(net, buf))
1da177e4 914 return i;
1da177e4
LT
915
916 /* It is possible to run out of possible slots
917 * when the name is long and there isn't enough space left
918 * for the digits, or if all bits are used.
919 */
920 return -ENFILE;
921}
922
b267b179
EB
923/**
924 * dev_alloc_name - allocate a name for a device
925 * @dev: device
926 * @name: name format string
927 *
928 * Passed a format string - eg "lt%d" it will try and find a suitable
929 * id. It scans list of devices to build up a free map, then chooses
930 * the first empty slot. The caller must hold the dev_base or rtnl lock
931 * while allocating the name and adding the device in order to avoid
932 * duplicates.
933 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
934 * Returns the number of the unit assigned or a negative errno code.
935 */
936
937int dev_alloc_name(struct net_device *dev, const char *name)
938{
939 char buf[IFNAMSIZ];
940 struct net *net;
941 int ret;
942
c346dca1
YH
943 BUG_ON(!dev_net(dev));
944 net = dev_net(dev);
b267b179
EB
945 ret = __dev_alloc_name(net, name, buf);
946 if (ret >= 0)
947 strlcpy(dev->name, buf, IFNAMSIZ);
948 return ret;
949}
d1b19dff 950EXPORT_SYMBOL(dev_alloc_name);
b267b179 951
8ce6cebc 952static int dev_get_valid_name(struct net_device *dev, const char *name, bool fmt)
d9031024 953{
8ce6cebc
DL
954 struct net *net;
955
956 BUG_ON(!dev_net(dev));
957 net = dev_net(dev);
958
d9031024
OP
959 if (!dev_valid_name(name))
960 return -EINVAL;
961
962 if (fmt && strchr(name, '%'))
8ce6cebc 963 return dev_alloc_name(dev, name);
d9031024
OP
964 else if (__dev_get_by_name(net, name))
965 return -EEXIST;
8ce6cebc
DL
966 else if (dev->name != name)
967 strlcpy(dev->name, name, IFNAMSIZ);
d9031024
OP
968
969 return 0;
970}
1da177e4
LT
971
972/**
973 * dev_change_name - change name of a device
974 * @dev: device
975 * @newname: name (or format string) must be at least IFNAMSIZ
976 *
977 * Change name of a device, can pass format strings "eth%d".
978 * for wildcarding.
979 */
cf04a4c7 980int dev_change_name(struct net_device *dev, const char *newname)
1da177e4 981{
fcc5a03a 982 char oldname[IFNAMSIZ];
1da177e4 983 int err = 0;
fcc5a03a 984 int ret;
881d966b 985 struct net *net;
1da177e4
LT
986
987 ASSERT_RTNL();
c346dca1 988 BUG_ON(!dev_net(dev));
1da177e4 989
c346dca1 990 net = dev_net(dev);
1da177e4
LT
991 if (dev->flags & IFF_UP)
992 return -EBUSY;
993
c8d90dca
SH
994 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
995 return 0;
996
fcc5a03a
HX
997 memcpy(oldname, dev->name, IFNAMSIZ);
998
8ce6cebc 999 err = dev_get_valid_name(dev, newname, 1);
d9031024
OP
1000 if (err < 0)
1001 return err;
1da177e4 1002
fcc5a03a 1003rollback:
a1b3f594
EB
1004 ret = device_rename(&dev->dev, dev->name);
1005 if (ret) {
1006 memcpy(dev->name, oldname, IFNAMSIZ);
1007 return ret;
dcc99773 1008 }
7f988eab
HX
1009
1010 write_lock_bh(&dev_base_lock);
92749821 1011 hlist_del(&dev->name_hlist);
72c9528b
ED
1012 write_unlock_bh(&dev_base_lock);
1013
1014 synchronize_rcu();
1015
1016 write_lock_bh(&dev_base_lock);
1017 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
7f988eab
HX
1018 write_unlock_bh(&dev_base_lock);
1019
056925ab 1020 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
1021 ret = notifier_to_errno(ret);
1022
1023 if (ret) {
91e9c07b
ED
1024 /* err >= 0 after dev_alloc_name() or stores the first errno */
1025 if (err >= 0) {
fcc5a03a
HX
1026 err = ret;
1027 memcpy(dev->name, oldname, IFNAMSIZ);
1028 goto rollback;
91e9c07b
ED
1029 } else {
1030 printk(KERN_ERR
1031 "%s: name change rollback failed: %d.\n",
1032 dev->name, ret);
fcc5a03a
HX
1033 }
1034 }
1da177e4
LT
1035
1036 return err;
1037}
1038
0b815a1a
SH
1039/**
1040 * dev_set_alias - change ifalias of a device
1041 * @dev: device
1042 * @alias: name up to IFALIASZ
f0db275a 1043 * @len: limit of bytes to copy from info
0b815a1a
SH
1044 *
1045 * Set ifalias for a device,
1046 */
1047int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1048{
1049 ASSERT_RTNL();
1050
1051 if (len >= IFALIASZ)
1052 return -EINVAL;
1053
96ca4a2c
OH
1054 if (!len) {
1055 if (dev->ifalias) {
1056 kfree(dev->ifalias);
1057 dev->ifalias = NULL;
1058 }
1059 return 0;
1060 }
1061
d1b19dff 1062 dev->ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
0b815a1a
SH
1063 if (!dev->ifalias)
1064 return -ENOMEM;
1065
1066 strlcpy(dev->ifalias, alias, len+1);
1067 return len;
1068}
1069
1070
d8a33ac4 1071/**
3041a069 1072 * netdev_features_change - device changes features
d8a33ac4
SH
1073 * @dev: device to cause notification
1074 *
1075 * Called to indicate a device has changed features.
1076 */
1077void netdev_features_change(struct net_device *dev)
1078{
056925ab 1079 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
1080}
1081EXPORT_SYMBOL(netdev_features_change);
1082
1da177e4
LT
1083/**
1084 * netdev_state_change - device changes state
1085 * @dev: device to cause notification
1086 *
1087 * Called to indicate a device has changed state. This function calls
1088 * the notifier chains for netdev_chain and sends a NEWLINK message
1089 * to the routing socket.
1090 */
1091void netdev_state_change(struct net_device *dev)
1092{
1093 if (dev->flags & IFF_UP) {
056925ab 1094 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1da177e4
LT
1095 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1096 }
1097}
d1b19dff 1098EXPORT_SYMBOL(netdev_state_change);
1da177e4 1099
3ca5b404 1100int netdev_bonding_change(struct net_device *dev, unsigned long event)
c1da4ac7 1101{
3ca5b404 1102 return call_netdevice_notifiers(event, dev);
c1da4ac7
OG
1103}
1104EXPORT_SYMBOL(netdev_bonding_change);
1105
1da177e4
LT
1106/**
1107 * dev_load - load a network module
c4ea43c5 1108 * @net: the applicable net namespace
1da177e4
LT
1109 * @name: name of interface
1110 *
1111 * If a network interface is not present and the process has suitable
1112 * privileges this function loads the module. If module loading is not
1113 * available in this kernel then it becomes a nop.
1114 */
1115
881d966b 1116void dev_load(struct net *net, const char *name)
1da177e4 1117{
4ec93edb 1118 struct net_device *dev;
1da177e4 1119
72c9528b
ED
1120 rcu_read_lock();
1121 dev = dev_get_by_name_rcu(net, name);
1122 rcu_read_unlock();
1da177e4 1123
a8f80e8f 1124 if (!dev && capable(CAP_NET_ADMIN))
1da177e4
LT
1125 request_module("%s", name);
1126}
d1b19dff 1127EXPORT_SYMBOL(dev_load);
1da177e4 1128
bd380811 1129static int __dev_open(struct net_device *dev)
1da177e4 1130{
d314774c 1131 const struct net_device_ops *ops = dev->netdev_ops;
3b8bcfd5 1132 int ret;
1da177e4 1133
e46b66bc
BH
1134 ASSERT_RTNL();
1135
1da177e4
LT
1136 /*
1137 * Is it even present?
1138 */
1139 if (!netif_device_present(dev))
1140 return -ENODEV;
1141
3b8bcfd5
JB
1142 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1143 ret = notifier_to_errno(ret);
1144 if (ret)
1145 return ret;
1146
1da177e4
LT
1147 /*
1148 * Call device private open method
1149 */
1150 set_bit(__LINK_STATE_START, &dev->state);
bada339b 1151
d314774c
SH
1152 if (ops->ndo_validate_addr)
1153 ret = ops->ndo_validate_addr(dev);
bada339b 1154
d314774c
SH
1155 if (!ret && ops->ndo_open)
1156 ret = ops->ndo_open(dev);
1da177e4 1157
4ec93edb 1158 /*
1da177e4
LT
1159 * If it went open OK then:
1160 */
1161
bada339b
JG
1162 if (ret)
1163 clear_bit(__LINK_STATE_START, &dev->state);
1164 else {
1da177e4
LT
1165 /*
1166 * Set the flags.
1167 */
1168 dev->flags |= IFF_UP;
1169
649274d9
DW
1170 /*
1171 * Enable NET_DMA
1172 */
b4bd07c2 1173 net_dmaengine_get();
649274d9 1174
1da177e4
LT
1175 /*
1176 * Initialize multicasting status
1177 */
4417da66 1178 dev_set_rx_mode(dev);
1da177e4
LT
1179
1180 /*
1181 * Wakeup transmit queue engine
1182 */
1183 dev_activate(dev);
1da177e4 1184 }
bada339b 1185
1da177e4
LT
1186 return ret;
1187}
1188
1189/**
bd380811
PM
1190 * dev_open - prepare an interface for use.
1191 * @dev: device to open
1da177e4 1192 *
bd380811
PM
1193 * Takes a device from down to up state. The device's private open
1194 * function is invoked and then the multicast lists are loaded. Finally
1195 * the device is moved into the up state and a %NETDEV_UP message is
1196 * sent to the netdev notifier chain.
1197 *
1198 * Calling this function on an active interface is a nop. On a failure
1199 * a negative errno code is returned.
1da177e4 1200 */
bd380811
PM
1201int dev_open(struct net_device *dev)
1202{
1203 int ret;
1204
1205 /*
1206 * Is it already up?
1207 */
1208 if (dev->flags & IFF_UP)
1209 return 0;
1210
1211 /*
1212 * Open device
1213 */
1214 ret = __dev_open(dev);
1215 if (ret < 0)
1216 return ret;
1217
1218 /*
1219 * ... and announce new interface.
1220 */
1221 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1222 call_netdevice_notifiers(NETDEV_UP, dev);
1223
1224 return ret;
1225}
1226EXPORT_SYMBOL(dev_open);
1227
1228static int __dev_close(struct net_device *dev)
1da177e4 1229{
d314774c 1230 const struct net_device_ops *ops = dev->netdev_ops;
e46b66bc 1231
bd380811 1232 ASSERT_RTNL();
9d5010db
DM
1233 might_sleep();
1234
1da177e4
LT
1235 /*
1236 * Tell people we are going down, so that they can
1237 * prepare to death, when device is still operating.
1238 */
056925ab 1239 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1240
1da177e4
LT
1241 clear_bit(__LINK_STATE_START, &dev->state);
1242
1243 /* Synchronize to scheduled poll. We cannot touch poll list,
bea3348e
SH
1244 * it can be even on different cpu. So just clear netif_running().
1245 *
1246 * dev->stop() will invoke napi_disable() on all of it's
1247 * napi_struct instances on this device.
1248 */
1da177e4 1249 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1da177e4 1250
d8b2a4d2
ML
1251 dev_deactivate(dev);
1252
1da177e4
LT
1253 /*
1254 * Call the device specific close. This cannot fail.
1255 * Only if device is UP
1256 *
1257 * We allow it to be called even after a DETACH hot-plug
1258 * event.
1259 */
d314774c
SH
1260 if (ops->ndo_stop)
1261 ops->ndo_stop(dev);
1da177e4
LT
1262
1263 /*
1264 * Device is now down.
1265 */
1266
1267 dev->flags &= ~IFF_UP;
1268
1269 /*
bd380811 1270 * Shutdown NET_DMA
1da177e4 1271 */
bd380811
PM
1272 net_dmaengine_put();
1273
1274 return 0;
1275}
1276
1277/**
1278 * dev_close - shutdown an interface.
1279 * @dev: device to shutdown
1280 *
1281 * This function moves an active device into down state. A
1282 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1283 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1284 * chain.
1285 */
1286int dev_close(struct net_device *dev)
1287{
1288 if (!(dev->flags & IFF_UP))
1289 return 0;
1290
1291 __dev_close(dev);
1da177e4 1292
649274d9 1293 /*
bd380811 1294 * Tell people we are down
649274d9 1295 */
bd380811
PM
1296 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1297 call_netdevice_notifiers(NETDEV_DOWN, dev);
649274d9 1298
1da177e4
LT
1299 return 0;
1300}
d1b19dff 1301EXPORT_SYMBOL(dev_close);
1da177e4
LT
1302
1303
0187bdfb
BH
1304/**
1305 * dev_disable_lro - disable Large Receive Offload on a device
1306 * @dev: device
1307 *
1308 * Disable Large Receive Offload (LRO) on a net device. Must be
1309 * called under RTNL. This is needed if received packets may be
1310 * forwarded to another interface.
1311 */
1312void dev_disable_lro(struct net_device *dev)
1313{
1314 if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
1315 dev->ethtool_ops->set_flags) {
1316 u32 flags = dev->ethtool_ops->get_flags(dev);
1317 if (flags & ETH_FLAG_LRO) {
1318 flags &= ~ETH_FLAG_LRO;
1319 dev->ethtool_ops->set_flags(dev, flags);
1320 }
1321 }
1322 WARN_ON(dev->features & NETIF_F_LRO);
1323}
1324EXPORT_SYMBOL(dev_disable_lro);
1325
1326
881d966b
EB
1327static int dev_boot_phase = 1;
1328
1da177e4
LT
1329/*
1330 * Device change register/unregister. These are not inline or static
1331 * as we export them to the world.
1332 */
1333
1334/**
1335 * register_netdevice_notifier - register a network notifier block
1336 * @nb: notifier
1337 *
1338 * Register a notifier to be called when network device events occur.
1339 * The notifier passed is linked into the kernel structures and must
1340 * not be reused until it has been unregistered. A negative errno code
1341 * is returned on a failure.
1342 *
1343 * When registered all registration and up events are replayed
4ec93edb 1344 * to the new notifier to allow device to have a race free
1da177e4
LT
1345 * view of the network device list.
1346 */
1347
1348int register_netdevice_notifier(struct notifier_block *nb)
1349{
1350 struct net_device *dev;
fcc5a03a 1351 struct net_device *last;
881d966b 1352 struct net *net;
1da177e4
LT
1353 int err;
1354
1355 rtnl_lock();
f07d5b94 1356 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1357 if (err)
1358 goto unlock;
881d966b
EB
1359 if (dev_boot_phase)
1360 goto unlock;
1361 for_each_net(net) {
1362 for_each_netdev(net, dev) {
1363 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1364 err = notifier_to_errno(err);
1365 if (err)
1366 goto rollback;
1367
1368 if (!(dev->flags & IFF_UP))
1369 continue;
1da177e4 1370
881d966b
EB
1371 nb->notifier_call(nb, NETDEV_UP, dev);
1372 }
1da177e4 1373 }
fcc5a03a
HX
1374
1375unlock:
1da177e4
LT
1376 rtnl_unlock();
1377 return err;
fcc5a03a
HX
1378
1379rollback:
1380 last = dev;
881d966b
EB
1381 for_each_net(net) {
1382 for_each_netdev(net, dev) {
1383 if (dev == last)
1384 break;
fcc5a03a 1385
881d966b
EB
1386 if (dev->flags & IFF_UP) {
1387 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1388 nb->notifier_call(nb, NETDEV_DOWN, dev);
1389 }
1390 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
a5ee1551 1391 nb->notifier_call(nb, NETDEV_UNREGISTER_BATCH, dev);
fcc5a03a 1392 }
fcc5a03a 1393 }
c67625a1
PE
1394
1395 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1396 goto unlock;
1da177e4 1397}
d1b19dff 1398EXPORT_SYMBOL(register_netdevice_notifier);
1da177e4
LT
1399
1400/**
1401 * unregister_netdevice_notifier - unregister a network notifier block
1402 * @nb: notifier
1403 *
1404 * Unregister a notifier previously registered by
1405 * register_netdevice_notifier(). The notifier is unlinked into the
1406 * kernel structures and may then be reused. A negative errno code
1407 * is returned on a failure.
1408 */
1409
1410int unregister_netdevice_notifier(struct notifier_block *nb)
1411{
9f514950
HX
1412 int err;
1413
1414 rtnl_lock();
f07d5b94 1415 err = raw_notifier_chain_unregister(&netdev_chain, nb);
9f514950
HX
1416 rtnl_unlock();
1417 return err;
1da177e4 1418}
d1b19dff 1419EXPORT_SYMBOL(unregister_netdevice_notifier);
1da177e4
LT
1420
1421/**
1422 * call_netdevice_notifiers - call all network notifier blocks
1423 * @val: value passed unmodified to notifier function
c4ea43c5 1424 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
1425 *
1426 * Call all network notifier blocks. Parameters and return value
f07d5b94 1427 * are as for raw_notifier_call_chain().
1da177e4
LT
1428 */
1429
ad7379d4 1430int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 1431{
ab930471 1432 ASSERT_RTNL();
ad7379d4 1433 return raw_notifier_call_chain(&netdev_chain, val, dev);
1da177e4
LT
1434}
1435
1436/* When > 0 there are consumers of rx skb time stamps */
1437static atomic_t netstamp_needed = ATOMIC_INIT(0);
1438
1439void net_enable_timestamp(void)
1440{
1441 atomic_inc(&netstamp_needed);
1442}
d1b19dff 1443EXPORT_SYMBOL(net_enable_timestamp);
1da177e4
LT
1444
1445void net_disable_timestamp(void)
1446{
1447 atomic_dec(&netstamp_needed);
1448}
d1b19dff 1449EXPORT_SYMBOL(net_disable_timestamp);
1da177e4 1450
3b098e2d 1451static inline void net_timestamp_set(struct sk_buff *skb)
1da177e4
LT
1452{
1453 if (atomic_read(&netstamp_needed))
a61bbcf2 1454 __net_timestamp(skb);
b7aa0bf7
ED
1455 else
1456 skb->tstamp.tv64 = 0;
1da177e4
LT
1457}
1458
3b098e2d
ED
1459static inline void net_timestamp_check(struct sk_buff *skb)
1460{
1461 if (!skb->tstamp.tv64 && atomic_read(&netstamp_needed))
1462 __net_timestamp(skb);
1463}
1464
44540960
AB
1465/**
1466 * dev_forward_skb - loopback an skb to another netif
1467 *
1468 * @dev: destination network device
1469 * @skb: buffer to forward
1470 *
1471 * return values:
1472 * NET_RX_SUCCESS (no congestion)
6ec82562 1473 * NET_RX_DROP (packet was dropped, but freed)
44540960
AB
1474 *
1475 * dev_forward_skb can be used for injecting an skb from the
1476 * start_xmit function of one device into the receive queue
1477 * of another device.
1478 *
1479 * The receiving device may be in another namespace, so
1480 * we have to clear all information in the skb that could
1481 * impact namespace isolation.
1482 */
1483int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1484{
1485 skb_orphan(skb);
c736eefa 1486 nf_reset(skb);
44540960 1487
6ec82562
ED
1488 if (!(dev->flags & IFF_UP) ||
1489 (skb->len > (dev->mtu + dev->hard_header_len))) {
1490 kfree_skb(skb);
44540960 1491 return NET_RX_DROP;
6ec82562 1492 }
8a83a00b 1493 skb_set_dev(skb, dev);
44540960
AB
1494 skb->tstamp.tv64 = 0;
1495 skb->pkt_type = PACKET_HOST;
1496 skb->protocol = eth_type_trans(skb, dev);
44540960
AB
1497 return netif_rx(skb);
1498}
1499EXPORT_SYMBOL_GPL(dev_forward_skb);
1500
1da177e4
LT
1501/*
1502 * Support routine. Sends outgoing frames to any network
1503 * taps currently in use.
1504 */
1505
f6a78bfc 1506static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
1507{
1508 struct packet_type *ptype;
a61bbcf2 1509
8caf1539
JP
1510#ifdef CONFIG_NET_CLS_ACT
1511 if (!(skb->tstamp.tv64 && (G_TC_FROM(skb->tc_verd) & AT_INGRESS)))
3b098e2d 1512 net_timestamp_set(skb);
8caf1539 1513#else
3b098e2d 1514 net_timestamp_set(skb);
8caf1539 1515#endif
1da177e4
LT
1516
1517 rcu_read_lock();
1518 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1519 /* Never send packets back to the socket
1520 * they originated from - MvS (miquels@drinkel.ow.org)
1521 */
1522 if ((ptype->dev == dev || !ptype->dev) &&
1523 (ptype->af_packet_priv == NULL ||
1524 (struct sock *)ptype->af_packet_priv != skb->sk)) {
d1b19dff 1525 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1da177e4
LT
1526 if (!skb2)
1527 break;
1528
1529 /* skb->nh should be correctly
1530 set by sender, so that the second statement is
1531 just protection against buggy protocols.
1532 */
459a98ed 1533 skb_reset_mac_header(skb2);
1da177e4 1534
d56f90a7 1535 if (skb_network_header(skb2) < skb2->data ||
27a884dc 1536 skb2->network_header > skb2->tail) {
1da177e4
LT
1537 if (net_ratelimit())
1538 printk(KERN_CRIT "protocol %04x is "
1539 "buggy, dev %s\n",
70777d03
SAS
1540 ntohs(skb2->protocol),
1541 dev->name);
c1d2bbe1 1542 skb_reset_network_header(skb2);
1da177e4
LT
1543 }
1544
b0e380b1 1545 skb2->transport_header = skb2->network_header;
1da177e4 1546 skb2->pkt_type = PACKET_OUTGOING;
f2ccd8fa 1547 ptype->func(skb2, skb->dev, ptype, skb->dev);
1da177e4
LT
1548 }
1549 }
1550 rcu_read_unlock();
1551}
1552
f0796d5c
JF
1553/*
1554 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
1555 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
1556 */
1557void netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
1558{
1559 unsigned int real_num = dev->real_num_tx_queues;
1560
1561 if (unlikely(txq > dev->num_tx_queues))
1562 ;
1563 else if (txq > real_num)
1564 dev->real_num_tx_queues = txq;
1565 else if (txq < real_num) {
1566 dev->real_num_tx_queues = txq;
1567 qdisc_reset_all_tx_gt(dev, txq);
1568 }
1569}
1570EXPORT_SYMBOL(netif_set_real_num_tx_queues);
56079431 1571
def82a1d 1572static inline void __netif_reschedule(struct Qdisc *q)
56079431 1573{
def82a1d
JP
1574 struct softnet_data *sd;
1575 unsigned long flags;
56079431 1576
def82a1d
JP
1577 local_irq_save(flags);
1578 sd = &__get_cpu_var(softnet_data);
a9cbd588
CG
1579 q->next_sched = NULL;
1580 *sd->output_queue_tailp = q;
1581 sd->output_queue_tailp = &q->next_sched;
def82a1d
JP
1582 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1583 local_irq_restore(flags);
1584}
1585
1586void __netif_schedule(struct Qdisc *q)
1587{
1588 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
1589 __netif_reschedule(q);
56079431
DV
1590}
1591EXPORT_SYMBOL(__netif_schedule);
1592
bea3348e 1593void dev_kfree_skb_irq(struct sk_buff *skb)
56079431 1594{
3578b0c8 1595 if (atomic_dec_and_test(&skb->users)) {
bea3348e
SH
1596 struct softnet_data *sd;
1597 unsigned long flags;
56079431 1598
bea3348e
SH
1599 local_irq_save(flags);
1600 sd = &__get_cpu_var(softnet_data);
1601 skb->next = sd->completion_queue;
1602 sd->completion_queue = skb;
1603 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1604 local_irq_restore(flags);
1605 }
56079431 1606}
bea3348e 1607EXPORT_SYMBOL(dev_kfree_skb_irq);
56079431
DV
1608
1609void dev_kfree_skb_any(struct sk_buff *skb)
1610{
1611 if (in_irq() || irqs_disabled())
1612 dev_kfree_skb_irq(skb);
1613 else
1614 dev_kfree_skb(skb);
1615}
1616EXPORT_SYMBOL(dev_kfree_skb_any);
1617
1618
bea3348e
SH
1619/**
1620 * netif_device_detach - mark device as removed
1621 * @dev: network device
1622 *
1623 * Mark device as removed from system and therefore no longer available.
1624 */
56079431
DV
1625void netif_device_detach(struct net_device *dev)
1626{
1627 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1628 netif_running(dev)) {
d543103a 1629 netif_tx_stop_all_queues(dev);
56079431
DV
1630 }
1631}
1632EXPORT_SYMBOL(netif_device_detach);
1633
bea3348e
SH
1634/**
1635 * netif_device_attach - mark device as attached
1636 * @dev: network device
1637 *
1638 * Mark device as attached from system and restart if needed.
1639 */
56079431
DV
1640void netif_device_attach(struct net_device *dev)
1641{
1642 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1643 netif_running(dev)) {
d543103a 1644 netif_tx_wake_all_queues(dev);
4ec93edb 1645 __netdev_watchdog_up(dev);
56079431
DV
1646 }
1647}
1648EXPORT_SYMBOL(netif_device_attach);
1649
6de329e2
BH
1650static bool can_checksum_protocol(unsigned long features, __be16 protocol)
1651{
1652 return ((features & NETIF_F_GEN_CSUM) ||
1653 ((features & NETIF_F_IP_CSUM) &&
1654 protocol == htons(ETH_P_IP)) ||
1655 ((features & NETIF_F_IPV6_CSUM) &&
1c8dbcf6
YZ
1656 protocol == htons(ETH_P_IPV6)) ||
1657 ((features & NETIF_F_FCOE_CRC) &&
1658 protocol == htons(ETH_P_FCOE)));
6de329e2
BH
1659}
1660
1661static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
1662{
1663 if (can_checksum_protocol(dev->features, skb->protocol))
1664 return true;
1665
1666 if (skb->protocol == htons(ETH_P_8021Q)) {
1667 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1668 if (can_checksum_protocol(dev->features & dev->vlan_features,
1669 veh->h_vlan_encapsulated_proto))
1670 return true;
1671 }
1672
1673 return false;
1674}
56079431 1675
8a83a00b
AB
1676/**
1677 * skb_dev_set -- assign a new device to a buffer
1678 * @skb: buffer for the new device
1679 * @dev: network device
1680 *
1681 * If an skb is owned by a device already, we have to reset
1682 * all data private to the namespace a device belongs to
1683 * before assigning it a new device.
1684 */
1685#ifdef CONFIG_NET_NS
1686void skb_set_dev(struct sk_buff *skb, struct net_device *dev)
1687{
1688 skb_dst_drop(skb);
1689 if (skb->dev && !net_eq(dev_net(skb->dev), dev_net(dev))) {
1690 secpath_reset(skb);
1691 nf_reset(skb);
1692 skb_init_secmark(skb);
1693 skb->mark = 0;
1694 skb->priority = 0;
1695 skb->nf_trace = 0;
1696 skb->ipvs_property = 0;
1697#ifdef CONFIG_NET_SCHED
1698 skb->tc_index = 0;
1699#endif
1700 }
1701 skb->dev = dev;
1702}
1703EXPORT_SYMBOL(skb_set_dev);
1704#endif /* CONFIG_NET_NS */
1705
1da177e4
LT
1706/*
1707 * Invalidate hardware checksum when packet is to be mangled, and
1708 * complete checksum manually on outgoing path.
1709 */
84fa7933 1710int skb_checksum_help(struct sk_buff *skb)
1da177e4 1711{
d3bc23e7 1712 __wsum csum;
663ead3b 1713 int ret = 0, offset;
1da177e4 1714
84fa7933 1715 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
1716 goto out_set_summed;
1717
1718 if (unlikely(skb_shinfo(skb)->gso_size)) {
a430a43d
HX
1719 /* Let GSO fix up the checksum. */
1720 goto out_set_summed;
1da177e4
LT
1721 }
1722
a030847e
HX
1723 offset = skb->csum_start - skb_headroom(skb);
1724 BUG_ON(offset >= skb_headlen(skb));
1725 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1726
1727 offset += skb->csum_offset;
1728 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1729
1730 if (skb_cloned(skb) &&
1731 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
1da177e4
LT
1732 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1733 if (ret)
1734 goto out;
1735 }
1736
a030847e 1737 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
a430a43d 1738out_set_summed:
1da177e4 1739 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 1740out:
1da177e4
LT
1741 return ret;
1742}
d1b19dff 1743EXPORT_SYMBOL(skb_checksum_help);
1da177e4 1744
f6a78bfc
HX
1745/**
1746 * skb_gso_segment - Perform segmentation on skb.
1747 * @skb: buffer to segment
576a30eb 1748 * @features: features for the output path (see dev->features)
f6a78bfc
HX
1749 *
1750 * This function segments the given skb and returns a list of segments.
576a30eb
HX
1751 *
1752 * It may return NULL if the skb requires no segmentation. This is
1753 * only possible when GSO is used for verifying header integrity.
f6a78bfc 1754 */
576a30eb 1755struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
f6a78bfc
HX
1756{
1757 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1758 struct packet_type *ptype;
252e3346 1759 __be16 type = skb->protocol;
a430a43d 1760 int err;
f6a78bfc 1761
459a98ed 1762 skb_reset_mac_header(skb);
b0e380b1 1763 skb->mac_len = skb->network_header - skb->mac_header;
f6a78bfc
HX
1764 __skb_pull(skb, skb->mac_len);
1765
67fd1a73
HX
1766 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1767 struct net_device *dev = skb->dev;
1768 struct ethtool_drvinfo info = {};
1769
1770 if (dev && dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
1771 dev->ethtool_ops->get_drvinfo(dev, &info);
1772
1773 WARN(1, "%s: caps=(0x%lx, 0x%lx) len=%d data_len=%d "
1774 "ip_summed=%d",
1775 info.driver, dev ? dev->features : 0L,
1776 skb->sk ? skb->sk->sk_route_caps : 0L,
1777 skb->len, skb->data_len, skb->ip_summed);
1778
a430a43d
HX
1779 if (skb_header_cloned(skb) &&
1780 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1781 return ERR_PTR(err);
1782 }
1783
f6a78bfc 1784 rcu_read_lock();
82d8a867
PE
1785 list_for_each_entry_rcu(ptype,
1786 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
f6a78bfc 1787 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
84fa7933 1788 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
a430a43d
HX
1789 err = ptype->gso_send_check(skb);
1790 segs = ERR_PTR(err);
1791 if (err || skb_gso_ok(skb, features))
1792 break;
d56f90a7
ACM
1793 __skb_push(skb, (skb->data -
1794 skb_network_header(skb)));
a430a43d 1795 }
576a30eb 1796 segs = ptype->gso_segment(skb, features);
f6a78bfc
HX
1797 break;
1798 }
1799 }
1800 rcu_read_unlock();
1801
98e399f8 1802 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 1803
f6a78bfc
HX
1804 return segs;
1805}
f6a78bfc
HX
1806EXPORT_SYMBOL(skb_gso_segment);
1807
fb286bb2
HX
1808/* Take action when hardware reception checksum errors are detected. */
1809#ifdef CONFIG_BUG
1810void netdev_rx_csum_fault(struct net_device *dev)
1811{
1812 if (net_ratelimit()) {
4ec93edb 1813 printk(KERN_ERR "%s: hw csum failure.\n",
246a4212 1814 dev ? dev->name : "<unknown>");
fb286bb2
HX
1815 dump_stack();
1816 }
1817}
1818EXPORT_SYMBOL(netdev_rx_csum_fault);
1819#endif
1820
1da177e4
LT
1821/* Actually, we should eliminate this check as soon as we know, that:
1822 * 1. IOMMU is present and allows to map all the memory.
1823 * 2. No high memory really exists on this machine.
1824 */
1825
9092c658 1826static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1da177e4 1827{
3d3a8533 1828#ifdef CONFIG_HIGHMEM
1da177e4 1829 int i;
5acbbd42
FT
1830 if (!(dev->features & NETIF_F_HIGHDMA)) {
1831 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1832 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1833 return 1;
1834 }
1da177e4 1835
5acbbd42
FT
1836 if (PCI_DMA_BUS_IS_PHYS) {
1837 struct device *pdev = dev->dev.parent;
1da177e4 1838
9092c658
ED
1839 if (!pdev)
1840 return 0;
5acbbd42
FT
1841 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1842 dma_addr_t addr = page_to_phys(skb_shinfo(skb)->frags[i].page);
1843 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
1844 return 1;
1845 }
1846 }
3d3a8533 1847#endif
1da177e4
LT
1848 return 0;
1849}
1da177e4 1850
f6a78bfc
HX
1851struct dev_gso_cb {
1852 void (*destructor)(struct sk_buff *skb);
1853};
1854
1855#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1856
1857static void dev_gso_skb_destructor(struct sk_buff *skb)
1858{
1859 struct dev_gso_cb *cb;
1860
1861 do {
1862 struct sk_buff *nskb = skb->next;
1863
1864 skb->next = nskb->next;
1865 nskb->next = NULL;
1866 kfree_skb(nskb);
1867 } while (skb->next);
1868
1869 cb = DEV_GSO_CB(skb);
1870 if (cb->destructor)
1871 cb->destructor(skb);
1872}
1873
1874/**
1875 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1876 * @skb: buffer to segment
1877 *
1878 * This function segments the given skb and stores the list of segments
1879 * in skb->next.
1880 */
1881static int dev_gso_segment(struct sk_buff *skb)
1882{
1883 struct net_device *dev = skb->dev;
1884 struct sk_buff *segs;
576a30eb
HX
1885 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1886 NETIF_F_SG : 0);
1887
1888 segs = skb_gso_segment(skb, features);
1889
1890 /* Verifying header integrity only. */
1891 if (!segs)
1892 return 0;
f6a78bfc 1893
801678c5 1894 if (IS_ERR(segs))
f6a78bfc
HX
1895 return PTR_ERR(segs);
1896
1897 skb->next = segs;
1898 DEV_GSO_CB(skb)->destructor = skb->destructor;
1899 skb->destructor = dev_gso_skb_destructor;
1900
1901 return 0;
1902}
1903
fc6055a5
ED
1904/*
1905 * Try to orphan skb early, right before transmission by the device.
1906 * We cannot orphan skb if tx timestamp is requested, since
1907 * drivers need to call skb_tstamp_tx() to send the timestamp.
1908 */
1909static inline void skb_orphan_try(struct sk_buff *skb)
1910{
87fd308c
ED
1911 struct sock *sk = skb->sk;
1912
1913 if (sk && !skb_tx(skb)->flags) {
1914 /* skb_tx_hash() wont be able to get sk.
1915 * We copy sk_hash into skb->rxhash
1916 */
1917 if (!skb->rxhash)
1918 skb->rxhash = sk->sk_hash;
fc6055a5 1919 skb_orphan(skb);
87fd308c 1920 }
fc6055a5
ED
1921}
1922
6afff0ca
JF
1923/*
1924 * Returns true if either:
1925 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
1926 * 2. skb is fragmented and the device does not support SG, or if
1927 * at least one of fragments is in highmem and device does not
1928 * support DMA from it.
1929 */
1930static inline int skb_needs_linearize(struct sk_buff *skb,
1931 struct net_device *dev)
1932{
1933 return skb_is_nonlinear(skb) &&
1934 ((skb_has_frags(skb) && !(dev->features & NETIF_F_FRAGLIST)) ||
1935 (skb_shinfo(skb)->nr_frags && (!(dev->features & NETIF_F_SG) ||
1936 illegal_highdma(dev, skb))));
1937}
1938
fd2ea0a7
DM
1939int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
1940 struct netdev_queue *txq)
f6a78bfc 1941{
00829823 1942 const struct net_device_ops *ops = dev->netdev_ops;
572a9d7b 1943 int rc = NETDEV_TX_OK;
00829823 1944
f6a78bfc 1945 if (likely(!skb->next)) {
9be9a6b9 1946 if (!list_empty(&ptype_all))
f6a78bfc
HX
1947 dev_queue_xmit_nit(skb, dev);
1948
93f154b5
ED
1949 /*
1950 * If device doesnt need skb->dst, release it right now while
1951 * its hot in this cpu cache
1952 */
adf30907
ED
1953 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
1954 skb_dst_drop(skb);
1955
fc6055a5 1956 skb_orphan_try(skb);
9ccb8975
DM
1957
1958 if (netif_needs_gso(dev, skb)) {
1959 if (unlikely(dev_gso_segment(skb)))
1960 goto out_kfree_skb;
1961 if (skb->next)
1962 goto gso;
6afff0ca
JF
1963 } else {
1964 if (skb_needs_linearize(skb, dev) &&
1965 __skb_linearize(skb))
1966 goto out_kfree_skb;
1967
1968 /* If packet is not checksummed and device does not
1969 * support checksumming for this protocol, complete
1970 * checksumming here.
1971 */
1972 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1973 skb_set_transport_header(skb, skb->csum_start -
1974 skb_headroom(skb));
1975 if (!dev_can_checksum(dev, skb) &&
1976 skb_checksum_help(skb))
1977 goto out_kfree_skb;
1978 }
9ccb8975
DM
1979 }
1980
ac45f602 1981 rc = ops->ndo_start_xmit(skb, dev);
cf66ba58 1982 trace_net_dev_xmit(skb, rc);
ec634fe3 1983 if (rc == NETDEV_TX_OK)
08baf561 1984 txq_trans_update(txq);
ac45f602 1985 return rc;
f6a78bfc
HX
1986 }
1987
576a30eb 1988gso:
f6a78bfc
HX
1989 do {
1990 struct sk_buff *nskb = skb->next;
f6a78bfc
HX
1991
1992 skb->next = nskb->next;
1993 nskb->next = NULL;
068a2de5
KK
1994
1995 /*
1996 * If device doesnt need nskb->dst, release it right now while
1997 * its hot in this cpu cache
1998 */
1999 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2000 skb_dst_drop(nskb);
2001
00829823 2002 rc = ops->ndo_start_xmit(nskb, dev);
cf66ba58 2003 trace_net_dev_xmit(nskb, rc);
ec634fe3 2004 if (unlikely(rc != NETDEV_TX_OK)) {
572a9d7b
PM
2005 if (rc & ~NETDEV_TX_MASK)
2006 goto out_kfree_gso_skb;
f54d9e8d 2007 nskb->next = skb->next;
f6a78bfc
HX
2008 skb->next = nskb;
2009 return rc;
2010 }
08baf561 2011 txq_trans_update(txq);
fd2ea0a7 2012 if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
f54d9e8d 2013 return NETDEV_TX_BUSY;
f6a78bfc 2014 } while (skb->next);
4ec93edb 2015
572a9d7b
PM
2016out_kfree_gso_skb:
2017 if (likely(skb->next == NULL))
2018 skb->destructor = DEV_GSO_CB(skb)->destructor;
f6a78bfc
HX
2019out_kfree_skb:
2020 kfree_skb(skb);
572a9d7b 2021 return rc;
f6a78bfc
HX
2022}
2023
0a9627f2 2024static u32 hashrnd __read_mostly;
b6b2fed1 2025
9247744e 2026u16 skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb)
8f0f2223 2027{
7019298a 2028 u32 hash;
b6b2fed1 2029
513de11b
DM
2030 if (skb_rx_queue_recorded(skb)) {
2031 hash = skb_get_rx_queue(skb);
d1b19dff 2032 while (unlikely(hash >= dev->real_num_tx_queues))
513de11b
DM
2033 hash -= dev->real_num_tx_queues;
2034 return hash;
2035 }
ec581f6a
ED
2036
2037 if (skb->sk && skb->sk->sk_hash)
7019298a 2038 hash = skb->sk->sk_hash;
ec581f6a 2039 else
87fd308c 2040 hash = (__force u16) skb->protocol ^ skb->rxhash;
0a9627f2 2041 hash = jhash_1word(hash, hashrnd);
b6b2fed1
DM
2042
2043 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
8f0f2223 2044}
9247744e 2045EXPORT_SYMBOL(skb_tx_hash);
8f0f2223 2046
ed04642f
ED
2047static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
2048{
2049 if (unlikely(queue_index >= dev->real_num_tx_queues)) {
2050 if (net_ratelimit()) {
7a161ea9
ED
2051 pr_warning("%s selects TX queue %d, but "
2052 "real number of TX queues is %d\n",
2053 dev->name, queue_index, dev->real_num_tx_queues);
ed04642f
ED
2054 }
2055 return 0;
2056 }
2057 return queue_index;
2058}
2059
e8a0464c
DM
2060static struct netdev_queue *dev_pick_tx(struct net_device *dev,
2061 struct sk_buff *skb)
2062{
b0f77d0e 2063 int queue_index;
a4ee3ce3
KK
2064 struct sock *sk = skb->sk;
2065
b0f77d0e
TH
2066 queue_index = sk_tx_queue_get(sk);
2067 if (queue_index < 0) {
a4ee3ce3
KK
2068 const struct net_device_ops *ops = dev->netdev_ops;
2069
2070 if (ops->ndo_select_queue) {
2071 queue_index = ops->ndo_select_queue(dev, skb);
ed04642f 2072 queue_index = dev_cap_txqueue(dev, queue_index);
a4ee3ce3
KK
2073 } else {
2074 queue_index = 0;
2075 if (dev->real_num_tx_queues > 1)
2076 queue_index = skb_tx_hash(dev, skb);
fd2ea0a7 2077
8728c544 2078 if (sk) {
87eb3670 2079 struct dst_entry *dst = rcu_dereference_check(sk->sk_dst_cache, 1);
8728c544
ED
2080
2081 if (dst && skb_dst(skb) == dst)
2082 sk_tx_queue_set(sk, queue_index);
2083 }
a4ee3ce3
KK
2084 }
2085 }
eae792b7 2086
fd2ea0a7
DM
2087 skb_set_queue_mapping(skb, queue_index);
2088 return netdev_get_tx_queue(dev, queue_index);
e8a0464c
DM
2089}
2090
bbd8a0d3
KK
2091static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2092 struct net_device *dev,
2093 struct netdev_queue *txq)
2094{
2095 spinlock_t *root_lock = qdisc_lock(q);
79640a4c 2096 bool contended = qdisc_is_running(q);
bbd8a0d3
KK
2097 int rc;
2098
79640a4c
ED
2099 /*
2100 * Heuristic to force contended enqueues to serialize on a
2101 * separate lock before trying to get qdisc main lock.
2102 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2103 * and dequeue packets faster.
2104 */
2105 if (unlikely(contended))
2106 spin_lock(&q->busylock);
2107
bbd8a0d3
KK
2108 spin_lock(root_lock);
2109 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2110 kfree_skb(skb);
2111 rc = NET_XMIT_DROP;
2112 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
bc135b23 2113 qdisc_run_begin(q)) {
bbd8a0d3
KK
2114 /*
2115 * This is a work-conserving queue; there are no old skbs
2116 * waiting to be sent out; and the qdisc is not running -
2117 * xmit the skb directly.
2118 */
7fee226a
ED
2119 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2120 skb_dst_force(skb);
bbd8a0d3 2121 __qdisc_update_bstats(q, skb->len);
79640a4c
ED
2122 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2123 if (unlikely(contended)) {
2124 spin_unlock(&q->busylock);
2125 contended = false;
2126 }
bbd8a0d3 2127 __qdisc_run(q);
79640a4c 2128 } else
bc135b23 2129 qdisc_run_end(q);
bbd8a0d3
KK
2130
2131 rc = NET_XMIT_SUCCESS;
2132 } else {
7fee226a 2133 skb_dst_force(skb);
bbd8a0d3 2134 rc = qdisc_enqueue_root(skb, q);
79640a4c
ED
2135 if (qdisc_run_begin(q)) {
2136 if (unlikely(contended)) {
2137 spin_unlock(&q->busylock);
2138 contended = false;
2139 }
2140 __qdisc_run(q);
2141 }
bbd8a0d3
KK
2142 }
2143 spin_unlock(root_lock);
79640a4c
ED
2144 if (unlikely(contended))
2145 spin_unlock(&q->busylock);
bbd8a0d3
KK
2146 return rc;
2147}
2148
d29f749e
DJ
2149/**
2150 * dev_queue_xmit - transmit a buffer
2151 * @skb: buffer to transmit
2152 *
2153 * Queue a buffer for transmission to a network device. The caller must
2154 * have set the device and priority and built the buffer before calling
2155 * this function. The function can be called from an interrupt.
2156 *
2157 * A negative errno code is returned on a failure. A success does not
2158 * guarantee the frame will be transmitted as it may be dropped due
2159 * to congestion or traffic shaping.
2160 *
2161 * -----------------------------------------------------------------------------------
2162 * I notice this method can also return errors from the queue disciplines,
2163 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2164 * be positive.
2165 *
2166 * Regardless of the return value, the skb is consumed, so it is currently
2167 * difficult to retry a send to this method. (You can bump the ref count
2168 * before sending to hold a reference for retry if you are careful.)
2169 *
2170 * When calling this method, interrupts MUST be enabled. This is because
2171 * the BH enable code must have IRQs enabled so that it will not deadlock.
2172 * --BLG
2173 */
1da177e4
LT
2174int dev_queue_xmit(struct sk_buff *skb)
2175{
2176 struct net_device *dev = skb->dev;
dc2b4847 2177 struct netdev_queue *txq;
1da177e4
LT
2178 struct Qdisc *q;
2179 int rc = -ENOMEM;
2180
4ec93edb
YH
2181 /* Disable soft irqs for various locks below. Also
2182 * stops preemption for RCU.
1da177e4 2183 */
4ec93edb 2184 rcu_read_lock_bh();
1da177e4 2185
eae792b7 2186 txq = dev_pick_tx(dev, skb);
a898def2 2187 q = rcu_dereference_bh(txq->qdisc);
37437bb2 2188
1da177e4 2189#ifdef CONFIG_NET_CLS_ACT
d1b19dff 2190 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
1da177e4 2191#endif
cf66ba58 2192 trace_net_dev_queue(skb);
1da177e4 2193 if (q->enqueue) {
bbd8a0d3 2194 rc = __dev_xmit_skb(skb, q, dev, txq);
37437bb2 2195 goto out;
1da177e4
LT
2196 }
2197
2198 /* The device has no queue. Common case for software devices:
2199 loopback, all the sorts of tunnels...
2200
932ff279
HX
2201 Really, it is unlikely that netif_tx_lock protection is necessary
2202 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1da177e4
LT
2203 counters.)
2204 However, it is possible, that they rely on protection
2205 made by us here.
2206
2207 Check this and shot the lock. It is not prone from deadlocks.
2208 Either shot noqueue qdisc, it is even simpler 8)
2209 */
2210 if (dev->flags & IFF_UP) {
2211 int cpu = smp_processor_id(); /* ok because BHs are off */
2212
c773e847 2213 if (txq->xmit_lock_owner != cpu) {
1da177e4 2214
c773e847 2215 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 2216
fd2ea0a7 2217 if (!netif_tx_queue_stopped(txq)) {
572a9d7b
PM
2218 rc = dev_hard_start_xmit(skb, dev, txq);
2219 if (dev_xmit_complete(rc)) {
c773e847 2220 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
2221 goto out;
2222 }
2223 }
c773e847 2224 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
2225 if (net_ratelimit())
2226 printk(KERN_CRIT "Virtual device %s asks to "
2227 "queue packet!\n", dev->name);
2228 } else {
2229 /* Recursion is detected! It is possible,
2230 * unfortunately */
2231 if (net_ratelimit())
2232 printk(KERN_CRIT "Dead loop on virtual device "
2233 "%s, fix it urgently!\n", dev->name);
2234 }
2235 }
2236
2237 rc = -ENETDOWN;
d4828d85 2238 rcu_read_unlock_bh();
1da177e4 2239
1da177e4
LT
2240 kfree_skb(skb);
2241 return rc;
2242out:
d4828d85 2243 rcu_read_unlock_bh();
1da177e4
LT
2244 return rc;
2245}
d1b19dff 2246EXPORT_SYMBOL(dev_queue_xmit);
1da177e4
LT
2247
2248
2249/*=======================================================================
2250 Receiver routines
2251 =======================================================================*/
2252
6b2bedc3 2253int netdev_max_backlog __read_mostly = 1000;
3b098e2d 2254int netdev_tstamp_prequeue __read_mostly = 1;
6b2bedc3
SH
2255int netdev_budget __read_mostly = 300;
2256int weight_p __read_mostly = 64; /* old backlog weight */
1da177e4 2257
eecfd7c4
ED
2258/* Called with irq disabled */
2259static inline void ____napi_schedule(struct softnet_data *sd,
2260 struct napi_struct *napi)
2261{
2262 list_add_tail(&napi->poll_list, &sd->poll_list);
2263 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2264}
2265
df334545 2266#ifdef CONFIG_RPS
fec5e652
TH
2267
2268/* One global table that all flow-based protocols share. */
8770acf0 2269struct rps_sock_flow_table *rps_sock_flow_table __read_mostly;
fec5e652
TH
2270EXPORT_SYMBOL(rps_sock_flow_table);
2271
0a9627f2
TH
2272/*
2273 * get_rps_cpu is called from netif_receive_skb and returns the target
2274 * CPU from the RPS map of the receiving queue for a given skb.
b0e28f1e 2275 * rcu_read_lock must be held on entry.
0a9627f2 2276 */
fec5e652
TH
2277static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2278 struct rps_dev_flow **rflowp)
0a9627f2
TH
2279{
2280 struct ipv6hdr *ip6;
2281 struct iphdr *ip;
2282 struct netdev_rx_queue *rxqueue;
2283 struct rps_map *map;
fec5e652
TH
2284 struct rps_dev_flow_table *flow_table;
2285 struct rps_sock_flow_table *sock_flow_table;
0a9627f2
TH
2286 int cpu = -1;
2287 u8 ip_proto;
fec5e652 2288 u16 tcpu;
8c52d509
CG
2289 u32 addr1, addr2, ihl;
2290 union {
2291 u32 v32;
2292 u16 v16[2];
2293 } ports;
0a9627f2 2294
0a9627f2
TH
2295 if (skb_rx_queue_recorded(skb)) {
2296 u16 index = skb_get_rx_queue(skb);
2297 if (unlikely(index >= dev->num_rx_queues)) {
08c801f8
TG
2298 WARN_ONCE(dev->num_rx_queues > 1, "%s received packet "
2299 "on queue %u, but number of RX queues is %u\n",
2300 dev->name, index, dev->num_rx_queues);
0a9627f2
TH
2301 goto done;
2302 }
2303 rxqueue = dev->_rx + index;
2304 } else
2305 rxqueue = dev->_rx;
2306
fec5e652 2307 if (!rxqueue->rps_map && !rxqueue->rps_flow_table)
0a9627f2
TH
2308 goto done;
2309
2310 if (skb->rxhash)
2311 goto got_hash; /* Skip hash computation on packet header */
2312
2313 switch (skb->protocol) {
2314 case __constant_htons(ETH_P_IP):
2315 if (!pskb_may_pull(skb, sizeof(*ip)))
2316 goto done;
2317
2318 ip = (struct iphdr *) skb->data;
2319 ip_proto = ip->protocol;
b249dcb8
ED
2320 addr1 = (__force u32) ip->saddr;
2321 addr2 = (__force u32) ip->daddr;
0a9627f2
TH
2322 ihl = ip->ihl;
2323 break;
2324 case __constant_htons(ETH_P_IPV6):
2325 if (!pskb_may_pull(skb, sizeof(*ip6)))
2326 goto done;
2327
2328 ip6 = (struct ipv6hdr *) skb->data;
2329 ip_proto = ip6->nexthdr;
b249dcb8
ED
2330 addr1 = (__force u32) ip6->saddr.s6_addr32[3];
2331 addr2 = (__force u32) ip6->daddr.s6_addr32[3];
0a9627f2
TH
2332 ihl = (40 >> 2);
2333 break;
2334 default:
2335 goto done;
2336 }
0a9627f2
TH
2337 switch (ip_proto) {
2338 case IPPROTO_TCP:
2339 case IPPROTO_UDP:
2340 case IPPROTO_DCCP:
2341 case IPPROTO_ESP:
2342 case IPPROTO_AH:
2343 case IPPROTO_SCTP:
2344 case IPPROTO_UDPLITE:
b249dcb8 2345 if (pskb_may_pull(skb, (ihl * 4) + 4)) {
8c52d509
CG
2346 ports.v32 = * (__force u32 *) (skb->data + (ihl * 4));
2347 if (ports.v16[1] < ports.v16[0])
2348 swap(ports.v16[0], ports.v16[1]);
2349 break;
b249dcb8 2350 }
0a9627f2 2351 default:
8c52d509 2352 ports.v32 = 0;
0a9627f2
TH
2353 break;
2354 }
2355
b249dcb8
ED
2356 /* get a consistent hash (same value on both flow directions) */
2357 if (addr2 < addr1)
2358 swap(addr1, addr2);
8c52d509 2359 skb->rxhash = jhash_3words(addr1, addr2, ports.v32, hashrnd);
0a9627f2
TH
2360 if (!skb->rxhash)
2361 skb->rxhash = 1;
2362
2363got_hash:
fec5e652
TH
2364 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2365 sock_flow_table = rcu_dereference(rps_sock_flow_table);
2366 if (flow_table && sock_flow_table) {
2367 u16 next_cpu;
2368 struct rps_dev_flow *rflow;
2369
2370 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
2371 tcpu = rflow->cpu;
2372
2373 next_cpu = sock_flow_table->ents[skb->rxhash &
2374 sock_flow_table->mask];
2375
2376 /*
2377 * If the desired CPU (where last recvmsg was done) is
2378 * different from current CPU (one in the rx-queue flow
2379 * table entry), switch if one of the following holds:
2380 * - Current CPU is unset (equal to RPS_NO_CPU).
2381 * - Current CPU is offline.
2382 * - The current CPU's queue tail has advanced beyond the
2383 * last packet that was enqueued using this table entry.
2384 * This guarantees that all previous packets for the flow
2385 * have been dequeued, thus preserving in order delivery.
2386 */
2387 if (unlikely(tcpu != next_cpu) &&
2388 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
2389 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
2390 rflow->last_qtail)) >= 0)) {
2391 tcpu = rflow->cpu = next_cpu;
2392 if (tcpu != RPS_NO_CPU)
2393 rflow->last_qtail = per_cpu(softnet_data,
2394 tcpu).input_queue_head;
2395 }
2396 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
2397 *rflowp = rflow;
2398 cpu = tcpu;
2399 goto done;
2400 }
2401 }
2402
0a9627f2
TH
2403 map = rcu_dereference(rxqueue->rps_map);
2404 if (map) {
fec5e652 2405 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
0a9627f2
TH
2406
2407 if (cpu_online(tcpu)) {
2408 cpu = tcpu;
2409 goto done;
2410 }
2411 }
2412
2413done:
0a9627f2
TH
2414 return cpu;
2415}
2416
0a9627f2 2417/* Called from hardirq (IPI) context */
e36fa2f7 2418static void rps_trigger_softirq(void *data)
0a9627f2 2419{
e36fa2f7
ED
2420 struct softnet_data *sd = data;
2421
eecfd7c4 2422 ____napi_schedule(sd, &sd->backlog);
dee42870 2423 sd->received_rps++;
0a9627f2 2424}
e36fa2f7 2425
fec5e652 2426#endif /* CONFIG_RPS */
0a9627f2 2427
e36fa2f7
ED
2428/*
2429 * Check if this softnet_data structure is another cpu one
2430 * If yes, queue it to our IPI list and return 1
2431 * If no, return 0
2432 */
2433static int rps_ipi_queued(struct softnet_data *sd)
2434{
2435#ifdef CONFIG_RPS
2436 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
2437
2438 if (sd != mysd) {
2439 sd->rps_ipi_next = mysd->rps_ipi_list;
2440 mysd->rps_ipi_list = sd;
2441
2442 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2443 return 1;
2444 }
2445#endif /* CONFIG_RPS */
2446 return 0;
2447}
2448
0a9627f2
TH
2449/*
2450 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
2451 * queue (may be a remote CPU queue).
2452 */
fec5e652
TH
2453static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
2454 unsigned int *qtail)
0a9627f2 2455{
e36fa2f7 2456 struct softnet_data *sd;
0a9627f2
TH
2457 unsigned long flags;
2458
e36fa2f7 2459 sd = &per_cpu(softnet_data, cpu);
0a9627f2
TH
2460
2461 local_irq_save(flags);
0a9627f2 2462
e36fa2f7 2463 rps_lock(sd);
6e7676c1
CG
2464 if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
2465 if (skb_queue_len(&sd->input_pkt_queue)) {
0a9627f2 2466enqueue:
e36fa2f7 2467 __skb_queue_tail(&sd->input_pkt_queue, skb);
76cc8b13 2468 input_queue_tail_incr_save(sd, qtail);
e36fa2f7 2469 rps_unlock(sd);
152102c7 2470 local_irq_restore(flags);
0a9627f2
TH
2471 return NET_RX_SUCCESS;
2472 }
2473
ebda37c2
ED
2474 /* Schedule NAPI for backlog device
2475 * We can use non atomic operation since we own the queue lock
2476 */
2477 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
e36fa2f7 2478 if (!rps_ipi_queued(sd))
eecfd7c4 2479 ____napi_schedule(sd, &sd->backlog);
0a9627f2
TH
2480 }
2481 goto enqueue;
2482 }
2483
dee42870 2484 sd->dropped++;
e36fa2f7 2485 rps_unlock(sd);
0a9627f2 2486
0a9627f2
TH
2487 local_irq_restore(flags);
2488
2489 kfree_skb(skb);
2490 return NET_RX_DROP;
2491}
1da177e4 2492
1da177e4
LT
2493/**
2494 * netif_rx - post buffer to the network code
2495 * @skb: buffer to post
2496 *
2497 * This function receives a packet from a device driver and queues it for
2498 * the upper (protocol) levels to process. It always succeeds. The buffer
2499 * may be dropped during processing for congestion control or by the
2500 * protocol layers.
2501 *
2502 * return values:
2503 * NET_RX_SUCCESS (no congestion)
1da177e4
LT
2504 * NET_RX_DROP (packet was dropped)
2505 *
2506 */
2507
2508int netif_rx(struct sk_buff *skb)
2509{
b0e28f1e 2510 int ret;
1da177e4
LT
2511
2512 /* if netpoll wants it, pretend we never saw it */
2513 if (netpoll_rx(skb))
2514 return NET_RX_DROP;
2515
3b098e2d
ED
2516 if (netdev_tstamp_prequeue)
2517 net_timestamp_check(skb);
1da177e4 2518
cf66ba58 2519 trace_netif_rx(skb);
df334545 2520#ifdef CONFIG_RPS
b0e28f1e 2521 {
fec5e652 2522 struct rps_dev_flow voidflow, *rflow = &voidflow;
b0e28f1e
ED
2523 int cpu;
2524
cece1945 2525 preempt_disable();
b0e28f1e 2526 rcu_read_lock();
fec5e652
TH
2527
2528 cpu = get_rps_cpu(skb->dev, skb, &rflow);
b0e28f1e
ED
2529 if (cpu < 0)
2530 cpu = smp_processor_id();
fec5e652
TH
2531
2532 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2533
b0e28f1e 2534 rcu_read_unlock();
cece1945 2535 preempt_enable();
b0e28f1e 2536 }
1e94d72f 2537#else
fec5e652
TH
2538 {
2539 unsigned int qtail;
2540 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
2541 put_cpu();
2542 }
1e94d72f 2543#endif
b0e28f1e 2544 return ret;
1da177e4 2545}
d1b19dff 2546EXPORT_SYMBOL(netif_rx);
1da177e4
LT
2547
2548int netif_rx_ni(struct sk_buff *skb)
2549{
2550 int err;
2551
2552 preempt_disable();
2553 err = netif_rx(skb);
2554 if (local_softirq_pending())
2555 do_softirq();
2556 preempt_enable();
2557
2558 return err;
2559}
1da177e4
LT
2560EXPORT_SYMBOL(netif_rx_ni);
2561
1da177e4
LT
2562static void net_tx_action(struct softirq_action *h)
2563{
2564 struct softnet_data *sd = &__get_cpu_var(softnet_data);
2565
2566 if (sd->completion_queue) {
2567 struct sk_buff *clist;
2568
2569 local_irq_disable();
2570 clist = sd->completion_queue;
2571 sd->completion_queue = NULL;
2572 local_irq_enable();
2573
2574 while (clist) {
2575 struct sk_buff *skb = clist;
2576 clist = clist->next;
2577
547b792c 2578 WARN_ON(atomic_read(&skb->users));
1da177e4
LT
2579 __kfree_skb(skb);
2580 }
2581 }
2582
2583 if (sd->output_queue) {
37437bb2 2584 struct Qdisc *head;
1da177e4
LT
2585
2586 local_irq_disable();
2587 head = sd->output_queue;
2588 sd->output_queue = NULL;
a9cbd588 2589 sd->output_queue_tailp = &sd->output_queue;
1da177e4
LT
2590 local_irq_enable();
2591
2592 while (head) {
37437bb2
DM
2593 struct Qdisc *q = head;
2594 spinlock_t *root_lock;
2595
1da177e4
LT
2596 head = head->next_sched;
2597
5fb66229 2598 root_lock = qdisc_lock(q);
37437bb2 2599 if (spin_trylock(root_lock)) {
def82a1d
JP
2600 smp_mb__before_clear_bit();
2601 clear_bit(__QDISC_STATE_SCHED,
2602 &q->state);
37437bb2
DM
2603 qdisc_run(q);
2604 spin_unlock(root_lock);
1da177e4 2605 } else {
195648bb 2606 if (!test_bit(__QDISC_STATE_DEACTIVATED,
e8a83e10 2607 &q->state)) {
195648bb 2608 __netif_reschedule(q);
e8a83e10
JP
2609 } else {
2610 smp_mb__before_clear_bit();
2611 clear_bit(__QDISC_STATE_SCHED,
2612 &q->state);
2613 }
1da177e4
LT
2614 }
2615 }
2616 }
2617}
2618
6f05f629
SH
2619static inline int deliver_skb(struct sk_buff *skb,
2620 struct packet_type *pt_prev,
2621 struct net_device *orig_dev)
1da177e4
LT
2622{
2623 atomic_inc(&skb->users);
f2ccd8fa 2624 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4
LT
2625}
2626
ab95bfe0
JP
2627#if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
2628 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
da678292
MM
2629/* This hook is defined here for ATM LANE */
2630int (*br_fdb_test_addr_hook)(struct net_device *dev,
2631 unsigned char *addr) __read_mostly;
4fb019a0 2632EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
da678292 2633#endif
1da177e4 2634
1da177e4
LT
2635#ifdef CONFIG_NET_CLS_ACT
2636/* TODO: Maybe we should just force sch_ingress to be compiled in
2637 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2638 * a compare and 2 stores extra right now if we dont have it on
2639 * but have CONFIG_NET_CLS_ACT
4ec93edb 2640 * NOTE: This doesnt stop any functionality; if you dont have
1da177e4
LT
2641 * the ingress scheduler, you just cant add policies on ingress.
2642 *
2643 */
4ec93edb 2644static int ing_filter(struct sk_buff *skb)
1da177e4 2645{
1da177e4 2646 struct net_device *dev = skb->dev;
f697c3e8 2647 u32 ttl = G_TC_RTTL(skb->tc_verd);
555353cf
DM
2648 struct netdev_queue *rxq;
2649 int result = TC_ACT_OK;
2650 struct Qdisc *q;
4ec93edb 2651
de384830
SH
2652 if (unlikely(MAX_RED_LOOP < ttl++)) {
2653 if (net_ratelimit())
2654 pr_warning( "Redir loop detected Dropping packet (%d->%d)\n",
2655 skb->skb_iif, dev->ifindex);
f697c3e8
HX
2656 return TC_ACT_SHOT;
2657 }
1da177e4 2658
f697c3e8
HX
2659 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
2660 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
1da177e4 2661
555353cf
DM
2662 rxq = &dev->rx_queue;
2663
83874000 2664 q = rxq->qdisc;
8d50b53d 2665 if (q != &noop_qdisc) {
83874000 2666 spin_lock(qdisc_lock(q));
a9312ae8
DM
2667 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
2668 result = qdisc_enqueue_root(skb, q);
83874000
DM
2669 spin_unlock(qdisc_lock(q));
2670 }
f697c3e8
HX
2671
2672 return result;
2673}
86e65da9 2674
f697c3e8
HX
2675static inline struct sk_buff *handle_ing(struct sk_buff *skb,
2676 struct packet_type **pt_prev,
2677 int *ret, struct net_device *orig_dev)
2678{
8d50b53d 2679 if (skb->dev->rx_queue.qdisc == &noop_qdisc)
f697c3e8 2680 goto out;
1da177e4 2681
f697c3e8
HX
2682 if (*pt_prev) {
2683 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2684 *pt_prev = NULL;
1da177e4
LT
2685 }
2686
f697c3e8
HX
2687 switch (ing_filter(skb)) {
2688 case TC_ACT_SHOT:
2689 case TC_ACT_STOLEN:
2690 kfree_skb(skb);
2691 return NULL;
2692 }
2693
2694out:
2695 skb->tc_verd = 0;
2696 return skb;
1da177e4
LT
2697}
2698#endif
2699
bc1d0411
PM
2700/*
2701 * netif_nit_deliver - deliver received packets to network taps
2702 * @skb: buffer
2703 *
2704 * This function is used to deliver incoming packets to network
2705 * taps. It should be used when the normal netif_receive_skb path
2706 * is bypassed, for example because of VLAN acceleration.
2707 */
2708void netif_nit_deliver(struct sk_buff *skb)
2709{
2710 struct packet_type *ptype;
2711
2712 if (list_empty(&ptype_all))
2713 return;
2714
2715 skb_reset_network_header(skb);
2716 skb_reset_transport_header(skb);
2717 skb->mac_len = skb->network_header - skb->mac_header;
2718
2719 rcu_read_lock();
2720 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2721 if (!ptype->dev || ptype->dev == skb->dev)
2722 deliver_skb(skb, ptype, skb->dev);
2723 }
2724 rcu_read_unlock();
2725}
2726
ab95bfe0
JP
2727/**
2728 * netdev_rx_handler_register - register receive handler
2729 * @dev: device to register a handler for
2730 * @rx_handler: receive handler to register
93e2c32b 2731 * @rx_handler_data: data pointer that is used by rx handler
ab95bfe0
JP
2732 *
2733 * Register a receive hander for a device. This handler will then be
2734 * called from __netif_receive_skb. A negative errno code is returned
2735 * on a failure.
2736 *
2737 * The caller must hold the rtnl_mutex.
2738 */
2739int netdev_rx_handler_register(struct net_device *dev,
93e2c32b
JP
2740 rx_handler_func_t *rx_handler,
2741 void *rx_handler_data)
ab95bfe0
JP
2742{
2743 ASSERT_RTNL();
2744
2745 if (dev->rx_handler)
2746 return -EBUSY;
2747
93e2c32b 2748 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
ab95bfe0
JP
2749 rcu_assign_pointer(dev->rx_handler, rx_handler);
2750
2751 return 0;
2752}
2753EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
2754
2755/**
2756 * netdev_rx_handler_unregister - unregister receive handler
2757 * @dev: device to unregister a handler from
2758 *
2759 * Unregister a receive hander from a device.
2760 *
2761 * The caller must hold the rtnl_mutex.
2762 */
2763void netdev_rx_handler_unregister(struct net_device *dev)
2764{
2765
2766 ASSERT_RTNL();
2767 rcu_assign_pointer(dev->rx_handler, NULL);
93e2c32b 2768 rcu_assign_pointer(dev->rx_handler_data, NULL);
ab95bfe0
JP
2769}
2770EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
2771
acbbc071
ED
2772static inline void skb_bond_set_mac_by_master(struct sk_buff *skb,
2773 struct net_device *master)
2774{
2775 if (skb->pkt_type == PACKET_HOST) {
2776 u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
2777
2778 memcpy(dest, master->dev_addr, ETH_ALEN);
2779 }
2780}
2781
2782/* On bonding slaves other than the currently active slave, suppress
2783 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
2784 * ARP on active-backup slaves with arp_validate enabled.
2785 */
2786int __skb_bond_should_drop(struct sk_buff *skb, struct net_device *master)
2787{
2788 struct net_device *dev = skb->dev;
2789
2790 if (master->priv_flags & IFF_MASTER_ARPMON)
2791 dev->last_rx = jiffies;
2792
f350a0a8
JP
2793 if ((master->priv_flags & IFF_MASTER_ALB) &&
2794 (master->priv_flags & IFF_BRIDGE_PORT)) {
acbbc071
ED
2795 /* Do address unmangle. The local destination address
2796 * will be always the one master has. Provides the right
2797 * functionality in a bridge.
2798 */
2799 skb_bond_set_mac_by_master(skb, master);
2800 }
2801
2802 if (dev->priv_flags & IFF_SLAVE_INACTIVE) {
2803 if ((dev->priv_flags & IFF_SLAVE_NEEDARP) &&
2804 skb->protocol == __cpu_to_be16(ETH_P_ARP))
2805 return 0;
2806
2807 if (master->priv_flags & IFF_MASTER_ALB) {
2808 if (skb->pkt_type != PACKET_BROADCAST &&
2809 skb->pkt_type != PACKET_MULTICAST)
2810 return 0;
2811 }
2812 if (master->priv_flags & IFF_MASTER_8023AD &&
2813 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
2814 return 0;
2815
2816 return 1;
2817 }
2818 return 0;
2819}
2820EXPORT_SYMBOL(__skb_bond_should_drop);
2821
10f744d2 2822static int __netif_receive_skb(struct sk_buff *skb)
1da177e4
LT
2823{
2824 struct packet_type *ptype, *pt_prev;
ab95bfe0 2825 rx_handler_func_t *rx_handler;
f2ccd8fa 2826 struct net_device *orig_dev;
0641e4fb 2827 struct net_device *master;
0d7a3681 2828 struct net_device *null_or_orig;
2df4a0fa 2829 struct net_device *orig_or_bond;
1da177e4 2830 int ret = NET_RX_DROP;
252e3346 2831 __be16 type;
1da177e4 2832
3b098e2d
ED
2833 if (!netdev_tstamp_prequeue)
2834 net_timestamp_check(skb);
81bbb3d4 2835
cf66ba58 2836 trace_netif_receive_skb(skb);
05423b24 2837 if (vlan_tx_tag_present(skb) && vlan_hwaccel_do_receive(skb))
9b22ea56
PM
2838 return NET_RX_SUCCESS;
2839
1da177e4 2840 /* if we've gotten here through NAPI, check netpoll */
bea3348e 2841 if (netpoll_receive_skb(skb))
1da177e4
LT
2842 return NET_RX_DROP;
2843
8964be4a
ED
2844 if (!skb->skb_iif)
2845 skb->skb_iif = skb->dev->ifindex;
86e65da9 2846
597a264b
JF
2847 /*
2848 * bonding note: skbs received on inactive slaves should only
2849 * be delivered to pkt handlers that are exact matches. Also
2850 * the deliver_no_wcard flag will be set. If packet handlers
2851 * are sensitive to duplicate packets these skbs will need to
2852 * be dropped at the handler. The vlan accel path may have
2853 * already set the deliver_no_wcard flag.
2854 */
0d7a3681 2855 null_or_orig = NULL;
cc9bd5ce 2856 orig_dev = skb->dev;
0641e4fb 2857 master = ACCESS_ONCE(orig_dev->master);
597a264b
JF
2858 if (skb->deliver_no_wcard)
2859 null_or_orig = orig_dev;
2860 else if (master) {
2861 if (skb_bond_should_drop(skb, master)) {
2862 skb->deliver_no_wcard = 1;
0d7a3681 2863 null_or_orig = orig_dev; /* deliver only exact match */
597a264b 2864 } else
0641e4fb 2865 skb->dev = master;
cc9bd5ce 2866 }
8f903c70 2867
27f39c73 2868 __this_cpu_inc(softnet_data.processed);
c1d2bbe1 2869 skb_reset_network_header(skb);
badff6d0 2870 skb_reset_transport_header(skb);
b0e380b1 2871 skb->mac_len = skb->network_header - skb->mac_header;
1da177e4
LT
2872
2873 pt_prev = NULL;
2874
2875 rcu_read_lock();
2876
2877#ifdef CONFIG_NET_CLS_ACT
2878 if (skb->tc_verd & TC_NCLS) {
2879 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2880 goto ncls;
2881 }
2882#endif
2883
2884 list_for_each_entry_rcu(ptype, &ptype_all, list) {
f982307f
JE
2885 if (ptype->dev == null_or_orig || ptype->dev == skb->dev ||
2886 ptype->dev == orig_dev) {
4ec93edb 2887 if (pt_prev)
f2ccd8fa 2888 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
2889 pt_prev = ptype;
2890 }
2891 }
2892
2893#ifdef CONFIG_NET_CLS_ACT
f697c3e8
HX
2894 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
2895 if (!skb)
1da177e4 2896 goto out;
1da177e4
LT
2897ncls:
2898#endif
2899
ab95bfe0
JP
2900 /* Handle special case of bridge or macvlan */
2901 rx_handler = rcu_dereference(skb->dev->rx_handler);
2902 if (rx_handler) {
2903 if (pt_prev) {
2904 ret = deliver_skb(skb, pt_prev, orig_dev);
2905 pt_prev = NULL;
2906 }
2907 skb = rx_handler(skb);
2908 if (!skb)
2909 goto out;
2910 }
1da177e4 2911
1f3c8804
AG
2912 /*
2913 * Make sure frames received on VLAN interfaces stacked on
2914 * bonding interfaces still make their way to any base bonding
2915 * device that may have registered for a specific ptype. The
2916 * handler may have to adjust skb->dev and orig_dev.
1f3c8804 2917 */
2df4a0fa 2918 orig_or_bond = orig_dev;
1f3c8804
AG
2919 if ((skb->dev->priv_flags & IFF_802_1Q_VLAN) &&
2920 (vlan_dev_real_dev(skb->dev)->priv_flags & IFF_BONDING)) {
2df4a0fa 2921 orig_or_bond = vlan_dev_real_dev(skb->dev);
1f3c8804
AG
2922 }
2923
1da177e4 2924 type = skb->protocol;
82d8a867
PE
2925 list_for_each_entry_rcu(ptype,
2926 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
1f3c8804 2927 if (ptype->type == type && (ptype->dev == null_or_orig ||
ca8d9ea3 2928 ptype->dev == skb->dev || ptype->dev == orig_dev ||
2df4a0fa 2929 ptype->dev == orig_or_bond)) {
4ec93edb 2930 if (pt_prev)
f2ccd8fa 2931 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
2932 pt_prev = ptype;
2933 }
2934 }
2935
2936 if (pt_prev) {
f2ccd8fa 2937 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4
LT
2938 } else {
2939 kfree_skb(skb);
2940 /* Jamal, now you will not able to escape explaining
2941 * me how you were going to use this. :-)
2942 */
2943 ret = NET_RX_DROP;
2944 }
2945
2946out:
2947 rcu_read_unlock();
2948 return ret;
2949}
0a9627f2
TH
2950
2951/**
2952 * netif_receive_skb - process receive buffer from network
2953 * @skb: buffer to process
2954 *
2955 * netif_receive_skb() is the main receive data processing function.
2956 * It always succeeds. The buffer may be dropped during processing
2957 * for congestion control or by the protocol layers.
2958 *
2959 * This function may only be called from softirq context and interrupts
2960 * should be enabled.
2961 *
2962 * Return values (usually ignored):
2963 * NET_RX_SUCCESS: no congestion
2964 * NET_RX_DROP: packet was dropped
2965 */
2966int netif_receive_skb(struct sk_buff *skb)
2967{
3b098e2d
ED
2968 if (netdev_tstamp_prequeue)
2969 net_timestamp_check(skb);
2970
c1f19b51
RC
2971 if (skb_defer_rx_timestamp(skb))
2972 return NET_RX_SUCCESS;
2973
df334545 2974#ifdef CONFIG_RPS
3b098e2d
ED
2975 {
2976 struct rps_dev_flow voidflow, *rflow = &voidflow;
2977 int cpu, ret;
fec5e652 2978
3b098e2d
ED
2979 rcu_read_lock();
2980
2981 cpu = get_rps_cpu(skb->dev, skb, &rflow);
0a9627f2 2982
3b098e2d
ED
2983 if (cpu >= 0) {
2984 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2985 rcu_read_unlock();
2986 } else {
2987 rcu_read_unlock();
2988 ret = __netif_receive_skb(skb);
2989 }
0a9627f2 2990
3b098e2d 2991 return ret;
fec5e652 2992 }
1e94d72f
TH
2993#else
2994 return __netif_receive_skb(skb);
2995#endif
0a9627f2 2996}
d1b19dff 2997EXPORT_SYMBOL(netif_receive_skb);
1da177e4 2998
88751275
ED
2999/* Network device is going away, flush any packets still pending
3000 * Called with irqs disabled.
3001 */
152102c7 3002static void flush_backlog(void *arg)
6e583ce5 3003{
152102c7 3004 struct net_device *dev = arg;
e36fa2f7 3005 struct softnet_data *sd = &__get_cpu_var(softnet_data);
6e583ce5
SH
3006 struct sk_buff *skb, *tmp;
3007
e36fa2f7 3008 rps_lock(sd);
6e7676c1 3009 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
6e583ce5 3010 if (skb->dev == dev) {
e36fa2f7 3011 __skb_unlink(skb, &sd->input_pkt_queue);
6e583ce5 3012 kfree_skb(skb);
76cc8b13 3013 input_queue_head_incr(sd);
6e583ce5 3014 }
6e7676c1 3015 }
e36fa2f7 3016 rps_unlock(sd);
6e7676c1
CG
3017
3018 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3019 if (skb->dev == dev) {
3020 __skb_unlink(skb, &sd->process_queue);
3021 kfree_skb(skb);
76cc8b13 3022 input_queue_head_incr(sd);
6e7676c1
CG
3023 }
3024 }
6e583ce5
SH
3025}
3026
d565b0a1
HX
3027static int napi_gro_complete(struct sk_buff *skb)
3028{
3029 struct packet_type *ptype;
3030 __be16 type = skb->protocol;
3031 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
3032 int err = -ENOENT;
3033
fc59f9a3
HX
3034 if (NAPI_GRO_CB(skb)->count == 1) {
3035 skb_shinfo(skb)->gso_size = 0;
d565b0a1 3036 goto out;
fc59f9a3 3037 }
d565b0a1
HX
3038
3039 rcu_read_lock();
3040 list_for_each_entry_rcu(ptype, head, list) {
3041 if (ptype->type != type || ptype->dev || !ptype->gro_complete)
3042 continue;
3043
3044 err = ptype->gro_complete(skb);
3045 break;
3046 }
3047 rcu_read_unlock();
3048
3049 if (err) {
3050 WARN_ON(&ptype->list == head);
3051 kfree_skb(skb);
3052 return NET_RX_SUCCESS;
3053 }
3054
3055out:
d565b0a1
HX
3056 return netif_receive_skb(skb);
3057}
3058
11380a4b 3059static void napi_gro_flush(struct napi_struct *napi)
d565b0a1
HX
3060{
3061 struct sk_buff *skb, *next;
3062
3063 for (skb = napi->gro_list; skb; skb = next) {
3064 next = skb->next;
3065 skb->next = NULL;
3066 napi_gro_complete(skb);
3067 }
3068
4ae5544f 3069 napi->gro_count = 0;
d565b0a1
HX
3070 napi->gro_list = NULL;
3071}
d565b0a1 3072
5b252f0c 3073enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1
HX
3074{
3075 struct sk_buff **pp = NULL;
3076 struct packet_type *ptype;
3077 __be16 type = skb->protocol;
3078 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
0da2afd5 3079 int same_flow;
d565b0a1 3080 int mac_len;
5b252f0c 3081 enum gro_result ret;
d565b0a1 3082
ce9e76c8 3083 if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
d565b0a1
HX
3084 goto normal;
3085
4cf704fb 3086 if (skb_is_gso(skb) || skb_has_frags(skb))
f17f5c91
HX
3087 goto normal;
3088
d565b0a1
HX
3089 rcu_read_lock();
3090 list_for_each_entry_rcu(ptype, head, list) {
d565b0a1
HX
3091 if (ptype->type != type || ptype->dev || !ptype->gro_receive)
3092 continue;
3093
86911732 3094 skb_set_network_header(skb, skb_gro_offset(skb));
d565b0a1
HX
3095 mac_len = skb->network_header - skb->mac_header;
3096 skb->mac_len = mac_len;
3097 NAPI_GRO_CB(skb)->same_flow = 0;
3098 NAPI_GRO_CB(skb)->flush = 0;
5d38a079 3099 NAPI_GRO_CB(skb)->free = 0;
d565b0a1 3100
d565b0a1
HX
3101 pp = ptype->gro_receive(&napi->gro_list, skb);
3102 break;
3103 }
3104 rcu_read_unlock();
3105
3106 if (&ptype->list == head)
3107 goto normal;
3108
0da2afd5 3109 same_flow = NAPI_GRO_CB(skb)->same_flow;
5d0d9be8 3110 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
0da2afd5 3111
d565b0a1
HX
3112 if (pp) {
3113 struct sk_buff *nskb = *pp;
3114
3115 *pp = nskb->next;
3116 nskb->next = NULL;
3117 napi_gro_complete(nskb);
4ae5544f 3118 napi->gro_count--;
d565b0a1
HX
3119 }
3120
0da2afd5 3121 if (same_flow)
d565b0a1
HX
3122 goto ok;
3123
4ae5544f 3124 if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
d565b0a1 3125 goto normal;
d565b0a1 3126
4ae5544f 3127 napi->gro_count++;
d565b0a1 3128 NAPI_GRO_CB(skb)->count = 1;
86911732 3129 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
d565b0a1
HX
3130 skb->next = napi->gro_list;
3131 napi->gro_list = skb;
5d0d9be8 3132 ret = GRO_HELD;
d565b0a1 3133
ad0f9904 3134pull:
cb18978c
HX
3135 if (skb_headlen(skb) < skb_gro_offset(skb)) {
3136 int grow = skb_gro_offset(skb) - skb_headlen(skb);
3137
3138 BUG_ON(skb->end - skb->tail < grow);
3139
3140 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
3141
3142 skb->tail += grow;
3143 skb->data_len -= grow;
3144
3145 skb_shinfo(skb)->frags[0].page_offset += grow;
3146 skb_shinfo(skb)->frags[0].size -= grow;
3147
3148 if (unlikely(!skb_shinfo(skb)->frags[0].size)) {
3149 put_page(skb_shinfo(skb)->frags[0].page);
3150 memmove(skb_shinfo(skb)->frags,
3151 skb_shinfo(skb)->frags + 1,
e5093aec 3152 --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
cb18978c 3153 }
ad0f9904
HX
3154 }
3155
d565b0a1 3156ok:
5d0d9be8 3157 return ret;
d565b0a1
HX
3158
3159normal:
ad0f9904
HX
3160 ret = GRO_NORMAL;
3161 goto pull;
5d38a079 3162}
96e93eab
HX
3163EXPORT_SYMBOL(dev_gro_receive);
3164
5b252f0c
BH
3165static gro_result_t
3166__napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
96e93eab
HX
3167{
3168 struct sk_buff *p;
3169
3170 for (p = napi->gro_list; p; p = p->next) {
f64f9e71
JP
3171 NAPI_GRO_CB(p)->same_flow =
3172 (p->dev == skb->dev) &&
3173 !compare_ether_header(skb_mac_header(p),
3174 skb_gro_mac_header(skb));
96e93eab
HX
3175 NAPI_GRO_CB(p)->flush = 0;
3176 }
3177
3178 return dev_gro_receive(napi, skb);
3179}
5d38a079 3180
c7c4b3b6 3181gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
5d38a079 3182{
5d0d9be8
HX
3183 switch (ret) {
3184 case GRO_NORMAL:
c7c4b3b6
BH
3185 if (netif_receive_skb(skb))
3186 ret = GRO_DROP;
3187 break;
5d38a079 3188
5d0d9be8 3189 case GRO_DROP:
5d0d9be8 3190 case GRO_MERGED_FREE:
5d38a079
HX
3191 kfree_skb(skb);
3192 break;
5b252f0c
BH
3193
3194 case GRO_HELD:
3195 case GRO_MERGED:
3196 break;
5d38a079
HX
3197 }
3198
c7c4b3b6 3199 return ret;
5d0d9be8
HX
3200}
3201EXPORT_SYMBOL(napi_skb_finish);
3202
78a478d0
HX
3203void skb_gro_reset_offset(struct sk_buff *skb)
3204{
3205 NAPI_GRO_CB(skb)->data_offset = 0;
3206 NAPI_GRO_CB(skb)->frag0 = NULL;
7489594c 3207 NAPI_GRO_CB(skb)->frag0_len = 0;
78a478d0 3208
78d3fd0b 3209 if (skb->mac_header == skb->tail &&
7489594c 3210 !PageHighMem(skb_shinfo(skb)->frags[0].page)) {
78a478d0
HX
3211 NAPI_GRO_CB(skb)->frag0 =
3212 page_address(skb_shinfo(skb)->frags[0].page) +
3213 skb_shinfo(skb)->frags[0].page_offset;
7489594c
HX
3214 NAPI_GRO_CB(skb)->frag0_len = skb_shinfo(skb)->frags[0].size;
3215 }
78a478d0
HX
3216}
3217EXPORT_SYMBOL(skb_gro_reset_offset);
3218
c7c4b3b6 3219gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
5d0d9be8 3220{
86911732
HX
3221 skb_gro_reset_offset(skb);
3222
5d0d9be8 3223 return napi_skb_finish(__napi_gro_receive(napi, skb), skb);
d565b0a1
HX
3224}
3225EXPORT_SYMBOL(napi_gro_receive);
3226
96e93eab
HX
3227void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
3228{
96e93eab
HX
3229 __skb_pull(skb, skb_headlen(skb));
3230 skb_reserve(skb, NET_IP_ALIGN - skb_headroom(skb));
3231
3232 napi->skb = skb;
3233}
3234EXPORT_SYMBOL(napi_reuse_skb);
3235
76620aaf 3236struct sk_buff *napi_get_frags(struct napi_struct *napi)
5d38a079 3237{
5d38a079 3238 struct sk_buff *skb = napi->skb;
5d38a079
HX
3239
3240 if (!skb) {
89d71a66
ED
3241 skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
3242 if (skb)
3243 napi->skb = skb;
80595d59 3244 }
96e93eab
HX
3245 return skb;
3246}
76620aaf 3247EXPORT_SYMBOL(napi_get_frags);
96e93eab 3248
c7c4b3b6
BH
3249gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
3250 gro_result_t ret)
96e93eab 3251{
5d0d9be8
HX
3252 switch (ret) {
3253 case GRO_NORMAL:
86911732 3254 case GRO_HELD:
e76b69cc 3255 skb->protocol = eth_type_trans(skb, skb->dev);
86911732 3256
c7c4b3b6
BH
3257 if (ret == GRO_HELD)
3258 skb_gro_pull(skb, -ETH_HLEN);
3259 else if (netif_receive_skb(skb))
3260 ret = GRO_DROP;
86911732 3261 break;
5d38a079 3262
5d0d9be8 3263 case GRO_DROP:
5d0d9be8
HX
3264 case GRO_MERGED_FREE:
3265 napi_reuse_skb(napi, skb);
3266 break;
5b252f0c
BH
3267
3268 case GRO_MERGED:
3269 break;
5d0d9be8 3270 }
5d38a079 3271
c7c4b3b6 3272 return ret;
5d38a079 3273}
5d0d9be8
HX
3274EXPORT_SYMBOL(napi_frags_finish);
3275
76620aaf
HX
3276struct sk_buff *napi_frags_skb(struct napi_struct *napi)
3277{
3278 struct sk_buff *skb = napi->skb;
3279 struct ethhdr *eth;
a5b1cf28
HX
3280 unsigned int hlen;
3281 unsigned int off;
76620aaf
HX
3282
3283 napi->skb = NULL;
3284
3285 skb_reset_mac_header(skb);
3286 skb_gro_reset_offset(skb);
3287
a5b1cf28
HX
3288 off = skb_gro_offset(skb);
3289 hlen = off + sizeof(*eth);
3290 eth = skb_gro_header_fast(skb, off);
3291 if (skb_gro_header_hard(skb, hlen)) {
3292 eth = skb_gro_header_slow(skb, hlen, off);
3293 if (unlikely(!eth)) {
3294 napi_reuse_skb(napi, skb);
3295 skb = NULL;
3296 goto out;
3297 }
76620aaf
HX
3298 }
3299
3300 skb_gro_pull(skb, sizeof(*eth));
3301
3302 /*
3303 * This works because the only protocols we care about don't require
3304 * special handling. We'll fix it up properly at the end.
3305 */
3306 skb->protocol = eth->h_proto;
3307
3308out:
3309 return skb;
3310}
3311EXPORT_SYMBOL(napi_frags_skb);
3312
c7c4b3b6 3313gro_result_t napi_gro_frags(struct napi_struct *napi)
5d0d9be8 3314{
76620aaf 3315 struct sk_buff *skb = napi_frags_skb(napi);
5d0d9be8
HX
3316
3317 if (!skb)
c7c4b3b6 3318 return GRO_DROP;
5d0d9be8
HX
3319
3320 return napi_frags_finish(napi, skb, __napi_gro_receive(napi, skb));
3321}
5d38a079
HX
3322EXPORT_SYMBOL(napi_gro_frags);
3323
e326bed2
ED
3324/*
3325 * net_rps_action sends any pending IPI's for rps.
3326 * Note: called with local irq disabled, but exits with local irq enabled.
3327 */
3328static void net_rps_action_and_irq_enable(struct softnet_data *sd)
3329{
3330#ifdef CONFIG_RPS
3331 struct softnet_data *remsd = sd->rps_ipi_list;
3332
3333 if (remsd) {
3334 sd->rps_ipi_list = NULL;
3335
3336 local_irq_enable();
3337
3338 /* Send pending IPI's to kick RPS processing on remote cpus. */
3339 while (remsd) {
3340 struct softnet_data *next = remsd->rps_ipi_next;
3341
3342 if (cpu_online(remsd->cpu))
3343 __smp_call_function_single(remsd->cpu,
3344 &remsd->csd, 0);
3345 remsd = next;
3346 }
3347 } else
3348#endif
3349 local_irq_enable();
3350}
3351
bea3348e 3352static int process_backlog(struct napi_struct *napi, int quota)
1da177e4
LT
3353{
3354 int work = 0;
eecfd7c4 3355 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
1da177e4 3356
e326bed2
ED
3357#ifdef CONFIG_RPS
3358 /* Check if we have pending ipi, its better to send them now,
3359 * not waiting net_rx_action() end.
3360 */
3361 if (sd->rps_ipi_list) {
3362 local_irq_disable();
3363 net_rps_action_and_irq_enable(sd);
3364 }
3365#endif
bea3348e 3366 napi->weight = weight_p;
6e7676c1
CG
3367 local_irq_disable();
3368 while (work < quota) {
1da177e4 3369 struct sk_buff *skb;
6e7676c1
CG
3370 unsigned int qlen;
3371
3372 while ((skb = __skb_dequeue(&sd->process_queue))) {
3373 local_irq_enable();
3374 __netif_receive_skb(skb);
6e7676c1 3375 local_irq_disable();
76cc8b13
TH
3376 input_queue_head_incr(sd);
3377 if (++work >= quota) {
3378 local_irq_enable();
3379 return work;
3380 }
6e7676c1 3381 }
1da177e4 3382
e36fa2f7 3383 rps_lock(sd);
6e7676c1 3384 qlen = skb_queue_len(&sd->input_pkt_queue);
76cc8b13 3385 if (qlen)
6e7676c1
CG
3386 skb_queue_splice_tail_init(&sd->input_pkt_queue,
3387 &sd->process_queue);
76cc8b13 3388
6e7676c1 3389 if (qlen < quota - work) {
eecfd7c4
ED
3390 /*
3391 * Inline a custom version of __napi_complete().
3392 * only current cpu owns and manipulates this napi,
3393 * and NAPI_STATE_SCHED is the only possible flag set on backlog.
3394 * we can use a plain write instead of clear_bit(),
3395 * and we dont need an smp_mb() memory barrier.
3396 */
3397 list_del(&napi->poll_list);
3398 napi->state = 0;
3399
6e7676c1 3400 quota = work + qlen;
bea3348e 3401 }
e36fa2f7 3402 rps_unlock(sd);
6e7676c1
CG
3403 }
3404 local_irq_enable();
1da177e4 3405
bea3348e
SH
3406 return work;
3407}
1da177e4 3408
bea3348e
SH
3409/**
3410 * __napi_schedule - schedule for receive
c4ea43c5 3411 * @n: entry to schedule
bea3348e
SH
3412 *
3413 * The entry's receive function will be scheduled to run
3414 */
b5606c2d 3415void __napi_schedule(struct napi_struct *n)
bea3348e
SH
3416{
3417 unsigned long flags;
1da177e4 3418
bea3348e 3419 local_irq_save(flags);
eecfd7c4 3420 ____napi_schedule(&__get_cpu_var(softnet_data), n);
bea3348e 3421 local_irq_restore(flags);
1da177e4 3422}
bea3348e
SH
3423EXPORT_SYMBOL(__napi_schedule);
3424
d565b0a1
HX
3425void __napi_complete(struct napi_struct *n)
3426{
3427 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
3428 BUG_ON(n->gro_list);
3429
3430 list_del(&n->poll_list);
3431 smp_mb__before_clear_bit();
3432 clear_bit(NAPI_STATE_SCHED, &n->state);
3433}
3434EXPORT_SYMBOL(__napi_complete);
3435
3436void napi_complete(struct napi_struct *n)
3437{
3438 unsigned long flags;
3439
3440 /*
3441 * don't let napi dequeue from the cpu poll list
3442 * just in case its running on a different cpu
3443 */
3444 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
3445 return;
3446
3447 napi_gro_flush(n);
3448 local_irq_save(flags);
3449 __napi_complete(n);
3450 local_irq_restore(flags);
3451}
3452EXPORT_SYMBOL(napi_complete);
3453
3454void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
3455 int (*poll)(struct napi_struct *, int), int weight)
3456{
3457 INIT_LIST_HEAD(&napi->poll_list);
4ae5544f 3458 napi->gro_count = 0;
d565b0a1 3459 napi->gro_list = NULL;
5d38a079 3460 napi->skb = NULL;
d565b0a1
HX
3461 napi->poll = poll;
3462 napi->weight = weight;
3463 list_add(&napi->dev_list, &dev->napi_list);
d565b0a1 3464 napi->dev = dev;
5d38a079 3465#ifdef CONFIG_NETPOLL
d565b0a1
HX
3466 spin_lock_init(&napi->poll_lock);
3467 napi->poll_owner = -1;
3468#endif
3469 set_bit(NAPI_STATE_SCHED, &napi->state);
3470}
3471EXPORT_SYMBOL(netif_napi_add);
3472
3473void netif_napi_del(struct napi_struct *napi)
3474{
3475 struct sk_buff *skb, *next;
3476
d7b06636 3477 list_del_init(&napi->dev_list);
76620aaf 3478 napi_free_frags(napi);
d565b0a1
HX
3479
3480 for (skb = napi->gro_list; skb; skb = next) {
3481 next = skb->next;
3482 skb->next = NULL;
3483 kfree_skb(skb);
3484 }
3485
3486 napi->gro_list = NULL;
4ae5544f 3487 napi->gro_count = 0;
d565b0a1
HX
3488}
3489EXPORT_SYMBOL(netif_napi_del);
3490
1da177e4
LT
3491static void net_rx_action(struct softirq_action *h)
3492{
e326bed2 3493 struct softnet_data *sd = &__get_cpu_var(softnet_data);
24f8b238 3494 unsigned long time_limit = jiffies + 2;
51b0bded 3495 int budget = netdev_budget;
53fb95d3
MM
3496 void *have;
3497
1da177e4
LT
3498 local_irq_disable();
3499
e326bed2 3500 while (!list_empty(&sd->poll_list)) {
bea3348e
SH
3501 struct napi_struct *n;
3502 int work, weight;
1da177e4 3503
bea3348e 3504 /* If softirq window is exhuasted then punt.
24f8b238
SH
3505 * Allow this to run for 2 jiffies since which will allow
3506 * an average latency of 1.5/HZ.
bea3348e 3507 */
24f8b238 3508 if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
1da177e4
LT
3509 goto softnet_break;
3510
3511 local_irq_enable();
3512
bea3348e
SH
3513 /* Even though interrupts have been re-enabled, this
3514 * access is safe because interrupts can only add new
3515 * entries to the tail of this list, and only ->poll()
3516 * calls can remove this head entry from the list.
3517 */
e326bed2 3518 n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
1da177e4 3519
bea3348e
SH
3520 have = netpoll_poll_lock(n);
3521
3522 weight = n->weight;
3523
0a7606c1
DM
3524 /* This NAPI_STATE_SCHED test is for avoiding a race
3525 * with netpoll's poll_napi(). Only the entity which
3526 * obtains the lock and sees NAPI_STATE_SCHED set will
3527 * actually make the ->poll() call. Therefore we avoid
3528 * accidently calling ->poll() when NAPI is not scheduled.
3529 */
3530 work = 0;
4ea7e386 3531 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
0a7606c1 3532 work = n->poll(n, weight);
4ea7e386
NH
3533 trace_napi_poll(n);
3534 }
bea3348e
SH
3535
3536 WARN_ON_ONCE(work > weight);
3537
3538 budget -= work;
3539
3540 local_irq_disable();
3541
3542 /* Drivers must not modify the NAPI state if they
3543 * consume the entire weight. In such cases this code
3544 * still "owns" the NAPI instance and therefore can
3545 * move the instance around on the list at-will.
3546 */
fed17f30 3547 if (unlikely(work == weight)) {
ff780cd8
HX
3548 if (unlikely(napi_disable_pending(n))) {
3549 local_irq_enable();
3550 napi_complete(n);
3551 local_irq_disable();
3552 } else
e326bed2 3553 list_move_tail(&n->poll_list, &sd->poll_list);
fed17f30 3554 }
bea3348e
SH
3555
3556 netpoll_poll_unlock(have);
1da177e4
LT
3557 }
3558out:
e326bed2 3559 net_rps_action_and_irq_enable(sd);
0a9627f2 3560
db217334
CL
3561#ifdef CONFIG_NET_DMA
3562 /*
3563 * There may not be any more sk_buffs coming right now, so push
3564 * any pending DMA copies to hardware
3565 */
2ba05622 3566 dma_issue_pending_all();
db217334 3567#endif
bea3348e 3568
1da177e4
LT
3569 return;
3570
3571softnet_break:
dee42870 3572 sd->time_squeeze++;
1da177e4
LT
3573 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3574 goto out;
3575}
3576
d1b19dff 3577static gifconf_func_t *gifconf_list[NPROTO];
1da177e4
LT
3578
3579/**
3580 * register_gifconf - register a SIOCGIF handler
3581 * @family: Address family
3582 * @gifconf: Function handler
3583 *
3584 * Register protocol dependent address dumping routines. The handler
3585 * that is passed must not be freed or reused until it has been replaced
3586 * by another handler.
3587 */
d1b19dff 3588int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
1da177e4
LT
3589{
3590 if (family >= NPROTO)
3591 return -EINVAL;
3592 gifconf_list[family] = gifconf;
3593 return 0;
3594}
d1b19dff 3595EXPORT_SYMBOL(register_gifconf);
1da177e4
LT
3596
3597
3598/*
3599 * Map an interface index to its name (SIOCGIFNAME)
3600 */
3601
3602/*
3603 * We need this ioctl for efficient implementation of the
3604 * if_indextoname() function required by the IPv6 API. Without
3605 * it, we would have to search all the interfaces to find a
3606 * match. --pb
3607 */
3608
881d966b 3609static int dev_ifname(struct net *net, struct ifreq __user *arg)
1da177e4
LT
3610{
3611 struct net_device *dev;
3612 struct ifreq ifr;
3613
3614 /*
3615 * Fetch the caller's info block.
3616 */
3617
3618 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3619 return -EFAULT;
3620
fb699dfd
ED
3621 rcu_read_lock();
3622 dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
1da177e4 3623 if (!dev) {
fb699dfd 3624 rcu_read_unlock();
1da177e4
LT
3625 return -ENODEV;
3626 }
3627
3628 strcpy(ifr.ifr_name, dev->name);
fb699dfd 3629 rcu_read_unlock();
1da177e4
LT
3630
3631 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
3632 return -EFAULT;
3633 return 0;
3634}
3635
3636/*
3637 * Perform a SIOCGIFCONF call. This structure will change
3638 * size eventually, and there is nothing I can do about it.
3639 * Thus we will need a 'compatibility mode'.
3640 */
3641
881d966b 3642static int dev_ifconf(struct net *net, char __user *arg)
1da177e4
LT
3643{
3644 struct ifconf ifc;
3645 struct net_device *dev;
3646 char __user *pos;
3647 int len;
3648 int total;
3649 int i;
3650
3651 /*
3652 * Fetch the caller's info block.
3653 */
3654
3655 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
3656 return -EFAULT;
3657
3658 pos = ifc.ifc_buf;
3659 len = ifc.ifc_len;
3660
3661 /*
3662 * Loop over the interfaces, and write an info block for each.
3663 */
3664
3665 total = 0;
881d966b 3666 for_each_netdev(net, dev) {
1da177e4
LT
3667 for (i = 0; i < NPROTO; i++) {
3668 if (gifconf_list[i]) {
3669 int done;
3670 if (!pos)
3671 done = gifconf_list[i](dev, NULL, 0);
3672 else
3673 done = gifconf_list[i](dev, pos + total,
3674 len - total);
3675 if (done < 0)
3676 return -EFAULT;
3677 total += done;
3678 }
3679 }
4ec93edb 3680 }
1da177e4
LT
3681
3682 /*
3683 * All done. Write the updated control block back to the caller.
3684 */
3685 ifc.ifc_len = total;
3686
3687 /*
3688 * Both BSD and Solaris return 0 here, so we do too.
3689 */
3690 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
3691}
3692
3693#ifdef CONFIG_PROC_FS
3694/*
3695 * This is invoked by the /proc filesystem handler to display a device
3696 * in detail.
3697 */
7562f876 3698void *dev_seq_start(struct seq_file *seq, loff_t *pos)
c6d14c84 3699 __acquires(RCU)
1da177e4 3700{
e372c414 3701 struct net *net = seq_file_net(seq);
7562f876 3702 loff_t off;
1da177e4 3703 struct net_device *dev;
1da177e4 3704
c6d14c84 3705 rcu_read_lock();
7562f876
PE
3706 if (!*pos)
3707 return SEQ_START_TOKEN;
1da177e4 3708
7562f876 3709 off = 1;
c6d14c84 3710 for_each_netdev_rcu(net, dev)
7562f876
PE
3711 if (off++ == *pos)
3712 return dev;
1da177e4 3713
7562f876 3714 return NULL;
1da177e4
LT
3715}
3716
3717void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3718{
c6d14c84
ED
3719 struct net_device *dev = (v == SEQ_START_TOKEN) ?
3720 first_net_device(seq_file_net(seq)) :
3721 next_net_device((struct net_device *)v);
3722
1da177e4 3723 ++*pos;
c6d14c84 3724 return rcu_dereference(dev);
1da177e4
LT
3725}
3726
3727void dev_seq_stop(struct seq_file *seq, void *v)
c6d14c84 3728 __releases(RCU)
1da177e4 3729{
c6d14c84 3730 rcu_read_unlock();
1da177e4
LT
3731}
3732
3733static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
3734{
28172739
ED
3735 struct rtnl_link_stats64 temp;
3736 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
1da177e4 3737
be1f3c2c
BH
3738 seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
3739 "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
5a1b5898
RR
3740 dev->name, stats->rx_bytes, stats->rx_packets,
3741 stats->rx_errors,
3742 stats->rx_dropped + stats->rx_missed_errors,
3743 stats->rx_fifo_errors,
3744 stats->rx_length_errors + stats->rx_over_errors +
3745 stats->rx_crc_errors + stats->rx_frame_errors,
3746 stats->rx_compressed, stats->multicast,
3747 stats->tx_bytes, stats->tx_packets,
3748 stats->tx_errors, stats->tx_dropped,
3749 stats->tx_fifo_errors, stats->collisions,
3750 stats->tx_carrier_errors +
3751 stats->tx_aborted_errors +
3752 stats->tx_window_errors +
3753 stats->tx_heartbeat_errors,
3754 stats->tx_compressed);
1da177e4
LT
3755}
3756
3757/*
3758 * Called from the PROCfs module. This now uses the new arbitrary sized
3759 * /proc/net interface to create /proc/net/dev
3760 */
3761static int dev_seq_show(struct seq_file *seq, void *v)
3762{
3763 if (v == SEQ_START_TOKEN)
3764 seq_puts(seq, "Inter-| Receive "
3765 " | Transmit\n"
3766 " face |bytes packets errs drop fifo frame "
3767 "compressed multicast|bytes packets errs "
3768 "drop fifo colls carrier compressed\n");
3769 else
3770 dev_seq_printf_stats(seq, v);
3771 return 0;
3772}
3773
dee42870 3774static struct softnet_data *softnet_get_online(loff_t *pos)
1da177e4 3775{
dee42870 3776 struct softnet_data *sd = NULL;
1da177e4 3777
0c0b0aca 3778 while (*pos < nr_cpu_ids)
4ec93edb 3779 if (cpu_online(*pos)) {
dee42870 3780 sd = &per_cpu(softnet_data, *pos);
1da177e4
LT
3781 break;
3782 } else
3783 ++*pos;
dee42870 3784 return sd;
1da177e4
LT
3785}
3786
3787static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
3788{
3789 return softnet_get_online(pos);
3790}
3791
3792static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3793{
3794 ++*pos;
3795 return softnet_get_online(pos);
3796}
3797
3798static void softnet_seq_stop(struct seq_file *seq, void *v)
3799{
3800}
3801
3802static int softnet_seq_show(struct seq_file *seq, void *v)
3803{
dee42870 3804 struct softnet_data *sd = v;
1da177e4 3805
0a9627f2 3806 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
dee42870 3807 sd->processed, sd->dropped, sd->time_squeeze, 0,
c1ebcdb8 3808 0, 0, 0, 0, /* was fastroute */
dee42870 3809 sd->cpu_collision, sd->received_rps);
1da177e4
LT
3810 return 0;
3811}
3812
f690808e 3813static const struct seq_operations dev_seq_ops = {
1da177e4
LT
3814 .start = dev_seq_start,
3815 .next = dev_seq_next,
3816 .stop = dev_seq_stop,
3817 .show = dev_seq_show,
3818};
3819
3820static int dev_seq_open(struct inode *inode, struct file *file)
3821{
e372c414
DL
3822 return seq_open_net(inode, file, &dev_seq_ops,
3823 sizeof(struct seq_net_private));
1da177e4
LT
3824}
3825
9a32144e 3826static const struct file_operations dev_seq_fops = {
1da177e4
LT
3827 .owner = THIS_MODULE,
3828 .open = dev_seq_open,
3829 .read = seq_read,
3830 .llseek = seq_lseek,
e372c414 3831 .release = seq_release_net,
1da177e4
LT
3832};
3833
f690808e 3834static const struct seq_operations softnet_seq_ops = {
1da177e4
LT
3835 .start = softnet_seq_start,
3836 .next = softnet_seq_next,
3837 .stop = softnet_seq_stop,
3838 .show = softnet_seq_show,
3839};
3840
3841static int softnet_seq_open(struct inode *inode, struct file *file)
3842{
3843 return seq_open(file, &softnet_seq_ops);
3844}
3845
9a32144e 3846static const struct file_operations softnet_seq_fops = {
1da177e4
LT
3847 .owner = THIS_MODULE,
3848 .open = softnet_seq_open,
3849 .read = seq_read,
3850 .llseek = seq_lseek,
3851 .release = seq_release,
3852};
3853
0e1256ff
SH
3854static void *ptype_get_idx(loff_t pos)
3855{
3856 struct packet_type *pt = NULL;
3857 loff_t i = 0;
3858 int t;
3859
3860 list_for_each_entry_rcu(pt, &ptype_all, list) {
3861 if (i == pos)
3862 return pt;
3863 ++i;
3864 }
3865
82d8a867 3866 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
0e1256ff
SH
3867 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
3868 if (i == pos)
3869 return pt;
3870 ++i;
3871 }
3872 }
3873 return NULL;
3874}
3875
3876static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
72348a42 3877 __acquires(RCU)
0e1256ff
SH
3878{
3879 rcu_read_lock();
3880 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
3881}
3882
3883static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3884{
3885 struct packet_type *pt;
3886 struct list_head *nxt;
3887 int hash;
3888
3889 ++*pos;
3890 if (v == SEQ_START_TOKEN)
3891 return ptype_get_idx(0);
3892
3893 pt = v;
3894 nxt = pt->list.next;
3895 if (pt->type == htons(ETH_P_ALL)) {
3896 if (nxt != &ptype_all)
3897 goto found;
3898 hash = 0;
3899 nxt = ptype_base[0].next;
3900 } else
82d8a867 3901 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
0e1256ff
SH
3902
3903 while (nxt == &ptype_base[hash]) {
82d8a867 3904 if (++hash >= PTYPE_HASH_SIZE)
0e1256ff
SH
3905 return NULL;
3906 nxt = ptype_base[hash].next;
3907 }
3908found:
3909 return list_entry(nxt, struct packet_type, list);
3910}
3911
3912static void ptype_seq_stop(struct seq_file *seq, void *v)
72348a42 3913 __releases(RCU)
0e1256ff
SH
3914{
3915 rcu_read_unlock();
3916}
3917
0e1256ff
SH
3918static int ptype_seq_show(struct seq_file *seq, void *v)
3919{
3920 struct packet_type *pt = v;
3921
3922 if (v == SEQ_START_TOKEN)
3923 seq_puts(seq, "Type Device Function\n");
c346dca1 3924 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
0e1256ff
SH
3925 if (pt->type == htons(ETH_P_ALL))
3926 seq_puts(seq, "ALL ");
3927 else
3928 seq_printf(seq, "%04x", ntohs(pt->type));
3929
908cd2da
AD
3930 seq_printf(seq, " %-8s %pF\n",
3931 pt->dev ? pt->dev->name : "", pt->func);
0e1256ff
SH
3932 }
3933
3934 return 0;
3935}
3936
3937static const struct seq_operations ptype_seq_ops = {
3938 .start = ptype_seq_start,
3939 .next = ptype_seq_next,
3940 .stop = ptype_seq_stop,
3941 .show = ptype_seq_show,
3942};
3943
3944static int ptype_seq_open(struct inode *inode, struct file *file)
3945{
2feb27db
PE
3946 return seq_open_net(inode, file, &ptype_seq_ops,
3947 sizeof(struct seq_net_private));
0e1256ff
SH
3948}
3949
3950static const struct file_operations ptype_seq_fops = {
3951 .owner = THIS_MODULE,
3952 .open = ptype_seq_open,
3953 .read = seq_read,
3954 .llseek = seq_lseek,
2feb27db 3955 .release = seq_release_net,
0e1256ff
SH
3956};
3957
3958
4665079c 3959static int __net_init dev_proc_net_init(struct net *net)
1da177e4
LT
3960{
3961 int rc = -ENOMEM;
3962
881d966b 3963 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
1da177e4 3964 goto out;
881d966b 3965 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
1da177e4 3966 goto out_dev;
881d966b 3967 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
457c4cbc 3968 goto out_softnet;
0e1256ff 3969
881d966b 3970 if (wext_proc_init(net))
457c4cbc 3971 goto out_ptype;
1da177e4
LT
3972 rc = 0;
3973out:
3974 return rc;
457c4cbc 3975out_ptype:
881d966b 3976 proc_net_remove(net, "ptype");
1da177e4 3977out_softnet:
881d966b 3978 proc_net_remove(net, "softnet_stat");
1da177e4 3979out_dev:
881d966b 3980 proc_net_remove(net, "dev");
1da177e4
LT
3981 goto out;
3982}
881d966b 3983
4665079c 3984static void __net_exit dev_proc_net_exit(struct net *net)
881d966b
EB
3985{
3986 wext_proc_exit(net);
3987
3988 proc_net_remove(net, "ptype");
3989 proc_net_remove(net, "softnet_stat");
3990 proc_net_remove(net, "dev");
3991}
3992
022cbae6 3993static struct pernet_operations __net_initdata dev_proc_ops = {
881d966b
EB
3994 .init = dev_proc_net_init,
3995 .exit = dev_proc_net_exit,
3996};
3997
3998static int __init dev_proc_init(void)
3999{
4000 return register_pernet_subsys(&dev_proc_ops);
4001}
1da177e4
LT
4002#else
4003#define dev_proc_init() 0
4004#endif /* CONFIG_PROC_FS */
4005
4006
4007/**
4008 * netdev_set_master - set up master/slave pair
4009 * @slave: slave device
4010 * @master: new master device
4011 *
4012 * Changes the master device of the slave. Pass %NULL to break the
4013 * bonding. The caller must hold the RTNL semaphore. On a failure
4014 * a negative errno code is returned. On success the reference counts
4015 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
4016 * function returns zero.
4017 */
4018int netdev_set_master(struct net_device *slave, struct net_device *master)
4019{
4020 struct net_device *old = slave->master;
4021
4022 ASSERT_RTNL();
4023
4024 if (master) {
4025 if (old)
4026 return -EBUSY;
4027 dev_hold(master);
4028 }
4029
4030 slave->master = master;
4ec93edb 4031
283f2fe8
ED
4032 if (old) {
4033 synchronize_net();
1da177e4 4034 dev_put(old);
283f2fe8 4035 }
1da177e4
LT
4036 if (master)
4037 slave->flags |= IFF_SLAVE;
4038 else
4039 slave->flags &= ~IFF_SLAVE;
4040
4041 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
4042 return 0;
4043}
d1b19dff 4044EXPORT_SYMBOL(netdev_set_master);
1da177e4 4045
b6c40d68
PM
4046static void dev_change_rx_flags(struct net_device *dev, int flags)
4047{
d314774c
SH
4048 const struct net_device_ops *ops = dev->netdev_ops;
4049
4050 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
4051 ops->ndo_change_rx_flags(dev, flags);
b6c40d68
PM
4052}
4053
dad9b335 4054static int __dev_set_promiscuity(struct net_device *dev, int inc)
1da177e4
LT
4055{
4056 unsigned short old_flags = dev->flags;
8192b0c4
DH
4057 uid_t uid;
4058 gid_t gid;
1da177e4 4059
24023451
PM
4060 ASSERT_RTNL();
4061
dad9b335
WC
4062 dev->flags |= IFF_PROMISC;
4063 dev->promiscuity += inc;
4064 if (dev->promiscuity == 0) {
4065 /*
4066 * Avoid overflow.
4067 * If inc causes overflow, untouch promisc and return error.
4068 */
4069 if (inc < 0)
4070 dev->flags &= ~IFF_PROMISC;
4071 else {
4072 dev->promiscuity -= inc;
4073 printk(KERN_WARNING "%s: promiscuity touches roof, "
4074 "set promiscuity failed, promiscuity feature "
4075 "of device might be broken.\n", dev->name);
4076 return -EOVERFLOW;
4077 }
4078 }
52609c0b 4079 if (dev->flags != old_flags) {
1da177e4
LT
4080 printk(KERN_INFO "device %s %s promiscuous mode\n",
4081 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
4ec93edb 4082 "left");
8192b0c4
DH
4083 if (audit_enabled) {
4084 current_uid_gid(&uid, &gid);
7759db82
KHK
4085 audit_log(current->audit_context, GFP_ATOMIC,
4086 AUDIT_ANOM_PROMISCUOUS,
4087 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
4088 dev->name, (dev->flags & IFF_PROMISC),
4089 (old_flags & IFF_PROMISC),
4090 audit_get_loginuid(current),
8192b0c4 4091 uid, gid,
7759db82 4092 audit_get_sessionid(current));
8192b0c4 4093 }
24023451 4094
b6c40d68 4095 dev_change_rx_flags(dev, IFF_PROMISC);
1da177e4 4096 }
dad9b335 4097 return 0;
1da177e4
LT
4098}
4099
4417da66
PM
4100/**
4101 * dev_set_promiscuity - update promiscuity count on a device
4102 * @dev: device
4103 * @inc: modifier
4104 *
4105 * Add or remove promiscuity from a device. While the count in the device
4106 * remains above zero the interface remains promiscuous. Once it hits zero
4107 * the device reverts back to normal filtering operation. A negative inc
4108 * value is used to drop promiscuity on the device.
dad9b335 4109 * Return 0 if successful or a negative errno code on error.
4417da66 4110 */
dad9b335 4111int dev_set_promiscuity(struct net_device *dev, int inc)
4417da66
PM
4112{
4113 unsigned short old_flags = dev->flags;
dad9b335 4114 int err;
4417da66 4115
dad9b335 4116 err = __dev_set_promiscuity(dev, inc);
4b5a698e 4117 if (err < 0)
dad9b335 4118 return err;
4417da66
PM
4119 if (dev->flags != old_flags)
4120 dev_set_rx_mode(dev);
dad9b335 4121 return err;
4417da66 4122}
d1b19dff 4123EXPORT_SYMBOL(dev_set_promiscuity);
4417da66 4124
1da177e4
LT
4125/**
4126 * dev_set_allmulti - update allmulti count on a device
4127 * @dev: device
4128 * @inc: modifier
4129 *
4130 * Add or remove reception of all multicast frames to a device. While the
4131 * count in the device remains above zero the interface remains listening
4132 * to all interfaces. Once it hits zero the device reverts back to normal
4133 * filtering operation. A negative @inc value is used to drop the counter
4134 * when releasing a resource needing all multicasts.
dad9b335 4135 * Return 0 if successful or a negative errno code on error.
1da177e4
LT
4136 */
4137
dad9b335 4138int dev_set_allmulti(struct net_device *dev, int inc)
1da177e4
LT
4139{
4140 unsigned short old_flags = dev->flags;
4141
24023451
PM
4142 ASSERT_RTNL();
4143
1da177e4 4144 dev->flags |= IFF_ALLMULTI;
dad9b335
WC
4145 dev->allmulti += inc;
4146 if (dev->allmulti == 0) {
4147 /*
4148 * Avoid overflow.
4149 * If inc causes overflow, untouch allmulti and return error.
4150 */
4151 if (inc < 0)
4152 dev->flags &= ~IFF_ALLMULTI;
4153 else {
4154 dev->allmulti -= inc;
4155 printk(KERN_WARNING "%s: allmulti touches roof, "
4156 "set allmulti failed, allmulti feature of "
4157 "device might be broken.\n", dev->name);
4158 return -EOVERFLOW;
4159 }
4160 }
24023451 4161 if (dev->flags ^ old_flags) {
b6c40d68 4162 dev_change_rx_flags(dev, IFF_ALLMULTI);
4417da66 4163 dev_set_rx_mode(dev);
24023451 4164 }
dad9b335 4165 return 0;
4417da66 4166}
d1b19dff 4167EXPORT_SYMBOL(dev_set_allmulti);
4417da66
PM
4168
4169/*
4170 * Upload unicast and multicast address lists to device and
4171 * configure RX filtering. When the device doesn't support unicast
53ccaae1 4172 * filtering it is put in promiscuous mode while unicast addresses
4417da66
PM
4173 * are present.
4174 */
4175void __dev_set_rx_mode(struct net_device *dev)
4176{
d314774c
SH
4177 const struct net_device_ops *ops = dev->netdev_ops;
4178
4417da66
PM
4179 /* dev_open will call this function so the list will stay sane. */
4180 if (!(dev->flags&IFF_UP))
4181 return;
4182
4183 if (!netif_device_present(dev))
40b77c94 4184 return;
4417da66 4185
d314774c
SH
4186 if (ops->ndo_set_rx_mode)
4187 ops->ndo_set_rx_mode(dev);
4417da66
PM
4188 else {
4189 /* Unicast addresses changes may only happen under the rtnl,
4190 * therefore calling __dev_set_promiscuity here is safe.
4191 */
32e7bfc4 4192 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
4417da66
PM
4193 __dev_set_promiscuity(dev, 1);
4194 dev->uc_promisc = 1;
32e7bfc4 4195 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
4417da66
PM
4196 __dev_set_promiscuity(dev, -1);
4197 dev->uc_promisc = 0;
4198 }
4199
d314774c
SH
4200 if (ops->ndo_set_multicast_list)
4201 ops->ndo_set_multicast_list(dev);
4417da66
PM
4202 }
4203}
4204
4205void dev_set_rx_mode(struct net_device *dev)
4206{
b9e40857 4207 netif_addr_lock_bh(dev);
4417da66 4208 __dev_set_rx_mode(dev);
b9e40857 4209 netif_addr_unlock_bh(dev);
1da177e4
LT
4210}
4211
f0db275a
SH
4212/**
4213 * dev_get_flags - get flags reported to userspace
4214 * @dev: device
4215 *
4216 * Get the combination of flag bits exported through APIs to userspace.
4217 */
1da177e4
LT
4218unsigned dev_get_flags(const struct net_device *dev)
4219{
4220 unsigned flags;
4221
4222 flags = (dev->flags & ~(IFF_PROMISC |
4223 IFF_ALLMULTI |
b00055aa
SR
4224 IFF_RUNNING |
4225 IFF_LOWER_UP |
4226 IFF_DORMANT)) |
1da177e4
LT
4227 (dev->gflags & (IFF_PROMISC |
4228 IFF_ALLMULTI));
4229
b00055aa
SR
4230 if (netif_running(dev)) {
4231 if (netif_oper_up(dev))
4232 flags |= IFF_RUNNING;
4233 if (netif_carrier_ok(dev))
4234 flags |= IFF_LOWER_UP;
4235 if (netif_dormant(dev))
4236 flags |= IFF_DORMANT;
4237 }
1da177e4
LT
4238
4239 return flags;
4240}
d1b19dff 4241EXPORT_SYMBOL(dev_get_flags);
1da177e4 4242
bd380811 4243int __dev_change_flags(struct net_device *dev, unsigned int flags)
1da177e4 4244{
1da177e4 4245 int old_flags = dev->flags;
bd380811 4246 int ret;
1da177e4 4247
24023451
PM
4248 ASSERT_RTNL();
4249
1da177e4
LT
4250 /*
4251 * Set the flags on our device.
4252 */
4253
4254 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
4255 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
4256 IFF_AUTOMEDIA)) |
4257 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
4258 IFF_ALLMULTI));
4259
4260 /*
4261 * Load in the correct multicast list now the flags have changed.
4262 */
4263
b6c40d68
PM
4264 if ((old_flags ^ flags) & IFF_MULTICAST)
4265 dev_change_rx_flags(dev, IFF_MULTICAST);
24023451 4266
4417da66 4267 dev_set_rx_mode(dev);
1da177e4
LT
4268
4269 /*
4270 * Have we downed the interface. We handle IFF_UP ourselves
4271 * according to user attempts to set it, rather than blindly
4272 * setting it.
4273 */
4274
4275 ret = 0;
4276 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
bd380811 4277 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
1da177e4
LT
4278
4279 if (!ret)
4417da66 4280 dev_set_rx_mode(dev);
1da177e4
LT
4281 }
4282
1da177e4 4283 if ((flags ^ dev->gflags) & IFF_PROMISC) {
d1b19dff
ED
4284 int inc = (flags & IFF_PROMISC) ? 1 : -1;
4285
1da177e4
LT
4286 dev->gflags ^= IFF_PROMISC;
4287 dev_set_promiscuity(dev, inc);
4288 }
4289
4290 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
4291 is important. Some (broken) drivers set IFF_PROMISC, when
4292 IFF_ALLMULTI is requested not asking us and not reporting.
4293 */
4294 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
d1b19dff
ED
4295 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
4296
1da177e4
LT
4297 dev->gflags ^= IFF_ALLMULTI;
4298 dev_set_allmulti(dev, inc);
4299 }
4300
bd380811
PM
4301 return ret;
4302}
4303
4304void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
4305{
4306 unsigned int changes = dev->flags ^ old_flags;
4307
4308 if (changes & IFF_UP) {
4309 if (dev->flags & IFF_UP)
4310 call_netdevice_notifiers(NETDEV_UP, dev);
4311 else
4312 call_netdevice_notifiers(NETDEV_DOWN, dev);
4313 }
4314
4315 if (dev->flags & IFF_UP &&
4316 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
4317 call_netdevice_notifiers(NETDEV_CHANGE, dev);
4318}
4319
4320/**
4321 * dev_change_flags - change device settings
4322 * @dev: device
4323 * @flags: device state flags
4324 *
4325 * Change settings on device based state flags. The flags are
4326 * in the userspace exported format.
4327 */
4328int dev_change_flags(struct net_device *dev, unsigned flags)
4329{
4330 int ret, changes;
4331 int old_flags = dev->flags;
4332
4333 ret = __dev_change_flags(dev, flags);
4334 if (ret < 0)
4335 return ret;
4336
4337 changes = old_flags ^ dev->flags;
7c355f53
TG
4338 if (changes)
4339 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
1da177e4 4340
bd380811 4341 __dev_notify_flags(dev, old_flags);
1da177e4
LT
4342 return ret;
4343}
d1b19dff 4344EXPORT_SYMBOL(dev_change_flags);
1da177e4 4345
f0db275a
SH
4346/**
4347 * dev_set_mtu - Change maximum transfer unit
4348 * @dev: device
4349 * @new_mtu: new transfer unit
4350 *
4351 * Change the maximum transfer size of the network device.
4352 */
1da177e4
LT
4353int dev_set_mtu(struct net_device *dev, int new_mtu)
4354{
d314774c 4355 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
4356 int err;
4357
4358 if (new_mtu == dev->mtu)
4359 return 0;
4360
4361 /* MTU must be positive. */
4362 if (new_mtu < 0)
4363 return -EINVAL;
4364
4365 if (!netif_device_present(dev))
4366 return -ENODEV;
4367
4368 err = 0;
d314774c
SH
4369 if (ops->ndo_change_mtu)
4370 err = ops->ndo_change_mtu(dev, new_mtu);
1da177e4
LT
4371 else
4372 dev->mtu = new_mtu;
d314774c 4373
1da177e4 4374 if (!err && dev->flags & IFF_UP)
056925ab 4375 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
1da177e4
LT
4376 return err;
4377}
d1b19dff 4378EXPORT_SYMBOL(dev_set_mtu);
1da177e4 4379
f0db275a
SH
4380/**
4381 * dev_set_mac_address - Change Media Access Control Address
4382 * @dev: device
4383 * @sa: new address
4384 *
4385 * Change the hardware (MAC) address of the device
4386 */
1da177e4
LT
4387int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
4388{
d314774c 4389 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
4390 int err;
4391
d314774c 4392 if (!ops->ndo_set_mac_address)
1da177e4
LT
4393 return -EOPNOTSUPP;
4394 if (sa->sa_family != dev->type)
4395 return -EINVAL;
4396 if (!netif_device_present(dev))
4397 return -ENODEV;
d314774c 4398 err = ops->ndo_set_mac_address(dev, sa);
1da177e4 4399 if (!err)
056925ab 4400 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
4401 return err;
4402}
d1b19dff 4403EXPORT_SYMBOL(dev_set_mac_address);
1da177e4
LT
4404
4405/*
3710becf 4406 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
1da177e4 4407 */
14e3e079 4408static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
1da177e4
LT
4409{
4410 int err;
3710becf 4411 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
1da177e4
LT
4412
4413 if (!dev)
4414 return -ENODEV;
4415
4416 switch (cmd) {
d1b19dff
ED
4417 case SIOCGIFFLAGS: /* Get interface flags */
4418 ifr->ifr_flags = (short) dev_get_flags(dev);
4419 return 0;
1da177e4 4420
d1b19dff
ED
4421 case SIOCGIFMETRIC: /* Get the metric on the interface
4422 (currently unused) */
4423 ifr->ifr_metric = 0;
4424 return 0;
1da177e4 4425
d1b19dff
ED
4426 case SIOCGIFMTU: /* Get the MTU of a device */
4427 ifr->ifr_mtu = dev->mtu;
4428 return 0;
1da177e4 4429
d1b19dff
ED
4430 case SIOCGIFHWADDR:
4431 if (!dev->addr_len)
4432 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
4433 else
4434 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
4435 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
4436 ifr->ifr_hwaddr.sa_family = dev->type;
4437 return 0;
1da177e4 4438
d1b19dff
ED
4439 case SIOCGIFSLAVE:
4440 err = -EINVAL;
4441 break;
14e3e079 4442
d1b19dff
ED
4443 case SIOCGIFMAP:
4444 ifr->ifr_map.mem_start = dev->mem_start;
4445 ifr->ifr_map.mem_end = dev->mem_end;
4446 ifr->ifr_map.base_addr = dev->base_addr;
4447 ifr->ifr_map.irq = dev->irq;
4448 ifr->ifr_map.dma = dev->dma;
4449 ifr->ifr_map.port = dev->if_port;
4450 return 0;
14e3e079 4451
d1b19dff
ED
4452 case SIOCGIFINDEX:
4453 ifr->ifr_ifindex = dev->ifindex;
4454 return 0;
14e3e079 4455
d1b19dff
ED
4456 case SIOCGIFTXQLEN:
4457 ifr->ifr_qlen = dev->tx_queue_len;
4458 return 0;
14e3e079 4459
d1b19dff
ED
4460 default:
4461 /* dev_ioctl() should ensure this case
4462 * is never reached
4463 */
4464 WARN_ON(1);
4465 err = -EINVAL;
4466 break;
14e3e079
JG
4467
4468 }
4469 return err;
4470}
4471
4472/*
4473 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
4474 */
4475static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
4476{
4477 int err;
4478 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
5f2f6da7 4479 const struct net_device_ops *ops;
14e3e079
JG
4480
4481 if (!dev)
4482 return -ENODEV;
4483
5f2f6da7
JP
4484 ops = dev->netdev_ops;
4485
14e3e079 4486 switch (cmd) {
d1b19dff
ED
4487 case SIOCSIFFLAGS: /* Set interface flags */
4488 return dev_change_flags(dev, ifr->ifr_flags);
14e3e079 4489
d1b19dff
ED
4490 case SIOCSIFMETRIC: /* Set the metric on the interface
4491 (currently unused) */
4492 return -EOPNOTSUPP;
14e3e079 4493
d1b19dff
ED
4494 case SIOCSIFMTU: /* Set the MTU of a device */
4495 return dev_set_mtu(dev, ifr->ifr_mtu);
1da177e4 4496
d1b19dff
ED
4497 case SIOCSIFHWADDR:
4498 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
1da177e4 4499
d1b19dff
ED
4500 case SIOCSIFHWBROADCAST:
4501 if (ifr->ifr_hwaddr.sa_family != dev->type)
4502 return -EINVAL;
4503 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
4504 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
4505 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
4506 return 0;
1da177e4 4507
d1b19dff
ED
4508 case SIOCSIFMAP:
4509 if (ops->ndo_set_config) {
1da177e4
LT
4510 if (!netif_device_present(dev))
4511 return -ENODEV;
d1b19dff
ED
4512 return ops->ndo_set_config(dev, &ifr->ifr_map);
4513 }
4514 return -EOPNOTSUPP;
1da177e4 4515
d1b19dff
ED
4516 case SIOCADDMULTI:
4517 if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
4518 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
4519 return -EINVAL;
4520 if (!netif_device_present(dev))
4521 return -ENODEV;
22bedad3 4522 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
d1b19dff
ED
4523
4524 case SIOCDELMULTI:
4525 if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
4526 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
4527 return -EINVAL;
4528 if (!netif_device_present(dev))
4529 return -ENODEV;
22bedad3 4530 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
1da177e4 4531
d1b19dff
ED
4532 case SIOCSIFTXQLEN:
4533 if (ifr->ifr_qlen < 0)
4534 return -EINVAL;
4535 dev->tx_queue_len = ifr->ifr_qlen;
4536 return 0;
1da177e4 4537
d1b19dff
ED
4538 case SIOCSIFNAME:
4539 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
4540 return dev_change_name(dev, ifr->ifr_newname);
1da177e4 4541
d1b19dff
ED
4542 /*
4543 * Unknown or private ioctl
4544 */
4545 default:
4546 if ((cmd >= SIOCDEVPRIVATE &&
4547 cmd <= SIOCDEVPRIVATE + 15) ||
4548 cmd == SIOCBONDENSLAVE ||
4549 cmd == SIOCBONDRELEASE ||
4550 cmd == SIOCBONDSETHWADDR ||
4551 cmd == SIOCBONDSLAVEINFOQUERY ||
4552 cmd == SIOCBONDINFOQUERY ||
4553 cmd == SIOCBONDCHANGEACTIVE ||
4554 cmd == SIOCGMIIPHY ||
4555 cmd == SIOCGMIIREG ||
4556 cmd == SIOCSMIIREG ||
4557 cmd == SIOCBRADDIF ||
4558 cmd == SIOCBRDELIF ||
4559 cmd == SIOCSHWTSTAMP ||
4560 cmd == SIOCWANDEV) {
4561 err = -EOPNOTSUPP;
4562 if (ops->ndo_do_ioctl) {
4563 if (netif_device_present(dev))
4564 err = ops->ndo_do_ioctl(dev, ifr, cmd);
4565 else
4566 err = -ENODEV;
4567 }
4568 } else
4569 err = -EINVAL;
1da177e4
LT
4570
4571 }
4572 return err;
4573}
4574
4575/*
4576 * This function handles all "interface"-type I/O control requests. The actual
4577 * 'doing' part of this is dev_ifsioc above.
4578 */
4579
4580/**
4581 * dev_ioctl - network device ioctl
c4ea43c5 4582 * @net: the applicable net namespace
1da177e4
LT
4583 * @cmd: command to issue
4584 * @arg: pointer to a struct ifreq in user space
4585 *
4586 * Issue ioctl functions to devices. This is normally called by the
4587 * user space syscall interfaces but can sometimes be useful for
4588 * other purposes. The return value is the return from the syscall if
4589 * positive or a negative errno code on error.
4590 */
4591
881d966b 4592int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1da177e4
LT
4593{
4594 struct ifreq ifr;
4595 int ret;
4596 char *colon;
4597
4598 /* One special case: SIOCGIFCONF takes ifconf argument
4599 and requires shared lock, because it sleeps writing
4600 to user space.
4601 */
4602
4603 if (cmd == SIOCGIFCONF) {
6756ae4b 4604 rtnl_lock();
881d966b 4605 ret = dev_ifconf(net, (char __user *) arg);
6756ae4b 4606 rtnl_unlock();
1da177e4
LT
4607 return ret;
4608 }
4609 if (cmd == SIOCGIFNAME)
881d966b 4610 return dev_ifname(net, (struct ifreq __user *)arg);
1da177e4
LT
4611
4612 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
4613 return -EFAULT;
4614
4615 ifr.ifr_name[IFNAMSIZ-1] = 0;
4616
4617 colon = strchr(ifr.ifr_name, ':');
4618 if (colon)
4619 *colon = 0;
4620
4621 /*
4622 * See which interface the caller is talking about.
4623 */
4624
4625 switch (cmd) {
d1b19dff
ED
4626 /*
4627 * These ioctl calls:
4628 * - can be done by all.
4629 * - atomic and do not require locking.
4630 * - return a value
4631 */
4632 case SIOCGIFFLAGS:
4633 case SIOCGIFMETRIC:
4634 case SIOCGIFMTU:
4635 case SIOCGIFHWADDR:
4636 case SIOCGIFSLAVE:
4637 case SIOCGIFMAP:
4638 case SIOCGIFINDEX:
4639 case SIOCGIFTXQLEN:
4640 dev_load(net, ifr.ifr_name);
3710becf 4641 rcu_read_lock();
d1b19dff 4642 ret = dev_ifsioc_locked(net, &ifr, cmd);
3710becf 4643 rcu_read_unlock();
d1b19dff
ED
4644 if (!ret) {
4645 if (colon)
4646 *colon = ':';
4647 if (copy_to_user(arg, &ifr,
4648 sizeof(struct ifreq)))
4649 ret = -EFAULT;
4650 }
4651 return ret;
1da177e4 4652
d1b19dff
ED
4653 case SIOCETHTOOL:
4654 dev_load(net, ifr.ifr_name);
4655 rtnl_lock();
4656 ret = dev_ethtool(net, &ifr);
4657 rtnl_unlock();
4658 if (!ret) {
4659 if (colon)
4660 *colon = ':';
4661 if (copy_to_user(arg, &ifr,
4662 sizeof(struct ifreq)))
4663 ret = -EFAULT;
4664 }
4665 return ret;
1da177e4 4666
d1b19dff
ED
4667 /*
4668 * These ioctl calls:
4669 * - require superuser power.
4670 * - require strict serialization.
4671 * - return a value
4672 */
4673 case SIOCGMIIPHY:
4674 case SIOCGMIIREG:
4675 case SIOCSIFNAME:
4676 if (!capable(CAP_NET_ADMIN))
4677 return -EPERM;
4678 dev_load(net, ifr.ifr_name);
4679 rtnl_lock();
4680 ret = dev_ifsioc(net, &ifr, cmd);
4681 rtnl_unlock();
4682 if (!ret) {
4683 if (colon)
4684 *colon = ':';
4685 if (copy_to_user(arg, &ifr,
4686 sizeof(struct ifreq)))
4687 ret = -EFAULT;
4688 }
4689 return ret;
1da177e4 4690
d1b19dff
ED
4691 /*
4692 * These ioctl calls:
4693 * - require superuser power.
4694 * - require strict serialization.
4695 * - do not return a value
4696 */
4697 case SIOCSIFFLAGS:
4698 case SIOCSIFMETRIC:
4699 case SIOCSIFMTU:
4700 case SIOCSIFMAP:
4701 case SIOCSIFHWADDR:
4702 case SIOCSIFSLAVE:
4703 case SIOCADDMULTI:
4704 case SIOCDELMULTI:
4705 case SIOCSIFHWBROADCAST:
4706 case SIOCSIFTXQLEN:
4707 case SIOCSMIIREG:
4708 case SIOCBONDENSLAVE:
4709 case SIOCBONDRELEASE:
4710 case SIOCBONDSETHWADDR:
4711 case SIOCBONDCHANGEACTIVE:
4712 case SIOCBRADDIF:
4713 case SIOCBRDELIF:
4714 case SIOCSHWTSTAMP:
4715 if (!capable(CAP_NET_ADMIN))
4716 return -EPERM;
4717 /* fall through */
4718 case SIOCBONDSLAVEINFOQUERY:
4719 case SIOCBONDINFOQUERY:
4720 dev_load(net, ifr.ifr_name);
4721 rtnl_lock();
4722 ret = dev_ifsioc(net, &ifr, cmd);
4723 rtnl_unlock();
4724 return ret;
4725
4726 case SIOCGIFMEM:
4727 /* Get the per device memory space. We can add this but
4728 * currently do not support it */
4729 case SIOCSIFMEM:
4730 /* Set the per device memory buffer space.
4731 * Not applicable in our case */
4732 case SIOCSIFLINK:
4733 return -EINVAL;
4734
4735 /*
4736 * Unknown or private ioctl.
4737 */
4738 default:
4739 if (cmd == SIOCWANDEV ||
4740 (cmd >= SIOCDEVPRIVATE &&
4741 cmd <= SIOCDEVPRIVATE + 15)) {
881d966b 4742 dev_load(net, ifr.ifr_name);
1da177e4 4743 rtnl_lock();
881d966b 4744 ret = dev_ifsioc(net, &ifr, cmd);
1da177e4 4745 rtnl_unlock();
d1b19dff
ED
4746 if (!ret && copy_to_user(arg, &ifr,
4747 sizeof(struct ifreq)))
4748 ret = -EFAULT;
1da177e4 4749 return ret;
d1b19dff
ED
4750 }
4751 /* Take care of Wireless Extensions */
4752 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
4753 return wext_handle_ioctl(net, &ifr, cmd, arg);
4754 return -EINVAL;
1da177e4
LT
4755 }
4756}
4757
4758
4759/**
4760 * dev_new_index - allocate an ifindex
c4ea43c5 4761 * @net: the applicable net namespace
1da177e4
LT
4762 *
4763 * Returns a suitable unique value for a new device interface
4764 * number. The caller must hold the rtnl semaphore or the
4765 * dev_base_lock to be sure it remains unique.
4766 */
881d966b 4767static int dev_new_index(struct net *net)
1da177e4
LT
4768{
4769 static int ifindex;
4770 for (;;) {
4771 if (++ifindex <= 0)
4772 ifindex = 1;
881d966b 4773 if (!__dev_get_by_index(net, ifindex))
1da177e4
LT
4774 return ifindex;
4775 }
4776}
4777
1da177e4 4778/* Delayed registration/unregisteration */
3b5b34fd 4779static LIST_HEAD(net_todo_list);
1da177e4 4780
6f05f629 4781static void net_set_todo(struct net_device *dev)
1da177e4 4782{
1da177e4 4783 list_add_tail(&dev->todo_list, &net_todo_list);
1da177e4
LT
4784}
4785
9b5e383c 4786static void rollback_registered_many(struct list_head *head)
93ee31f1 4787{
e93737b0 4788 struct net_device *dev, *tmp;
9b5e383c 4789
93ee31f1
DL
4790 BUG_ON(dev_boot_phase);
4791 ASSERT_RTNL();
4792
e93737b0 4793 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
9b5e383c 4794 /* Some devices call without registering
e93737b0
KK
4795 * for initialization unwind. Remove those
4796 * devices and proceed with the remaining.
9b5e383c
ED
4797 */
4798 if (dev->reg_state == NETREG_UNINITIALIZED) {
4799 pr_debug("unregister_netdevice: device %s/%p never "
4800 "was registered\n", dev->name, dev);
93ee31f1 4801
9b5e383c 4802 WARN_ON(1);
e93737b0
KK
4803 list_del(&dev->unreg_list);
4804 continue;
9b5e383c 4805 }
93ee31f1 4806
9b5e383c 4807 BUG_ON(dev->reg_state != NETREG_REGISTERED);
93ee31f1 4808
9b5e383c
ED
4809 /* If device is running, close it first. */
4810 dev_close(dev);
93ee31f1 4811
9b5e383c
ED
4812 /* And unlink it from device chain. */
4813 unlist_netdevice(dev);
93ee31f1 4814
9b5e383c
ED
4815 dev->reg_state = NETREG_UNREGISTERING;
4816 }
93ee31f1
DL
4817
4818 synchronize_net();
4819
9b5e383c
ED
4820 list_for_each_entry(dev, head, unreg_list) {
4821 /* Shutdown queueing discipline. */
4822 dev_shutdown(dev);
93ee31f1
DL
4823
4824
9b5e383c
ED
4825 /* Notify protocols, that we are about to destroy
4826 this device. They should clean all the things.
4827 */
4828 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
93ee31f1 4829
a2835763
PM
4830 if (!dev->rtnl_link_ops ||
4831 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
4832 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
4833
9b5e383c
ED
4834 /*
4835 * Flush the unicast and multicast chains
4836 */
a748ee24 4837 dev_uc_flush(dev);
22bedad3 4838 dev_mc_flush(dev);
93ee31f1 4839
9b5e383c
ED
4840 if (dev->netdev_ops->ndo_uninit)
4841 dev->netdev_ops->ndo_uninit(dev);
93ee31f1 4842
9b5e383c
ED
4843 /* Notifier chain MUST detach us from master device. */
4844 WARN_ON(dev->master);
93ee31f1 4845
9b5e383c
ED
4846 /* Remove entries from kobject tree */
4847 netdev_unregister_kobject(dev);
4848 }
93ee31f1 4849
a5ee1551 4850 /* Process any work delayed until the end of the batch */
e5e26d75 4851 dev = list_first_entry(head, struct net_device, unreg_list);
a5ee1551 4852 call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
93ee31f1 4853
a5ee1551 4854 synchronize_net();
395264d5 4855
a5ee1551 4856 list_for_each_entry(dev, head, unreg_list)
9b5e383c
ED
4857 dev_put(dev);
4858}
4859
4860static void rollback_registered(struct net_device *dev)
4861{
4862 LIST_HEAD(single);
4863
4864 list_add(&dev->unreg_list, &single);
4865 rollback_registered_many(&single);
93ee31f1
DL
4866}
4867
e8a0464c
DM
4868static void __netdev_init_queue_locks_one(struct net_device *dev,
4869 struct netdev_queue *dev_queue,
4870 void *_unused)
c773e847
DM
4871{
4872 spin_lock_init(&dev_queue->_xmit_lock);
cf508b12 4873 netdev_set_xmit_lockdep_class(&dev_queue->_xmit_lock, dev->type);
c773e847
DM
4874 dev_queue->xmit_lock_owner = -1;
4875}
4876
4877static void netdev_init_queue_locks(struct net_device *dev)
4878{
e8a0464c
DM
4879 netdev_for_each_tx_queue(dev, __netdev_init_queue_locks_one, NULL);
4880 __netdev_init_queue_locks_one(dev, &dev->rx_queue, NULL);
c773e847
DM
4881}
4882
b63365a2
HX
4883unsigned long netdev_fix_features(unsigned long features, const char *name)
4884{
4885 /* Fix illegal SG+CSUM combinations. */
4886 if ((features & NETIF_F_SG) &&
4887 !(features & NETIF_F_ALL_CSUM)) {
4888 if (name)
4889 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no "
4890 "checksum feature.\n", name);
4891 features &= ~NETIF_F_SG;
4892 }
4893
4894 /* TSO requires that SG is present as well. */
4895 if ((features & NETIF_F_TSO) && !(features & NETIF_F_SG)) {
4896 if (name)
4897 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no "
4898 "SG feature.\n", name);
4899 features &= ~NETIF_F_TSO;
4900 }
4901
4902 if (features & NETIF_F_UFO) {
4903 if (!(features & NETIF_F_GEN_CSUM)) {
4904 if (name)
4905 printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
4906 "since no NETIF_F_HW_CSUM feature.\n",
4907 name);
4908 features &= ~NETIF_F_UFO;
4909 }
4910
4911 if (!(features & NETIF_F_SG)) {
4912 if (name)
4913 printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
4914 "since no NETIF_F_SG feature.\n", name);
4915 features &= ~NETIF_F_UFO;
4916 }
4917 }
4918
4919 return features;
4920}
4921EXPORT_SYMBOL(netdev_fix_features);
4922
fc4a7489
PM
4923/**
4924 * netif_stacked_transfer_operstate - transfer operstate
4925 * @rootdev: the root or lower level device to transfer state from
4926 * @dev: the device to transfer operstate to
4927 *
4928 * Transfer operational state from root to device. This is normally
4929 * called when a stacking relationship exists between the root
4930 * device and the device(a leaf device).
4931 */
4932void netif_stacked_transfer_operstate(const struct net_device *rootdev,
4933 struct net_device *dev)
4934{
4935 if (rootdev->operstate == IF_OPER_DORMANT)
4936 netif_dormant_on(dev);
4937 else
4938 netif_dormant_off(dev);
4939
4940 if (netif_carrier_ok(rootdev)) {
4941 if (!netif_carrier_ok(dev))
4942 netif_carrier_on(dev);
4943 } else {
4944 if (netif_carrier_ok(dev))
4945 netif_carrier_off(dev);
4946 }
4947}
4948EXPORT_SYMBOL(netif_stacked_transfer_operstate);
4949
1da177e4
LT
4950/**
4951 * register_netdevice - register a network device
4952 * @dev: device to register
4953 *
4954 * Take a completed network device structure and add it to the kernel
4955 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4956 * chain. 0 is returned on success. A negative errno code is returned
4957 * on a failure to set up the device, or if the name is a duplicate.
4958 *
4959 * Callers must hold the rtnl semaphore. You may want
4960 * register_netdev() instead of this.
4961 *
4962 * BUGS:
4963 * The locking appears insufficient to guarantee two parallel registers
4964 * will not get the same name.
4965 */
4966
4967int register_netdevice(struct net_device *dev)
4968{
1da177e4 4969 int ret;
d314774c 4970 struct net *net = dev_net(dev);
1da177e4
LT
4971
4972 BUG_ON(dev_boot_phase);
4973 ASSERT_RTNL();
4974
b17a7c17
SH
4975 might_sleep();
4976
1da177e4
LT
4977 /* When net_device's are persistent, this will be fatal. */
4978 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
d314774c 4979 BUG_ON(!net);
1da177e4 4980
f1f28aa3 4981 spin_lock_init(&dev->addr_list_lock);
cf508b12 4982 netdev_set_addr_lockdep_class(dev);
c773e847 4983 netdev_init_queue_locks(dev);
1da177e4 4984
1da177e4
LT
4985 dev->iflink = -1;
4986
df334545 4987#ifdef CONFIG_RPS
0a9627f2
TH
4988 if (!dev->num_rx_queues) {
4989 /*
4990 * Allocate a single RX queue if driver never called
4991 * alloc_netdev_mq
4992 */
4993
4994 dev->_rx = kzalloc(sizeof(struct netdev_rx_queue), GFP_KERNEL);
4995 if (!dev->_rx) {
4996 ret = -ENOMEM;
4997 goto out;
4998 }
4999
5000 dev->_rx->first = dev->_rx;
5001 atomic_set(&dev->_rx->count, 1);
5002 dev->num_rx_queues = 1;
5003 }
df334545 5004#endif
1da177e4 5005 /* Init, if this function is available */
d314774c
SH
5006 if (dev->netdev_ops->ndo_init) {
5007 ret = dev->netdev_ops->ndo_init(dev);
1da177e4
LT
5008 if (ret) {
5009 if (ret > 0)
5010 ret = -EIO;
90833aa4 5011 goto out;
1da177e4
LT
5012 }
5013 }
4ec93edb 5014
8ce6cebc 5015 ret = dev_get_valid_name(dev, dev->name, 0);
d9031024 5016 if (ret)
7ce1b0ed 5017 goto err_uninit;
1da177e4 5018
881d966b 5019 dev->ifindex = dev_new_index(net);
1da177e4
LT
5020 if (dev->iflink == -1)
5021 dev->iflink = dev->ifindex;
5022
d212f87b
SH
5023 /* Fix illegal checksum combinations */
5024 if ((dev->features & NETIF_F_HW_CSUM) &&
5025 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5026 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
5027 dev->name);
5028 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5029 }
5030
5031 if ((dev->features & NETIF_F_NO_CSUM) &&
5032 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5033 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
5034 dev->name);
5035 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
5036 }
5037
b63365a2 5038 dev->features = netdev_fix_features(dev->features, dev->name);
1da177e4 5039
e5a4a72d
LB
5040 /* Enable software GSO if SG is supported. */
5041 if (dev->features & NETIF_F_SG)
5042 dev->features |= NETIF_F_GSO;
5043
7ffbe3fd
JB
5044 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
5045 ret = notifier_to_errno(ret);
5046 if (ret)
5047 goto err_uninit;
5048
8b41d188 5049 ret = netdev_register_kobject(dev);
b17a7c17 5050 if (ret)
7ce1b0ed 5051 goto err_uninit;
b17a7c17
SH
5052 dev->reg_state = NETREG_REGISTERED;
5053
1da177e4
LT
5054 /*
5055 * Default initial state at registry is that the
5056 * device is present.
5057 */
5058
5059 set_bit(__LINK_STATE_PRESENT, &dev->state);
5060
1da177e4 5061 dev_init_scheduler(dev);
1da177e4 5062 dev_hold(dev);
ce286d32 5063 list_netdevice(dev);
1da177e4
LT
5064
5065 /* Notify protocols, that a new device appeared. */
056925ab 5066 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
fcc5a03a 5067 ret = notifier_to_errno(ret);
93ee31f1
DL
5068 if (ret) {
5069 rollback_registered(dev);
5070 dev->reg_state = NETREG_UNREGISTERED;
5071 }
d90a909e
EB
5072 /*
5073 * Prevent userspace races by waiting until the network
5074 * device is fully setup before sending notifications.
5075 */
a2835763
PM
5076 if (!dev->rtnl_link_ops ||
5077 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5078 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1da177e4
LT
5079
5080out:
5081 return ret;
7ce1b0ed
HX
5082
5083err_uninit:
d314774c
SH
5084 if (dev->netdev_ops->ndo_uninit)
5085 dev->netdev_ops->ndo_uninit(dev);
7ce1b0ed 5086 goto out;
1da177e4 5087}
d1b19dff 5088EXPORT_SYMBOL(register_netdevice);
1da177e4 5089
937f1ba5
BH
5090/**
5091 * init_dummy_netdev - init a dummy network device for NAPI
5092 * @dev: device to init
5093 *
5094 * This takes a network device structure and initialize the minimum
5095 * amount of fields so it can be used to schedule NAPI polls without
5096 * registering a full blown interface. This is to be used by drivers
5097 * that need to tie several hardware interfaces to a single NAPI
5098 * poll scheduler due to HW limitations.
5099 */
5100int init_dummy_netdev(struct net_device *dev)
5101{
5102 /* Clear everything. Note we don't initialize spinlocks
5103 * are they aren't supposed to be taken by any of the
5104 * NAPI code and this dummy netdev is supposed to be
5105 * only ever used for NAPI polls
5106 */
5107 memset(dev, 0, sizeof(struct net_device));
5108
5109 /* make sure we BUG if trying to hit standard
5110 * register/unregister code path
5111 */
5112 dev->reg_state = NETREG_DUMMY;
5113
5114 /* initialize the ref count */
5115 atomic_set(&dev->refcnt, 1);
5116
5117 /* NAPI wants this */
5118 INIT_LIST_HEAD(&dev->napi_list);
5119
5120 /* a dummy interface is started by default */
5121 set_bit(__LINK_STATE_PRESENT, &dev->state);
5122 set_bit(__LINK_STATE_START, &dev->state);
5123
5124 return 0;
5125}
5126EXPORT_SYMBOL_GPL(init_dummy_netdev);
5127
5128
1da177e4
LT
5129/**
5130 * register_netdev - register a network device
5131 * @dev: device to register
5132 *
5133 * Take a completed network device structure and add it to the kernel
5134 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
5135 * chain. 0 is returned on success. A negative errno code is returned
5136 * on a failure to set up the device, or if the name is a duplicate.
5137 *
38b4da38 5138 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
5139 * and expands the device name if you passed a format string to
5140 * alloc_netdev.
5141 */
5142int register_netdev(struct net_device *dev)
5143{
5144 int err;
5145
5146 rtnl_lock();
5147
5148 /*
5149 * If the name is a format string the caller wants us to do a
5150 * name allocation.
5151 */
5152 if (strchr(dev->name, '%')) {
5153 err = dev_alloc_name(dev, dev->name);
5154 if (err < 0)
5155 goto out;
5156 }
4ec93edb 5157
1da177e4
LT
5158 err = register_netdevice(dev);
5159out:
5160 rtnl_unlock();
5161 return err;
5162}
5163EXPORT_SYMBOL(register_netdev);
5164
5165/*
5166 * netdev_wait_allrefs - wait until all references are gone.
5167 *
5168 * This is called when unregistering network devices.
5169 *
5170 * Any protocol or device that holds a reference should register
5171 * for netdevice notification, and cleanup and put back the
5172 * reference if they receive an UNREGISTER event.
5173 * We can get stuck here if buggy protocols don't correctly
4ec93edb 5174 * call dev_put.
1da177e4
LT
5175 */
5176static void netdev_wait_allrefs(struct net_device *dev)
5177{
5178 unsigned long rebroadcast_time, warning_time;
5179
e014debe
ED
5180 linkwatch_forget_dev(dev);
5181
1da177e4
LT
5182 rebroadcast_time = warning_time = jiffies;
5183 while (atomic_read(&dev->refcnt) != 0) {
5184 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 5185 rtnl_lock();
1da177e4
LT
5186
5187 /* Rebroadcast unregister notification */
056925ab 5188 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
a5ee1551 5189 /* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users
395264d5 5190 * should have already handle it the first time */
1da177e4
LT
5191
5192 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
5193 &dev->state)) {
5194 /* We must not have linkwatch events
5195 * pending on unregister. If this
5196 * happens, we simply run the queue
5197 * unscheduled, resulting in a noop
5198 * for this device.
5199 */
5200 linkwatch_run_queue();
5201 }
5202
6756ae4b 5203 __rtnl_unlock();
1da177e4
LT
5204
5205 rebroadcast_time = jiffies;
5206 }
5207
5208 msleep(250);
5209
5210 if (time_after(jiffies, warning_time + 10 * HZ)) {
5211 printk(KERN_EMERG "unregister_netdevice: "
5212 "waiting for %s to become free. Usage "
5213 "count = %d\n",
5214 dev->name, atomic_read(&dev->refcnt));
5215 warning_time = jiffies;
5216 }
5217 }
5218}
5219
5220/* The sequence is:
5221 *
5222 * rtnl_lock();
5223 * ...
5224 * register_netdevice(x1);
5225 * register_netdevice(x2);
5226 * ...
5227 * unregister_netdevice(y1);
5228 * unregister_netdevice(y2);
5229 * ...
5230 * rtnl_unlock();
5231 * free_netdev(y1);
5232 * free_netdev(y2);
5233 *
58ec3b4d 5234 * We are invoked by rtnl_unlock().
1da177e4 5235 * This allows us to deal with problems:
b17a7c17 5236 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
5237 * without deadlocking with linkwatch via keventd.
5238 * 2) Since we run with the RTNL semaphore not held, we can sleep
5239 * safely in order to wait for the netdev refcnt to drop to zero.
58ec3b4d
HX
5240 *
5241 * We must not return until all unregister events added during
5242 * the interval the lock was held have been completed.
1da177e4 5243 */
1da177e4
LT
5244void netdev_run_todo(void)
5245{
626ab0e6 5246 struct list_head list;
1da177e4 5247
1da177e4 5248 /* Snapshot list, allow later requests */
626ab0e6 5249 list_replace_init(&net_todo_list, &list);
58ec3b4d
HX
5250
5251 __rtnl_unlock();
626ab0e6 5252
1da177e4
LT
5253 while (!list_empty(&list)) {
5254 struct net_device *dev
e5e26d75 5255 = list_first_entry(&list, struct net_device, todo_list);
1da177e4
LT
5256 list_del(&dev->todo_list);
5257
b17a7c17
SH
5258 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
5259 printk(KERN_ERR "network todo '%s' but state %d\n",
5260 dev->name, dev->reg_state);
5261 dump_stack();
5262 continue;
5263 }
1da177e4 5264
b17a7c17 5265 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 5266
152102c7 5267 on_each_cpu(flush_backlog, dev, 1);
6e583ce5 5268
b17a7c17 5269 netdev_wait_allrefs(dev);
1da177e4 5270
b17a7c17
SH
5271 /* paranoia */
5272 BUG_ON(atomic_read(&dev->refcnt));
547b792c
IJ
5273 WARN_ON(dev->ip_ptr);
5274 WARN_ON(dev->ip6_ptr);
5275 WARN_ON(dev->dn_ptr);
1da177e4 5276
b17a7c17
SH
5277 if (dev->destructor)
5278 dev->destructor(dev);
9093bbb2
SH
5279
5280 /* Free network device */
5281 kobject_put(&dev->dev.kobj);
1da177e4 5282 }
1da177e4
LT
5283}
5284
d83345ad
ED
5285/**
5286 * dev_txq_stats_fold - fold tx_queues stats
5287 * @dev: device to get statistics from
3cfde79c 5288 * @stats: struct rtnl_link_stats64 to hold results
d83345ad
ED
5289 */
5290void dev_txq_stats_fold(const struct net_device *dev,
3cfde79c 5291 struct rtnl_link_stats64 *stats)
d83345ad 5292{
bd27290a 5293 u64 tx_bytes = 0, tx_packets = 0, tx_dropped = 0;
d83345ad
ED
5294 unsigned int i;
5295 struct netdev_queue *txq;
5296
5297 for (i = 0; i < dev->num_tx_queues; i++) {
5298 txq = netdev_get_tx_queue(dev, i);
bd27290a 5299 spin_lock_bh(&txq->_xmit_lock);
d83345ad
ED
5300 tx_bytes += txq->tx_bytes;
5301 tx_packets += txq->tx_packets;
5302 tx_dropped += txq->tx_dropped;
bd27290a 5303 spin_unlock_bh(&txq->_xmit_lock);
d83345ad
ED
5304 }
5305 if (tx_bytes || tx_packets || tx_dropped) {
5306 stats->tx_bytes = tx_bytes;
5307 stats->tx_packets = tx_packets;
5308 stats->tx_dropped = tx_dropped;
5309 }
5310}
5311EXPORT_SYMBOL(dev_txq_stats_fold);
5312
3cfde79c
BH
5313/* Convert net_device_stats to rtnl_link_stats64. They have the same
5314 * fields in the same order, with only the type differing.
5315 */
5316static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
5317 const struct net_device_stats *netdev_stats)
5318{
5319#if BITS_PER_LONG == 64
5320 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
5321 memcpy(stats64, netdev_stats, sizeof(*stats64));
5322#else
5323 size_t i, n = sizeof(*stats64) / sizeof(u64);
5324 const unsigned long *src = (const unsigned long *)netdev_stats;
5325 u64 *dst = (u64 *)stats64;
5326
5327 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
5328 sizeof(*stats64) / sizeof(u64));
5329 for (i = 0; i < n; i++)
5330 dst[i] = src[i];
5331#endif
5332}
5333
eeda3fd6
SH
5334/**
5335 * dev_get_stats - get network device statistics
5336 * @dev: device to get statistics from
28172739 5337 * @storage: place to store stats
eeda3fd6 5338 *
d7753516
BH
5339 * Get network statistics from device. Return @storage.
5340 * The device driver may provide its own method by setting
5341 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
5342 * otherwise the internal statistics structure is used.
eeda3fd6 5343 */
d7753516
BH
5344struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
5345 struct rtnl_link_stats64 *storage)
7004bf25 5346{
eeda3fd6
SH
5347 const struct net_device_ops *ops = dev->netdev_ops;
5348
28172739
ED
5349 if (ops->ndo_get_stats64) {
5350 memset(storage, 0, sizeof(*storage));
5351 return ops->ndo_get_stats64(dev, storage);
5352 }
5353 if (ops->ndo_get_stats) {
3cfde79c 5354 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
28172739
ED
5355 return storage;
5356 }
3cfde79c
BH
5357 netdev_stats_to_stats64(storage, &dev->stats);
5358 dev_txq_stats_fold(dev, storage);
28172739 5359 return storage;
c45d286e 5360}
eeda3fd6 5361EXPORT_SYMBOL(dev_get_stats);
c45d286e 5362
dc2b4847 5363static void netdev_init_one_queue(struct net_device *dev,
e8a0464c
DM
5364 struct netdev_queue *queue,
5365 void *_unused)
dc2b4847 5366{
dc2b4847
DM
5367 queue->dev = dev;
5368}
5369
bb949fbd
DM
5370static void netdev_init_queues(struct net_device *dev)
5371{
e8a0464c
DM
5372 netdev_init_one_queue(dev, &dev->rx_queue, NULL);
5373 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
c3f26a26 5374 spin_lock_init(&dev->tx_global_lock);
bb949fbd
DM
5375}
5376
1da177e4 5377/**
f25f4e44 5378 * alloc_netdev_mq - allocate network device
1da177e4
LT
5379 * @sizeof_priv: size of private data to allocate space for
5380 * @name: device name format string
5381 * @setup: callback to initialize device
f25f4e44 5382 * @queue_count: the number of subqueues to allocate
1da177e4
LT
5383 *
5384 * Allocates a struct net_device with private data area for driver use
f25f4e44
PWJ
5385 * and performs basic initialization. Also allocates subquue structs
5386 * for each queue on the device at the end of the netdevice.
1da177e4 5387 */
f25f4e44
PWJ
5388struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
5389 void (*setup)(struct net_device *), unsigned int queue_count)
1da177e4 5390{
e8a0464c 5391 struct netdev_queue *tx;
1da177e4 5392 struct net_device *dev;
7943986c 5393 size_t alloc_size;
1ce8e7b5 5394 struct net_device *p;
df334545
ED
5395#ifdef CONFIG_RPS
5396 struct netdev_rx_queue *rx;
0a9627f2 5397 int i;
df334545 5398#endif
1da177e4 5399
b6fe17d6
SH
5400 BUG_ON(strlen(name) >= sizeof(dev->name));
5401
fd2ea0a7 5402 alloc_size = sizeof(struct net_device);
d1643d24
AD
5403 if (sizeof_priv) {
5404 /* ensure 32-byte alignment of private area */
1ce8e7b5 5405 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
d1643d24
AD
5406 alloc_size += sizeof_priv;
5407 }
5408 /* ensure 32-byte alignment of whole construct */
1ce8e7b5 5409 alloc_size += NETDEV_ALIGN - 1;
1da177e4 5410
31380de9 5411 p = kzalloc(alloc_size, GFP_KERNEL);
1da177e4 5412 if (!p) {
b6fe17d6 5413 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
1da177e4
LT
5414 return NULL;
5415 }
1da177e4 5416
7943986c 5417 tx = kcalloc(queue_count, sizeof(struct netdev_queue), GFP_KERNEL);
e8a0464c
DM
5418 if (!tx) {
5419 printk(KERN_ERR "alloc_netdev: Unable to allocate "
5420 "tx qdiscs.\n");
ab9c73cc 5421 goto free_p;
e8a0464c
DM
5422 }
5423
df334545 5424#ifdef CONFIG_RPS
0a9627f2
TH
5425 rx = kcalloc(queue_count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
5426 if (!rx) {
5427 printk(KERN_ERR "alloc_netdev: Unable to allocate "
5428 "rx queues.\n");
5429 goto free_tx;
5430 }
5431
5432 atomic_set(&rx->count, queue_count);
5433
5434 /*
5435 * Set a pointer to first element in the array which holds the
5436 * reference count.
5437 */
5438 for (i = 0; i < queue_count; i++)
5439 rx[i].first = rx;
df334545 5440#endif
0a9627f2 5441
1ce8e7b5 5442 dev = PTR_ALIGN(p, NETDEV_ALIGN);
1da177e4 5443 dev->padded = (char *)dev - (char *)p;
ab9c73cc
JP
5444
5445 if (dev_addr_init(dev))
0a9627f2 5446 goto free_rx;
ab9c73cc 5447
22bedad3 5448 dev_mc_init(dev);
a748ee24 5449 dev_uc_init(dev);
ccffad25 5450
c346dca1 5451 dev_net_set(dev, &init_net);
1da177e4 5452
e8a0464c
DM
5453 dev->_tx = tx;
5454 dev->num_tx_queues = queue_count;
fd2ea0a7 5455 dev->real_num_tx_queues = queue_count;
e8a0464c 5456
df334545 5457#ifdef CONFIG_RPS
0a9627f2
TH
5458 dev->_rx = rx;
5459 dev->num_rx_queues = queue_count;
df334545 5460#endif
0a9627f2 5461
82cc1a7a 5462 dev->gso_max_size = GSO_MAX_SIZE;
1da177e4 5463
bb949fbd
DM
5464 netdev_init_queues(dev);
5465
15682bc4
PWJ
5466 INIT_LIST_HEAD(&dev->ethtool_ntuple_list.list);
5467 dev->ethtool_ntuple_list.count = 0;
d565b0a1 5468 INIT_LIST_HEAD(&dev->napi_list);
9fdce099 5469 INIT_LIST_HEAD(&dev->unreg_list);
e014debe 5470 INIT_LIST_HEAD(&dev->link_watch_list);
93f154b5 5471 dev->priv_flags = IFF_XMIT_DST_RELEASE;
1da177e4
LT
5472 setup(dev);
5473 strcpy(dev->name, name);
5474 return dev;
ab9c73cc 5475
0a9627f2 5476free_rx:
df334545 5477#ifdef CONFIG_RPS
0a9627f2 5478 kfree(rx);
ab9c73cc 5479free_tx:
df334545 5480#endif
ab9c73cc 5481 kfree(tx);
ab9c73cc
JP
5482free_p:
5483 kfree(p);
5484 return NULL;
1da177e4 5485}
f25f4e44 5486EXPORT_SYMBOL(alloc_netdev_mq);
1da177e4
LT
5487
5488/**
5489 * free_netdev - free network device
5490 * @dev: device
5491 *
4ec93edb
YH
5492 * This function does the last stage of destroying an allocated device
5493 * interface. The reference to the device object is released.
1da177e4
LT
5494 * If this is the last reference then it will be freed.
5495 */
5496void free_netdev(struct net_device *dev)
5497{
d565b0a1
HX
5498 struct napi_struct *p, *n;
5499
f3005d7f
DL
5500 release_net(dev_net(dev));
5501
e8a0464c
DM
5502 kfree(dev->_tx);
5503
f001fde5
JP
5504 /* Flush device addresses */
5505 dev_addr_flush(dev);
5506
15682bc4
PWJ
5507 /* Clear ethtool n-tuple list */
5508 ethtool_ntuple_flush(dev);
5509
d565b0a1
HX
5510 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
5511 netif_napi_del(p);
5512
3041a069 5513 /* Compatibility with error handling in drivers */
1da177e4
LT
5514 if (dev->reg_state == NETREG_UNINITIALIZED) {
5515 kfree((char *)dev - dev->padded);
5516 return;
5517 }
5518
5519 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
5520 dev->reg_state = NETREG_RELEASED;
5521
43cb76d9
GKH
5522 /* will free via device release */
5523 put_device(&dev->dev);
1da177e4 5524}
d1b19dff 5525EXPORT_SYMBOL(free_netdev);
4ec93edb 5526
f0db275a
SH
5527/**
5528 * synchronize_net - Synchronize with packet receive processing
5529 *
5530 * Wait for packets currently being received to be done.
5531 * Does not block later packets from starting.
5532 */
4ec93edb 5533void synchronize_net(void)
1da177e4
LT
5534{
5535 might_sleep();
fbd568a3 5536 synchronize_rcu();
1da177e4 5537}
d1b19dff 5538EXPORT_SYMBOL(synchronize_net);
1da177e4
LT
5539
5540/**
44a0873d 5541 * unregister_netdevice_queue - remove device from the kernel
1da177e4 5542 * @dev: device
44a0873d 5543 * @head: list
6ebfbc06 5544 *
1da177e4 5545 * This function shuts down a device interface and removes it
d59b54b1 5546 * from the kernel tables.
44a0873d 5547 * If head not NULL, device is queued to be unregistered later.
1da177e4
LT
5548 *
5549 * Callers must hold the rtnl semaphore. You may want
5550 * unregister_netdev() instead of this.
5551 */
5552
44a0873d 5553void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
1da177e4 5554{
a6620712
HX
5555 ASSERT_RTNL();
5556
44a0873d 5557 if (head) {
9fdce099 5558 list_move_tail(&dev->unreg_list, head);
44a0873d
ED
5559 } else {
5560 rollback_registered(dev);
5561 /* Finish processing unregister after unlock */
5562 net_set_todo(dev);
5563 }
1da177e4 5564}
44a0873d 5565EXPORT_SYMBOL(unregister_netdevice_queue);
1da177e4 5566
9b5e383c
ED
5567/**
5568 * unregister_netdevice_many - unregister many devices
5569 * @head: list of devices
9b5e383c
ED
5570 */
5571void unregister_netdevice_many(struct list_head *head)
5572{
5573 struct net_device *dev;
5574
5575 if (!list_empty(head)) {
5576 rollback_registered_many(head);
5577 list_for_each_entry(dev, head, unreg_list)
5578 net_set_todo(dev);
5579 }
5580}
63c8099d 5581EXPORT_SYMBOL(unregister_netdevice_many);
9b5e383c 5582
1da177e4
LT
5583/**
5584 * unregister_netdev - remove device from the kernel
5585 * @dev: device
5586 *
5587 * This function shuts down a device interface and removes it
d59b54b1 5588 * from the kernel tables.
1da177e4
LT
5589 *
5590 * This is just a wrapper for unregister_netdevice that takes
5591 * the rtnl semaphore. In general you want to use this and not
5592 * unregister_netdevice.
5593 */
5594void unregister_netdev(struct net_device *dev)
5595{
5596 rtnl_lock();
5597 unregister_netdevice(dev);
5598 rtnl_unlock();
5599}
1da177e4
LT
5600EXPORT_SYMBOL(unregister_netdev);
5601
ce286d32
EB
5602/**
5603 * dev_change_net_namespace - move device to different nethost namespace
5604 * @dev: device
5605 * @net: network namespace
5606 * @pat: If not NULL name pattern to try if the current device name
5607 * is already taken in the destination network namespace.
5608 *
5609 * This function shuts down a device interface and moves it
5610 * to a new network namespace. On success 0 is returned, on
5611 * a failure a netagive errno code is returned.
5612 *
5613 * Callers must hold the rtnl semaphore.
5614 */
5615
5616int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
5617{
ce286d32
EB
5618 int err;
5619
5620 ASSERT_RTNL();
5621
5622 /* Don't allow namespace local devices to be moved. */
5623 err = -EINVAL;
5624 if (dev->features & NETIF_F_NETNS_LOCAL)
5625 goto out;
5626
5627 /* Ensure the device has been registrered */
5628 err = -EINVAL;
5629 if (dev->reg_state != NETREG_REGISTERED)
5630 goto out;
5631
5632 /* Get out if there is nothing todo */
5633 err = 0;
878628fb 5634 if (net_eq(dev_net(dev), net))
ce286d32
EB
5635 goto out;
5636
5637 /* Pick the destination device name, and ensure
5638 * we can use it in the destination network namespace.
5639 */
5640 err = -EEXIST;
d9031024 5641 if (__dev_get_by_name(net, dev->name)) {
ce286d32
EB
5642 /* We get here if we can't use the current device name */
5643 if (!pat)
5644 goto out;
8ce6cebc 5645 if (dev_get_valid_name(dev, pat, 1))
ce286d32
EB
5646 goto out;
5647 }
5648
5649 /*
5650 * And now a mini version of register_netdevice unregister_netdevice.
5651 */
5652
5653 /* If device is running close it first. */
9b772652 5654 dev_close(dev);
ce286d32
EB
5655
5656 /* And unlink it from device chain */
5657 err = -ENODEV;
5658 unlist_netdevice(dev);
5659
5660 synchronize_net();
5661
5662 /* Shutdown queueing discipline. */
5663 dev_shutdown(dev);
5664
5665 /* Notify protocols, that we are about to destroy
5666 this device. They should clean all the things.
5667 */
5668 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
a5ee1551 5669 call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
ce286d32
EB
5670
5671 /*
5672 * Flush the unicast and multicast chains
5673 */
a748ee24 5674 dev_uc_flush(dev);
22bedad3 5675 dev_mc_flush(dev);
ce286d32
EB
5676
5677 /* Actually switch the network namespace */
c346dca1 5678 dev_net_set(dev, net);
ce286d32 5679
ce286d32
EB
5680 /* If there is an ifindex conflict assign a new one */
5681 if (__dev_get_by_index(net, dev->ifindex)) {
5682 int iflink = (dev->iflink == dev->ifindex);
5683 dev->ifindex = dev_new_index(net);
5684 if (iflink)
5685 dev->iflink = dev->ifindex;
5686 }
5687
8b41d188 5688 /* Fixup kobjects */
a1b3f594 5689 err = device_rename(&dev->dev, dev->name);
8b41d188 5690 WARN_ON(err);
ce286d32
EB
5691
5692 /* Add the device back in the hashes */
5693 list_netdevice(dev);
5694
5695 /* Notify protocols, that a new device appeared. */
5696 call_netdevice_notifiers(NETDEV_REGISTER, dev);
5697
d90a909e
EB
5698 /*
5699 * Prevent userspace races by waiting until the network
5700 * device is fully setup before sending notifications.
5701 */
5702 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
5703
ce286d32
EB
5704 synchronize_net();
5705 err = 0;
5706out:
5707 return err;
5708}
463d0183 5709EXPORT_SYMBOL_GPL(dev_change_net_namespace);
ce286d32 5710
1da177e4
LT
5711static int dev_cpu_callback(struct notifier_block *nfb,
5712 unsigned long action,
5713 void *ocpu)
5714{
5715 struct sk_buff **list_skb;
1da177e4
LT
5716 struct sk_buff *skb;
5717 unsigned int cpu, oldcpu = (unsigned long)ocpu;
5718 struct softnet_data *sd, *oldsd;
5719
8bb78442 5720 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1da177e4
LT
5721 return NOTIFY_OK;
5722
5723 local_irq_disable();
5724 cpu = smp_processor_id();
5725 sd = &per_cpu(softnet_data, cpu);
5726 oldsd = &per_cpu(softnet_data, oldcpu);
5727
5728 /* Find end of our completion_queue. */
5729 list_skb = &sd->completion_queue;
5730 while (*list_skb)
5731 list_skb = &(*list_skb)->next;
5732 /* Append completion queue from offline CPU. */
5733 *list_skb = oldsd->completion_queue;
5734 oldsd->completion_queue = NULL;
5735
1da177e4 5736 /* Append output queue from offline CPU. */
a9cbd588
CG
5737 if (oldsd->output_queue) {
5738 *sd->output_queue_tailp = oldsd->output_queue;
5739 sd->output_queue_tailp = oldsd->output_queue_tailp;
5740 oldsd->output_queue = NULL;
5741 oldsd->output_queue_tailp = &oldsd->output_queue;
5742 }
1da177e4
LT
5743
5744 raise_softirq_irqoff(NET_TX_SOFTIRQ);
5745 local_irq_enable();
5746
5747 /* Process offline CPU's input_pkt_queue */
76cc8b13 5748 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
1da177e4 5749 netif_rx(skb);
76cc8b13 5750 input_queue_head_incr(oldsd);
fec5e652 5751 }
76cc8b13 5752 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
6e7676c1 5753 netif_rx(skb);
76cc8b13
TH
5754 input_queue_head_incr(oldsd);
5755 }
1da177e4
LT
5756
5757 return NOTIFY_OK;
5758}
1da177e4
LT
5759
5760
7f353bf2 5761/**
b63365a2
HX
5762 * netdev_increment_features - increment feature set by one
5763 * @all: current feature set
5764 * @one: new feature set
5765 * @mask: mask feature set
7f353bf2
HX
5766 *
5767 * Computes a new feature set after adding a device with feature set
b63365a2
HX
5768 * @one to the master device with current feature set @all. Will not
5769 * enable anything that is off in @mask. Returns the new feature set.
7f353bf2 5770 */
b63365a2
HX
5771unsigned long netdev_increment_features(unsigned long all, unsigned long one,
5772 unsigned long mask)
5773{
5774 /* If device needs checksumming, downgrade to it. */
d1b19dff 5775 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
b63365a2
HX
5776 all ^= NETIF_F_NO_CSUM | (one & NETIF_F_ALL_CSUM);
5777 else if (mask & NETIF_F_ALL_CSUM) {
5778 /* If one device supports v4/v6 checksumming, set for all. */
5779 if (one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM) &&
5780 !(all & NETIF_F_GEN_CSUM)) {
5781 all &= ~NETIF_F_ALL_CSUM;
5782 all |= one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
5783 }
e2a6b852 5784
b63365a2
HX
5785 /* If one device supports hw checksumming, set for all. */
5786 if (one & NETIF_F_GEN_CSUM && !(all & NETIF_F_GEN_CSUM)) {
5787 all &= ~NETIF_F_ALL_CSUM;
5788 all |= NETIF_F_HW_CSUM;
5789 }
5790 }
7f353bf2 5791
b63365a2 5792 one |= NETIF_F_ALL_CSUM;
7f353bf2 5793
b63365a2 5794 one |= all & NETIF_F_ONE_FOR_ALL;
d9f5950f 5795 all &= one | NETIF_F_LLTX | NETIF_F_GSO | NETIF_F_UFO;
b63365a2 5796 all |= one & mask & NETIF_F_ONE_FOR_ALL;
7f353bf2
HX
5797
5798 return all;
5799}
b63365a2 5800EXPORT_SYMBOL(netdev_increment_features);
7f353bf2 5801
30d97d35
PE
5802static struct hlist_head *netdev_create_hash(void)
5803{
5804 int i;
5805 struct hlist_head *hash;
5806
5807 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
5808 if (hash != NULL)
5809 for (i = 0; i < NETDEV_HASHENTRIES; i++)
5810 INIT_HLIST_HEAD(&hash[i]);
5811
5812 return hash;
5813}
5814
881d966b 5815/* Initialize per network namespace state */
4665079c 5816static int __net_init netdev_init(struct net *net)
881d966b 5817{
881d966b 5818 INIT_LIST_HEAD(&net->dev_base_head);
881d966b 5819
30d97d35
PE
5820 net->dev_name_head = netdev_create_hash();
5821 if (net->dev_name_head == NULL)
5822 goto err_name;
881d966b 5823
30d97d35
PE
5824 net->dev_index_head = netdev_create_hash();
5825 if (net->dev_index_head == NULL)
5826 goto err_idx;
881d966b
EB
5827
5828 return 0;
30d97d35
PE
5829
5830err_idx:
5831 kfree(net->dev_name_head);
5832err_name:
5833 return -ENOMEM;
881d966b
EB
5834}
5835
f0db275a
SH
5836/**
5837 * netdev_drivername - network driver for the device
5838 * @dev: network device
5839 * @buffer: buffer for resulting name
5840 * @len: size of buffer
5841 *
5842 * Determine network driver for device.
5843 */
cf04a4c7 5844char *netdev_drivername(const struct net_device *dev, char *buffer, int len)
6579e57b 5845{
cf04a4c7
SH
5846 const struct device_driver *driver;
5847 const struct device *parent;
6579e57b
AV
5848
5849 if (len <= 0 || !buffer)
5850 return buffer;
5851 buffer[0] = 0;
5852
5853 parent = dev->dev.parent;
5854
5855 if (!parent)
5856 return buffer;
5857
5858 driver = parent->driver;
5859 if (driver && driver->name)
5860 strlcpy(buffer, driver->name, len);
5861 return buffer;
5862}
5863
256df2f3
JP
5864static int __netdev_printk(const char *level, const struct net_device *dev,
5865 struct va_format *vaf)
5866{
5867 int r;
5868
5869 if (dev && dev->dev.parent)
5870 r = dev_printk(level, dev->dev.parent, "%s: %pV",
5871 netdev_name(dev), vaf);
5872 else if (dev)
5873 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
5874 else
5875 r = printk("%s(NULL net_device): %pV", level, vaf);
5876
5877 return r;
5878}
5879
5880int netdev_printk(const char *level, const struct net_device *dev,
5881 const char *format, ...)
5882{
5883 struct va_format vaf;
5884 va_list args;
5885 int r;
5886
5887 va_start(args, format);
5888
5889 vaf.fmt = format;
5890 vaf.va = &args;
5891
5892 r = __netdev_printk(level, dev, &vaf);
5893 va_end(args);
5894
5895 return r;
5896}
5897EXPORT_SYMBOL(netdev_printk);
5898
5899#define define_netdev_printk_level(func, level) \
5900int func(const struct net_device *dev, const char *fmt, ...) \
5901{ \
5902 int r; \
5903 struct va_format vaf; \
5904 va_list args; \
5905 \
5906 va_start(args, fmt); \
5907 \
5908 vaf.fmt = fmt; \
5909 vaf.va = &args; \
5910 \
5911 r = __netdev_printk(level, dev, &vaf); \
5912 va_end(args); \
5913 \
5914 return r; \
5915} \
5916EXPORT_SYMBOL(func);
5917
5918define_netdev_printk_level(netdev_emerg, KERN_EMERG);
5919define_netdev_printk_level(netdev_alert, KERN_ALERT);
5920define_netdev_printk_level(netdev_crit, KERN_CRIT);
5921define_netdev_printk_level(netdev_err, KERN_ERR);
5922define_netdev_printk_level(netdev_warn, KERN_WARNING);
5923define_netdev_printk_level(netdev_notice, KERN_NOTICE);
5924define_netdev_printk_level(netdev_info, KERN_INFO);
5925
4665079c 5926static void __net_exit netdev_exit(struct net *net)
881d966b
EB
5927{
5928 kfree(net->dev_name_head);
5929 kfree(net->dev_index_head);
5930}
5931
022cbae6 5932static struct pernet_operations __net_initdata netdev_net_ops = {
881d966b
EB
5933 .init = netdev_init,
5934 .exit = netdev_exit,
5935};
5936
4665079c 5937static void __net_exit default_device_exit(struct net *net)
ce286d32 5938{
e008b5fc 5939 struct net_device *dev, *aux;
ce286d32 5940 /*
e008b5fc 5941 * Push all migratable network devices back to the
ce286d32
EB
5942 * initial network namespace
5943 */
5944 rtnl_lock();
e008b5fc 5945 for_each_netdev_safe(net, dev, aux) {
ce286d32 5946 int err;
aca51397 5947 char fb_name[IFNAMSIZ];
ce286d32
EB
5948
5949 /* Ignore unmoveable devices (i.e. loopback) */
5950 if (dev->features & NETIF_F_NETNS_LOCAL)
5951 continue;
5952
e008b5fc
EB
5953 /* Leave virtual devices for the generic cleanup */
5954 if (dev->rtnl_link_ops)
5955 continue;
d0c082ce 5956
ce286d32 5957 /* Push remaing network devices to init_net */
aca51397
PE
5958 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
5959 err = dev_change_net_namespace(dev, &init_net, fb_name);
ce286d32 5960 if (err) {
aca51397 5961 printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
ce286d32 5962 __func__, dev->name, err);
aca51397 5963 BUG();
ce286d32
EB
5964 }
5965 }
5966 rtnl_unlock();
5967}
5968
04dc7f6b
EB
5969static void __net_exit default_device_exit_batch(struct list_head *net_list)
5970{
5971 /* At exit all network devices most be removed from a network
5972 * namespace. Do this in the reverse order of registeration.
5973 * Do this across as many network namespaces as possible to
5974 * improve batching efficiency.
5975 */
5976 struct net_device *dev;
5977 struct net *net;
5978 LIST_HEAD(dev_kill_list);
5979
5980 rtnl_lock();
5981 list_for_each_entry(net, net_list, exit_list) {
5982 for_each_netdev_reverse(net, dev) {
5983 if (dev->rtnl_link_ops)
5984 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
5985 else
5986 unregister_netdevice_queue(dev, &dev_kill_list);
5987 }
5988 }
5989 unregister_netdevice_many(&dev_kill_list);
5990 rtnl_unlock();
5991}
5992
022cbae6 5993static struct pernet_operations __net_initdata default_device_ops = {
ce286d32 5994 .exit = default_device_exit,
04dc7f6b 5995 .exit_batch = default_device_exit_batch,
ce286d32
EB
5996};
5997
1da177e4
LT
5998/*
5999 * Initialize the DEV module. At boot time this walks the device list and
6000 * unhooks any devices that fail to initialise (normally hardware not
6001 * present) and leaves us with a valid list of present and active devices.
6002 *
6003 */
6004
6005/*
6006 * This is called single threaded during boot, so no need
6007 * to take the rtnl semaphore.
6008 */
6009static int __init net_dev_init(void)
6010{
6011 int i, rc = -ENOMEM;
6012
6013 BUG_ON(!dev_boot_phase);
6014
1da177e4
LT
6015 if (dev_proc_init())
6016 goto out;
6017
8b41d188 6018 if (netdev_kobject_init())
1da177e4
LT
6019 goto out;
6020
6021 INIT_LIST_HEAD(&ptype_all);
82d8a867 6022 for (i = 0; i < PTYPE_HASH_SIZE; i++)
1da177e4
LT
6023 INIT_LIST_HEAD(&ptype_base[i]);
6024
881d966b
EB
6025 if (register_pernet_subsys(&netdev_net_ops))
6026 goto out;
1da177e4
LT
6027
6028 /*
6029 * Initialise the packet receive queues.
6030 */
6031
6f912042 6032 for_each_possible_cpu(i) {
e36fa2f7 6033 struct softnet_data *sd = &per_cpu(softnet_data, i);
1da177e4 6034
dee42870 6035 memset(sd, 0, sizeof(*sd));
e36fa2f7 6036 skb_queue_head_init(&sd->input_pkt_queue);
6e7676c1 6037 skb_queue_head_init(&sd->process_queue);
e36fa2f7
ED
6038 sd->completion_queue = NULL;
6039 INIT_LIST_HEAD(&sd->poll_list);
a9cbd588
CG
6040 sd->output_queue = NULL;
6041 sd->output_queue_tailp = &sd->output_queue;
df334545 6042#ifdef CONFIG_RPS
e36fa2f7
ED
6043 sd->csd.func = rps_trigger_softirq;
6044 sd->csd.info = sd;
6045 sd->csd.flags = 0;
6046 sd->cpu = i;
1e94d72f 6047#endif
0a9627f2 6048
e36fa2f7
ED
6049 sd->backlog.poll = process_backlog;
6050 sd->backlog.weight = weight_p;
6051 sd->backlog.gro_list = NULL;
6052 sd->backlog.gro_count = 0;
1da177e4
LT
6053 }
6054
1da177e4
LT
6055 dev_boot_phase = 0;
6056
505d4f73
EB
6057 /* The loopback device is special if any other network devices
6058 * is present in a network namespace the loopback device must
6059 * be present. Since we now dynamically allocate and free the
6060 * loopback device ensure this invariant is maintained by
6061 * keeping the loopback device as the first device on the
6062 * list of network devices. Ensuring the loopback devices
6063 * is the first device that appears and the last network device
6064 * that disappears.
6065 */
6066 if (register_pernet_device(&loopback_net_ops))
6067 goto out;
6068
6069 if (register_pernet_device(&default_device_ops))
6070 goto out;
6071
962cf36c
CM
6072 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
6073 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
1da177e4
LT
6074
6075 hotcpu_notifier(dev_cpu_callback, 0);
6076 dst_init();
6077 dev_mcast_init();
6078 rc = 0;
6079out:
6080 return rc;
6081}
6082
6083subsys_initcall(net_dev_init);
6084
e88721f8
KK
6085static int __init initialize_hashrnd(void)
6086{
0a9627f2 6087 get_random_bytes(&hashrnd, sizeof(hashrnd));
e88721f8
KK
6088 return 0;
6089}
6090
6091late_initcall_sync(initialize_hashrnd);
6092