2 * NET3 Protocol independent device support routines.
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.
9 * Derived from the non IP parts of dev.c 1.0.19
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
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>
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
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
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
51 * Rudi Cilibrasi : Pass the right thing to
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
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
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
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/bitops.h>
78 #include <linux/capability.h>
79 #include <linux/cpu.h>
80 #include <linux/types.h>
81 #include <linux/kernel.h>
82 #include <linux/hash.h>
83 #include <linux/slab.h>
84 #include <linux/sched.h>
85 #include <linux/mutex.h>
86 #include <linux/string.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>
95 #include <linux/ethtool.h>
96 #include <linux/notifier.h>
97 #include <linux/skbuff.h>
98 #include <net/net_namespace.h>
100 #include <linux/rtnetlink.h>
101 #include <linux/proc_fs.h>
102 #include <linux/seq_file.h>
103 #include <linux/stat.h>
105 #include <net/pkt_sched.h>
106 #include <net/checksum.h>
107 #include <net/xfrm.h>
108 #include <linux/highmem.h>
109 #include <linux/init.h>
110 #include <linux/kmod.h>
111 #include <linux/module.h>
112 #include <linux/netpoll.h>
113 #include <linux/rcupdate.h>
114 #include <linux/delay.h>
115 #include <net/wext.h>
116 #include <net/iw_handler.h>
117 #include <asm/current.h>
118 #include <linux/audit.h>
119 #include <linux/dmaengine.h>
120 #include <linux/err.h>
121 #include <linux/ctype.h>
122 #include <linux/if_arp.h>
123 #include <linux/if_vlan.h>
124 #include <linux/ip.h>
126 #include <linux/ipv6.h>
127 #include <linux/in.h>
128 #include <linux/jhash.h>
129 #include <linux/random.h>
130 #include <trace/events/napi.h>
131 #include <trace/events/net.h>
132 #include <trace/events/skb.h>
133 #include <linux/pci.h>
134 #include <linux/inetdevice.h>
135 #include <linux/cpu_rmap.h>
137 #include "net-sysfs.h"
139 /* Instead of increasing this, you should create a hash table. */
140 #define MAX_GRO_SKBS 8
142 /* This should be increased if a protocol with a bigger head is added. */
143 #define GRO_MAX_HEAD (MAX_HEADER + 128)
146 * The list of packet types we will receive (as opposed to discard)
147 * and the routines to invoke.
149 * Why 16. Because with 16 the only overlap we get on a hash of the
150 * low nibble of the protocol value is RARP/SNAP/X.25.
152 * NOTE: That is no longer true with the addition of VLAN tags. Not
153 * sure which should go first, but I bet it won't make much
154 * difference if we are running VLANs. The good news is that
155 * this protocol won't be in the list unless compiled in, so
156 * the average user (w/out VLANs) will not be adversely affected.
173 #define PTYPE_HASH_SIZE (16)
174 #define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
176 static DEFINE_SPINLOCK(ptype_lock);
177 static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
178 static struct list_head ptype_all __read_mostly; /* Taps */
181 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
184 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
186 * Writers must hold the rtnl semaphore while they loop through the
187 * dev_base_head list, and hold dev_base_lock for writing when they do the
188 * actual updates. This allows pure readers to access the list even
189 * while a writer is preparing to update it.
191 * To put it another way, dev_base_lock is held for writing only to
192 * protect against pure readers; the rtnl semaphore provides the
193 * protection against other writers.
195 * See, for example usages, register_netdevice() and
196 * unregister_netdevice(), which must be called with the rtnl
199 DEFINE_RWLOCK(dev_base_lock);
200 EXPORT_SYMBOL(dev_base_lock);
202 static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
204 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
205 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
208 static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
210 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
213 static inline void rps_lock(struct softnet_data *sd)
216 spin_lock(&sd->input_pkt_queue.lock);
220 static inline void rps_unlock(struct softnet_data *sd)
223 spin_unlock(&sd->input_pkt_queue.lock);
227 /* Device list insertion */
228 static int list_netdevice(struct net_device *dev)
230 struct net *net = dev_net(dev);
234 write_lock_bh(&dev_base_lock);
235 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
236 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
237 hlist_add_head_rcu(&dev->index_hlist,
238 dev_index_hash(net, dev->ifindex));
239 write_unlock_bh(&dev_base_lock);
243 /* Device list removal
244 * caller must respect a RCU grace period before freeing/reusing dev
246 static void unlist_netdevice(struct net_device *dev)
250 /* Unlink dev from the device chain */
251 write_lock_bh(&dev_base_lock);
252 list_del_rcu(&dev->dev_list);
253 hlist_del_rcu(&dev->name_hlist);
254 hlist_del_rcu(&dev->index_hlist);
255 write_unlock_bh(&dev_base_lock);
262 static RAW_NOTIFIER_HEAD(netdev_chain);
265 * Device drivers call our routines to queue packets here. We empty the
266 * queue in the local softnet handler.
269 DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
270 EXPORT_PER_CPU_SYMBOL(softnet_data);
272 #ifdef CONFIG_LOCKDEP
274 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
275 * according to dev->type
277 static const unsigned short netdev_lock_type[] =
278 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
279 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
280 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
281 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
282 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
283 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
284 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
285 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
286 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
287 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
288 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
289 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
290 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
291 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET,
292 ARPHRD_PHONET_PIPE, ARPHRD_IEEE802154,
293 ARPHRD_VOID, ARPHRD_NONE};
295 static const char *const netdev_lock_name[] =
296 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
297 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
298 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
299 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
300 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
301 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
302 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
303 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
304 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
305 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
306 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
307 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
308 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
309 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET",
310 "_xmit_PHONET_PIPE", "_xmit_IEEE802154",
311 "_xmit_VOID", "_xmit_NONE"};
313 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
314 static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
316 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
320 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
321 if (netdev_lock_type[i] == dev_type)
323 /* the last key is used by default */
324 return ARRAY_SIZE(netdev_lock_type) - 1;
327 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
328 unsigned short dev_type)
332 i = netdev_lock_pos(dev_type);
333 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
334 netdev_lock_name[i]);
337 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
341 i = netdev_lock_pos(dev->type);
342 lockdep_set_class_and_name(&dev->addr_list_lock,
343 &netdev_addr_lock_key[i],
344 netdev_lock_name[i]);
347 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
348 unsigned short dev_type)
351 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
356 /*******************************************************************************
358 Protocol management and registration routines
360 *******************************************************************************/
363 * Add a protocol ID to the list. Now that the input handler is
364 * smarter we can dispense with all the messy stuff that used to be
367 * BEWARE!!! Protocol handlers, mangling input packets,
368 * MUST BE last in hash buckets and checking protocol handlers
369 * MUST start from promiscuous ptype_all chain in net_bh.
370 * It is true now, do not change it.
371 * Explanation follows: if protocol handler, mangling packet, will
372 * be the first on list, it is not able to sense, that packet
373 * is cloned and should be copied-on-write, so that it will
374 * change it and subsequent readers will get broken packet.
378 static inline struct list_head *ptype_head(const struct packet_type *pt)
380 if (pt->type == htons(ETH_P_ALL))
383 return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
387 * dev_add_pack - add packet handler
388 * @pt: packet type declaration
390 * Add a protocol handler to the networking stack. The passed &packet_type
391 * is linked into kernel lists and may not be freed until it has been
392 * removed from the kernel lists.
394 * This call does not sleep therefore it can not
395 * guarantee all CPU's that are in middle of receiving packets
396 * will see the new packet type (until the next received packet).
399 void dev_add_pack(struct packet_type *pt)
401 struct list_head *head = ptype_head(pt);
403 spin_lock(&ptype_lock);
404 list_add_rcu(&pt->list, head);
405 spin_unlock(&ptype_lock);
407 EXPORT_SYMBOL(dev_add_pack);
410 * __dev_remove_pack - remove packet handler
411 * @pt: packet type declaration
413 * Remove a protocol handler that was previously added to the kernel
414 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
415 * from the kernel lists and can be freed or reused once this function
418 * The packet type might still be in use by receivers
419 * and must not be freed until after all the CPU's have gone
420 * through a quiescent state.
422 void __dev_remove_pack(struct packet_type *pt)
424 struct list_head *head = ptype_head(pt);
425 struct packet_type *pt1;
427 spin_lock(&ptype_lock);
429 list_for_each_entry(pt1, head, list) {
431 list_del_rcu(&pt->list);
436 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
438 spin_unlock(&ptype_lock);
440 EXPORT_SYMBOL(__dev_remove_pack);
443 * dev_remove_pack - remove packet handler
444 * @pt: packet type declaration
446 * Remove a protocol handler that was previously added to the kernel
447 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
448 * from the kernel lists and can be freed or reused once this function
451 * This call sleeps to guarantee that no CPU is looking at the packet
454 void dev_remove_pack(struct packet_type *pt)
456 __dev_remove_pack(pt);
460 EXPORT_SYMBOL(dev_remove_pack);
462 /******************************************************************************
464 Device Boot-time Settings Routines
466 *******************************************************************************/
468 /* Boot time configuration table */
469 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
472 * netdev_boot_setup_add - add new setup entry
473 * @name: name of the device
474 * @map: configured settings for the device
476 * Adds new setup entry to the dev_boot_setup list. The function
477 * returns 0 on error and 1 on success. This is a generic routine to
480 static int netdev_boot_setup_add(char *name, struct ifmap *map)
482 struct netdev_boot_setup *s;
486 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
487 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
488 memset(s[i].name, 0, sizeof(s[i].name));
489 strlcpy(s[i].name, name, IFNAMSIZ);
490 memcpy(&s[i].map, map, sizeof(s[i].map));
495 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
499 * netdev_boot_setup_check - check boot time settings
500 * @dev: the netdevice
502 * Check boot time settings for the device.
503 * The found settings are set for the device to be used
504 * later in the device probing.
505 * Returns 0 if no settings found, 1 if they are.
507 int netdev_boot_setup_check(struct net_device *dev)
509 struct netdev_boot_setup *s = dev_boot_setup;
512 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
513 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
514 !strcmp(dev->name, s[i].name)) {
515 dev->irq = s[i].map.irq;
516 dev->base_addr = s[i].map.base_addr;
517 dev->mem_start = s[i].map.mem_start;
518 dev->mem_end = s[i].map.mem_end;
524 EXPORT_SYMBOL(netdev_boot_setup_check);
528 * netdev_boot_base - get address from boot time settings
529 * @prefix: prefix for network device
530 * @unit: id for network device
532 * Check boot time settings for the base address of device.
533 * The found settings are set for the device to be used
534 * later in the device probing.
535 * Returns 0 if no settings found.
537 unsigned long netdev_boot_base(const char *prefix, int unit)
539 const struct netdev_boot_setup *s = dev_boot_setup;
543 sprintf(name, "%s%d", prefix, unit);
546 * If device already registered then return base of 1
547 * to indicate not to probe for this interface
549 if (__dev_get_by_name(&init_net, name))
552 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
553 if (!strcmp(name, s[i].name))
554 return s[i].map.base_addr;
559 * Saves at boot time configured settings for any netdevice.
561 int __init netdev_boot_setup(char *str)
566 str = get_options(str, ARRAY_SIZE(ints), ints);
571 memset(&map, 0, sizeof(map));
575 map.base_addr = ints[2];
577 map.mem_start = ints[3];
579 map.mem_end = ints[4];
581 /* Add new entry to the list */
582 return netdev_boot_setup_add(str, &map);
585 __setup("netdev=", netdev_boot_setup);
587 /*******************************************************************************
589 Device Interface Subroutines
591 *******************************************************************************/
594 * __dev_get_by_name - find a device by its name
595 * @net: the applicable net namespace
596 * @name: name to find
598 * Find an interface by name. Must be called under RTNL semaphore
599 * or @dev_base_lock. If the name is found a pointer to the device
600 * is returned. If the name is not found then %NULL is returned. The
601 * reference counters are not incremented so the caller must be
602 * careful with locks.
605 struct net_device *__dev_get_by_name(struct net *net, const char *name)
607 struct hlist_node *p;
608 struct net_device *dev;
609 struct hlist_head *head = dev_name_hash(net, name);
611 hlist_for_each_entry(dev, p, head, name_hlist)
612 if (!strncmp(dev->name, name, IFNAMSIZ))
617 EXPORT_SYMBOL(__dev_get_by_name);
620 * dev_get_by_name_rcu - find a device by its name
621 * @net: the applicable net namespace
622 * @name: name to find
624 * Find an interface by name.
625 * If the name is found a pointer to the device is returned.
626 * If the name is not found then %NULL is returned.
627 * The reference counters are not incremented so the caller must be
628 * careful with locks. The caller must hold RCU lock.
631 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
633 struct hlist_node *p;
634 struct net_device *dev;
635 struct hlist_head *head = dev_name_hash(net, name);
637 hlist_for_each_entry_rcu(dev, p, head, name_hlist)
638 if (!strncmp(dev->name, name, IFNAMSIZ))
643 EXPORT_SYMBOL(dev_get_by_name_rcu);
646 * dev_get_by_name - find a device by its name
647 * @net: the applicable net namespace
648 * @name: name to find
650 * Find an interface by name. This can be called from any
651 * context and does its own locking. The returned handle has
652 * the usage count incremented and the caller must use dev_put() to
653 * release it when it is no longer needed. %NULL is returned if no
654 * matching device is found.
657 struct net_device *dev_get_by_name(struct net *net, const char *name)
659 struct net_device *dev;
662 dev = dev_get_by_name_rcu(net, name);
668 EXPORT_SYMBOL(dev_get_by_name);
671 * __dev_get_by_index - find a device by its ifindex
672 * @net: the applicable net namespace
673 * @ifindex: index of device
675 * Search for an interface by index. Returns %NULL if the device
676 * is not found or a pointer to the device. The device has not
677 * had its reference counter increased so the caller must be careful
678 * about locking. The caller must hold either the RTNL semaphore
682 struct net_device *__dev_get_by_index(struct net *net, int ifindex)
684 struct hlist_node *p;
685 struct net_device *dev;
686 struct hlist_head *head = dev_index_hash(net, ifindex);
688 hlist_for_each_entry(dev, p, head, index_hlist)
689 if (dev->ifindex == ifindex)
694 EXPORT_SYMBOL(__dev_get_by_index);
697 * dev_get_by_index_rcu - find a device by its ifindex
698 * @net: the applicable net namespace
699 * @ifindex: index of device
701 * Search for an interface by index. Returns %NULL if the device
702 * is not found or a pointer to the device. The device has not
703 * had its reference counter increased so the caller must be careful
704 * about locking. The caller must hold RCU lock.
707 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
709 struct hlist_node *p;
710 struct net_device *dev;
711 struct hlist_head *head = dev_index_hash(net, ifindex);
713 hlist_for_each_entry_rcu(dev, p, head, index_hlist)
714 if (dev->ifindex == ifindex)
719 EXPORT_SYMBOL(dev_get_by_index_rcu);
723 * dev_get_by_index - find a device by its ifindex
724 * @net: the applicable net namespace
725 * @ifindex: index of device
727 * Search for an interface by index. Returns NULL if the device
728 * is not found or a pointer to the device. The device returned has
729 * had a reference added and the pointer is safe until the user calls
730 * dev_put to indicate they have finished with it.
733 struct net_device *dev_get_by_index(struct net *net, int ifindex)
735 struct net_device *dev;
738 dev = dev_get_by_index_rcu(net, ifindex);
744 EXPORT_SYMBOL(dev_get_by_index);
747 * dev_getbyhwaddr_rcu - find a device by its hardware address
748 * @net: the applicable net namespace
749 * @type: media type of device
750 * @ha: hardware address
752 * Search for an interface by MAC address. Returns NULL if the device
753 * is not found or a pointer to the device.
754 * The caller must hold RCU or RTNL.
755 * The returned device has not had its ref count increased
756 * and the caller must therefore be careful about locking
760 struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
763 struct net_device *dev;
765 for_each_netdev_rcu(net, dev)
766 if (dev->type == type &&
767 !memcmp(dev->dev_addr, ha, dev->addr_len))
772 EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
774 struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
776 struct net_device *dev;
779 for_each_netdev(net, dev)
780 if (dev->type == type)
785 EXPORT_SYMBOL(__dev_getfirstbyhwtype);
787 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
789 struct net_device *dev, *ret = NULL;
792 for_each_netdev_rcu(net, dev)
793 if (dev->type == type) {
801 EXPORT_SYMBOL(dev_getfirstbyhwtype);
804 * dev_get_by_flags_rcu - find any device with given flags
805 * @net: the applicable net namespace
806 * @if_flags: IFF_* values
807 * @mask: bitmask of bits in if_flags to check
809 * Search for any interface with the given flags. Returns NULL if a device
810 * is not found or a pointer to the device. Must be called inside
811 * rcu_read_lock(), and result refcount is unchanged.
814 struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
817 struct net_device *dev, *ret;
820 for_each_netdev_rcu(net, dev) {
821 if (((dev->flags ^ if_flags) & mask) == 0) {
828 EXPORT_SYMBOL(dev_get_by_flags_rcu);
831 * dev_valid_name - check if name is okay for network device
834 * Network device names need to be valid file names to
835 * to allow sysfs to work. We also disallow any kind of
838 int dev_valid_name(const char *name)
842 if (strlen(name) >= IFNAMSIZ)
844 if (!strcmp(name, ".") || !strcmp(name, ".."))
848 if (*name == '/' || isspace(*name))
854 EXPORT_SYMBOL(dev_valid_name);
857 * __dev_alloc_name - allocate a name for a device
858 * @net: network namespace to allocate the device name in
859 * @name: name format string
860 * @buf: scratch buffer and result name string
862 * Passed a format string - eg "lt%d" it will try and find a suitable
863 * id. It scans list of devices to build up a free map, then chooses
864 * the first empty slot. The caller must hold the dev_base or rtnl lock
865 * while allocating the name and adding the device in order to avoid
867 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
868 * Returns the number of the unit assigned or a negative errno code.
871 static int __dev_alloc_name(struct net *net, const char *name, char *buf)
875 const int max_netdevices = 8*PAGE_SIZE;
876 unsigned long *inuse;
877 struct net_device *d;
879 p = strnchr(name, IFNAMSIZ-1, '%');
882 * Verify the string as this thing may have come from
883 * the user. There must be either one "%d" and no other "%"
886 if (p[1] != 'd' || strchr(p + 2, '%'))
889 /* Use one page as a bit array of possible slots */
890 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
894 for_each_netdev(net, d) {
895 if (!sscanf(d->name, name, &i))
897 if (i < 0 || i >= max_netdevices)
900 /* avoid cases where sscanf is not exact inverse of printf */
901 snprintf(buf, IFNAMSIZ, name, i);
902 if (!strncmp(buf, d->name, IFNAMSIZ))
906 i = find_first_zero_bit(inuse, max_netdevices);
907 free_page((unsigned long) inuse);
911 snprintf(buf, IFNAMSIZ, name, i);
912 if (!__dev_get_by_name(net, buf))
915 /* It is possible to run out of possible slots
916 * when the name is long and there isn't enough space left
917 * for the digits, or if all bits are used.
923 * dev_alloc_name - allocate a name for a device
925 * @name: name format string
927 * Passed a format string - eg "lt%d" it will try and find a suitable
928 * id. It scans list of devices to build up a free map, then chooses
929 * the first empty slot. The caller must hold the dev_base or rtnl lock
930 * while allocating the name and adding the device in order to avoid
932 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
933 * Returns the number of the unit assigned or a negative errno code.
936 int dev_alloc_name(struct net_device *dev, const char *name)
942 BUG_ON(!dev_net(dev));
944 ret = __dev_alloc_name(net, name, buf);
946 strlcpy(dev->name, buf, IFNAMSIZ);
949 EXPORT_SYMBOL(dev_alloc_name);
951 static int dev_get_valid_name(struct net_device *dev, const char *name, bool fmt)
955 BUG_ON(!dev_net(dev));
958 if (!dev_valid_name(name))
961 if (fmt && strchr(name, '%'))
962 return dev_alloc_name(dev, name);
963 else if (__dev_get_by_name(net, name))
965 else if (dev->name != name)
966 strlcpy(dev->name, name, IFNAMSIZ);
972 * dev_change_name - change name of a device
974 * @newname: name (or format string) must be at least IFNAMSIZ
976 * Change name of a device, can pass format strings "eth%d".
979 int dev_change_name(struct net_device *dev, const char *newname)
981 char oldname[IFNAMSIZ];
987 BUG_ON(!dev_net(dev));
990 if (dev->flags & IFF_UP)
993 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
996 memcpy(oldname, dev->name, IFNAMSIZ);
998 err = dev_get_valid_name(dev, newname, 1);
1003 ret = device_rename(&dev->dev, dev->name);
1005 memcpy(dev->name, oldname, IFNAMSIZ);
1009 write_lock_bh(&dev_base_lock);
1010 hlist_del(&dev->name_hlist);
1011 write_unlock_bh(&dev_base_lock);
1015 write_lock_bh(&dev_base_lock);
1016 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
1017 write_unlock_bh(&dev_base_lock);
1019 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1020 ret = notifier_to_errno(ret);
1023 /* err >= 0 after dev_alloc_name() or stores the first errno */
1026 memcpy(dev->name, oldname, IFNAMSIZ);
1030 "%s: name change rollback failed: %d.\n",
1039 * dev_set_alias - change ifalias of a device
1041 * @alias: name up to IFALIASZ
1042 * @len: limit of bytes to copy from info
1044 * Set ifalias for a device,
1046 int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1050 if (len >= IFALIASZ)
1055 kfree(dev->ifalias);
1056 dev->ifalias = NULL;
1061 dev->ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1065 strlcpy(dev->ifalias, alias, len+1);
1071 * netdev_features_change - device changes features
1072 * @dev: device to cause notification
1074 * Called to indicate a device has changed features.
1076 void netdev_features_change(struct net_device *dev)
1078 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1080 EXPORT_SYMBOL(netdev_features_change);
1083 * netdev_state_change - device changes state
1084 * @dev: device to cause notification
1086 * Called to indicate a device has changed state. This function calls
1087 * the notifier chains for netdev_chain and sends a NEWLINK message
1088 * to the routing socket.
1090 void netdev_state_change(struct net_device *dev)
1092 if (dev->flags & IFF_UP) {
1093 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1094 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1097 EXPORT_SYMBOL(netdev_state_change);
1099 int netdev_bonding_change(struct net_device *dev, unsigned long event)
1101 return call_netdevice_notifiers(event, dev);
1103 EXPORT_SYMBOL(netdev_bonding_change);
1106 * dev_load - load a network module
1107 * @net: the applicable net namespace
1108 * @name: name of interface
1110 * If a network interface is not present and the process has suitable
1111 * privileges this function loads the module. If module loading is not
1112 * available in this kernel then it becomes a nop.
1115 void dev_load(struct net *net, const char *name)
1117 struct net_device *dev;
1121 dev = dev_get_by_name_rcu(net, name);
1125 if (no_module && capable(CAP_NET_ADMIN))
1126 no_module = request_module("netdev-%s", name);
1127 if (no_module && capable(CAP_SYS_MODULE)) {
1128 if (!request_module("%s", name))
1129 pr_err("Loading kernel module for a network device "
1130 "with CAP_SYS_MODULE (deprecated). Use CAP_NET_ADMIN and alias netdev-%s "
1134 EXPORT_SYMBOL(dev_load);
1136 static int __dev_open(struct net_device *dev)
1138 const struct net_device_ops *ops = dev->netdev_ops;
1143 if (!netif_device_present(dev))
1146 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1147 ret = notifier_to_errno(ret);
1151 set_bit(__LINK_STATE_START, &dev->state);
1153 if (ops->ndo_validate_addr)
1154 ret = ops->ndo_validate_addr(dev);
1156 if (!ret && ops->ndo_open)
1157 ret = ops->ndo_open(dev);
1160 clear_bit(__LINK_STATE_START, &dev->state);
1162 dev->flags |= IFF_UP;
1163 net_dmaengine_get();
1164 dev_set_rx_mode(dev);
1172 * dev_open - prepare an interface for use.
1173 * @dev: device to open
1175 * Takes a device from down to up state. The device's private open
1176 * function is invoked and then the multicast lists are loaded. Finally
1177 * the device is moved into the up state and a %NETDEV_UP message is
1178 * sent to the netdev notifier chain.
1180 * Calling this function on an active interface is a nop. On a failure
1181 * a negative errno code is returned.
1183 int dev_open(struct net_device *dev)
1187 if (dev->flags & IFF_UP)
1190 ret = __dev_open(dev);
1194 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1195 call_netdevice_notifiers(NETDEV_UP, dev);
1199 EXPORT_SYMBOL(dev_open);
1201 static int __dev_close_many(struct list_head *head)
1203 struct net_device *dev;
1208 list_for_each_entry(dev, head, unreg_list) {
1209 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1211 clear_bit(__LINK_STATE_START, &dev->state);
1213 /* Synchronize to scheduled poll. We cannot touch poll list, it
1214 * can be even on different cpu. So just clear netif_running().
1216 * dev->stop() will invoke napi_disable() on all of it's
1217 * napi_struct instances on this device.
1219 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1222 dev_deactivate_many(head);
1224 list_for_each_entry(dev, head, unreg_list) {
1225 const struct net_device_ops *ops = dev->netdev_ops;
1228 * Call the device specific close. This cannot fail.
1229 * Only if device is UP
1231 * We allow it to be called even after a DETACH hot-plug
1237 dev->flags &= ~IFF_UP;
1238 net_dmaengine_put();
1244 static int __dev_close(struct net_device *dev)
1249 list_add(&dev->unreg_list, &single);
1250 retval = __dev_close_many(&single);
1255 static int dev_close_many(struct list_head *head)
1257 struct net_device *dev, *tmp;
1258 LIST_HEAD(tmp_list);
1260 list_for_each_entry_safe(dev, tmp, head, unreg_list)
1261 if (!(dev->flags & IFF_UP))
1262 list_move(&dev->unreg_list, &tmp_list);
1264 __dev_close_many(head);
1266 list_for_each_entry(dev, head, unreg_list) {
1267 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1268 call_netdevice_notifiers(NETDEV_DOWN, dev);
1271 /* rollback_registered_many needs the complete original list */
1272 list_splice(&tmp_list, head);
1277 * dev_close - shutdown an interface.
1278 * @dev: device to shutdown
1280 * This function moves an active device into down state. A
1281 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1282 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1285 int dev_close(struct net_device *dev)
1287 if (dev->flags & IFF_UP) {
1290 list_add(&dev->unreg_list, &single);
1291 dev_close_many(&single);
1296 EXPORT_SYMBOL(dev_close);
1300 * dev_disable_lro - disable Large Receive Offload on a device
1303 * Disable Large Receive Offload (LRO) on a net device. Must be
1304 * called under RTNL. This is needed if received packets may be
1305 * forwarded to another interface.
1307 void dev_disable_lro(struct net_device *dev)
1311 if (dev->ethtool_ops && dev->ethtool_ops->get_flags)
1312 flags = dev->ethtool_ops->get_flags(dev);
1314 flags = ethtool_op_get_flags(dev);
1316 if (!(flags & ETH_FLAG_LRO))
1319 __ethtool_set_flags(dev, flags & ~ETH_FLAG_LRO);
1320 WARN_ON(dev->features & NETIF_F_LRO);
1322 EXPORT_SYMBOL(dev_disable_lro);
1325 static int dev_boot_phase = 1;
1328 * register_netdevice_notifier - register a network notifier block
1331 * Register a notifier to be called when network device events occur.
1332 * The notifier passed is linked into the kernel structures and must
1333 * not be reused until it has been unregistered. A negative errno code
1334 * is returned on a failure.
1336 * When registered all registration and up events are replayed
1337 * to the new notifier to allow device to have a race free
1338 * view of the network device list.
1341 int register_netdevice_notifier(struct notifier_block *nb)
1343 struct net_device *dev;
1344 struct net_device *last;
1349 err = raw_notifier_chain_register(&netdev_chain, nb);
1355 for_each_netdev(net, dev) {
1356 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1357 err = notifier_to_errno(err);
1361 if (!(dev->flags & IFF_UP))
1364 nb->notifier_call(nb, NETDEV_UP, dev);
1375 for_each_netdev(net, dev) {
1379 if (dev->flags & IFF_UP) {
1380 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1381 nb->notifier_call(nb, NETDEV_DOWN, dev);
1383 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
1384 nb->notifier_call(nb, NETDEV_UNREGISTER_BATCH, dev);
1388 raw_notifier_chain_unregister(&netdev_chain, nb);
1391 EXPORT_SYMBOL(register_netdevice_notifier);
1394 * unregister_netdevice_notifier - unregister a network notifier block
1397 * Unregister a notifier previously registered by
1398 * register_netdevice_notifier(). The notifier is unlinked into the
1399 * kernel structures and may then be reused. A negative errno code
1400 * is returned on a failure.
1403 int unregister_netdevice_notifier(struct notifier_block *nb)
1408 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1412 EXPORT_SYMBOL(unregister_netdevice_notifier);
1415 * call_netdevice_notifiers - call all network notifier blocks
1416 * @val: value passed unmodified to notifier function
1417 * @dev: net_device pointer passed unmodified to notifier function
1419 * Call all network notifier blocks. Parameters and return value
1420 * are as for raw_notifier_call_chain().
1423 int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1426 return raw_notifier_call_chain(&netdev_chain, val, dev);
1428 EXPORT_SYMBOL(call_netdevice_notifiers);
1430 /* When > 0 there are consumers of rx skb time stamps */
1431 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1433 void net_enable_timestamp(void)
1435 atomic_inc(&netstamp_needed);
1437 EXPORT_SYMBOL(net_enable_timestamp);
1439 void net_disable_timestamp(void)
1441 atomic_dec(&netstamp_needed);
1443 EXPORT_SYMBOL(net_disable_timestamp);
1445 static inline void net_timestamp_set(struct sk_buff *skb)
1447 if (atomic_read(&netstamp_needed))
1448 __net_timestamp(skb);
1450 skb->tstamp.tv64 = 0;
1453 static inline void net_timestamp_check(struct sk_buff *skb)
1455 if (!skb->tstamp.tv64 && atomic_read(&netstamp_needed))
1456 __net_timestamp(skb);
1459 static inline bool is_skb_forwardable(struct net_device *dev,
1460 struct sk_buff *skb)
1464 if (!(dev->flags & IFF_UP))
1467 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1468 if (skb->len <= len)
1471 /* if TSO is enabled, we don't care about the length as the packet
1472 * could be forwarded without being segmented before
1474 if (skb_is_gso(skb))
1481 * dev_forward_skb - loopback an skb to another netif
1483 * @dev: destination network device
1484 * @skb: buffer to forward
1487 * NET_RX_SUCCESS (no congestion)
1488 * NET_RX_DROP (packet was dropped, but freed)
1490 * dev_forward_skb can be used for injecting an skb from the
1491 * start_xmit function of one device into the receive queue
1492 * of another device.
1494 * The receiving device may be in another namespace, so
1495 * we have to clear all information in the skb that could
1496 * impact namespace isolation.
1498 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1503 if (unlikely(!is_skb_forwardable(dev, skb))) {
1504 atomic_long_inc(&dev->rx_dropped);
1508 skb_set_dev(skb, dev);
1509 skb->tstamp.tv64 = 0;
1510 skb->pkt_type = PACKET_HOST;
1511 skb->protocol = eth_type_trans(skb, dev);
1512 return netif_rx(skb);
1514 EXPORT_SYMBOL_GPL(dev_forward_skb);
1516 static inline int deliver_skb(struct sk_buff *skb,
1517 struct packet_type *pt_prev,
1518 struct net_device *orig_dev)
1520 atomic_inc(&skb->users);
1521 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1525 * Support routine. Sends outgoing frames to any network
1526 * taps currently in use.
1529 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1531 struct packet_type *ptype;
1532 struct sk_buff *skb2 = NULL;
1533 struct packet_type *pt_prev = NULL;
1536 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1537 /* Never send packets back to the socket
1538 * they originated from - MvS (miquels@drinkel.ow.org)
1540 if ((ptype->dev == dev || !ptype->dev) &&
1541 (ptype->af_packet_priv == NULL ||
1542 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1544 deliver_skb(skb2, pt_prev, skb->dev);
1549 skb2 = skb_clone(skb, GFP_ATOMIC);
1553 net_timestamp_set(skb2);
1555 /* skb->nh should be correctly
1556 set by sender, so that the second statement is
1557 just protection against buggy protocols.
1559 skb_reset_mac_header(skb2);
1561 if (skb_network_header(skb2) < skb2->data ||
1562 skb2->network_header > skb2->tail) {
1563 if (net_ratelimit())
1564 printk(KERN_CRIT "protocol %04x is "
1566 ntohs(skb2->protocol),
1568 skb_reset_network_header(skb2);
1571 skb2->transport_header = skb2->network_header;
1572 skb2->pkt_type = PACKET_OUTGOING;
1577 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1581 /* netif_setup_tc - Handle tc mappings on real_num_tx_queues change
1582 * @dev: Network device
1583 * @txq: number of queues available
1585 * If real_num_tx_queues is changed the tc mappings may no longer be
1586 * valid. To resolve this verify the tc mapping remains valid and if
1587 * not NULL the mapping. With no priorities mapping to this
1588 * offset/count pair it will no longer be used. In the worst case TC0
1589 * is invalid nothing can be done so disable priority mappings. If is
1590 * expected that drivers will fix this mapping if they can before
1591 * calling netif_set_real_num_tx_queues.
1593 static void netif_setup_tc(struct net_device *dev, unsigned int txq)
1596 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1598 /* If TC0 is invalidated disable TC mapping */
1599 if (tc->offset + tc->count > txq) {
1600 pr_warning("Number of in use tx queues changed "
1601 "invalidating tc mappings. Priority "
1602 "traffic classification disabled!\n");
1607 /* Invalidated prio to tc mappings set to TC0 */
1608 for (i = 1; i < TC_BITMASK + 1; i++) {
1609 int q = netdev_get_prio_tc_map(dev, i);
1611 tc = &dev->tc_to_txq[q];
1612 if (tc->offset + tc->count > txq) {
1613 pr_warning("Number of in use tx queues "
1614 "changed. Priority %i to tc "
1615 "mapping %i is no longer valid "
1616 "setting map to 0\n",
1618 netdev_set_prio_tc_map(dev, i, 0);
1624 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
1625 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
1627 int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
1631 if (txq < 1 || txq > dev->num_tx_queues)
1634 if (dev->reg_state == NETREG_REGISTERED ||
1635 dev->reg_state == NETREG_UNREGISTERING) {
1638 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
1644 netif_setup_tc(dev, txq);
1646 if (txq < dev->real_num_tx_queues)
1647 qdisc_reset_all_tx_gt(dev, txq);
1650 dev->real_num_tx_queues = txq;
1653 EXPORT_SYMBOL(netif_set_real_num_tx_queues);
1657 * netif_set_real_num_rx_queues - set actual number of RX queues used
1658 * @dev: Network device
1659 * @rxq: Actual number of RX queues
1661 * This must be called either with the rtnl_lock held or before
1662 * registration of the net device. Returns 0 on success, or a
1663 * negative error code. If called before registration, it always
1666 int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
1670 if (rxq < 1 || rxq > dev->num_rx_queues)
1673 if (dev->reg_state == NETREG_REGISTERED) {
1676 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
1682 dev->real_num_rx_queues = rxq;
1685 EXPORT_SYMBOL(netif_set_real_num_rx_queues);
1688 static inline void __netif_reschedule(struct Qdisc *q)
1690 struct softnet_data *sd;
1691 unsigned long flags;
1693 local_irq_save(flags);
1694 sd = &__get_cpu_var(softnet_data);
1695 q->next_sched = NULL;
1696 *sd->output_queue_tailp = q;
1697 sd->output_queue_tailp = &q->next_sched;
1698 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1699 local_irq_restore(flags);
1702 void __netif_schedule(struct Qdisc *q)
1704 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
1705 __netif_reschedule(q);
1707 EXPORT_SYMBOL(__netif_schedule);
1709 void dev_kfree_skb_irq(struct sk_buff *skb)
1711 if (atomic_dec_and_test(&skb->users)) {
1712 struct softnet_data *sd;
1713 unsigned long flags;
1715 local_irq_save(flags);
1716 sd = &__get_cpu_var(softnet_data);
1717 skb->next = sd->completion_queue;
1718 sd->completion_queue = skb;
1719 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1720 local_irq_restore(flags);
1723 EXPORT_SYMBOL(dev_kfree_skb_irq);
1725 void dev_kfree_skb_any(struct sk_buff *skb)
1727 if (in_irq() || irqs_disabled())
1728 dev_kfree_skb_irq(skb);
1732 EXPORT_SYMBOL(dev_kfree_skb_any);
1736 * netif_device_detach - mark device as removed
1737 * @dev: network device
1739 * Mark device as removed from system and therefore no longer available.
1741 void netif_device_detach(struct net_device *dev)
1743 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1744 netif_running(dev)) {
1745 netif_tx_stop_all_queues(dev);
1748 EXPORT_SYMBOL(netif_device_detach);
1751 * netif_device_attach - mark device as attached
1752 * @dev: network device
1754 * Mark device as attached from system and restart if needed.
1756 void netif_device_attach(struct net_device *dev)
1758 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1759 netif_running(dev)) {
1760 netif_tx_wake_all_queues(dev);
1761 __netdev_watchdog_up(dev);
1764 EXPORT_SYMBOL(netif_device_attach);
1767 * skb_dev_set -- assign a new device to a buffer
1768 * @skb: buffer for the new device
1769 * @dev: network device
1771 * If an skb is owned by a device already, we have to reset
1772 * all data private to the namespace a device belongs to
1773 * before assigning it a new device.
1775 #ifdef CONFIG_NET_NS
1776 void skb_set_dev(struct sk_buff *skb, struct net_device *dev)
1779 if (skb->dev && !net_eq(dev_net(skb->dev), dev_net(dev))) {
1782 skb_init_secmark(skb);
1786 skb->ipvs_property = 0;
1787 #ifdef CONFIG_NET_SCHED
1793 EXPORT_SYMBOL(skb_set_dev);
1794 #endif /* CONFIG_NET_NS */
1797 * Invalidate hardware checksum when packet is to be mangled, and
1798 * complete checksum manually on outgoing path.
1800 int skb_checksum_help(struct sk_buff *skb)
1803 int ret = 0, offset;
1805 if (skb->ip_summed == CHECKSUM_COMPLETE)
1806 goto out_set_summed;
1808 if (unlikely(skb_shinfo(skb)->gso_size)) {
1809 /* Let GSO fix up the checksum. */
1810 goto out_set_summed;
1813 offset = skb_checksum_start_offset(skb);
1814 BUG_ON(offset >= skb_headlen(skb));
1815 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1817 offset += skb->csum_offset;
1818 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1820 if (skb_cloned(skb) &&
1821 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
1822 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1827 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1829 skb->ip_summed = CHECKSUM_NONE;
1833 EXPORT_SYMBOL(skb_checksum_help);
1836 * skb_gso_segment - Perform segmentation on skb.
1837 * @skb: buffer to segment
1838 * @features: features for the output path (see dev->features)
1840 * This function segments the given skb and returns a list of segments.
1842 * It may return NULL if the skb requires no segmentation. This is
1843 * only possible when GSO is used for verifying header integrity.
1845 struct sk_buff *skb_gso_segment(struct sk_buff *skb, u32 features)
1847 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1848 struct packet_type *ptype;
1849 __be16 type = skb->protocol;
1850 int vlan_depth = ETH_HLEN;
1853 while (type == htons(ETH_P_8021Q)) {
1854 struct vlan_hdr *vh;
1856 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
1857 return ERR_PTR(-EINVAL);
1859 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
1860 type = vh->h_vlan_encapsulated_proto;
1861 vlan_depth += VLAN_HLEN;
1864 skb_reset_mac_header(skb);
1865 skb->mac_len = skb->network_header - skb->mac_header;
1866 __skb_pull(skb, skb->mac_len);
1868 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1869 struct net_device *dev = skb->dev;
1870 struct ethtool_drvinfo info = {};
1872 if (dev && dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
1873 dev->ethtool_ops->get_drvinfo(dev, &info);
1875 WARN(1, "%s: caps=(0x%lx, 0x%lx) len=%d data_len=%d ip_summed=%d\n",
1876 info.driver, dev ? dev->features : 0L,
1877 skb->sk ? skb->sk->sk_route_caps : 0L,
1878 skb->len, skb->data_len, skb->ip_summed);
1880 if (skb_header_cloned(skb) &&
1881 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1882 return ERR_PTR(err);
1886 list_for_each_entry_rcu(ptype,
1887 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
1888 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1889 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1890 err = ptype->gso_send_check(skb);
1891 segs = ERR_PTR(err);
1892 if (err || skb_gso_ok(skb, features))
1894 __skb_push(skb, (skb->data -
1895 skb_network_header(skb)));
1897 segs = ptype->gso_segment(skb, features);
1903 __skb_push(skb, skb->data - skb_mac_header(skb));
1907 EXPORT_SYMBOL(skb_gso_segment);
1909 /* Take action when hardware reception checksum errors are detected. */
1911 void netdev_rx_csum_fault(struct net_device *dev)
1913 if (net_ratelimit()) {
1914 printk(KERN_ERR "%s: hw csum failure.\n",
1915 dev ? dev->name : "<unknown>");
1919 EXPORT_SYMBOL(netdev_rx_csum_fault);
1922 /* Actually, we should eliminate this check as soon as we know, that:
1923 * 1. IOMMU is present and allows to map all the memory.
1924 * 2. No high memory really exists on this machine.
1927 static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1929 #ifdef CONFIG_HIGHMEM
1931 if (!(dev->features & NETIF_F_HIGHDMA)) {
1932 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1933 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1937 if (PCI_DMA_BUS_IS_PHYS) {
1938 struct device *pdev = dev->dev.parent;
1942 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1943 dma_addr_t addr = page_to_phys(skb_shinfo(skb)->frags[i].page);
1944 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
1953 void (*destructor)(struct sk_buff *skb);
1956 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1958 static void dev_gso_skb_destructor(struct sk_buff *skb)
1960 struct dev_gso_cb *cb;
1963 struct sk_buff *nskb = skb->next;
1965 skb->next = nskb->next;
1968 } while (skb->next);
1970 cb = DEV_GSO_CB(skb);
1972 cb->destructor(skb);
1976 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1977 * @skb: buffer to segment
1978 * @features: device features as applicable to this skb
1980 * This function segments the given skb and stores the list of segments
1983 static int dev_gso_segment(struct sk_buff *skb, int features)
1985 struct sk_buff *segs;
1987 segs = skb_gso_segment(skb, features);
1989 /* Verifying header integrity only. */
1994 return PTR_ERR(segs);
1997 DEV_GSO_CB(skb)->destructor = skb->destructor;
1998 skb->destructor = dev_gso_skb_destructor;
2004 * Try to orphan skb early, right before transmission by the device.
2005 * We cannot orphan skb if tx timestamp is requested or the sk-reference
2006 * is needed on driver level for other reasons, e.g. see net/can/raw.c
2008 static inline void skb_orphan_try(struct sk_buff *skb)
2010 struct sock *sk = skb->sk;
2012 if (sk && !skb_shinfo(skb)->tx_flags) {
2013 /* skb_tx_hash() wont be able to get sk.
2014 * We copy sk_hash into skb->rxhash
2017 skb->rxhash = sk->sk_hash;
2022 static bool can_checksum_protocol(unsigned long features, __be16 protocol)
2024 return ((features & NETIF_F_GEN_CSUM) ||
2025 ((features & NETIF_F_V4_CSUM) &&
2026 protocol == htons(ETH_P_IP)) ||
2027 ((features & NETIF_F_V6_CSUM) &&
2028 protocol == htons(ETH_P_IPV6)) ||
2029 ((features & NETIF_F_FCOE_CRC) &&
2030 protocol == htons(ETH_P_FCOE)));
2033 static u32 harmonize_features(struct sk_buff *skb, __be16 protocol, u32 features)
2035 if (!can_checksum_protocol(features, protocol)) {
2036 features &= ~NETIF_F_ALL_CSUM;
2037 features &= ~NETIF_F_SG;
2038 } else if (illegal_highdma(skb->dev, skb)) {
2039 features &= ~NETIF_F_SG;
2045 u32 netif_skb_features(struct sk_buff *skb)
2047 __be16 protocol = skb->protocol;
2048 u32 features = skb->dev->features;
2050 if (protocol == htons(ETH_P_8021Q)) {
2051 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
2052 protocol = veh->h_vlan_encapsulated_proto;
2053 } else if (!vlan_tx_tag_present(skb)) {
2054 return harmonize_features(skb, protocol, features);
2057 features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_TX);
2059 if (protocol != htons(ETH_P_8021Q)) {
2060 return harmonize_features(skb, protocol, features);
2062 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
2063 NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_TX;
2064 return harmonize_features(skb, protocol, features);
2067 EXPORT_SYMBOL(netif_skb_features);
2070 * Returns true if either:
2071 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
2072 * 2. skb is fragmented and the device does not support SG, or if
2073 * at least one of fragments is in highmem and device does not
2074 * support DMA from it.
2076 static inline int skb_needs_linearize(struct sk_buff *skb,
2079 return skb_is_nonlinear(skb) &&
2080 ((skb_has_frag_list(skb) &&
2081 !(features & NETIF_F_FRAGLIST)) ||
2082 (skb_shinfo(skb)->nr_frags &&
2083 !(features & NETIF_F_SG)));
2086 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2087 struct netdev_queue *txq)
2089 const struct net_device_ops *ops = dev->netdev_ops;
2090 int rc = NETDEV_TX_OK;
2092 if (likely(!skb->next)) {
2096 * If device doesn't need skb->dst, release it right now while
2097 * its hot in this cpu cache
2099 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2102 if (!list_empty(&ptype_all))
2103 dev_queue_xmit_nit(skb, dev);
2105 skb_orphan_try(skb);
2107 features = netif_skb_features(skb);
2109 if (vlan_tx_tag_present(skb) &&
2110 !(features & NETIF_F_HW_VLAN_TX)) {
2111 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
2118 if (netif_needs_gso(skb, features)) {
2119 if (unlikely(dev_gso_segment(skb, features)))
2124 if (skb_needs_linearize(skb, features) &&
2125 __skb_linearize(skb))
2128 /* If packet is not checksummed and device does not
2129 * support checksumming for this protocol, complete
2130 * checksumming here.
2132 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2133 skb_set_transport_header(skb,
2134 skb_checksum_start_offset(skb));
2135 if (!(features & NETIF_F_ALL_CSUM) &&
2136 skb_checksum_help(skb))
2141 rc = ops->ndo_start_xmit(skb, dev);
2142 trace_net_dev_xmit(skb, rc);
2143 if (rc == NETDEV_TX_OK)
2144 txq_trans_update(txq);
2150 struct sk_buff *nskb = skb->next;
2152 skb->next = nskb->next;
2156 * If device doesn't need nskb->dst, release it right now while
2157 * its hot in this cpu cache
2159 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2162 rc = ops->ndo_start_xmit(nskb, dev);
2163 trace_net_dev_xmit(nskb, rc);
2164 if (unlikely(rc != NETDEV_TX_OK)) {
2165 if (rc & ~NETDEV_TX_MASK)
2166 goto out_kfree_gso_skb;
2167 nskb->next = skb->next;
2171 txq_trans_update(txq);
2172 if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
2173 return NETDEV_TX_BUSY;
2174 } while (skb->next);
2177 if (likely(skb->next == NULL))
2178 skb->destructor = DEV_GSO_CB(skb)->destructor;
2185 static u32 hashrnd __read_mostly;
2188 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2189 * to be used as a distribution range.
2191 u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
2192 unsigned int num_tx_queues)
2196 u16 qcount = num_tx_queues;
2198 if (skb_rx_queue_recorded(skb)) {
2199 hash = skb_get_rx_queue(skb);
2200 while (unlikely(hash >= num_tx_queues))
2201 hash -= num_tx_queues;
2206 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2207 qoffset = dev->tc_to_txq[tc].offset;
2208 qcount = dev->tc_to_txq[tc].count;
2211 if (skb->sk && skb->sk->sk_hash)
2212 hash = skb->sk->sk_hash;
2214 hash = (__force u16) skb->protocol ^ skb->rxhash;
2215 hash = jhash_1word(hash, hashrnd);
2217 return (u16) (((u64) hash * qcount) >> 32) + qoffset;
2219 EXPORT_SYMBOL(__skb_tx_hash);
2221 static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
2223 if (unlikely(queue_index >= dev->real_num_tx_queues)) {
2224 if (net_ratelimit()) {
2225 pr_warning("%s selects TX queue %d, but "
2226 "real number of TX queues is %d\n",
2227 dev->name, queue_index, dev->real_num_tx_queues);
2234 static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
2237 struct xps_dev_maps *dev_maps;
2238 struct xps_map *map;
2239 int queue_index = -1;
2242 dev_maps = rcu_dereference(dev->xps_maps);
2244 map = rcu_dereference(
2245 dev_maps->cpu_map[raw_smp_processor_id()]);
2248 queue_index = map->queues[0];
2251 if (skb->sk && skb->sk->sk_hash)
2252 hash = skb->sk->sk_hash;
2254 hash = (__force u16) skb->protocol ^
2256 hash = jhash_1word(hash, hashrnd);
2257 queue_index = map->queues[
2258 ((u64)hash * map->len) >> 32];
2260 if (unlikely(queue_index >= dev->real_num_tx_queues))
2272 static struct netdev_queue *dev_pick_tx(struct net_device *dev,
2273 struct sk_buff *skb)
2276 const struct net_device_ops *ops = dev->netdev_ops;
2278 if (dev->real_num_tx_queues == 1)
2280 else if (ops->ndo_select_queue) {
2281 queue_index = ops->ndo_select_queue(dev, skb);
2282 queue_index = dev_cap_txqueue(dev, queue_index);
2284 struct sock *sk = skb->sk;
2285 queue_index = sk_tx_queue_get(sk);
2287 if (queue_index < 0 || skb->ooo_okay ||
2288 queue_index >= dev->real_num_tx_queues) {
2289 int old_index = queue_index;
2291 queue_index = get_xps_queue(dev, skb);
2292 if (queue_index < 0)
2293 queue_index = skb_tx_hash(dev, skb);
2295 if (queue_index != old_index && sk) {
2296 struct dst_entry *dst =
2297 rcu_dereference_check(sk->sk_dst_cache, 1);
2299 if (dst && skb_dst(skb) == dst)
2300 sk_tx_queue_set(sk, queue_index);
2305 skb_set_queue_mapping(skb, queue_index);
2306 return netdev_get_tx_queue(dev, queue_index);
2309 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2310 struct net_device *dev,
2311 struct netdev_queue *txq)
2313 spinlock_t *root_lock = qdisc_lock(q);
2317 qdisc_skb_cb(skb)->pkt_len = skb->len;
2318 qdisc_calculate_pkt_len(skb, q);
2320 * Heuristic to force contended enqueues to serialize on a
2321 * separate lock before trying to get qdisc main lock.
2322 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2323 * and dequeue packets faster.
2325 contended = qdisc_is_running(q);
2326 if (unlikely(contended))
2327 spin_lock(&q->busylock);
2329 spin_lock(root_lock);
2330 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2333 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
2334 qdisc_run_begin(q)) {
2336 * This is a work-conserving queue; there are no old skbs
2337 * waiting to be sent out; and the qdisc is not running -
2338 * xmit the skb directly.
2340 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2343 qdisc_bstats_update(q, skb);
2345 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2346 if (unlikely(contended)) {
2347 spin_unlock(&q->busylock);
2354 rc = NET_XMIT_SUCCESS;
2357 rc = q->enqueue(skb, q) & NET_XMIT_MASK;
2358 if (qdisc_run_begin(q)) {
2359 if (unlikely(contended)) {
2360 spin_unlock(&q->busylock);
2366 spin_unlock(root_lock);
2367 if (unlikely(contended))
2368 spin_unlock(&q->busylock);
2372 static DEFINE_PER_CPU(int, xmit_recursion);
2373 #define RECURSION_LIMIT 10
2376 * dev_queue_xmit - transmit a buffer
2377 * @skb: buffer to transmit
2379 * Queue a buffer for transmission to a network device. The caller must
2380 * have set the device and priority and built the buffer before calling
2381 * this function. The function can be called from an interrupt.
2383 * A negative errno code is returned on a failure. A success does not
2384 * guarantee the frame will be transmitted as it may be dropped due
2385 * to congestion or traffic shaping.
2387 * -----------------------------------------------------------------------------------
2388 * I notice this method can also return errors from the queue disciplines,
2389 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2392 * Regardless of the return value, the skb is consumed, so it is currently
2393 * difficult to retry a send to this method. (You can bump the ref count
2394 * before sending to hold a reference for retry if you are careful.)
2396 * When calling this method, interrupts MUST be enabled. This is because
2397 * the BH enable code must have IRQs enabled so that it will not deadlock.
2400 int dev_queue_xmit(struct sk_buff *skb)
2402 struct net_device *dev = skb->dev;
2403 struct netdev_queue *txq;
2407 /* Disable soft irqs for various locks below. Also
2408 * stops preemption for RCU.
2412 txq = dev_pick_tx(dev, skb);
2413 q = rcu_dereference_bh(txq->qdisc);
2415 #ifdef CONFIG_NET_CLS_ACT
2416 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
2418 trace_net_dev_queue(skb);
2420 rc = __dev_xmit_skb(skb, q, dev, txq);
2424 /* The device has no queue. Common case for software devices:
2425 loopback, all the sorts of tunnels...
2427 Really, it is unlikely that netif_tx_lock protection is necessary
2428 here. (f.e. loopback and IP tunnels are clean ignoring statistics
2430 However, it is possible, that they rely on protection
2433 Check this and shot the lock. It is not prone from deadlocks.
2434 Either shot noqueue qdisc, it is even simpler 8)
2436 if (dev->flags & IFF_UP) {
2437 int cpu = smp_processor_id(); /* ok because BHs are off */
2439 if (txq->xmit_lock_owner != cpu) {
2441 if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
2442 goto recursion_alert;
2444 HARD_TX_LOCK(dev, txq, cpu);
2446 if (!netif_tx_queue_stopped(txq)) {
2447 __this_cpu_inc(xmit_recursion);
2448 rc = dev_hard_start_xmit(skb, dev, txq);
2449 __this_cpu_dec(xmit_recursion);
2450 if (dev_xmit_complete(rc)) {
2451 HARD_TX_UNLOCK(dev, txq);
2455 HARD_TX_UNLOCK(dev, txq);
2456 if (net_ratelimit())
2457 printk(KERN_CRIT "Virtual device %s asks to "
2458 "queue packet!\n", dev->name);
2460 /* Recursion is detected! It is possible,
2464 if (net_ratelimit())
2465 printk(KERN_CRIT "Dead loop on virtual device "
2466 "%s, fix it urgently!\n", dev->name);
2471 rcu_read_unlock_bh();
2476 rcu_read_unlock_bh();
2479 EXPORT_SYMBOL(dev_queue_xmit);
2482 /*=======================================================================
2484 =======================================================================*/
2486 int netdev_max_backlog __read_mostly = 1000;
2487 int netdev_tstamp_prequeue __read_mostly = 1;
2488 int netdev_budget __read_mostly = 300;
2489 int weight_p __read_mostly = 64; /* old backlog weight */
2491 /* Called with irq disabled */
2492 static inline void ____napi_schedule(struct softnet_data *sd,
2493 struct napi_struct *napi)
2495 list_add_tail(&napi->poll_list, &sd->poll_list);
2496 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2500 * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
2501 * and src/dst port numbers. Returns a non-zero hash number on success
2504 __u32 __skb_get_rxhash(struct sk_buff *skb)
2506 int nhoff, hash = 0, poff;
2507 struct ipv6hdr *ip6;
2510 u32 addr1, addr2, ihl;
2516 nhoff = skb_network_offset(skb);
2518 switch (skb->protocol) {
2519 case __constant_htons(ETH_P_IP):
2520 if (!pskb_may_pull(skb, sizeof(*ip) + nhoff))
2523 ip = (struct iphdr *) (skb->data + nhoff);
2524 if (ip->frag_off & htons(IP_MF | IP_OFFSET))
2527 ip_proto = ip->protocol;
2528 addr1 = (__force u32) ip->saddr;
2529 addr2 = (__force u32) ip->daddr;
2532 case __constant_htons(ETH_P_IPV6):
2533 if (!pskb_may_pull(skb, sizeof(*ip6) + nhoff))
2536 ip6 = (struct ipv6hdr *) (skb->data + nhoff);
2537 ip_proto = ip6->nexthdr;
2538 addr1 = (__force u32) ip6->saddr.s6_addr32[3];
2539 addr2 = (__force u32) ip6->daddr.s6_addr32[3];
2547 poff = proto_ports_offset(ip_proto);
2549 nhoff += ihl * 4 + poff;
2550 if (pskb_may_pull(skb, nhoff + 4)) {
2551 ports.v32 = * (__force u32 *) (skb->data + nhoff);
2552 if (ports.v16[1] < ports.v16[0])
2553 swap(ports.v16[0], ports.v16[1]);
2557 /* get a consistent hash (same value on both flow directions) */
2561 hash = jhash_3words(addr1, addr2, ports.v32, hashrnd);
2568 EXPORT_SYMBOL(__skb_get_rxhash);
2572 /* One global table that all flow-based protocols share. */
2573 struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
2574 EXPORT_SYMBOL(rps_sock_flow_table);
2576 static struct rps_dev_flow *
2577 set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2578 struct rps_dev_flow *rflow, u16 next_cpu)
2582 tcpu = rflow->cpu = next_cpu;
2583 if (tcpu != RPS_NO_CPU) {
2584 #ifdef CONFIG_RFS_ACCEL
2585 struct netdev_rx_queue *rxqueue;
2586 struct rps_dev_flow_table *flow_table;
2587 struct rps_dev_flow *old_rflow;
2592 /* Should we steer this flow to a different hardware queue? */
2593 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
2594 !(dev->features & NETIF_F_NTUPLE))
2596 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
2597 if (rxq_index == skb_get_rx_queue(skb))
2600 rxqueue = dev->_rx + rxq_index;
2601 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2604 flow_id = skb->rxhash & flow_table->mask;
2605 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
2606 rxq_index, flow_id);
2610 rflow = &flow_table->flows[flow_id];
2611 rflow->cpu = next_cpu;
2613 if (old_rflow->filter == rflow->filter)
2614 old_rflow->filter = RPS_NO_FILTER;
2618 per_cpu(softnet_data, tcpu).input_queue_head;
2625 * get_rps_cpu is called from netif_receive_skb and returns the target
2626 * CPU from the RPS map of the receiving queue for a given skb.
2627 * rcu_read_lock must be held on entry.
2629 static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2630 struct rps_dev_flow **rflowp)
2632 struct netdev_rx_queue *rxqueue;
2633 struct rps_map *map;
2634 struct rps_dev_flow_table *flow_table;
2635 struct rps_sock_flow_table *sock_flow_table;
2639 if (skb_rx_queue_recorded(skb)) {
2640 u16 index = skb_get_rx_queue(skb);
2641 if (unlikely(index >= dev->real_num_rx_queues)) {
2642 WARN_ONCE(dev->real_num_rx_queues > 1,
2643 "%s received packet on queue %u, but number "
2644 "of RX queues is %u\n",
2645 dev->name, index, dev->real_num_rx_queues);
2648 rxqueue = dev->_rx + index;
2652 map = rcu_dereference(rxqueue->rps_map);
2654 if (map->len == 1 &&
2655 !rcu_dereference_raw(rxqueue->rps_flow_table)) {
2656 tcpu = map->cpus[0];
2657 if (cpu_online(tcpu))
2661 } else if (!rcu_dereference_raw(rxqueue->rps_flow_table)) {
2665 skb_reset_network_header(skb);
2666 if (!skb_get_rxhash(skb))
2669 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2670 sock_flow_table = rcu_dereference(rps_sock_flow_table);
2671 if (flow_table && sock_flow_table) {
2673 struct rps_dev_flow *rflow;
2675 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
2678 next_cpu = sock_flow_table->ents[skb->rxhash &
2679 sock_flow_table->mask];
2682 * If the desired CPU (where last recvmsg was done) is
2683 * different from current CPU (one in the rx-queue flow
2684 * table entry), switch if one of the following holds:
2685 * - Current CPU is unset (equal to RPS_NO_CPU).
2686 * - Current CPU is offline.
2687 * - The current CPU's queue tail has advanced beyond the
2688 * last packet that was enqueued using this table entry.
2689 * This guarantees that all previous packets for the flow
2690 * have been dequeued, thus preserving in order delivery.
2692 if (unlikely(tcpu != next_cpu) &&
2693 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
2694 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
2695 rflow->last_qtail)) >= 0))
2696 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
2698 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
2706 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
2708 if (cpu_online(tcpu)) {
2718 #ifdef CONFIG_RFS_ACCEL
2721 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
2722 * @dev: Device on which the filter was set
2723 * @rxq_index: RX queue index
2724 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
2725 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
2727 * Drivers that implement ndo_rx_flow_steer() should periodically call
2728 * this function for each installed filter and remove the filters for
2729 * which it returns %true.
2731 bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
2732 u32 flow_id, u16 filter_id)
2734 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
2735 struct rps_dev_flow_table *flow_table;
2736 struct rps_dev_flow *rflow;
2741 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2742 if (flow_table && flow_id <= flow_table->mask) {
2743 rflow = &flow_table->flows[flow_id];
2744 cpu = ACCESS_ONCE(rflow->cpu);
2745 if (rflow->filter == filter_id && cpu != RPS_NO_CPU &&
2746 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
2747 rflow->last_qtail) <
2748 (int)(10 * flow_table->mask)))
2754 EXPORT_SYMBOL(rps_may_expire_flow);
2756 #endif /* CONFIG_RFS_ACCEL */
2758 /* Called from hardirq (IPI) context */
2759 static void rps_trigger_softirq(void *data)
2761 struct softnet_data *sd = data;
2763 ____napi_schedule(sd, &sd->backlog);
2767 #endif /* CONFIG_RPS */
2770 * Check if this softnet_data structure is another cpu one
2771 * If yes, queue it to our IPI list and return 1
2774 static int rps_ipi_queued(struct softnet_data *sd)
2777 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
2780 sd->rps_ipi_next = mysd->rps_ipi_list;
2781 mysd->rps_ipi_list = sd;
2783 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2786 #endif /* CONFIG_RPS */
2791 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
2792 * queue (may be a remote CPU queue).
2794 static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
2795 unsigned int *qtail)
2797 struct softnet_data *sd;
2798 unsigned long flags;
2800 sd = &per_cpu(softnet_data, cpu);
2802 local_irq_save(flags);
2805 if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
2806 if (skb_queue_len(&sd->input_pkt_queue)) {
2808 __skb_queue_tail(&sd->input_pkt_queue, skb);
2809 input_queue_tail_incr_save(sd, qtail);
2811 local_irq_restore(flags);
2812 return NET_RX_SUCCESS;
2815 /* Schedule NAPI for backlog device
2816 * We can use non atomic operation since we own the queue lock
2818 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
2819 if (!rps_ipi_queued(sd))
2820 ____napi_schedule(sd, &sd->backlog);
2828 local_irq_restore(flags);
2830 atomic_long_inc(&skb->dev->rx_dropped);
2836 * netif_rx - post buffer to the network code
2837 * @skb: buffer to post
2839 * This function receives a packet from a device driver and queues it for
2840 * the upper (protocol) levels to process. It always succeeds. The buffer
2841 * may be dropped during processing for congestion control or by the
2845 * NET_RX_SUCCESS (no congestion)
2846 * NET_RX_DROP (packet was dropped)
2850 int netif_rx(struct sk_buff *skb)
2854 /* if netpoll wants it, pretend we never saw it */
2855 if (netpoll_rx(skb))
2858 if (netdev_tstamp_prequeue)
2859 net_timestamp_check(skb);
2861 trace_netif_rx(skb);
2864 struct rps_dev_flow voidflow, *rflow = &voidflow;
2870 cpu = get_rps_cpu(skb->dev, skb, &rflow);
2872 cpu = smp_processor_id();
2874 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2882 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
2888 EXPORT_SYMBOL(netif_rx);
2890 int netif_rx_ni(struct sk_buff *skb)
2895 err = netif_rx(skb);
2896 if (local_softirq_pending())
2902 EXPORT_SYMBOL(netif_rx_ni);
2904 static void net_tx_action(struct softirq_action *h)
2906 struct softnet_data *sd = &__get_cpu_var(softnet_data);
2908 if (sd->completion_queue) {
2909 struct sk_buff *clist;
2911 local_irq_disable();
2912 clist = sd->completion_queue;
2913 sd->completion_queue = NULL;
2917 struct sk_buff *skb = clist;
2918 clist = clist->next;
2920 WARN_ON(atomic_read(&skb->users));
2921 trace_kfree_skb(skb, net_tx_action);
2926 if (sd->output_queue) {
2929 local_irq_disable();
2930 head = sd->output_queue;
2931 sd->output_queue = NULL;
2932 sd->output_queue_tailp = &sd->output_queue;
2936 struct Qdisc *q = head;
2937 spinlock_t *root_lock;
2939 head = head->next_sched;
2941 root_lock = qdisc_lock(q);
2942 if (spin_trylock(root_lock)) {
2943 smp_mb__before_clear_bit();
2944 clear_bit(__QDISC_STATE_SCHED,
2947 spin_unlock(root_lock);
2949 if (!test_bit(__QDISC_STATE_DEACTIVATED,
2951 __netif_reschedule(q);
2953 smp_mb__before_clear_bit();
2954 clear_bit(__QDISC_STATE_SCHED,
2962 #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
2963 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
2964 /* This hook is defined here for ATM LANE */
2965 int (*br_fdb_test_addr_hook)(struct net_device *dev,
2966 unsigned char *addr) __read_mostly;
2967 EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
2970 #ifdef CONFIG_NET_CLS_ACT
2971 /* TODO: Maybe we should just force sch_ingress to be compiled in
2972 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2973 * a compare and 2 stores extra right now if we dont have it on
2974 * but have CONFIG_NET_CLS_ACT
2975 * NOTE: This doesn't stop any functionality; if you dont have
2976 * the ingress scheduler, you just can't add policies on ingress.
2979 static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq)
2981 struct net_device *dev = skb->dev;
2982 u32 ttl = G_TC_RTTL(skb->tc_verd);
2983 int result = TC_ACT_OK;
2986 if (unlikely(MAX_RED_LOOP < ttl++)) {
2987 if (net_ratelimit())
2988 pr_warning( "Redir loop detected Dropping packet (%d->%d)\n",
2989 skb->skb_iif, dev->ifindex);
2993 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
2994 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
2997 if (q != &noop_qdisc) {
2998 spin_lock(qdisc_lock(q));
2999 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
3000 result = qdisc_enqueue_root(skb, q);
3001 spin_unlock(qdisc_lock(q));
3007 static inline struct sk_buff *handle_ing(struct sk_buff *skb,
3008 struct packet_type **pt_prev,
3009 int *ret, struct net_device *orig_dev)
3011 struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
3013 if (!rxq || rxq->qdisc == &noop_qdisc)
3017 *ret = deliver_skb(skb, *pt_prev, orig_dev);
3021 switch (ing_filter(skb, rxq)) {
3035 * netdev_rx_handler_register - register receive handler
3036 * @dev: device to register a handler for
3037 * @rx_handler: receive handler to register
3038 * @rx_handler_data: data pointer that is used by rx handler
3040 * Register a receive hander for a device. This handler will then be
3041 * called from __netif_receive_skb. A negative errno code is returned
3044 * The caller must hold the rtnl_mutex.
3046 * For a general description of rx_handler, see enum rx_handler_result.
3048 int netdev_rx_handler_register(struct net_device *dev,
3049 rx_handler_func_t *rx_handler,
3050 void *rx_handler_data)
3054 if (dev->rx_handler)
3057 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
3058 rcu_assign_pointer(dev->rx_handler, rx_handler);
3062 EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
3065 * netdev_rx_handler_unregister - unregister receive handler
3066 * @dev: device to unregister a handler from
3068 * Unregister a receive hander from a device.
3070 * The caller must hold the rtnl_mutex.
3072 void netdev_rx_handler_unregister(struct net_device *dev)
3076 rcu_assign_pointer(dev->rx_handler, NULL);
3077 rcu_assign_pointer(dev->rx_handler_data, NULL);
3079 EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
3081 static void vlan_on_bond_hook(struct sk_buff *skb)
3084 * Make sure ARP frames received on VLAN interfaces stacked on
3085 * bonding interfaces still make their way to any base bonding
3086 * device that may have registered for a specific ptype.
3088 if (skb->dev->priv_flags & IFF_802_1Q_VLAN &&
3089 vlan_dev_real_dev(skb->dev)->priv_flags & IFF_BONDING &&
3090 skb->protocol == htons(ETH_P_ARP)) {
3091 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
3095 skb2->dev = vlan_dev_real_dev(skb->dev);
3100 static int __netif_receive_skb(struct sk_buff *skb)
3102 struct packet_type *ptype, *pt_prev;
3103 rx_handler_func_t *rx_handler;
3104 struct net_device *orig_dev;
3105 struct net_device *null_or_dev;
3106 bool deliver_exact = false;
3107 int ret = NET_RX_DROP;
3110 if (!netdev_tstamp_prequeue)
3111 net_timestamp_check(skb);
3113 trace_netif_receive_skb(skb);
3115 /* if we've gotten here through NAPI, check netpoll */
3116 if (netpoll_receive_skb(skb))
3120 skb->skb_iif = skb->dev->ifindex;
3121 orig_dev = skb->dev;
3123 skb_reset_network_header(skb);
3124 skb_reset_transport_header(skb);
3125 skb->mac_len = skb->network_header - skb->mac_header;
3133 __this_cpu_inc(softnet_data.processed);
3135 #ifdef CONFIG_NET_CLS_ACT
3136 if (skb->tc_verd & TC_NCLS) {
3137 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
3142 list_for_each_entry_rcu(ptype, &ptype_all, list) {
3143 if (!ptype->dev || ptype->dev == skb->dev) {
3145 ret = deliver_skb(skb, pt_prev, orig_dev);
3150 #ifdef CONFIG_NET_CLS_ACT
3151 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
3157 rx_handler = rcu_dereference(skb->dev->rx_handler);
3160 ret = deliver_skb(skb, pt_prev, orig_dev);
3163 switch (rx_handler(&skb)) {
3164 case RX_HANDLER_CONSUMED:
3166 case RX_HANDLER_ANOTHER:
3168 case RX_HANDLER_EXACT:
3169 deliver_exact = true;
3170 case RX_HANDLER_PASS:
3177 if (vlan_tx_tag_present(skb)) {
3179 ret = deliver_skb(skb, pt_prev, orig_dev);
3182 if (vlan_hwaccel_do_receive(&skb)) {
3183 ret = __netif_receive_skb(skb);
3185 } else if (unlikely(!skb))
3189 vlan_on_bond_hook(skb);
3191 /* deliver only exact match when indicated */
3192 null_or_dev = deliver_exact ? skb->dev : NULL;
3194 type = skb->protocol;
3195 list_for_each_entry_rcu(ptype,
3196 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
3197 if (ptype->type == type &&
3198 (ptype->dev == null_or_dev || ptype->dev == skb->dev ||
3199 ptype->dev == orig_dev)) {
3201 ret = deliver_skb(skb, pt_prev, orig_dev);
3207 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
3209 atomic_long_inc(&skb->dev->rx_dropped);
3211 /* Jamal, now you will not able to escape explaining
3212 * me how you were going to use this. :-)
3223 * netif_receive_skb - process receive buffer from network
3224 * @skb: buffer to process
3226 * netif_receive_skb() is the main receive data processing function.
3227 * It always succeeds. The buffer may be dropped during processing
3228 * for congestion control or by the protocol layers.
3230 * This function may only be called from softirq context and interrupts
3231 * should be enabled.
3233 * Return values (usually ignored):
3234 * NET_RX_SUCCESS: no congestion
3235 * NET_RX_DROP: packet was dropped
3237 int netif_receive_skb(struct sk_buff *skb)
3239 if (netdev_tstamp_prequeue)
3240 net_timestamp_check(skb);
3242 if (skb_defer_rx_timestamp(skb))
3243 return NET_RX_SUCCESS;
3247 struct rps_dev_flow voidflow, *rflow = &voidflow;
3252 cpu = get_rps_cpu(skb->dev, skb, &rflow);
3255 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3259 ret = __netif_receive_skb(skb);
3265 return __netif_receive_skb(skb);
3268 EXPORT_SYMBOL(netif_receive_skb);
3270 /* Network device is going away, flush any packets still pending
3271 * Called with irqs disabled.
3273 static void flush_backlog(void *arg)
3275 struct net_device *dev = arg;
3276 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3277 struct sk_buff *skb, *tmp;
3280 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
3281 if (skb->dev == dev) {
3282 __skb_unlink(skb, &sd->input_pkt_queue);
3284 input_queue_head_incr(sd);
3289 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3290 if (skb->dev == dev) {
3291 __skb_unlink(skb, &sd->process_queue);
3293 input_queue_head_incr(sd);
3298 static int napi_gro_complete(struct sk_buff *skb)
3300 struct packet_type *ptype;
3301 __be16 type = skb->protocol;
3302 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
3305 if (NAPI_GRO_CB(skb)->count == 1) {
3306 skb_shinfo(skb)->gso_size = 0;
3311 list_for_each_entry_rcu(ptype, head, list) {
3312 if (ptype->type != type || ptype->dev || !ptype->gro_complete)
3315 err = ptype->gro_complete(skb);