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 <linux/bitops.h>
77 #include <linux/capability.h>
78 #include <linux/cpu.h>
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/hash.h>
82 #include <linux/slab.h>
83 #include <linux/sched.h>
84 #include <linux/mutex.h>
85 #include <linux/string.h>
87 #include <linux/socket.h>
88 #include <linux/sockios.h>
89 #include <linux/errno.h>
90 #include <linux/interrupt.h>
91 #include <linux/if_ether.h>
92 #include <linux/netdevice.h>
93 #include <linux/etherdevice.h>
94 #include <linux/ethtool.h>
95 #include <linux/notifier.h>
96 #include <linux/skbuff.h>
97 #include <net/net_namespace.h>
99 #include <linux/rtnetlink.h>
100 #include <linux/proc_fs.h>
101 #include <linux/seq_file.h>
102 #include <linux/stat.h>
104 #include <net/pkt_sched.h>
105 #include <net/checksum.h>
106 #include <net/xfrm.h>
107 #include <linux/highmem.h>
108 #include <linux/init.h>
109 #include <linux/kmod.h>
110 #include <linux/module.h>
111 #include <linux/netpoll.h>
112 #include <linux/rcupdate.h>
113 #include <linux/delay.h>
114 #include <net/wext.h>
115 #include <net/iw_handler.h>
116 #include <asm/current.h>
117 #include <linux/audit.h>
118 #include <linux/dmaengine.h>
119 #include <linux/err.h>
120 #include <linux/ctype.h>
121 #include <linux/if_arp.h>
122 #include <linux/if_vlan.h>
123 #include <linux/ip.h>
125 #include <linux/ipv6.h>
126 #include <linux/in.h>
127 #include <linux/jhash.h>
128 #include <linux/random.h>
129 #include <trace/events/napi.h>
130 #include <trace/events/net.h>
131 #include <trace/events/skb.h>
132 #include <linux/pci.h>
133 #include <linux/inetdevice.h>
134 #include <linux/cpu_rmap.h>
135 #include <linux/net_tstamp.h>
136 #include <linux/static_key.h>
138 #include "net-sysfs.h"
140 /* Instead of increasing this, you should create a hash table. */
141 #define MAX_GRO_SKBS 8
143 /* This should be increased if a protocol with a bigger head is added. */
144 #define GRO_MAX_HEAD (MAX_HEADER + 128)
147 * The list of packet types we will receive (as opposed to discard)
148 * and the routines to invoke.
150 * Why 16. Because with 16 the only overlap we get on a hash of the
151 * low nibble of the protocol value is RARP/SNAP/X.25.
153 * NOTE: That is no longer true with the addition of VLAN tags. Not
154 * sure which should go first, but I bet it won't make much
155 * difference if we are running VLANs. The good news is that
156 * this protocol won't be in the list unless compiled in, so
157 * the average user (w/out VLANs) will not be adversely affected.
174 #define PTYPE_HASH_SIZE (16)
175 #define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
177 static DEFINE_SPINLOCK(ptype_lock);
178 static DEFINE_SPINLOCK(offload_lock);
179 static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
180 static struct list_head ptype_all __read_mostly; /* Taps */
181 static struct list_head offload_base __read_mostly;
184 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
187 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
189 * Writers must hold the rtnl semaphore while they loop through the
190 * dev_base_head list, and hold dev_base_lock for writing when they do the
191 * actual updates. This allows pure readers to access the list even
192 * while a writer is preparing to update it.
194 * To put it another way, dev_base_lock is held for writing only to
195 * protect against pure readers; the rtnl semaphore provides the
196 * protection against other writers.
198 * See, for example usages, register_netdevice() and
199 * unregister_netdevice(), which must be called with the rtnl
202 DEFINE_RWLOCK(dev_base_lock);
203 EXPORT_SYMBOL(dev_base_lock);
205 seqcount_t devnet_rename_seq;
207 static inline void dev_base_seq_inc(struct net *net)
209 while (++net->dev_base_seq == 0);
212 static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
214 unsigned int hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
216 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
219 static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
221 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
224 static inline void rps_lock(struct softnet_data *sd)
227 spin_lock(&sd->input_pkt_queue.lock);
231 static inline void rps_unlock(struct softnet_data *sd)
234 spin_unlock(&sd->input_pkt_queue.lock);
238 /* Device list insertion */
239 static int list_netdevice(struct net_device *dev)
241 struct net *net = dev_net(dev);
245 write_lock_bh(&dev_base_lock);
246 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
247 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
248 hlist_add_head_rcu(&dev->index_hlist,
249 dev_index_hash(net, dev->ifindex));
250 write_unlock_bh(&dev_base_lock);
252 dev_base_seq_inc(net);
257 /* Device list removal
258 * caller must respect a RCU grace period before freeing/reusing dev
260 static void unlist_netdevice(struct net_device *dev)
264 /* Unlink dev from the device chain */
265 write_lock_bh(&dev_base_lock);
266 list_del_rcu(&dev->dev_list);
267 hlist_del_rcu(&dev->name_hlist);
268 hlist_del_rcu(&dev->index_hlist);
269 write_unlock_bh(&dev_base_lock);
271 dev_base_seq_inc(dev_net(dev));
278 static RAW_NOTIFIER_HEAD(netdev_chain);
281 * Device drivers call our routines to queue packets here. We empty the
282 * queue in the local softnet handler.
285 DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
286 EXPORT_PER_CPU_SYMBOL(softnet_data);
288 #ifdef CONFIG_LOCKDEP
290 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
291 * according to dev->type
293 static const unsigned short netdev_lock_type[] =
294 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
295 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
296 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
297 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
298 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
299 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
300 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
301 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
302 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
303 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
304 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
305 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
306 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
307 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
308 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
310 static const char *const netdev_lock_name[] =
311 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
312 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
313 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
314 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
315 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
316 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
317 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
318 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
319 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
320 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
321 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
322 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
323 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
324 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
325 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
327 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
328 static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
330 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
334 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
335 if (netdev_lock_type[i] == dev_type)
337 /* the last key is used by default */
338 return ARRAY_SIZE(netdev_lock_type) - 1;
341 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
342 unsigned short dev_type)
346 i = netdev_lock_pos(dev_type);
347 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
348 netdev_lock_name[i]);
351 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
355 i = netdev_lock_pos(dev->type);
356 lockdep_set_class_and_name(&dev->addr_list_lock,
357 &netdev_addr_lock_key[i],
358 netdev_lock_name[i]);
361 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
362 unsigned short dev_type)
365 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
370 /*******************************************************************************
372 Protocol management and registration routines
374 *******************************************************************************/
377 * Add a protocol ID to the list. Now that the input handler is
378 * smarter we can dispense with all the messy stuff that used to be
381 * BEWARE!!! Protocol handlers, mangling input packets,
382 * MUST BE last in hash buckets and checking protocol handlers
383 * MUST start from promiscuous ptype_all chain in net_bh.
384 * It is true now, do not change it.
385 * Explanation follows: if protocol handler, mangling packet, will
386 * be the first on list, it is not able to sense, that packet
387 * is cloned and should be copied-on-write, so that it will
388 * change it and subsequent readers will get broken packet.
392 static inline struct list_head *ptype_head(const struct packet_type *pt)
394 if (pt->type == htons(ETH_P_ALL))
397 return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
401 * dev_add_pack - add packet handler
402 * @pt: packet type declaration
404 * Add a protocol handler to the networking stack. The passed &packet_type
405 * is linked into kernel lists and may not be freed until it has been
406 * removed from the kernel lists.
408 * This call does not sleep therefore it can not
409 * guarantee all CPU's that are in middle of receiving packets
410 * will see the new packet type (until the next received packet).
413 void dev_add_pack(struct packet_type *pt)
415 struct list_head *head = ptype_head(pt);
417 spin_lock(&ptype_lock);
418 list_add_rcu(&pt->list, head);
419 spin_unlock(&ptype_lock);
421 EXPORT_SYMBOL(dev_add_pack);
424 * __dev_remove_pack - remove packet handler
425 * @pt: packet type declaration
427 * Remove a protocol handler that was previously added to the kernel
428 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
429 * from the kernel lists and can be freed or reused once this function
432 * The packet type might still be in use by receivers
433 * and must not be freed until after all the CPU's have gone
434 * through a quiescent state.
436 void __dev_remove_pack(struct packet_type *pt)
438 struct list_head *head = ptype_head(pt);
439 struct packet_type *pt1;
441 spin_lock(&ptype_lock);
443 list_for_each_entry(pt1, head, list) {
445 list_del_rcu(&pt->list);
450 pr_warn("dev_remove_pack: %p not found\n", pt);
452 spin_unlock(&ptype_lock);
454 EXPORT_SYMBOL(__dev_remove_pack);
457 * dev_remove_pack - remove packet handler
458 * @pt: packet type declaration
460 * Remove a protocol handler that was previously added to the kernel
461 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
462 * from the kernel lists and can be freed or reused once this function
465 * This call sleeps to guarantee that no CPU is looking at the packet
468 void dev_remove_pack(struct packet_type *pt)
470 __dev_remove_pack(pt);
474 EXPORT_SYMBOL(dev_remove_pack);
478 * dev_add_offload - register offload handlers
479 * @po: protocol offload declaration
481 * Add protocol offload handlers to the networking stack. The passed
482 * &proto_offload is linked into kernel lists and may not be freed until
483 * it has been removed from the kernel lists.
485 * This call does not sleep therefore it can not
486 * guarantee all CPU's that are in middle of receiving packets
487 * will see the new offload handlers (until the next received packet).
489 void dev_add_offload(struct packet_offload *po)
491 struct list_head *head = &offload_base;
493 spin_lock(&offload_lock);
494 list_add_rcu(&po->list, head);
495 spin_unlock(&offload_lock);
497 EXPORT_SYMBOL(dev_add_offload);
500 * __dev_remove_offload - remove offload handler
501 * @po: packet offload declaration
503 * Remove a protocol offload handler that was previously added to the
504 * kernel offload handlers by dev_add_offload(). The passed &offload_type
505 * is removed from the kernel lists and can be freed or reused once this
508 * The packet type might still be in use by receivers
509 * and must not be freed until after all the CPU's have gone
510 * through a quiescent state.
512 void __dev_remove_offload(struct packet_offload *po)
514 struct list_head *head = &offload_base;
515 struct packet_offload *po1;
517 spin_lock(&offload_lock);
519 list_for_each_entry(po1, head, list) {
521 list_del_rcu(&po->list);
526 pr_warn("dev_remove_offload: %p not found\n", po);
528 spin_unlock(&offload_lock);
530 EXPORT_SYMBOL(__dev_remove_offload);
533 * dev_remove_offload - remove packet offload handler
534 * @po: packet offload declaration
536 * Remove a packet offload handler that was previously added to the kernel
537 * offload handlers by dev_add_offload(). The passed &offload_type is
538 * removed from the kernel lists and can be freed or reused once this
541 * This call sleeps to guarantee that no CPU is looking at the packet
544 void dev_remove_offload(struct packet_offload *po)
546 __dev_remove_offload(po);
550 EXPORT_SYMBOL(dev_remove_offload);
552 /******************************************************************************
554 Device Boot-time Settings Routines
556 *******************************************************************************/
558 /* Boot time configuration table */
559 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
562 * netdev_boot_setup_add - add new setup entry
563 * @name: name of the device
564 * @map: configured settings for the device
566 * Adds new setup entry to the dev_boot_setup list. The function
567 * returns 0 on error and 1 on success. This is a generic routine to
570 static int netdev_boot_setup_add(char *name, struct ifmap *map)
572 struct netdev_boot_setup *s;
576 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
577 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
578 memset(s[i].name, 0, sizeof(s[i].name));
579 strlcpy(s[i].name, name, IFNAMSIZ);
580 memcpy(&s[i].map, map, sizeof(s[i].map));
585 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
589 * netdev_boot_setup_check - check boot time settings
590 * @dev: the netdevice
592 * Check boot time settings for the device.
593 * The found settings are set for the device to be used
594 * later in the device probing.
595 * Returns 0 if no settings found, 1 if they are.
597 int netdev_boot_setup_check(struct net_device *dev)
599 struct netdev_boot_setup *s = dev_boot_setup;
602 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
603 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
604 !strcmp(dev->name, s[i].name)) {
605 dev->irq = s[i].map.irq;
606 dev->base_addr = s[i].map.base_addr;
607 dev->mem_start = s[i].map.mem_start;
608 dev->mem_end = s[i].map.mem_end;
614 EXPORT_SYMBOL(netdev_boot_setup_check);
618 * netdev_boot_base - get address from boot time settings
619 * @prefix: prefix for network device
620 * @unit: id for network device
622 * Check boot time settings for the base address of device.
623 * The found settings are set for the device to be used
624 * later in the device probing.
625 * Returns 0 if no settings found.
627 unsigned long netdev_boot_base(const char *prefix, int unit)
629 const struct netdev_boot_setup *s = dev_boot_setup;
633 sprintf(name, "%s%d", prefix, unit);
636 * If device already registered then return base of 1
637 * to indicate not to probe for this interface
639 if (__dev_get_by_name(&init_net, name))
642 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
643 if (!strcmp(name, s[i].name))
644 return s[i].map.base_addr;
649 * Saves at boot time configured settings for any netdevice.
651 int __init netdev_boot_setup(char *str)
656 str = get_options(str, ARRAY_SIZE(ints), ints);
661 memset(&map, 0, sizeof(map));
665 map.base_addr = ints[2];
667 map.mem_start = ints[3];
669 map.mem_end = ints[4];
671 /* Add new entry to the list */
672 return netdev_boot_setup_add(str, &map);
675 __setup("netdev=", netdev_boot_setup);
677 /*******************************************************************************
679 Device Interface Subroutines
681 *******************************************************************************/
684 * __dev_get_by_name - find a device by its name
685 * @net: the applicable net namespace
686 * @name: name to find
688 * Find an interface by name. Must be called under RTNL semaphore
689 * or @dev_base_lock. If the name is found a pointer to the device
690 * is returned. If the name is not found then %NULL is returned. The
691 * reference counters are not incremented so the caller must be
692 * careful with locks.
695 struct net_device *__dev_get_by_name(struct net *net, const char *name)
697 struct hlist_node *p;
698 struct net_device *dev;
699 struct hlist_head *head = dev_name_hash(net, name);
701 hlist_for_each_entry(dev, p, head, name_hlist)
702 if (!strncmp(dev->name, name, IFNAMSIZ))
707 EXPORT_SYMBOL(__dev_get_by_name);
710 * dev_get_by_name_rcu - find a device by its name
711 * @net: the applicable net namespace
712 * @name: name to find
714 * Find an interface by name.
715 * If the name is found a pointer to the device is returned.
716 * If the name is not found then %NULL is returned.
717 * The reference counters are not incremented so the caller must be
718 * careful with locks. The caller must hold RCU lock.
721 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
723 struct hlist_node *p;
724 struct net_device *dev;
725 struct hlist_head *head = dev_name_hash(net, name);
727 hlist_for_each_entry_rcu(dev, p, head, name_hlist)
728 if (!strncmp(dev->name, name, IFNAMSIZ))
733 EXPORT_SYMBOL(dev_get_by_name_rcu);
736 * dev_get_by_name - find a device by its name
737 * @net: the applicable net namespace
738 * @name: name to find
740 * Find an interface by name. This can be called from any
741 * context and does its own locking. The returned handle has
742 * the usage count incremented and the caller must use dev_put() to
743 * release it when it is no longer needed. %NULL is returned if no
744 * matching device is found.
747 struct net_device *dev_get_by_name(struct net *net, const char *name)
749 struct net_device *dev;
752 dev = dev_get_by_name_rcu(net, name);
758 EXPORT_SYMBOL(dev_get_by_name);
761 * __dev_get_by_index - find a device by its ifindex
762 * @net: the applicable net namespace
763 * @ifindex: index of device
765 * Search for an interface by index. Returns %NULL if the device
766 * is not found or a pointer to the device. The device has not
767 * had its reference counter increased so the caller must be careful
768 * about locking. The caller must hold either the RTNL semaphore
772 struct net_device *__dev_get_by_index(struct net *net, int ifindex)
774 struct hlist_node *p;
775 struct net_device *dev;
776 struct hlist_head *head = dev_index_hash(net, ifindex);
778 hlist_for_each_entry(dev, p, head, index_hlist)
779 if (dev->ifindex == ifindex)
784 EXPORT_SYMBOL(__dev_get_by_index);
787 * dev_get_by_index_rcu - find a device by its ifindex
788 * @net: the applicable net namespace
789 * @ifindex: index of device
791 * Search for an interface by index. Returns %NULL if the device
792 * is not found or a pointer to the device. The device has not
793 * had its reference counter increased so the caller must be careful
794 * about locking. The caller must hold RCU lock.
797 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
799 struct hlist_node *p;
800 struct net_device *dev;
801 struct hlist_head *head = dev_index_hash(net, ifindex);
803 hlist_for_each_entry_rcu(dev, p, head, index_hlist)
804 if (dev->ifindex == ifindex)
809 EXPORT_SYMBOL(dev_get_by_index_rcu);
813 * dev_get_by_index - find a device by its ifindex
814 * @net: the applicable net namespace
815 * @ifindex: index of device
817 * Search for an interface by index. Returns NULL if the device
818 * is not found or a pointer to the device. The device returned has
819 * had a reference added and the pointer is safe until the user calls
820 * dev_put to indicate they have finished with it.
823 struct net_device *dev_get_by_index(struct net *net, int ifindex)
825 struct net_device *dev;
828 dev = dev_get_by_index_rcu(net, ifindex);
834 EXPORT_SYMBOL(dev_get_by_index);
837 * dev_getbyhwaddr_rcu - find a device by its hardware address
838 * @net: the applicable net namespace
839 * @type: media type of device
840 * @ha: hardware address
842 * Search for an interface by MAC address. Returns NULL if the device
843 * is not found or a pointer to the device.
844 * The caller must hold RCU or RTNL.
845 * The returned device has not had its ref count increased
846 * and the caller must therefore be careful about locking
850 struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
853 struct net_device *dev;
855 for_each_netdev_rcu(net, dev)
856 if (dev->type == type &&
857 !memcmp(dev->dev_addr, ha, dev->addr_len))
862 EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
864 struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
866 struct net_device *dev;
869 for_each_netdev(net, dev)
870 if (dev->type == type)
875 EXPORT_SYMBOL(__dev_getfirstbyhwtype);
877 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
879 struct net_device *dev, *ret = NULL;
882 for_each_netdev_rcu(net, dev)
883 if (dev->type == type) {
891 EXPORT_SYMBOL(dev_getfirstbyhwtype);
894 * dev_get_by_flags_rcu - find any device with given flags
895 * @net: the applicable net namespace
896 * @if_flags: IFF_* values
897 * @mask: bitmask of bits in if_flags to check
899 * Search for any interface with the given flags. Returns NULL if a device
900 * is not found or a pointer to the device. Must be called inside
901 * rcu_read_lock(), and result refcount is unchanged.
904 struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
907 struct net_device *dev, *ret;
910 for_each_netdev_rcu(net, dev) {
911 if (((dev->flags ^ if_flags) & mask) == 0) {
918 EXPORT_SYMBOL(dev_get_by_flags_rcu);
921 * dev_valid_name - check if name is okay for network device
924 * Network device names need to be valid file names to
925 * to allow sysfs to work. We also disallow any kind of
928 bool dev_valid_name(const char *name)
932 if (strlen(name) >= IFNAMSIZ)
934 if (!strcmp(name, ".") || !strcmp(name, ".."))
938 if (*name == '/' || isspace(*name))
944 EXPORT_SYMBOL(dev_valid_name);
947 * __dev_alloc_name - allocate a name for a device
948 * @net: network namespace to allocate the device name in
949 * @name: name format string
950 * @buf: scratch buffer and result name string
952 * Passed a format string - eg "lt%d" it will try and find a suitable
953 * id. It scans list of devices to build up a free map, then chooses
954 * the first empty slot. The caller must hold the dev_base or rtnl lock
955 * while allocating the name and adding the device in order to avoid
957 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
958 * Returns the number of the unit assigned or a negative errno code.
961 static int __dev_alloc_name(struct net *net, const char *name, char *buf)
965 const int max_netdevices = 8*PAGE_SIZE;
966 unsigned long *inuse;
967 struct net_device *d;
969 p = strnchr(name, IFNAMSIZ-1, '%');
972 * Verify the string as this thing may have come from
973 * the user. There must be either one "%d" and no other "%"
976 if (p[1] != 'd' || strchr(p + 2, '%'))
979 /* Use one page as a bit array of possible slots */
980 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
984 for_each_netdev(net, d) {
985 if (!sscanf(d->name, name, &i))
987 if (i < 0 || i >= max_netdevices)
990 /* avoid cases where sscanf is not exact inverse of printf */
991 snprintf(buf, IFNAMSIZ, name, i);
992 if (!strncmp(buf, d->name, IFNAMSIZ))
996 i = find_first_zero_bit(inuse, max_netdevices);
997 free_page((unsigned long) inuse);
1001 snprintf(buf, IFNAMSIZ, name, i);
1002 if (!__dev_get_by_name(net, buf))
1005 /* It is possible to run out of possible slots
1006 * when the name is long and there isn't enough space left
1007 * for the digits, or if all bits are used.
1013 * dev_alloc_name - allocate a name for a device
1015 * @name: name format string
1017 * Passed a format string - eg "lt%d" it will try and find a suitable
1018 * id. It scans list of devices to build up a free map, then chooses
1019 * the first empty slot. The caller must hold the dev_base or rtnl lock
1020 * while allocating the name and adding the device in order to avoid
1022 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1023 * Returns the number of the unit assigned or a negative errno code.
1026 int dev_alloc_name(struct net_device *dev, const char *name)
1032 BUG_ON(!dev_net(dev));
1034 ret = __dev_alloc_name(net, name, buf);
1036 strlcpy(dev->name, buf, IFNAMSIZ);
1039 EXPORT_SYMBOL(dev_alloc_name);
1041 static int dev_alloc_name_ns(struct net *net,
1042 struct net_device *dev,
1048 ret = __dev_alloc_name(net, name, buf);
1050 strlcpy(dev->name, buf, IFNAMSIZ);
1054 static int dev_get_valid_name(struct net *net,
1055 struct net_device *dev,
1060 if (!dev_valid_name(name))
1063 if (strchr(name, '%'))
1064 return dev_alloc_name_ns(net, dev, name);
1065 else if (__dev_get_by_name(net, name))
1067 else if (dev->name != name)
1068 strlcpy(dev->name, name, IFNAMSIZ);
1074 * dev_change_name - change name of a device
1076 * @newname: name (or format string) must be at least IFNAMSIZ
1078 * Change name of a device, can pass format strings "eth%d".
1081 int dev_change_name(struct net_device *dev, const char *newname)
1083 char oldname[IFNAMSIZ];
1089 BUG_ON(!dev_net(dev));
1092 if (dev->flags & IFF_UP)
1095 write_seqcount_begin(&devnet_rename_seq);
1097 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1098 write_seqcount_end(&devnet_rename_seq);
1102 memcpy(oldname, dev->name, IFNAMSIZ);
1104 err = dev_get_valid_name(net, dev, newname);
1106 write_seqcount_end(&devnet_rename_seq);
1111 ret = device_rename(&dev->dev, dev->name);
1113 memcpy(dev->name, oldname, IFNAMSIZ);
1114 write_seqcount_end(&devnet_rename_seq);
1118 write_seqcount_end(&devnet_rename_seq);
1120 write_lock_bh(&dev_base_lock);
1121 hlist_del_rcu(&dev->name_hlist);
1122 write_unlock_bh(&dev_base_lock);
1126 write_lock_bh(&dev_base_lock);
1127 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
1128 write_unlock_bh(&dev_base_lock);
1130 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1131 ret = notifier_to_errno(ret);
1134 /* err >= 0 after dev_alloc_name() or stores the first errno */
1137 write_seqcount_begin(&devnet_rename_seq);
1138 memcpy(dev->name, oldname, IFNAMSIZ);
1141 pr_err("%s: name change rollback failed: %d\n",
1150 * dev_set_alias - change ifalias of a device
1152 * @alias: name up to IFALIASZ
1153 * @len: limit of bytes to copy from info
1155 * Set ifalias for a device,
1157 int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1163 if (len >= IFALIASZ)
1167 kfree(dev->ifalias);
1168 dev->ifalias = NULL;
1172 new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1175 dev->ifalias = new_ifalias;
1177 strlcpy(dev->ifalias, alias, len+1);
1183 * netdev_features_change - device changes features
1184 * @dev: device to cause notification
1186 * Called to indicate a device has changed features.
1188 void netdev_features_change(struct net_device *dev)
1190 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1192 EXPORT_SYMBOL(netdev_features_change);
1195 * netdev_state_change - device changes state
1196 * @dev: device to cause notification
1198 * Called to indicate a device has changed state. This function calls
1199 * the notifier chains for netdev_chain and sends a NEWLINK message
1200 * to the routing socket.
1202 void netdev_state_change(struct net_device *dev)
1204 if (dev->flags & IFF_UP) {
1205 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1206 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1209 EXPORT_SYMBOL(netdev_state_change);
1212 * netdev_notify_peers - notify network peers about existence of @dev
1213 * @dev: network device
1215 * Generate traffic such that interested network peers are aware of
1216 * @dev, such as by generating a gratuitous ARP. This may be used when
1217 * a device wants to inform the rest of the network about some sort of
1218 * reconfiguration such as a failover event or virtual machine
1221 void netdev_notify_peers(struct net_device *dev)
1224 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1227 EXPORT_SYMBOL(netdev_notify_peers);
1230 * dev_load - load a network module
1231 * @net: the applicable net namespace
1232 * @name: name of interface
1234 * If a network interface is not present and the process has suitable
1235 * privileges this function loads the module. If module loading is not
1236 * available in this kernel then it becomes a nop.
1239 void dev_load(struct net *net, const char *name)
1241 struct net_device *dev;
1245 dev = dev_get_by_name_rcu(net, name);
1249 if (no_module && capable(CAP_NET_ADMIN))
1250 no_module = request_module("netdev-%s", name);
1251 if (no_module && capable(CAP_SYS_MODULE)) {
1252 if (!request_module("%s", name))
1253 pr_warn("Loading kernel module for a network device with CAP_SYS_MODULE (deprecated). Use CAP_NET_ADMIN and alias netdev-%s instead.\n",
1257 EXPORT_SYMBOL(dev_load);
1259 static int __dev_open(struct net_device *dev)
1261 const struct net_device_ops *ops = dev->netdev_ops;
1266 if (!netif_device_present(dev))
1269 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1270 ret = notifier_to_errno(ret);
1274 set_bit(__LINK_STATE_START, &dev->state);
1276 if (ops->ndo_validate_addr)
1277 ret = ops->ndo_validate_addr(dev);
1279 if (!ret && ops->ndo_open)
1280 ret = ops->ndo_open(dev);
1283 clear_bit(__LINK_STATE_START, &dev->state);
1285 dev->flags |= IFF_UP;
1286 net_dmaengine_get();
1287 dev_set_rx_mode(dev);
1289 add_device_randomness(dev->dev_addr, dev->addr_len);
1296 * dev_open - prepare an interface for use.
1297 * @dev: device to open
1299 * Takes a device from down to up state. The device's private open
1300 * function is invoked and then the multicast lists are loaded. Finally
1301 * the device is moved into the up state and a %NETDEV_UP message is
1302 * sent to the netdev notifier chain.
1304 * Calling this function on an active interface is a nop. On a failure
1305 * a negative errno code is returned.
1307 int dev_open(struct net_device *dev)
1311 if (dev->flags & IFF_UP)
1314 ret = __dev_open(dev);
1318 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1319 call_netdevice_notifiers(NETDEV_UP, dev);
1323 EXPORT_SYMBOL(dev_open);
1325 static int __dev_close_many(struct list_head *head)
1327 struct net_device *dev;
1332 list_for_each_entry(dev, head, unreg_list) {
1333 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1335 clear_bit(__LINK_STATE_START, &dev->state);
1337 /* Synchronize to scheduled poll. We cannot touch poll list, it
1338 * can be even on different cpu. So just clear netif_running().
1340 * dev->stop() will invoke napi_disable() on all of it's
1341 * napi_struct instances on this device.
1343 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1346 dev_deactivate_many(head);
1348 list_for_each_entry(dev, head, unreg_list) {
1349 const struct net_device_ops *ops = dev->netdev_ops;
1352 * Call the device specific close. This cannot fail.
1353 * Only if device is UP
1355 * We allow it to be called even after a DETACH hot-plug
1361 dev->flags &= ~IFF_UP;
1362 net_dmaengine_put();
1368 static int __dev_close(struct net_device *dev)
1373 list_add(&dev->unreg_list, &single);
1374 retval = __dev_close_many(&single);
1379 static int dev_close_many(struct list_head *head)
1381 struct net_device *dev, *tmp;
1382 LIST_HEAD(tmp_list);
1384 list_for_each_entry_safe(dev, tmp, head, unreg_list)
1385 if (!(dev->flags & IFF_UP))
1386 list_move(&dev->unreg_list, &tmp_list);
1388 __dev_close_many(head);
1390 list_for_each_entry(dev, head, unreg_list) {
1391 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1392 call_netdevice_notifiers(NETDEV_DOWN, dev);
1395 /* rollback_registered_many needs the complete original list */
1396 list_splice(&tmp_list, head);
1401 * dev_close - shutdown an interface.
1402 * @dev: device to shutdown
1404 * This function moves an active device into down state. A
1405 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1406 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1409 int dev_close(struct net_device *dev)
1411 if (dev->flags & IFF_UP) {
1414 list_add(&dev->unreg_list, &single);
1415 dev_close_many(&single);
1420 EXPORT_SYMBOL(dev_close);
1424 * dev_disable_lro - disable Large Receive Offload on a device
1427 * Disable Large Receive Offload (LRO) on a net device. Must be
1428 * called under RTNL. This is needed if received packets may be
1429 * forwarded to another interface.
1431 void dev_disable_lro(struct net_device *dev)
1434 * If we're trying to disable lro on a vlan device
1435 * use the underlying physical device instead
1437 if (is_vlan_dev(dev))
1438 dev = vlan_dev_real_dev(dev);
1440 dev->wanted_features &= ~NETIF_F_LRO;
1441 netdev_update_features(dev);
1443 if (unlikely(dev->features & NETIF_F_LRO))
1444 netdev_WARN(dev, "failed to disable LRO!\n");
1446 EXPORT_SYMBOL(dev_disable_lro);
1449 static int dev_boot_phase = 1;
1452 * register_netdevice_notifier - register a network notifier block
1455 * Register a notifier to be called when network device events occur.
1456 * The notifier passed is linked into the kernel structures and must
1457 * not be reused until it has been unregistered. A negative errno code
1458 * is returned on a failure.
1460 * When registered all registration and up events are replayed
1461 * to the new notifier to allow device to have a race free
1462 * view of the network device list.
1465 int register_netdevice_notifier(struct notifier_block *nb)
1467 struct net_device *dev;
1468 struct net_device *last;
1473 err = raw_notifier_chain_register(&netdev_chain, nb);
1479 for_each_netdev(net, dev) {
1480 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1481 err = notifier_to_errno(err);
1485 if (!(dev->flags & IFF_UP))
1488 nb->notifier_call(nb, NETDEV_UP, dev);
1499 for_each_netdev(net, dev) {
1503 if (dev->flags & IFF_UP) {
1504 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1505 nb->notifier_call(nb, NETDEV_DOWN, dev);
1507 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
1512 raw_notifier_chain_unregister(&netdev_chain, nb);
1515 EXPORT_SYMBOL(register_netdevice_notifier);
1518 * unregister_netdevice_notifier - unregister a network notifier block
1521 * Unregister a notifier previously registered by
1522 * register_netdevice_notifier(). The notifier is unlinked into the
1523 * kernel structures and may then be reused. A negative errno code
1524 * is returned on a failure.
1526 * After unregistering unregister and down device events are synthesized
1527 * for all devices on the device list to the removed notifier to remove
1528 * the need for special case cleanup code.
1531 int unregister_netdevice_notifier(struct notifier_block *nb)
1533 struct net_device *dev;
1538 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1543 for_each_netdev(net, dev) {
1544 if (dev->flags & IFF_UP) {
1545 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1546 nb->notifier_call(nb, NETDEV_DOWN, dev);
1548 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
1555 EXPORT_SYMBOL(unregister_netdevice_notifier);
1558 * call_netdevice_notifiers - call all network notifier blocks
1559 * @val: value passed unmodified to notifier function
1560 * @dev: net_device pointer passed unmodified to notifier function
1562 * Call all network notifier blocks. Parameters and return value
1563 * are as for raw_notifier_call_chain().
1566 int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1569 return raw_notifier_call_chain(&netdev_chain, val, dev);
1571 EXPORT_SYMBOL(call_netdevice_notifiers);
1573 static struct static_key netstamp_needed __read_mostly;
1574 #ifdef HAVE_JUMP_LABEL
1575 /* We are not allowed to call static_key_slow_dec() from irq context
1576 * If net_disable_timestamp() is called from irq context, defer the
1577 * static_key_slow_dec() calls.
1579 static atomic_t netstamp_needed_deferred;
1582 void net_enable_timestamp(void)
1584 #ifdef HAVE_JUMP_LABEL
1585 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
1589 static_key_slow_dec(&netstamp_needed);
1593 WARN_ON(in_interrupt());
1594 static_key_slow_inc(&netstamp_needed);
1596 EXPORT_SYMBOL(net_enable_timestamp);
1598 void net_disable_timestamp(void)
1600 #ifdef HAVE_JUMP_LABEL
1601 if (in_interrupt()) {
1602 atomic_inc(&netstamp_needed_deferred);
1606 static_key_slow_dec(&netstamp_needed);
1608 EXPORT_SYMBOL(net_disable_timestamp);
1610 static inline void net_timestamp_set(struct sk_buff *skb)
1612 skb->tstamp.tv64 = 0;
1613 if (static_key_false(&netstamp_needed))
1614 __net_timestamp(skb);
1617 #define net_timestamp_check(COND, SKB) \
1618 if (static_key_false(&netstamp_needed)) { \
1619 if ((COND) && !(SKB)->tstamp.tv64) \
1620 __net_timestamp(SKB); \
1623 static int net_hwtstamp_validate(struct ifreq *ifr)
1625 struct hwtstamp_config cfg;
1626 enum hwtstamp_tx_types tx_type;
1627 enum hwtstamp_rx_filters rx_filter;
1628 int tx_type_valid = 0;
1629 int rx_filter_valid = 0;
1631 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1634 if (cfg.flags) /* reserved for future extensions */
1637 tx_type = cfg.tx_type;
1638 rx_filter = cfg.rx_filter;
1641 case HWTSTAMP_TX_OFF:
1642 case HWTSTAMP_TX_ON:
1643 case HWTSTAMP_TX_ONESTEP_SYNC:
1648 switch (rx_filter) {
1649 case HWTSTAMP_FILTER_NONE:
1650 case HWTSTAMP_FILTER_ALL:
1651 case HWTSTAMP_FILTER_SOME:
1652 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1653 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1654 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1655 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1656 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1657 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1658 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1659 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1660 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1661 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1662 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1663 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1664 rx_filter_valid = 1;
1668 if (!tx_type_valid || !rx_filter_valid)
1674 static inline bool is_skb_forwardable(struct net_device *dev,
1675 struct sk_buff *skb)
1679 if (!(dev->flags & IFF_UP))
1682 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1683 if (skb->len <= len)
1686 /* if TSO is enabled, we don't care about the length as the packet
1687 * could be forwarded without being segmented before
1689 if (skb_is_gso(skb))
1696 * dev_forward_skb - loopback an skb to another netif
1698 * @dev: destination network device
1699 * @skb: buffer to forward
1702 * NET_RX_SUCCESS (no congestion)
1703 * NET_RX_DROP (packet was dropped, but freed)
1705 * dev_forward_skb can be used for injecting an skb from the
1706 * start_xmit function of one device into the receive queue
1707 * of another device.
1709 * The receiving device may be in another namespace, so
1710 * we have to clear all information in the skb that could
1711 * impact namespace isolation.
1713 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1715 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
1716 if (skb_copy_ubufs(skb, GFP_ATOMIC)) {
1717 atomic_long_inc(&dev->rx_dropped);
1726 if (unlikely(!is_skb_forwardable(dev, skb))) {
1727 atomic_long_inc(&dev->rx_dropped);
1734 skb->tstamp.tv64 = 0;
1735 skb->pkt_type = PACKET_HOST;
1736 skb->protocol = eth_type_trans(skb, dev);
1740 return netif_rx(skb);
1742 EXPORT_SYMBOL_GPL(dev_forward_skb);
1744 static inline int deliver_skb(struct sk_buff *skb,
1745 struct packet_type *pt_prev,
1746 struct net_device *orig_dev)
1748 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
1750 atomic_inc(&skb->users);
1751 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1754 static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1756 if (!ptype->af_packet_priv || !skb->sk)
1759 if (ptype->id_match)
1760 return ptype->id_match(ptype, skb->sk);
1761 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1768 * Support routine. Sends outgoing frames to any network
1769 * taps currently in use.
1772 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1774 struct packet_type *ptype;
1775 struct sk_buff *skb2 = NULL;
1776 struct packet_type *pt_prev = NULL;
1779 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1780 /* Never send packets back to the socket
1781 * they originated from - MvS (miquels@drinkel.ow.org)
1783 if ((ptype->dev == dev || !ptype->dev) &&
1784 (!skb_loop_sk(ptype, skb))) {
1786 deliver_skb(skb2, pt_prev, skb->dev);
1791 skb2 = skb_clone(skb, GFP_ATOMIC);
1795 net_timestamp_set(skb2);
1797 /* skb->nh should be correctly
1798 set by sender, so that the second statement is
1799 just protection against buggy protocols.
1801 skb_reset_mac_header(skb2);
1803 if (skb_network_header(skb2) < skb2->data ||
1804 skb2->network_header > skb2->tail) {
1805 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1806 ntohs(skb2->protocol),
1808 skb_reset_network_header(skb2);
1811 skb2->transport_header = skb2->network_header;
1812 skb2->pkt_type = PACKET_OUTGOING;
1817 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1822 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
1823 * @dev: Network device
1824 * @txq: number of queues available
1826 * If real_num_tx_queues is changed the tc mappings may no longer be
1827 * valid. To resolve this verify the tc mapping remains valid and if
1828 * not NULL the mapping. With no priorities mapping to this
1829 * offset/count pair it will no longer be used. In the worst case TC0
1830 * is invalid nothing can be done so disable priority mappings. If is
1831 * expected that drivers will fix this mapping if they can before
1832 * calling netif_set_real_num_tx_queues.
1834 static void netif_setup_tc(struct net_device *dev, unsigned int txq)
1837 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1839 /* If TC0 is invalidated disable TC mapping */
1840 if (tc->offset + tc->count > txq) {
1841 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
1846 /* Invalidated prio to tc mappings set to TC0 */
1847 for (i = 1; i < TC_BITMASK + 1; i++) {
1848 int q = netdev_get_prio_tc_map(dev, i);
1850 tc = &dev->tc_to_txq[q];
1851 if (tc->offset + tc->count > txq) {
1852 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
1854 netdev_set_prio_tc_map(dev, i, 0);
1860 static DEFINE_MUTEX(xps_map_mutex);
1861 #define xmap_dereference(P) \
1862 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1864 static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps,
1867 struct xps_map *map = NULL;
1871 map = xmap_dereference(dev_maps->cpu_map[cpu]);
1873 for (pos = 0; map && pos < map->len; pos++) {
1874 if (map->queues[pos] == index) {
1876 map->queues[pos] = map->queues[--map->len];
1878 RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL);
1879 kfree_rcu(map, rcu);
1889 static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
1891 struct xps_dev_maps *dev_maps;
1893 bool active = false;
1895 mutex_lock(&xps_map_mutex);
1896 dev_maps = xmap_dereference(dev->xps_maps);
1901 for_each_possible_cpu(cpu) {
1902 for (i = index; i < dev->num_tx_queues; i++) {
1903 if (!remove_xps_queue(dev_maps, cpu, i))
1906 if (i == dev->num_tx_queues)
1911 RCU_INIT_POINTER(dev->xps_maps, NULL);
1912 kfree_rcu(dev_maps, rcu);
1915 for (i = index; i < dev->num_tx_queues; i++)
1916 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
1920 mutex_unlock(&xps_map_mutex);
1923 static struct xps_map *expand_xps_map(struct xps_map *map,
1926 struct xps_map *new_map;
1927 int alloc_len = XPS_MIN_MAP_ALLOC;
1930 for (pos = 0; map && pos < map->len; pos++) {
1931 if (map->queues[pos] != index)
1936 /* Need to add queue to this CPU's existing map */
1938 if (pos < map->alloc_len)
1941 alloc_len = map->alloc_len * 2;
1944 /* Need to allocate new map to store queue on this CPU's map */
1945 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
1950 for (i = 0; i < pos; i++)
1951 new_map->queues[i] = map->queues[i];
1952 new_map->alloc_len = alloc_len;
1958 int netif_set_xps_queue(struct net_device *dev, struct cpumask *mask, u16 index)
1960 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
1961 struct xps_map *map, *new_map;
1962 int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES);
1963 int cpu, numa_node_id = -2;
1964 bool active = false;
1966 mutex_lock(&xps_map_mutex);
1968 dev_maps = xmap_dereference(dev->xps_maps);
1970 /* allocate memory for queue storage */
1971 for_each_online_cpu(cpu) {
1972 if (!cpumask_test_cpu(cpu, mask))
1976 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
1980 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
1983 map = expand_xps_map(map, cpu, index);
1987 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
1991 goto out_no_new_maps;
1993 for_each_possible_cpu(cpu) {
1994 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
1995 /* add queue to CPU maps */
1998 map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
1999 while ((pos < map->len) && (map->queues[pos] != index))
2002 if (pos == map->len)
2003 map->queues[map->len++] = index;
2005 if (numa_node_id == -2)
2006 numa_node_id = cpu_to_node(cpu);
2007 else if (numa_node_id != cpu_to_node(cpu))
2010 } else if (dev_maps) {
2011 /* fill in the new device map from the old device map */
2012 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2013 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
2018 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2020 /* Cleanup old maps */
2022 for_each_possible_cpu(cpu) {
2023 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2024 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2025 if (map && map != new_map)
2026 kfree_rcu(map, rcu);
2029 kfree_rcu(dev_maps, rcu);
2032 dev_maps = new_dev_maps;
2036 /* update Tx queue numa node */
2037 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2038 (numa_node_id >= 0) ? numa_node_id :
2044 /* removes queue from unused CPUs */
2045 for_each_possible_cpu(cpu) {
2046 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu))
2049 if (remove_xps_queue(dev_maps, cpu, index))
2053 /* free map if not active */
2055 RCU_INIT_POINTER(dev->xps_maps, NULL);
2056 kfree_rcu(dev_maps, rcu);
2060 mutex_unlock(&xps_map_mutex);
2064 /* remove any maps that we added */
2065 for_each_possible_cpu(cpu) {
2066 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2067 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
2069 if (new_map && new_map != map)
2073 mutex_unlock(&xps_map_mutex);
2075 kfree(new_dev_maps);
2078 EXPORT_SYMBOL(netif_set_xps_queue);
2082 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2083 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2085 int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
2089 if (txq < 1 || txq > dev->num_tx_queues)
2092 if (dev->reg_state == NETREG_REGISTERED ||
2093 dev->reg_state == NETREG_UNREGISTERING) {
2096 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2102 netif_setup_tc(dev, txq);
2104 if (txq < dev->real_num_tx_queues) {
2105 qdisc_reset_all_tx_gt(dev, txq);
2107 netif_reset_xps_queues_gt(dev, txq);
2112 dev->real_num_tx_queues = txq;
2115 EXPORT_SYMBOL(netif_set_real_num_tx_queues);
2119 * netif_set_real_num_rx_queues - set actual number of RX queues used
2120 * @dev: Network device
2121 * @rxq: Actual number of RX queues
2123 * This must be called either with the rtnl_lock held or before
2124 * registration of the net device. Returns 0 on success, or a
2125 * negative error code. If called before registration, it always
2128 int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2132 if (rxq < 1 || rxq > dev->num_rx_queues)
2135 if (dev->reg_state == NETREG_REGISTERED) {
2138 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2144 dev->real_num_rx_queues = rxq;
2147 EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2151 * netif_get_num_default_rss_queues - default number of RSS queues
2153 * This routine should set an upper limit on the number of RSS queues
2154 * used by default by multiqueue devices.
2156 int netif_get_num_default_rss_queues(void)
2158 return min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
2160 EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2162 static inline void __netif_reschedule(struct Qdisc *q)
2164 struct softnet_data *sd;
2165 unsigned long flags;
2167 local_irq_save(flags);
2168 sd = &__get_cpu_var(softnet_data);
2169 q->next_sched = NULL;
2170 *sd->output_queue_tailp = q;
2171 sd->output_queue_tailp = &q->next_sched;
2172 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2173 local_irq_restore(flags);
2176 void __netif_schedule(struct Qdisc *q)
2178 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2179 __netif_reschedule(q);
2181 EXPORT_SYMBOL(__netif_schedule);
2183 void dev_kfree_skb_irq(struct sk_buff *skb)
2185 if (atomic_dec_and_test(&skb->users)) {
2186 struct softnet_data *sd;
2187 unsigned long flags;
2189 local_irq_save(flags);
2190 sd = &__get_cpu_var(softnet_data);
2191 skb->next = sd->completion_queue;
2192 sd->completion_queue = skb;
2193 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2194 local_irq_restore(flags);
2197 EXPORT_SYMBOL(dev_kfree_skb_irq);
2199 void dev_kfree_skb_any(struct sk_buff *skb)
2201 if (in_irq() || irqs_disabled())
2202 dev_kfree_skb_irq(skb);
2206 EXPORT_SYMBOL(dev_kfree_skb_any);
2210 * netif_device_detach - mark device as removed
2211 * @dev: network device
2213 * Mark device as removed from system and therefore no longer available.
2215 void netif_device_detach(struct net_device *dev)
2217 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2218 netif_running(dev)) {
2219 netif_tx_stop_all_queues(dev);
2222 EXPORT_SYMBOL(netif_device_detach);
2225 * netif_device_attach - mark device as attached
2226 * @dev: network device
2228 * Mark device as attached from system and restart if needed.
2230 void netif_device_attach(struct net_device *dev)
2232 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2233 netif_running(dev)) {
2234 netif_tx_wake_all_queues(dev);
2235 __netdev_watchdog_up(dev);
2238 EXPORT_SYMBOL(netif_device_attach);
2240 static void skb_warn_bad_offload(const struct sk_buff *skb)
2242 static const netdev_features_t null_features = 0;
2243 struct net_device *dev = skb->dev;
2244 const char *driver = "";
2246 if (dev && dev->dev.parent)
2247 driver = dev_driver_string(dev->dev.parent);
2249 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2250 "gso_type=%d ip_summed=%d\n",
2251 driver, dev ? &dev->features : &null_features,
2252 skb->sk ? &skb->sk->sk_route_caps : &null_features,
2253 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2254 skb_shinfo(skb)->gso_type, skb->ip_summed);
2258 * Invalidate hardware checksum when packet is to be mangled, and
2259 * complete checksum manually on outgoing path.
2261 int skb_checksum_help(struct sk_buff *skb)
2264 int ret = 0, offset;
2266 if (skb->ip_summed == CHECKSUM_COMPLETE)
2267 goto out_set_summed;
2269 if (unlikely(skb_shinfo(skb)->gso_size)) {
2270 skb_warn_bad_offload(skb);
2274 offset = skb_checksum_start_offset(skb);
2275 BUG_ON(offset >= skb_headlen(skb));
2276 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2278 offset += skb->csum_offset;
2279 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2281 if (skb_cloned(skb) &&
2282 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
2283 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2288 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
2290 skb->ip_summed = CHECKSUM_NONE;
2294 EXPORT_SYMBOL(skb_checksum_help);
2297 * skb_gso_segment - Perform segmentation on skb.
2298 * @skb: buffer to segment
2299 * @features: features for the output path (see dev->features)
2301 * This function segments the given skb and returns a list of segments.
2303 * It may return NULL if the skb requires no segmentation. This is
2304 * only possible when GSO is used for verifying header integrity.
2306 struct sk_buff *skb_gso_segment(struct sk_buff *skb,
2307 netdev_features_t features)
2309 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2310 struct packet_offload *ptype;
2311 __be16 type = skb->protocol;
2312 int vlan_depth = ETH_HLEN;
2315 while (type == htons(ETH_P_8021Q)) {
2316 struct vlan_hdr *vh;
2318 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
2319 return ERR_PTR(-EINVAL);
2321 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
2322 type = vh->h_vlan_encapsulated_proto;
2323 vlan_depth += VLAN_HLEN;
2326 skb_reset_mac_header(skb);
2327 skb->mac_len = skb->network_header - skb->mac_header;
2328 __skb_pull(skb, skb->mac_len);
2330 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
2331 skb_warn_bad_offload(skb);
2333 if (skb_header_cloned(skb) &&
2334 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
2335 return ERR_PTR(err);
2339 list_for_each_entry_rcu(ptype, &offload_base, list) {
2340 if (ptype->type == type && ptype->callbacks.gso_segment) {
2341 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
2342 err = ptype->callbacks.gso_send_check(skb);
2343 segs = ERR_PTR(err);
2344 if (err || skb_gso_ok(skb, features))
2346 __skb_push(skb, (skb->data -
2347 skb_network_header(skb)));
2349 segs = ptype->callbacks.gso_segment(skb, features);
2355 __skb_push(skb, skb->data - skb_mac_header(skb));
2359 EXPORT_SYMBOL(skb_gso_segment);
2361 /* Take action when hardware reception checksum errors are detected. */
2363 void netdev_rx_csum_fault(struct net_device *dev)
2365 if (net_ratelimit()) {
2366 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
2370 EXPORT_SYMBOL(netdev_rx_csum_fault);
2373 /* Actually, we should eliminate this check as soon as we know, that:
2374 * 1. IOMMU is present and allows to map all the memory.
2375 * 2. No high memory really exists on this machine.
2378 static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
2380 #ifdef CONFIG_HIGHMEM
2382 if (!(dev->features & NETIF_F_HIGHDMA)) {
2383 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2384 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2385 if (PageHighMem(skb_frag_page(frag)))
2390 if (PCI_DMA_BUS_IS_PHYS) {
2391 struct device *pdev = dev->dev.parent;
2395 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2396 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2397 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
2398 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2407 void (*destructor)(struct sk_buff *skb);
2410 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
2412 static void dev_gso_skb_destructor(struct sk_buff *skb)
2414 struct dev_gso_cb *cb;
2417 struct sk_buff *nskb = skb->next;
2419 skb->next = nskb->next;
2422 } while (skb->next);
2424 cb = DEV_GSO_CB(skb);
2426 cb->destructor(skb);
2430 * dev_gso_segment - Perform emulated hardware segmentation on skb.
2431 * @skb: buffer to segment
2432 * @features: device features as applicable to this skb
2434 * This function segments the given skb and stores the list of segments
2437 static int dev_gso_segment(struct sk_buff *skb, netdev_features_t features)
2439 struct sk_buff *segs;
2441 segs = skb_gso_segment(skb, features);
2443 /* Verifying header integrity only. */
2448 return PTR_ERR(segs);
2451 DEV_GSO_CB(skb)->destructor = skb->destructor;
2452 skb->destructor = dev_gso_skb_destructor;
2457 static bool can_checksum_protocol(netdev_features_t features, __be16 protocol)
2459 return ((features & NETIF_F_GEN_CSUM) ||
2460 ((features & NETIF_F_V4_CSUM) &&
2461 protocol == htons(ETH_P_IP)) ||
2462 ((features & NETIF_F_V6_CSUM) &&
2463 protocol == htons(ETH_P_IPV6)) ||
2464 ((features & NETIF_F_FCOE_CRC) &&
2465 protocol == htons(ETH_P_FCOE)));
2468 static netdev_features_t harmonize_features(struct sk_buff *skb,
2469 __be16 protocol, netdev_features_t features)
2471 if (skb->ip_summed != CHECKSUM_NONE &&
2472 !can_checksum_protocol(features, protocol)) {
2473 features &= ~NETIF_F_ALL_CSUM;
2474 features &= ~NETIF_F_SG;
2475 } else if (illegal_highdma(skb->dev, skb)) {
2476 features &= ~NETIF_F_SG;
2482 netdev_features_t netif_skb_features(struct sk_buff *skb)
2484 __be16 protocol = skb->protocol;
2485 netdev_features_t features = skb->dev->features;
2487 if (skb_shinfo(skb)->gso_segs > skb->dev->gso_max_segs)
2488 features &= ~NETIF_F_GSO_MASK;
2490 if (protocol == htons(ETH_P_8021Q)) {
2491 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
2492 protocol = veh->h_vlan_encapsulated_proto;
2493 } else if (!vlan_tx_tag_present(skb)) {
2494 return harmonize_features(skb, protocol, features);
2497 features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_TX);
2499 if (protocol != htons(ETH_P_8021Q)) {
2500 return harmonize_features(skb, protocol, features);
2502 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
2503 NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_TX;
2504 return harmonize_features(skb, protocol, features);
2507 EXPORT_SYMBOL(netif_skb_features);
2510 * Returns true if either:
2511 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
2512 * 2. skb is fragmented and the device does not support SG.
2514 static inline int skb_needs_linearize(struct sk_buff *skb,
2517 return skb_is_nonlinear(skb) &&
2518 ((skb_has_frag_list(skb) &&
2519 !(features & NETIF_F_FRAGLIST)) ||
2520 (skb_shinfo(skb)->nr_frags &&
2521 !(features & NETIF_F_SG)));
2524 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2525 struct netdev_queue *txq)
2527 const struct net_device_ops *ops = dev->netdev_ops;
2528 int rc = NETDEV_TX_OK;
2529 unsigned int skb_len;
2531 if (likely(!skb->next)) {
2532 netdev_features_t features;
2535 * If device doesn't need skb->dst, release it right now while
2536 * its hot in this cpu cache
2538 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2541 features = netif_skb_features(skb);
2543 if (vlan_tx_tag_present(skb) &&
2544 !(features & NETIF_F_HW_VLAN_TX)) {
2545 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
2552 /* If encapsulation offload request, verify we are testing
2553 * hardware encapsulation features instead of standard
2554 * features for the netdev
2556 if (skb->encapsulation)
2557 features &= dev->hw_enc_features;
2559 if (netif_needs_gso(skb, features)) {
2560 if (unlikely(dev_gso_segment(skb, features)))
2565 if (skb_needs_linearize(skb, features) &&
2566 __skb_linearize(skb))
2569 /* If packet is not checksummed and device does not
2570 * support checksumming for this protocol, complete
2571 * checksumming here.
2573 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2574 if (skb->encapsulation)
2575 skb_set_inner_transport_header(skb,
2576 skb_checksum_start_offset(skb));
2578 skb_set_transport_header(skb,
2579 skb_checksum_start_offset(skb));
2580 if (!(features & NETIF_F_ALL_CSUM) &&
2581 skb_checksum_help(skb))
2586 if (!list_empty(&ptype_all))
2587 dev_queue_xmit_nit(skb, dev);
2590 rc = ops->ndo_start_xmit(skb, dev);
2591 trace_net_dev_xmit(skb, rc, dev, skb_len);
2592 if (rc == NETDEV_TX_OK)
2593 txq_trans_update(txq);
2599 struct sk_buff *nskb = skb->next;
2601 skb->next = nskb->next;
2605 * If device doesn't need nskb->dst, release it right now while
2606 * its hot in this cpu cache
2608 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2611 if (!list_empty(&ptype_all))
2612 dev_queue_xmit_nit(nskb, dev);
2614 skb_len = nskb->len;
2615 rc = ops->ndo_start_xmit(nskb, dev);
2616 trace_net_dev_xmit(nskb, rc, dev, skb_len);
2617 if (unlikely(rc != NETDEV_TX_OK)) {
2618 if (rc & ~NETDEV_TX_MASK)
2619 goto out_kfree_gso_skb;
2620 nskb->next = skb->next;
2624 txq_trans_update(txq);
2625 if (unlikely(netif_xmit_stopped(txq) && skb->next))
2626 return NETDEV_TX_BUSY;
2627 } while (skb->next);
2630 if (likely(skb->next == NULL))
2631 skb->destructor = DEV_GSO_CB(skb)->destructor;
2638 static void qdisc_pkt_len_init(struct sk_buff *skb)
2640 const struct skb_shared_info *shinfo = skb_shinfo(skb);
2642 qdisc_skb_cb(skb)->pkt_len = skb->len;
2644 /* To get more precise estimation of bytes sent on wire,
2645 * we add to pkt_len the headers size of all segments
2647 if (shinfo->gso_size) {
2648 unsigned int hdr_len;
2650 /* mac layer + network layer */
2651 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
2653 /* + transport layer */
2654 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
2655 hdr_len += tcp_hdrlen(skb);
2657 hdr_len += sizeof(struct udphdr);
2658 qdisc_skb_cb(skb)->pkt_len += (shinfo->gso_segs - 1) * hdr_len;
2662 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2663 struct net_device *dev,
2664 struct netdev_queue *txq)
2666 spinlock_t *root_lock = qdisc_lock(q);
2670 qdisc_pkt_len_init(skb);
2671 qdisc_calculate_pkt_len(skb, q);
2673 * Heuristic to force contended enqueues to serialize on a
2674 * separate lock before trying to get qdisc main lock.
2675 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2676 * and dequeue packets faster.
2678 contended = qdisc_is_running(q);
2679 if (unlikely(contended))
2680 spin_lock(&q->busylock);
2682 spin_lock(root_lock);
2683 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2686 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
2687 qdisc_run_begin(q)) {
2689 * This is a work-conserving queue; there are no old skbs
2690 * waiting to be sent out; and the qdisc is not running -
2691 * xmit the skb directly.
2693 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2696 qdisc_bstats_update(q, skb);
2698 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2699 if (unlikely(contended)) {
2700 spin_unlock(&q->busylock);
2707 rc = NET_XMIT_SUCCESS;
2710 rc = q->enqueue(skb, q) & NET_XMIT_MASK;
2711 if (qdisc_run_begin(q)) {
2712 if (unlikely(contended)) {
2713 spin_unlock(&q->busylock);
2719 spin_unlock(root_lock);
2720 if (unlikely(contended))
2721 spin_unlock(&q->busylock);
2725 #if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
2726 static void skb_update_prio(struct sk_buff *skb)
2728 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
2730 if (!skb->priority && skb->sk && map) {
2731 unsigned int prioidx = skb->sk->sk_cgrp_prioidx;
2733 if (prioidx < map->priomap_len)
2734 skb->priority = map->priomap[prioidx];
2738 #define skb_update_prio(skb)
2741 static DEFINE_PER_CPU(int, xmit_recursion);
2742 #define RECURSION_LIMIT 10
2745 * dev_loopback_xmit - loop back @skb
2746 * @skb: buffer to transmit
2748 int dev_loopback_xmit(struct sk_buff *skb)
2750 skb_reset_mac_header(skb);
2751 __skb_pull(skb, skb_network_offset(skb));
2752 skb->pkt_type = PACKET_LOOPBACK;
2753 skb->ip_summed = CHECKSUM_UNNECESSARY;
2754 WARN_ON(!skb_dst(skb));
2759 EXPORT_SYMBOL(dev_loopback_xmit);
2762 * dev_queue_xmit - transmit a buffer
2763 * @skb: buffer to transmit
2765 * Queue a buffer for transmission to a network device. The caller must
2766 * have set the device and priority and built the buffer before calling
2767 * this function. The function can be called from an interrupt.
2769 * A negative errno code is returned on a failure. A success does not
2770 * guarantee the frame will be transmitted as it may be dropped due
2771 * to congestion or traffic shaping.
2773 * -----------------------------------------------------------------------------------
2774 * I notice this method can also return errors from the queue disciplines,
2775 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2778 * Regardless of the return value, the skb is consumed, so it is currently
2779 * difficult to retry a send to this method. (You can bump the ref count
2780 * before sending to hold a reference for retry if you are careful.)
2782 * When calling this method, interrupts MUST be enabled. This is because
2783 * the BH enable code must have IRQs enabled so that it will not deadlock.
2786 int dev_queue_xmit(struct sk_buff *skb)
2788 struct net_device *dev = skb->dev;
2789 struct netdev_queue *txq;
2793 /* Disable soft irqs for various locks below. Also
2794 * stops preemption for RCU.
2798 skb_update_prio(skb);
2800 txq = netdev_pick_tx(dev, skb);
2801 q = rcu_dereference_bh(txq->qdisc);
2803 #ifdef CONFIG_NET_CLS_ACT
2804 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
2806 trace_net_dev_queue(skb);
2808 rc = __dev_xmit_skb(skb, q, dev, txq);
2812 /* The device has no queue. Common case for software devices:
2813 loopback, all the sorts of tunnels...
2815 Really, it is unlikely that netif_tx_lock protection is necessary
2816 here. (f.e. loopback and IP tunnels are clean ignoring statistics
2818 However, it is possible, that they rely on protection
2821 Check this and shot the lock. It is not prone from deadlocks.
2822 Either shot noqueue qdisc, it is even simpler 8)
2824 if (dev->flags & IFF_UP) {
2825 int cpu = smp_processor_id(); /* ok because BHs are off */
2827 if (txq->xmit_lock_owner != cpu) {
2829 if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
2830 goto recursion_alert;
2832 HARD_TX_LOCK(dev, txq, cpu);
2834 if (!netif_xmit_stopped(txq)) {
2835 __this_cpu_inc(xmit_recursion);
2836 rc = dev_hard_start_xmit(skb, dev, txq);
2837 __this_cpu_dec(xmit_recursion);
2838 if (dev_xmit_complete(rc)) {
2839 HARD_TX_UNLOCK(dev, txq);
2843 HARD_TX_UNLOCK(dev, txq);
2844 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
2847 /* Recursion is detected! It is possible,
2851 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
2857 rcu_read_unlock_bh();
2862 rcu_read_unlock_bh();
2865 EXPORT_SYMBOL(dev_queue_xmit);
2868 /*=======================================================================
2870 =======================================================================*/
2872 int netdev_max_backlog __read_mostly = 1000;
2873 EXPORT_SYMBOL(netdev_max_backlog);
2875 int netdev_tstamp_prequeue __read_mostly = 1;
2876 int netdev_budget __read_mostly = 300;
2877 int weight_p __read_mostly = 64; /* old backlog weight */
2879 /* Called with irq disabled */
2880 static inline void ____napi_schedule(struct softnet_data *sd,
2881 struct napi_struct *napi)
2883 list_add_tail(&napi->poll_list, &sd->poll_list);
2884 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2889 /* One global table that all flow-based protocols share. */
2890 struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
2891 EXPORT_SYMBOL(rps_sock_flow_table);
2893 struct static_key rps_needed __read_mostly;
2895 static struct rps_dev_flow *
2896 set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2897 struct rps_dev_flow *rflow, u16 next_cpu)
2899 if (next_cpu != RPS_NO_CPU) {
2900 #ifdef CONFIG_RFS_ACCEL
2901 struct netdev_rx_queue *rxqueue;
2902 struct rps_dev_flow_table *flow_table;
2903 struct rps_dev_flow *old_rflow;
2908 /* Should we steer this flow to a different hardware queue? */
2909 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
2910 !(dev->features & NETIF_F_NTUPLE))
2912 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
2913 if (rxq_index == skb_get_rx_queue(skb))
2916 rxqueue = dev->_rx + rxq_index;
2917 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2920 flow_id = skb->rxhash & flow_table->mask;
2921 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
2922 rxq_index, flow_id);
2926 rflow = &flow_table->flows[flow_id];
2928 if (old_rflow->filter == rflow->filter)
2929 old_rflow->filter = RPS_NO_FILTER;
2933 per_cpu(softnet_data, next_cpu).input_queue_head;
2936 rflow->cpu = next_cpu;
2941 * get_rps_cpu is called from netif_receive_skb and returns the target
2942 * CPU from the RPS map of the receiving queue for a given skb.
2943 * rcu_read_lock must be held on entry.
2945 static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2946 struct rps_dev_flow **rflowp)
2948 struct netdev_rx_queue *rxqueue;
2949 struct rps_map *map;
2950 struct rps_dev_flow_table *flow_table;
2951 struct rps_sock_flow_table *sock_flow_table;
2955 if (skb_rx_queue_recorded(skb)) {
2956 u16 index = skb_get_rx_queue(skb);
2957 if (unlikely(index >= dev->real_num_rx_queues)) {
2958 WARN_ONCE(dev->real_num_rx_queues > 1,
2959 "%s received packet on queue %u, but number "
2960 "of RX queues is %u\n",
2961 dev->name, index, dev->real_num_rx_queues);
2964 rxqueue = dev->_rx + index;
2968 map = rcu_dereference(rxqueue->rps_map);
2970 if (map->len == 1 &&
2971 !rcu_access_pointer(rxqueue->rps_flow_table)) {
2972 tcpu = map->cpus[0];
2973 if (cpu_online(tcpu))
2977 } else if (!rcu_access_pointer(rxqueue->rps_flow_table)) {
2981 skb_reset_network_header(skb);
2982 if (!skb_get_rxhash(skb))
2985 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2986 sock_flow_table = rcu_dereference(rps_sock_flow_table);
2987 if (flow_table && sock_flow_table) {
2989 struct rps_dev_flow *rflow;
2991 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
2994 next_cpu = sock_flow_table->ents[skb->rxhash &
2995 sock_flow_table->mask];
2998 * If the desired CPU (where last recvmsg was done) is
2999 * different from current CPU (one in the rx-queue flow
3000 * table entry), switch if one of the following holds:
3001 * - Current CPU is unset (equal to RPS_NO_CPU).
3002 * - Current CPU is offline.
3003 * - The current CPU's queue tail has advanced beyond the
3004 * last packet that was enqueued using this table entry.
3005 * This guarantees that all previous packets for the flow
3006 * have been dequeued, thus preserving in order delivery.
3008 if (unlikely(tcpu != next_cpu) &&
3009 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
3010 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
3011 rflow->last_qtail)) >= 0)) {
3013 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
3016 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
3024 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
3026 if (cpu_online(tcpu)) {
3036 #ifdef CONFIG_RFS_ACCEL
3039 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3040 * @dev: Device on which the filter was set
3041 * @rxq_index: RX queue index
3042 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3043 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3045 * Drivers that implement ndo_rx_flow_steer() should periodically call
3046 * this function for each installed filter and remove the filters for
3047 * which it returns %true.
3049 bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3050 u32 flow_id, u16 filter_id)
3052 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3053 struct rps_dev_flow_table *flow_table;
3054 struct rps_dev_flow *rflow;
3059 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3060 if (flow_table && flow_id <= flow_table->mask) {
3061 rflow = &flow_table->flows[flow_id];
3062 cpu = ACCESS_ONCE(rflow->cpu);
3063 if (rflow->filter == filter_id && cpu != RPS_NO_CPU &&
3064 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3065 rflow->last_qtail) <
3066 (int)(10 * flow_table->mask)))
3072 EXPORT_SYMBOL(rps_may_expire_flow);
3074 #endif /* CONFIG_RFS_ACCEL */
3076 /* Called from hardirq (IPI) context */
3077 static void rps_trigger_softirq(void *data)
3079 struct softnet_data *sd = data;
3081 ____napi_schedule(sd, &sd->backlog);
3085 #endif /* CONFIG_RPS */
3088 * Check if this softnet_data structure is another cpu one
3089 * If yes, queue it to our IPI list and return 1
3092 static int rps_ipi_queued(struct softnet_data *sd)
3095 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
3098 sd->rps_ipi_next = mysd->rps_ipi_list;
3099 mysd->rps_ipi_list = sd;
3101 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3104 #endif /* CONFIG_RPS */
3109 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3110 * queue (may be a remote CPU queue).
3112 static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3113 unsigned int *qtail)
3115 struct softnet_data *sd;
3116 unsigned long flags;
3118 sd = &per_cpu(softnet_data, cpu);
3120 local_irq_save(flags);
3123 if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
3124 if (skb_queue_len(&sd->input_pkt_queue)) {
3126 __skb_queue_tail(&sd->input_pkt_queue, skb);
3127 input_queue_tail_incr_save(sd, qtail);
3129 local_irq_restore(flags);
3130 return NET_RX_SUCCESS;
3133 /* Schedule NAPI for backlog device
3134 * We can use non atomic operation since we own the queue lock
3136 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
3137 if (!rps_ipi_queued(sd))
3138 ____napi_schedule(sd, &sd->backlog);
3146 local_irq_restore(flags);
3148 atomic_long_inc(&skb->dev->rx_dropped);
3154 * netif_rx - post buffer to the network code
3155 * @skb: buffer to post
3157 * This function receives a packet from a device driver and queues it for
3158 * the upper (protocol) levels to process. It always succeeds. The buffer
3159 * may be dropped during processing for congestion control or by the
3163 * NET_RX_SUCCESS (no congestion)
3164 * NET_RX_DROP (packet was dropped)
3168 int netif_rx(struct sk_buff *skb)
3172 /* if netpoll wants it, pretend we never saw it */
3173 if (netpoll_rx(skb))
3176 net_timestamp_check(netdev_tstamp_prequeue, skb);
3178 trace_netif_rx(skb);
3180 if (static_key_false(&rps_needed)) {
3181 struct rps_dev_flow voidflow, *rflow = &voidflow;
3187 cpu = get_rps_cpu(skb->dev, skb, &rflow);
3189 cpu = smp_processor_id();
3191 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3199 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
3204 EXPORT_SYMBOL(netif_rx);
3206 int netif_rx_ni(struct sk_buff *skb)
3211 err = netif_rx(skb);
3212 if (local_softirq_pending())
3218 EXPORT_SYMBOL(netif_rx_ni);
3220 static void net_tx_action(struct softirq_action *h)
3222 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3224 if (sd->completion_queue) {
3225 struct sk_buff *clist;
3227 local_irq_disable();
3228 clist = sd->completion_queue;
3229 sd->completion_queue = NULL;
3233 struct sk_buff *skb = clist;
3234 clist = clist->next;
3236 WARN_ON(atomic_read(&skb->users));
3237 trace_kfree_skb(skb, net_tx_action);
3242 if (sd->output_queue) {
3245 local_irq_disable();
3246 head = sd->output_queue;
3247 sd->output_queue = NULL;
3248 sd->output_queue_tailp = &sd->output_queue;
3252 struct Qdisc *q = head;
3253 spinlock_t *root_lock;
3255 head = head->next_sched;
3257 root_lock = qdisc_lock(q);
3258 if (spin_trylock(root_lock)) {
3259 smp_mb__before_clear_bit();
3260 clear_bit(__QDISC_STATE_SCHED,
3263 spin_unlock(root_lock);
3265 if (!test_bit(__QDISC_STATE_DEACTIVATED,
3267 __netif_reschedule(q);
3269 smp_mb__before_clear_bit();
3270 clear_bit(__QDISC_STATE_SCHED,
3278 #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
3279 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
3280 /* This hook is defined here for ATM LANE */
3281 int (*br_fdb_test_addr_hook)(struct net_device *dev,
3282 unsigned char *addr) __read_mostly;
3283 EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
3286 #ifdef CONFIG_NET_CLS_ACT
3287 /* TODO: Maybe we should just force sch_ingress to be compiled in
3288 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
3289 * a compare and 2 stores extra right now if we dont have it on
3290 * but have CONFIG_NET_CLS_ACT
3291 * NOTE: This doesn't stop any functionality; if you dont have
3292 * the ingress scheduler, you just can't add policies on ingress.
3295 static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq)
3297 struct net_device *dev = skb->dev;
3298 u32 ttl = G_TC_RTTL(skb->tc_verd);
3299 int result = TC_ACT_OK;
3302 if (unlikely(MAX_RED_LOOP < ttl++)) {
3303 net_warn_ratelimited("Redir loop detected Dropping packet (%d->%d)\n",
3304 skb->skb_iif, dev->ifindex);
3308 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
3309 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
3312 if (q != &noop_qdisc) {
3313 spin_lock(qdisc_lock(q));
3314 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
3315 result = qdisc_enqueue_root(skb, q);
3316 spin_unlock(qdisc_lock(q));
3322 static inline struct sk_buff *handle_ing(struct sk_buff *skb,
3323 struct packet_type **pt_prev,
3324 int *ret, struct net_device *orig_dev)
3326 struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
3328 if (!rxq || rxq->qdisc == &noop_qdisc)
3332 *ret = deliver_skb(skb, *pt_prev, orig_dev);
3336 switch (ing_filter(skb, rxq)) {
3350 * netdev_rx_handler_register - register receive handler
3351 * @dev: device to register a handler for
3352 * @rx_handler: receive handler to register
3353 * @rx_handler_data: data pointer that is used by rx handler
3355 * Register a receive hander for a device. This handler will then be
3356 * called from __netif_receive_skb. A negative errno code is returned
3359 * The caller must hold the rtnl_mutex.
3361 * For a general description of rx_handler, see enum rx_handler_result.
3363 int netdev_rx_handler_register(struct net_device *dev,
3364 rx_handler_func_t *rx_handler,
3365 void *rx_handler_data)
3369 if (dev->rx_handler)
3372 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
3373 rcu_assign_pointer(dev->rx_handler, rx_handler);
3377 EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
3380 * netdev_rx_handler_unregister - unregister receive handler
3381 * @dev: device to unregister a handler from
3383 * Unregister a receive hander from a device.
3385 * The caller must hold the rtnl_mutex.
3387 void netdev_rx_handler_unregister(struct net_device *dev)
3391 RCU_INIT_POINTER(dev->rx_handler, NULL);
3392 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
3394 EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
3397 * Limit the use of PFMEMALLOC reserves to those protocols that implement
3398 * the special handling of PFMEMALLOC skbs.
3400 static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
3402 switch (skb->protocol) {
3403 case __constant_htons(ETH_P_ARP):
3404 case __constant_htons(ETH_P_IP):
3405 case __constant_htons(ETH_P_IPV6):
3406 case __constant_htons(ETH_P_8021Q):
3413 static int __netif_receive_skb(struct sk_buff *skb)
3415 struct packet_type *ptype, *pt_prev;
3416 rx_handler_func_t *rx_handler;
3417 struct net_device *orig_dev;
3418 struct net_device *null_or_dev;
3419 bool deliver_exact = false;
3420 int ret = NET_RX_DROP;
3422 unsigned long pflags = current->flags;
3424 net_timestamp_check(!netdev_tstamp_prequeue, skb);
3426 trace_netif_receive_skb(skb);
3429 * PFMEMALLOC skbs are special, they should
3430 * - be delivered to SOCK_MEMALLOC sockets only
3431 * - stay away from userspace
3432 * - have bounded memory usage
3434 * Use PF_MEMALLOC as this saves us from propagating the allocation
3435 * context down to all allocation sites.
3437 if (sk_memalloc_socks() && skb_pfmemalloc(skb))
3438 current->flags |= PF_MEMALLOC;
3440 /* if we've gotten here through NAPI, check netpoll */
3441 if (netpoll_receive_skb(skb))
3444 orig_dev = skb->dev;
3446 skb_reset_network_header(skb);
3447 if (!skb_transport_header_was_set(skb))
3448 skb_reset_transport_header(skb);
3449 skb_reset_mac_len(skb);
3456 skb->skb_iif = skb->dev->ifindex;
3458 __this_cpu_inc(softnet_data.processed);
3460 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
3461 skb = vlan_untag(skb);
3466 #ifdef CONFIG_NET_CLS_ACT
3467 if (skb->tc_verd & TC_NCLS) {
3468 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
3473 if (sk_memalloc_socks() && skb_pfmemalloc(skb))
3476 list_for_each_entry_rcu(ptype, &ptype_all, list) {
3477 if (!ptype->dev || ptype->dev == skb->dev) {
3479 ret = deliver_skb(skb, pt_prev, orig_dev);
3485 #ifdef CONFIG_NET_CLS_ACT
3486 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
3492 if (sk_memalloc_socks() && skb_pfmemalloc(skb)
3493 && !skb_pfmemalloc_protocol(skb))
3496 if (vlan_tx_tag_present(skb)) {
3498 ret = deliver_skb(skb, pt_prev, orig_dev);
3501 if (vlan_do_receive(&skb))
3503 else if (unlikely(!skb))
3507 rx_handler = rcu_dereference(skb->dev->rx_handler);
3510 ret = deliver_skb(skb, pt_prev, orig_dev);
3513 switch (rx_handler(&skb)) {
3514 case RX_HANDLER_CONSUMED:
3516 case RX_HANDLER_ANOTHER:
3518 case RX_HANDLER_EXACT:
3519 deliver_exact = true;
3520 case RX_HANDLER_PASS:
3527 if (vlan_tx_nonzero_tag_present(skb))
3528 skb->pkt_type = PACKET_OTHERHOST;
3530 /* deliver only exact match when indicated */
3531 null_or_dev = deliver_exact ? skb->dev : NULL;
3533 type = skb->protocol;
3534 list_for_each_entry_rcu(ptype,
3535 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
3536 if (ptype->type == type &&
3537 (ptype->dev == null_or_dev || ptype->dev == skb->dev ||
3538 ptype->dev == orig_dev)) {
3540 ret = deliver_skb(skb, pt_prev, orig_dev);
3546 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
3549 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
3552 atomic_long_inc(&skb->dev->rx_dropped);
3554 /* Jamal, now you will not able to escape explaining
3555 * me how you were going to use this. :-)
3563 tsk_restore_flags(current, pflags, PF_MEMALLOC);
3568 * netif_receive_skb - process receive buffer from network
3569 * @skb: buffer to process
3571 * netif_receive_skb() is the main receive data processing function.
3572 * It always succeeds. The buffer may be dropped during processing
3573 * for congestion control or by the protocol layers.
3575 * This function may only be called from softirq context and interrupts
3576 * should be enabled.
3578 * Return values (usually ignored):
3579 * NET_RX_SUCCESS: no congestion
3580 * NET_RX_DROP: packet was dropped
3582 int netif_receive_skb(struct sk_buff *skb)
3584 net_timestamp_check(netdev_tstamp_prequeue, skb);
3586 if (skb_defer_rx_timestamp(skb))
3587 return NET_RX_SUCCESS;
3590 if (static_key_false(&rps_needed)) {
3591 struct rps_dev_flow voidflow, *rflow = &voidflow;
3596 cpu = get_rps_cpu(skb->dev, skb, &rflow);
3599 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3606 return __netif_receive_skb(skb);
3608 EXPORT_SYMBOL(netif_receive_skb);
3610 /* Network device is going away, flush any packets still pending
3611 * Called with irqs disabled.
3613 static void flush_backlog(void *arg)
3615 struct net_device *dev = arg;
3616 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3617 struct sk_buff *skb, *tmp;
3620 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
3621 if (skb->dev == dev) {
3622 __skb_unlink(skb, &sd->input_pkt_queue);
3624 input_queue_head_incr(sd);
3629 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3630 if (skb->dev == dev) {
3631 __skb_unlink(skb, &sd->process_queue);
3633 input_queue_head_incr(sd);
3638 static int napi_gro_complete(struct sk_buff *skb)
3640 struct packet_offload *ptype;
3641 __be16 type = skb->protocol;
3642 struct list_head *head = &offload_base;
3645 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
3647 if (NAPI_GRO_CB(skb)->count == 1) {
3648 skb_shinfo(skb)->gso_size = 0;
3653 list_for_each_entry_rcu(ptype, head, list) {
3654 if (ptype->type != type || !ptype->callbacks.gro_complete)
3657 err = ptype->callbacks.gro_complete(skb);
3663 WARN_ON(&ptype->list == head);
3665 return NET_RX_SUCCESS;
3669 return netif_receive_skb(skb);
3672 /* napi->gro_list contains packets ordered by age.
3673 * youngest packets at the head of it.
3674 * Complete skbs in reverse order to reduce latencies.
3676 void napi_gro_flush(struct napi_struct *napi, bool flush_old)
3678 struct sk_buff *skb, *prev = NULL;
3680 /* scan list and build reverse chain */
3681 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
3686 for (skb = prev; skb; skb = prev) {
3689 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
3693 napi_gro_complete(skb);
3697 napi->gro_list = NULL;
3699 EXPORT_SYMBOL(napi_gro_flush);
3701 static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
3704 unsigned int maclen = skb->dev->hard_header_len;
3706 for (p = napi->gro_list; p; p = p->next) {
3707 unsigned long diffs;
3709 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
3710 diffs |= p->vlan_tci ^ skb->vlan_tci;
3711 if (maclen == ETH_HLEN)
3712 diffs |= compare_ether_header(skb_mac_header(p),
3713 skb_gro_mac_header(skb));
3715 diffs = memcmp(skb_mac_header(p),
3716 skb_gro_mac_header(skb),
3718 NAPI_GRO_CB(p)->same_flow = !diffs;
3719 NAPI_GRO_CB(p)->flush = 0;
3723 static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
3725 struct sk_buff **pp = NULL;
3726 struct packet_offload *ptype;
3727 __be16 type = skb->protocol;
3728 struct list_head *head = &offload_base;
3731 enum gro_result ret;
3733 if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
3736 if (skb_is_gso(skb) || skb_has_frag_list(skb))
3739 gro_list_prepare(napi, skb);
3742 list_for_each_entry_rcu(ptype, head, list) {
3743 if (ptype->type != type || !ptype->callbacks.gro_receive)
3746 skb_set_network_header(skb, skb_gro_offset(skb));
3747 mac_len = skb->network_header - skb->mac_header;
3748 skb->mac_len = mac_len;
3749 NAPI_GRO_CB(skb)->same_flow = 0;
3750 NAPI_GRO_CB(skb)->flush = 0;
3751 NAPI_GRO_CB(skb)->free = 0;
3753 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
3758 if (&ptype->list == head)
3761 same_flow = NAPI_GRO_CB(skb)->same_flow;
3762 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
3765 struct sk_buff *nskb = *pp;
3769 napi_gro_complete(nskb);
3776 if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
3780 NAPI_GRO_CB(skb)->count = 1;
3781 NAPI_GRO_CB(skb)->age = jiffies;
3782 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
3783 skb->next = napi->gro_list;
3784 napi->gro_list = skb;
3788 if (skb_headlen(skb) < skb_gro_offset(skb)) {
3789 int grow = skb_gro_offset(skb) - skb_headlen(skb);
3791 BUG_ON(skb->end - skb->tail < grow);
3793 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
3796 skb->data_len -= grow;
3798 skb_shinfo(skb)->frags[0].page_offset += grow;
3799 skb_frag_size_sub(&skb_shinfo(skb)->frags[0], grow);
3801 if (unlikely(!skb_frag_size(&skb_shinfo(skb)->frags[0]))) {
3802 skb_frag_unref(skb, 0);
3803 memmove(skb_shinfo(skb)->frags,
3804 skb_shinfo(skb)->frags + 1,
3805 --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
3818 static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
3822 if (netif_receive_skb(skb))
3830 case GRO_MERGED_FREE:
3831 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
3832 kmem_cache_free(skbuff_head_cache, skb);
3845 static void skb_gro_reset_offset(struct sk_buff *skb)
3847 const struct skb_shared_info *pinfo = skb_shinfo(skb);
3848 const skb_frag_t *frag0 = &pinfo->frags[0];
3850 NAPI_GRO_CB(skb)->data_offset = 0;
3851 NAPI_GRO_CB(skb)->frag0 = NULL;
3852 NAPI_GRO_CB(skb)->frag0_len = 0;
3854 if (skb->mac_header == skb->tail &&
3856 !PageHighMem(skb_frag_page(frag0))) {
3857 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
3858 NAPI_GRO_CB(skb)->frag0_len = skb_frag_size(frag0);
3862 gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
3864 skb_gro_reset_offset(skb);
3866 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
3868 EXPORT_SYMBOL(napi_gro_receive);
3870 static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
3872 __skb_pull(skb, skb_headlen(skb));
3873 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
3874 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
3876 skb->dev = napi->dev;
3882 struct sk_buff *napi_get_frags(struct napi_struct *napi)
3884 struct sk_buff *skb = napi->skb;
3887 skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
3893 EXPORT_SYMBOL(napi_get_frags);
3895 static gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
3901 skb->protocol = eth_type_trans(skb, skb->dev);
3903 if (ret == GRO_HELD)
3904 skb_gro_pull(skb, -ETH_HLEN);
3905 else if (netif_receive_skb(skb))
3910 case GRO_MERGED_FREE:
3911 napi_reuse_skb(napi, skb);
3921 static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
3923 struct sk_buff *skb = napi->skb;
3930 skb_reset_mac_header(skb);
3931 skb_gro_reset_offset(skb);
3933 off = skb_gro_offset(skb);
3934 hlen = off + sizeof(*eth);
3935 eth = skb_gro_header_fast(skb, off);
3936 if (skb_gro_header_hard(skb, hlen)) {
3937 eth = skb_gro_header_slow(skb, hlen, off);
3938 if (unlikely(!eth)) {
3939 napi_reuse_skb(napi, skb);
3945 skb_gro_pull(skb, sizeof(*eth));
3948 * This works because the only protocols we care about don't require
3949 * special handling. We'll fix it up properly at the end.
3951 skb->protocol = eth->h_proto;
3957 gro_result_t napi_gro_frags(struct napi_struct *napi)
3959 struct sk_buff *skb = napi_frags_skb(napi);
3964 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
3966 EXPORT_SYMBOL(napi_gro_frags);
3969 * net_rps_action sends any pending IPI's for rps.
3970 * Note: called with local irq disabled, but exits with local irq enabled.
3972 static void net_rps_action_and_irq_enable(struct softnet_data *sd)
3975 struct softnet_data *remsd = sd->rps_ipi_list;
3978 sd->rps_ipi_list = NULL;
3982 /* Send pending IPI's to kick RPS processing on remote cpus. */
3984 struct softnet_data *next = remsd->rps_ipi_next;
3986 if (cpu_online(remsd->cpu))
3987 __smp_call_function_single(remsd->cpu,
3996 static int process_backlog(struct napi_struct *napi, int quota)
3999 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
4002 /* Check if we have pending ipi, its better to send them now,
4003 * not waiting net_rx_action() end.
4005 if (sd->rps_ipi_list) {
4006 local_irq_disable();
4007 net_rps_action_and_irq_enable(sd);
4010 napi->weight = weight_p;
4011 local_irq_disable();
4012 while (work < quota) {
4013 struct sk_buff *skb;
4016 while ((skb = __skb_dequeue(&sd->process_queue))) {
4018 __netif_receive_skb(skb);
4019 local_irq_disable();
4020 input_queue_head_incr(sd);
4021 if (++work >= quota) {
4028 qlen = skb_queue_len(&sd->input_pkt_queue);
4030 skb_queue_splice_tail_init(&sd->input_pkt_queue,
4031 &sd->process_queue);
4033 if (qlen < quota - work) {
4035 * Inline a custom version of __napi_complete().
4036 * only current cpu owns and manipulates this napi,
4037 * and NAPI_STATE_SCHED is the only possible flag set on backlog.
4038 * we can use a plain write instead of clear_bit(),
4039 * and we dont need an smp_mb() memory barrier.
4041 list_del(&napi->poll_list);
4044 quota = work + qlen;
4054 * __napi_schedule - schedule for receive
4055 * @n: entry to schedule
4057 * The entry's receive function will be scheduled to run
4059 void __napi_schedule(struct napi_struct *n)
4061 unsigned long flags;
4063 local_irq_save(flags);
4064 ____napi_schedule(&__get_cpu_var(softnet_data), n);
4065 local_irq_restore(flags);
4067 EXPORT_SYMBOL(__napi_schedule);
4069 void __napi_complete(struct napi_struct *n)
4071 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
4072 BUG_ON(n->gro_list);
4074 list_del(&n->poll_list);
4075 smp_mb__before_clear_bit();
4076 clear_bit(NAPI_STATE_SCHED, &n->state);
4078 EXPORT_SYMBOL(__napi_complete);
4080 void napi_complete(struct napi_struct *n)
4082 unsigned long flags;
4085 * don't let napi dequeue from the cpu poll list
4086 * just in case its running on a different cpu
4088 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
4091 napi_gro_flush(n, false);
4092 local_irq_save(flags);
4094 local_irq_restore(flags);
4096 EXPORT_SYMBOL(napi_complete);
4098 void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
4099 int (*poll)(struct napi_struct *, int), int weight)
4101 INIT_LIST_HEAD(&napi->poll_list);
4102 napi->gro_count = 0;
4103 napi->gro_list = NULL;
4106 napi->weight = weight;
4107 list_add(&napi->dev_list, &dev->napi_list);
4109 #ifdef CONFIG_NETPOLL
4110 spin_lock_init(&napi->poll_lock);
4111 napi->poll_owner = -1;
4113 set_bit(NAPI_STATE_SCHED, &napi->state);
4115 EXPORT_SYMBOL(netif_napi_add);
4117 void netif_napi_del(struct napi_struct *napi)
4119 struct sk_buff *skb, *next;
4121 list_del_init(&napi->dev_list);
4122 napi_free_frags(napi);
4124 for (skb = napi->gro_list; skb; skb = next) {
4130 napi->gro_list = NULL;
4131 napi->gro_count = 0;
4133 EXPORT_SYMBOL(netif_napi_del);
4135 static void net_rx_action(struct softirq_action *h)
4137 struct softnet_data *sd = &__get_cpu_var(softnet_data);
4138 unsigned long time_limit = jiffies + 2;
4139 int budget = netdev_budget;
4142 local_irq_disable();
4144 while (!list_empty(&sd->poll_list)) {
4145 struct napi_struct *n;
4148 /* If softirq window is exhuasted then punt.
4149 * Allow this to run for 2 jiffies since which will allow
4150 * an average latency of 1.5/HZ.
4152 if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
4157 /* Even though interrupts have been re-enabled, this
4158 * access is safe because interrupts can only add new
4159 * entries to the tail of this list, and only ->poll()
4160 * calls can remove this head entry from the list.
4162 n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
4164 have = netpoll_poll_lock(n);
4168 /* This NAPI_STATE_SCHED test is for avoiding a race
4169 * with netpoll's poll_napi(). Only the entity which
4170 * obtains the lock and sees NAPI_STATE_SCHED set will
4171 * actually make the ->poll() call. Therefore we avoid
4172 * accidentally calling ->poll() when NAPI is not scheduled.
4175 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
4176 work = n->poll(n, weight);
4180 WARN_ON_ONCE(work > weight);
4184 local_irq_disable();
4186 /* Drivers must not modify the NAPI state if they
4187 * consume the entire weight. In such cases this code
4188 * still "owns" the NAPI instance and therefore can
4189 * move the instance around on the list at-will.
4191 if (unlikely(work == weight)) {
4192 if (unlikely(napi_disable_pending(n))) {
4195 local_irq_disable();
4198 /* flush too old packets
4199 * If HZ < 1000, flush all packets.
4202 napi_gro_flush(n, HZ >= 1000);
4203 local_irq_disable();
4205 list_move_tail(&n->poll_list, &sd->poll_list);
4209 netpoll_poll_unlock(have);
4212 net_rps_action_and_irq_enable(sd);
4214 #ifdef CONFIG_NET_DMA
4216 * There may not be any more sk_buffs coming right now, so push
4217 * any pending DMA copies to hardware
4219 dma_issue_pending_all();
4226 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4230 static gifconf_func_t *gifconf_list[NPROTO];
4233 * register_gifconf - register a SIOCGIF handler
4234 * @family: Address family
4235 * @gifconf: Function handler
4237 * Register protocol dependent address dumping routines. The handler
4238 * that is passed must not be freed or reused until it has been replaced
4239 * by another handler.
4241 int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
4243 if (family >= NPROTO)
4245 gifconf_list[family] = gifconf;
4248 EXPORT_SYMBOL(register_gifconf);
4252 * Map an interface index to its name (SIOCGIFNAME)
4256 * We need this ioctl for efficient implementation of the
4257 * if_indextoname() function required by the IPv6 API. Without
4258 * it, we would have to search all the interfaces to find a
4262 static int dev_ifname(struct net *net, struct ifreq __user *arg)
4264 struct net_device *dev;
4269 * Fetch the caller's info block.
4272 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
4276 seq = read_seqcount_begin(&devnet_rename_seq);
4278 dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
4284 strcpy(ifr.ifr_name, dev->name);
4286 if (read_seqcount_retry(&devnet_rename_seq, seq))
4289 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
4295 * Perform a SIOCGIFCONF call. This structure will change
4296 * size eventually, and there is nothing I can do about it.
4297 * Thus we will need a 'compatibility mode'.
4300 static int dev_ifconf(struct net *net, char __user *arg)
4303 struct net_device *dev;
4310 * Fetch the caller's info block.
4313 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
4320 * Loop over the interfaces, and write an info block for each.
4324 for_each_netdev(net, dev) {
4325 for (i = 0; i < NPROTO; i++) {
4326 if (gifconf_list[i]) {
4329 done = gifconf_list[i](dev, NULL, 0);
4331 done = gifconf_list[i](dev, pos + total,
4341 * All done. Write the updated control block back to the caller.
4343 ifc.ifc_len = total;
4346 * Both BSD and Solaris return 0 here, so we do too.
4348 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
4351 #ifdef CONFIG_PROC_FS
4353 #define BUCKET_SPACE (32 - NETDEV_HASHBITS - 1)
4355 #define get_bucket(x) ((x) >> BUCKET_SPACE)
4356 #define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1))
4357 #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
4359 static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff_t *pos)
4361 struct net *net = seq_file_net(seq);
4362 struct net_device *dev;
4363 struct hlist_node *p;
4364 struct hlist_head *h;
4365 unsigned int count = 0, offset = get_offset(*pos);
4367 h = &net->dev_name_head[get_bucket(*pos)];
4368 hlist_for_each_entry_rcu(dev, p, h, name_hlist) {
4369 if (++count == offset)
4376 static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *pos)
4378 struct net_device *dev;
4379 unsigned int bucket;
4382 dev = dev_from_same_bucket(seq, pos);
4386 bucket = get_bucket(*pos) + 1;
4387 *pos = set_bucket_offset(bucket, 1);
4388 } while (bucket < NETDEV_HASHENTRIES);
4394 * This is invoked by the /proc filesystem handler to display a device
4397 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
4402 return SEQ_START_TOKEN;
4404 if (get_bucket(*pos) >= NETDEV_HASHENTRIES)
4407 return dev_from_bucket(seq, pos);
4410 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4413 return dev_from_bucket(seq, pos);
4416 void dev_seq_stop(struct seq_file *seq, void *v)
4422 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
4424 struct rtnl_link_stats64 temp;
4425 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
4427 seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
4428 "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
4429 dev->name, stats->rx_bytes, stats->rx_packets,
4431 stats->rx_dropped + stats->rx_missed_errors,
4432 stats->rx_fifo_errors,
4433 stats->rx_length_errors + stats->rx_over_errors +
4434 stats->rx_crc_errors + stats->rx_frame_errors,
4435 stats->rx_compressed, stats->multicast,
4436 stats->tx_bytes, stats->tx_packets,
4437 stats->tx_errors, stats->tx_dropped,
4438 stats->tx_fifo_errors, stats->collisions,
4439 stats->tx_carrier_errors +
4440 stats->tx_aborted_errors +
4441 stats->tx_window_errors +
4442 stats->tx_heartbeat_errors,
4443 stats->tx_compressed);
4447 * Called from the PROCfs module. This now uses the new arbitrary sized
4448 * /proc/net interface to create /proc/net/dev
4450 static int dev_seq_show(struct seq_file *seq, void *v)
4452 if (v == SEQ_START_TOKEN)
4453 seq_puts(seq, "Inter-| Receive "
4455 " face |bytes packets errs drop fifo frame "
4456 "compressed multicast|bytes packets errs "
4457 "drop fifo colls carrier compressed\n");
4459 dev_seq_printf_stats(seq, v);
4463 static struct softnet_data *softnet_get_online(loff_t *pos)
4465 struct softnet_data *sd = NULL;
4467 while (*pos < nr_cpu_ids)
4468 if (cpu_online(*pos)) {
4469 sd = &per_cpu(softnet_data, *pos);
4476 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
4478 return softnet_get_online(pos);
4481 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4484 return softnet_get_online(pos);
4487 static void softnet_seq_stop(struct seq_file *seq, void *v)
4491 static int softnet_seq_show(struct seq_file *seq, void *v)
4493 struct softnet_data *sd = v;
4495 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
4496 sd->processed, sd->dropped, sd->time_squeeze, 0,
4497 0, 0, 0, 0, /* was fastroute */
4498 sd->cpu_collision, sd->received_rps);
4502 static const struct seq_operations dev_seq_ops = {
4503 .start = dev_seq_start,
4504 .next = dev_seq_next,
4505 .stop = dev_seq_stop,
4506 .show = dev_seq_show,
4509 static int dev_seq_open(struct inode *inode, struct file *file)
4511 return seq_open_net(inode, file, &dev_seq_ops,
4512 sizeof(struct seq_net_private));
4515 static const struct file_operations dev_seq_fops = {
4516 .owner = THIS_MODULE,
4517 .open = dev_seq_open,
4519 .llseek = seq_lseek,
4520 .release = seq_release_net,
4523 static const struct seq_operations softnet_seq_ops = {
4524 .start = softnet_seq_start,
4525 .next = softnet_seq_next,
4526 .stop = softnet_seq_stop,
4527 .show = softnet_seq_show,
4530 static int softnet_seq_open(struct inode *inode, struct file *file)
4532 return seq_open(file, &softnet_seq_ops);
4535 static const struct file_operations softnet_seq_fops = {
4536 .owner = THIS_MODULE,
4537 .open = softnet_seq_open,
4539 .llseek = seq_lseek,
4540 .release = seq_release,
4543 static void *ptype_get_idx(loff_t pos)
4545 struct packet_type *pt = NULL;
4549 list_for_each_entry_rcu(pt, &ptype_all, list) {
4555 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
4556 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
4565 static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
4569 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
4572 static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4574 struct packet_type *pt;
4575 struct list_head *nxt;
4579 if (v == SEQ_START_TOKEN)
4580 return ptype_get_idx(0);
4583 nxt = pt->list.next;
4584 if (pt->type == htons(ETH_P_ALL)) {
4585 if (nxt != &ptype_all)
4588 nxt = ptype_base[0].next;
4590 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
4592 while (nxt == &ptype_base[hash]) {
4593 if (++hash >= PTYPE_HASH_SIZE)
4595 nxt = ptype_base[hash].next;
4598 return list_entry(nxt, struct packet_type, list);
4601 static void ptype_seq_stop(struct seq_file *seq, void *v)
4607 static int ptype_seq_show(struct seq_file *seq, void *v)
4609 struct packet_type *pt = v;
4611 if (v == SEQ_START_TOKEN)
4612 seq_puts(seq, "Type Device Function\n");
4613 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
4614 if (pt->type == htons(ETH_P_ALL))
4615 seq_puts(seq, "ALL ");
4617 seq_printf(seq, "%04x", ntohs(pt->type));
4619 seq_printf(seq, " %-8s %pF\n",
4620 pt->dev ? pt->dev->name : "", pt->func);
4626 static const struct seq_operations ptype_seq_ops = {
4627 .start = ptype_seq_start,
4628 .next = ptype_seq_next,
4629 .stop = ptype_seq_stop,
4630 .show = ptype_seq_show,
4633 static int ptype_seq_open(struct inode *inode, struct file *file)
4635 return seq_open_net(inode, file, &ptype_seq_ops,
4636 sizeof(struct seq_net_private));
4639 static const struct file_operations ptype_seq_fops = {
4640 .owner = THIS_MODULE,
4641 .open = ptype_seq_open,
4643 .llseek = seq_lseek,
4644 .release = seq_release_net,
4648 static int __net_init dev_proc_net_init(struct net *net)
4652 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
4654 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
4656 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
4659 if (wext_proc_init(net))
4665 proc_net_remove(net, "ptype");
4667 proc_net_remove(net, "softnet_stat");
4669 proc_net_remove(net, "dev");
4673 static void __net_exit dev_proc_net_exit(struct net *net)
4675 wext_proc_exit(net);
4677 proc_net_remove(net, "ptype");
4678 proc_net_remove(net, "softnet_stat");
4679 proc_net_remove(net, "dev");
4682 static struct pernet_operations __net_initdata dev_proc_ops = {
4683 .init = dev_proc_net_init,
4684 .exit = dev_proc_net_exit,
4687 static int __init dev_proc_init(void)
4689 return register_pernet_subsys(&dev_proc_ops);
4692 #define dev_proc_init() 0
4693 #endif /* CONFIG_PROC_FS */
4696 struct netdev_upper {
4697 struct net_device *dev;
4699 struct list_head list;
4700 struct rcu_head rcu;
4701 struct list_head search_list;
4704 static void __append_search_uppers(struct list_head *search_list,
4705 struct net_device *dev)
4707 struct netdev_upper *upper;
4709 list_for_each_entry(upper, &dev->upper_dev_list, list) {
4710 /* check if this upper is not already in search list */
4711 if (list_empty(&upper->search_list))
4712 list_add_tail(&upper->search_list, search_list);
4716 static bool __netdev_search_upper_dev(struct net_device *dev,
4717 struct net_device *upper_dev)
4719 LIST_HEAD(search_list);
4720 struct netdev_upper *upper;
4721 struct netdev_upper *tmp;
4724 __append_search_uppers(&search_list, dev);
4725 list_for_each_entry(upper, &search_list, search_list) {
4726 if (upper->dev == upper_dev) {
4730 __append_search_uppers(&search_list, upper->dev);
4732 list_for_each_entry_safe(upper, tmp, &search_list, search_list)
4733 INIT_LIST_HEAD(&upper->search_list);
4737 static struct netdev_upper *__netdev_find_upper(struct net_device *dev,
4738 struct net_device *upper_dev)
4740 struct netdev_upper *upper;
4742 list_for_each_entry(upper, &dev->upper_dev_list, list) {
4743 if (upper->dev == upper_dev)
4750 * netdev_has_upper_dev - Check if device is linked to an upper device
4752 * @upper_dev: upper device to check
4754 * Find out if a device is linked to specified upper device and return true
4755 * in case it is. Note that this checks only immediate upper device,
4756 * not through a complete stack of devices. The caller must hold the RTNL lock.
4758 bool netdev_has_upper_dev(struct net_device *dev,
4759 struct net_device *upper_dev)
4763 return __netdev_find_upper(dev, upper_dev);
4765 EXPORT_SYMBOL(netdev_has_upper_dev);
4768 * netdev_has_any_upper_dev - Check if device is linked to some device
4771 * Find out if a device is linked to an upper device and return true in case
4772 * it is. The caller must hold the RTNL lock.
4774 bool netdev_has_any_upper_dev(struct net_device *dev)
4778 return !list_empty(&dev->upper_dev_list);
4780 EXPORT_SYMBOL(netdev_has_any_upper_dev);
4783 * netdev_master_upper_dev_get - Get master upper device
4786 * Find a master upper device and return pointer to it or NULL in case
4787 * it's not there. The caller must hold the RTNL lock.
4789 struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4791 struct netdev_upper *upper;
4795 if (list_empty(&dev->upper_dev_list))
4798 upper = list_first_entry(&dev->upper_dev_list,
4799 struct netdev_upper, list);
4800 if (likely(upper->master))
4804 EXPORT_SYMBOL(netdev_master_upper_dev_get);
4807 * netdev_master_upper_dev_get_rcu - Get master upper device
4810 * Find a master upper device and return pointer to it or NULL in case
4811 * it's not there. The caller must hold the RCU read lock.
4813 struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
4815 struct netdev_upper *upper;
4817 upper = list_first_or_null_rcu(&dev->upper_dev_list,
4818 struct netdev_upper, list);
4819 if (upper && likely(upper->master))
4823 EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
4825 static int __netdev_upper_dev_link(struct net_device *dev,
4826 struct net_device *upper_dev, bool master)
4828 struct netdev_upper *upper;
4832 if (dev == upper_dev)
4835 /* To prevent loops, check if dev is not upper device to upper_dev. */
4836 if (__netdev_search_upper_dev(upper_dev, dev))
4839 if (__netdev_find_upper(dev, upper_dev))
4842 if (master && netdev_master_upper_dev_get(dev))
4845 upper = kmalloc(sizeof(*upper), GFP_KERNEL);
4849 upper->dev = upper_dev;
4850 upper->master = master;
4851 INIT_LIST_HEAD(&upper->search_list);
4853 /* Ensure that master upper link is always the first item in list. */
4855 list_add_rcu(&upper->list, &dev->upper_dev_list);
4857 list_add_tail_rcu(&upper->list, &dev->upper_dev_list);
4858 dev_hold(upper_dev);
4864 * netdev_upper_dev_link - Add a link to the upper device
4866 * @upper_dev: new upper device
4868 * Adds a link to device which is upper to this one. The caller must hold
4869 * the RTNL lock. On a failure a negative errno code is returned.
4870 * On success the reference counts are adjusted and the function
4873 int netdev_upper_dev_link(struct net_device *dev,
4874 struct net_device *upper_dev)
4876 return __netdev_upper_dev_link(dev, upper_dev, false);
4878 EXPORT_SYMBOL(netdev_upper_dev_link);
4881 * netdev_master_upper_dev_link - Add a master link to the upper device
4883 * @upper_dev: new upper device
4885 * Adds a link to device which is upper to this one. In this case, only
4886 * one master upper device can be linked, although other non-master devices
4887 * might be linked as well. The caller must hold the RTNL lock.
4888 * On a failure a negative errno code is returned. On success the reference
4889 * counts are adjusted and the function returns zero.
4891 int netdev_master_upper_dev_link(struct net_device *dev,
4892 struct net_device *upper_dev)
4894 return __netdev_upper_dev_link(dev, upper_dev, true);
4896 EXPORT_SYMBOL(netdev_master_upper_dev_link);
4899 * netdev_upper_dev_unlink - Removes a link to upper device
4901 * @upper_dev: new upper device
4903 * Removes a link to device which is upper to this one. The caller must hold
4906 void netdev_upper_dev_unlink(struct net_device *dev,
4907 struct net_device *upper_dev)
4909 struct netdev_upper *upper;
4913 upper = __netdev_find_upper(dev, upper_dev);
4916 list_del_rcu(&upper->list);
4918 kfree_rcu(upper, rcu);
4920 EXPORT_SYMBOL(netdev_upper_dev_unlink);
4922 static void dev_change_rx_flags(struct net_device *dev, int flags)
4924 const struct net_device_ops *ops = dev->netdev_ops;
4926 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
4927 ops->ndo_change_rx_flags(dev, flags);
4930 static int __dev_set_promiscuity(struct net_device *dev, int inc)
4932 unsigned int old_flags = dev->flags;
4938 dev->flags |= IFF_PROMISC;
4939 dev->promiscuity += inc;
4940 if (dev->promiscuity == 0) {
4943 * If inc causes overflow, untouch promisc and return error.
4946 dev->flags &= ~IFF_PROMISC;
4948 dev->promiscuity -= inc;
4949 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
4954 if (dev->flags != old_flags) {
4955 pr_info("device %s %s promiscuous mode\n",
4957 dev->flags & IFF_PROMISC ? "entered" : "left");
4958 if (audit_enabled) {
4959 current_uid_gid(&uid, &gid);
4960 audit_log(current->audit_context, GFP_ATOMIC,
4961 AUDIT_ANOM_PROMISCUOUS,
4962 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
4963 dev->name, (dev->flags & IFF_PROMISC),
4964 (old_flags & IFF_PROMISC),
4965 from_kuid(&init_user_ns, audit_get_loginuid(current)),
4966 from_kuid(&init_user_ns, uid),
4967 from_kgid(&init_user_ns, gid),
4968 audit_get_sessionid(current));
4971 dev_change_rx_flags(dev, IFF_PROMISC);
4977 * dev_set_promiscuity - update promiscuity count on a device
4981 * Add or remove promiscuity from a device. While the count in the device
4982 * remains above zero the interface remains promiscuous. Once it hits zero
4983 * the device reverts back to normal filtering operation. A negative inc
4984 * value is used to drop promiscuity on the device.
4985 * Return 0 if successful or a negative errno code on error.
4987 int dev_set_promiscuity(struct net_device *dev, int inc)
4989 unsigned int old_flags = dev->flags;
4992 err = __dev_set_promiscuity(dev, inc);
4995 if (dev->flags != old_flags)
4996 dev_set_rx_mode(dev);
4999 EXPORT_SYMBOL(dev_set_promiscuity);
5002 * dev_set_allmulti - update allmulti count on a device
5006 * Add or remove reception of all multicast frames to a device. While the
5007 * count in the device remains above zero the interface remains listening
5008 * to all interfaces. Once it hits zero the device reverts back to normal
5009 * filtering operation. A negative @inc value is used to drop the counter
5010 * when releasing a resource needing all multicasts.
5011 * Return 0 if successful or a negative errno code on error.
5014 int dev_set_allmulti(struct net_device *dev, int inc)
5016 unsigned int old_flags = dev->flags;
5020 dev->flags |= IFF_ALLMULTI;
5021 dev->allmulti += inc;
5022 if (dev->allmulti == 0) {
5025 * If inc causes overflow, untouch allmulti and return error.
5028 dev->flags &= ~IFF_ALLMULTI;
5030 dev->allmulti -= inc;
5031 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
5036 if (dev->flags ^ old_flags) {
5037 dev_change_rx_flags(dev, IFF_ALLMULTI);
5038 dev_set_rx_mode(dev);
5042 EXPORT_SYMBOL(dev_set_allmulti);
5045 * Upload unicast and multicast address lists to device and
5046 * configure RX filtering. When the device doesn't support unicast
5047 * filtering it is put in promiscuous mode while unicast addresses
5050 void __dev_set_rx_mode(struct net_device *dev)
5052 const struct net_device_ops *ops = dev->netdev_ops;
5054 /* dev_open will call this function so the list will stay sane. */
5055 if (!(dev->flags&IFF_UP))
5058 if (!netif_device_present(dev))
5061 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
5062 /* Unicast addresses changes may only happen under the rtnl,
5063 * therefore calling __dev_set_promiscuity here is safe.
5065 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
5066 __dev_set_promiscuity(dev, 1);
5067 dev->uc_promisc = true;
5068 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
5069 __dev_set_promiscuity(dev, -1);
5070 dev->uc_promisc = false;
5074 if (ops->ndo_set_rx_mode)
5075 ops->ndo_set_rx_mode(dev);
5078 void dev_set_rx_mode(struct net_device *dev)
5080 netif_addr_lock_bh(dev);
5081 __dev_set_rx_mode(dev);
5082 netif_addr_unlock_bh(dev);
5086 * dev_get_flags - get flags reported to userspace
5089 * Get the combination of flag bits exported through APIs to userspace.
5091 unsigned int dev_get_flags(const struct net_device *dev)
5095 flags = (dev->flags & ~(IFF_PROMISC |
5100 (dev->gflags & (IFF_PROMISC |
5103 if (netif_running(dev)) {
5104 if (netif_oper_up(dev))
5105 flags |= IFF_RUNNING;
5106 if (netif_carrier_ok(dev))
5107 flags |= IFF_LOWER_UP;
5108 if (netif_dormant(dev))
5109 flags |= IFF_DORMANT;
5114 EXPORT_SYMBOL(dev_get_flags);
5116 int __dev_change_flags(struct net_device *dev, unsigned int flags)
5118 unsigned int old_flags = dev->flags;
5124 * Set the flags on our device.
5127 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
5128 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
5130 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
5134 * Load in the correct multicast list now the flags have changed.
5137 if ((old_flags ^ flags) & IFF_MULTICAST)
5138 dev_change_rx_flags(dev, IFF_MULTICAST);
5140 dev_set_rx_mode(dev);
5143 * Have we downed the interface. We handle IFF_UP ourselves
5144 * according to user attempts to set it, rather than blindly
5149 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
5150 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
5153 dev_set_rx_mode(dev);
5156 if ((flags ^ dev->gflags) & IFF_PROMISC) {
5157 int inc = (flags & IFF_PROMISC) ? 1 : -1;
5159 dev->gflags ^= IFF_PROMISC;
5160 dev_set_promiscuity(dev, inc);
5163 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
5164 is important. Some (broken) drivers set IFF_PROMISC, when
5165 IFF_ALLMULTI is requested not asking us and not reporting.
5167 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
5168 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
5170 dev->gflags ^= IFF_ALLMULTI;
5171 dev_set_allmulti(dev, inc);
5177 void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
5179 unsigned int changes = dev->flags ^ old_flags;
5181 if (changes & IFF_UP) {
5182 if (dev->flags & IFF_UP)
5183 call_netdevice_notifiers(NETDEV_UP, dev);
5185 call_netdevice_notifiers(NETDEV_DOWN, dev);
5188 if (dev->flags & IFF_UP &&
5189 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
5190 call_netdevice_notifiers(NETDEV_CHANGE, dev);
5194 * dev_change_flags - change device settings
5196 * @flags: device state flags
5198 * Change settings on device based state flags. The flags are
5199 * in the userspace exported format.
5201 int dev_change_flags(struct net_device *dev, unsigned int flags)
5204 unsigned int changes, old_flags = dev->flags;
5206 ret = __dev_change_flags(dev, flags);
5210 changes = old_flags ^ dev->flags;
5212 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
5214 __dev_notify_flags(dev, old_flags);
5217 EXPORT_SYMBOL(dev_change_flags);
5220 * dev_set_mtu - Change maximum transfer unit
5222 * @new_mtu: new transfer unit
5224 * Change the maximum transfer size of the network device.
5226 int dev_set_mtu(struct net_device *dev, int new_mtu)
5228 const struct net_device_ops *ops = dev->netdev_ops;
5231 if (new_mtu == dev->mtu)
5234 /* MTU must be positive. */
5238 if (!netif_device_present(dev))
5242 if (ops->ndo_change_mtu)
5243 err = ops->ndo_change_mtu(dev, new_mtu);
5248 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
5251 EXPORT_SYMBOL(dev_set_mtu);
5254 * dev_set_group - Change group this device belongs to
5256 * @new_group: group this device should belong to
5258 void dev_set_group(struct net_device *dev, int new_group)
5260 dev->group = new_group;
5262 EXPORT_SYMBOL(dev_set_group);
5265 * dev_set_mac_address - Change Media Access Control Address
5269 * Change the hardware (MAC) address of the device
5271 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
5273 const struct net_device_ops *ops = dev->netdev_ops;
5276 if (!ops->ndo_set_mac_address)
5278 if (sa->sa_family != dev->type)
5280 if (!netif_device_present(dev))
5282 err = ops->ndo_set_mac_address(dev, sa);
5285 dev->addr_assign_type = NET_ADDR_SET;
5286 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
5287 add_device_randomness(dev->dev_addr, dev->addr_len);
5290 EXPORT_SYMBOL(dev_set_mac_address);
5293 * dev_change_carrier - Change device carrier
5295 * @new_carries: new value
5297 * Change device carrier
5299 int dev_change_carrier(struct net_device *dev, bool new_carrier)
5301 const struct net_device_ops *ops = dev->netdev_ops;
5303 if (!ops->ndo_change_carrier)
5305 if (!netif_device_present(dev))
5307 return ops->ndo_change_carrier(dev, new_carrier);
5309 EXPORT_SYMBOL(dev_change_carrier);
5312 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
5314 static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
5317 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
5323 case SIOCGIFFLAGS: /* Get interface flags */
5324 ifr->ifr_flags = (short) dev_get_flags(dev);
5327 case SIOCGIFMETRIC: /* Get the metric on the interface
5328 (currently unused) */
5329 ifr->ifr_metric = 0;
5332 case SIOCGIFMTU: /* Get the MTU of a device */
5333 ifr->ifr_mtu = dev->mtu;
5338 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
5340 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
5341 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
5342 ifr->ifr_hwaddr.sa_family = dev->type;
5350 ifr->ifr_map.mem_start = dev->mem_start;
5351 ifr->ifr_map.mem_end = dev->mem_end;
5352 ifr->ifr_map.base_addr = dev->base_addr;
5353 ifr->ifr_map.irq = dev->irq;
5354 ifr->ifr_map.dma = dev->dma;
5355 ifr->ifr_map.port = dev->if_port;
5359 ifr->ifr_ifindex = dev->ifindex;
5363 ifr->ifr_qlen = dev->tx_queue_len;
5367 /* dev_ioctl() should ensure this case
5379 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
5381 static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
5384 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
5385 const struct net_device_ops *ops;
5390 ops = dev->netdev_ops;
5393 case SIOCSIFFLAGS: /* Set interface flags */
5394 return dev_change_flags(dev, ifr->ifr_flags);
5396 case SIOCSIFMETRIC: /* Set the metric on the interface
5397 (currently unused) */
5400 case SIOCSIFMTU: /* Set the MTU of a device */
5401 return dev_set_mtu(dev, ifr->ifr_mtu);
5404 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
5406 case SIOCSIFHWBROADCAST:
5407 if (ifr->ifr_hwaddr.sa_family != dev->type)
5409 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
5410 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
5411 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
5415 if (ops->ndo_set_config) {
5416 if (!netif_device_present(dev))
5418 return ops->ndo_set_config(dev, &ifr->ifr_map);
5423 if (!ops->ndo_set_rx_mode ||
5424 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
5426 if (!netif_device_present(dev))
5428 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
5431 if (!ops->ndo_set_rx_mode ||
5432 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
5434 if (!netif_device_present(dev))
5436 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
5439 if (ifr->ifr_qlen < 0)
5441 dev->tx_queue_len = ifr->ifr_qlen;
5445 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
5446 return dev_change_name(dev, ifr->ifr_newname);
5449 err = net_hwtstamp_validate(ifr);
5455 * Unknown or private ioctl
5458 if ((cmd >= SIOCDEVPRIVATE &&
5459 cmd <= SIOCDEVPRIVATE + 15) ||
5460 cmd == SIOCBONDENSLAVE ||
5461 cmd == SIOCBONDRELEASE ||
5462 cmd == SIOCBONDSETHWADDR ||
5463 cmd == SIOCBONDSLAVEINFOQUERY ||
5464 cmd == SIOCBONDINFOQUERY ||
5465 cmd == SIOCBONDCHANGEACTIVE ||
5466 cmd == SIOCGMIIPHY ||
5467 cmd == SIOCGMIIREG ||
5468 cmd == SIOCSMIIREG ||
5469 cmd == SIOCBRADDIF ||
5470 cmd == SIOCBRDELIF ||
5471 cmd == SIOCSHWTSTAMP ||
5472 cmd == SIOCWANDEV) {
5474 if (ops->ndo_do_ioctl) {
5475 if (netif_device_present(dev))
5476 err = ops->ndo_do_ioctl(dev, ifr, cmd);
5488 * This function handles all "interface"-type I/O control requests. The actual
5489 * 'doing' part of this is dev_ifsioc above.
5493 * dev_ioctl - network device ioctl
5494 * @net: the applicable net namespace
5495 * @cmd: command to issue
5496 * @arg: pointer to a struct ifreq in user space
5498 * Issue ioctl functions to devices. This is normally called by the
5499 * user space syscall interfaces but can sometimes be useful for
5500 * other purposes. The return value is the return from the syscall if
5501 * positive or a negative errno code on error.
5504 int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
5510 /* One special case: SIOCGIFCONF takes ifconf argument
5511 and requires shared lock, because it sleeps writing
5515 if (cmd == SIOCGIFCONF) {
5517 ret = dev_ifconf(net, (char __user *) arg);
5521 if (cmd == SIOCGIFNAME)
5522 return dev_ifname(net, (struct ifreq __user *)arg);
5524 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
5527 ifr.ifr_name[IFNAMSIZ-1] = 0;
5529 colon = strchr(ifr.ifr_name, ':');
5534 * See which interface the caller is talking about.
5539 * These ioctl calls:
5540 * - can be done by all.
5541 * - atomic and do not require locking.
5552 dev_load(net, ifr.ifr_name);
5554 ret = dev_ifsioc_locked(net, &ifr, cmd);
5559 if (copy_to_user(arg, &ifr,
5560 sizeof(struct ifreq)))
5566 dev_load(net, ifr.ifr_name);
5568 ret = dev_ethtool(net, &ifr);
5573 if (copy_to_user(arg, &ifr,
5574 sizeof(struct ifreq)))
5580 * These ioctl calls:
5581 * - require superuser power.
5582 * - require strict serialization.
5588 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5590 dev_load(net, ifr.ifr_name);
5592 ret = dev_ifsioc(net, &ifr, cmd);
5597 if (copy_to_user(arg, &ifr,
5598 sizeof(struct ifreq)))
5604 * These ioctl calls:
5605 * - require superuser power.
5606 * - require strict serialization.
5607 * - do not return a value
5611 if (!capable(CAP_NET_ADMIN))
5615 * These ioctl calls:
5616 * - require local superuser power.
5617 * - require strict serialization.
5618 * - do not return a value
5627 case SIOCSIFHWBROADCAST:
5629 case SIOCBONDENSLAVE:
5630 case SIOCBONDRELEASE:
5631 case SIOCBONDSETHWADDR:
5632 case SIOCBONDCHANGEACTIVE:
5636 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5639 case SIOCBONDSLAVEINFOQUERY:
5640 case SIOCBONDINFOQUERY:
5641 dev_load(net, ifr.ifr_name);
5643 ret = dev_ifsioc(net, &ifr, cmd);
5648 /* Get the per device memory space. We can add this but
5649 * currently do not support it */
5651 /* Set the per device memory buffer space.
5652 * Not applicable in our case */
5657 * Unknown or private ioctl.
5660 if (cmd == SIOCWANDEV ||
5661 (cmd >= SIOCDEVPRIVATE &&
5662 cmd <= SIOCDEVPRIVATE + 15)) {
5663 dev_load(net, ifr.ifr_name);
5665 ret = dev_ifsioc(net, &ifr, cmd);
5667 if (!ret && copy_to_user(arg, &ifr,
5668 sizeof(struct ifreq)))
5672 /* Take care of Wireless Extensions */
5673 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
5674 return wext_handle_ioctl(net, &ifr, cmd, arg);
5681 * dev_new_index - allocate an ifindex
5682 * @net: the applicable net namespace
5684 * Returns a suitable unique value for a new device interface
5685 * number. The caller must hold the rtnl semaphore or the
5686 * dev_base_lock to be sure it remains unique.
5688 static int dev_new_index(struct net *net)
5690 int ifindex = net->ifindex;
5694 if (!__dev_get_by_index(net, ifindex))
5695 return net->ifindex = ifindex;
5699 /* Delayed registration/unregisteration */
5700 static LIST_HEAD(net_todo_list);
5702 static void net_set_todo(struct net_device *dev)
5704 list_add_tail(&dev->todo_list, &net_todo_list);
5707 static void rollback_registered_many(struct list_head *head)
5709 struct net_device *dev, *tmp;
5711 BUG_ON(dev_boot_phase);
5714 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
5715 /* Some devices call without registering
5716 * for initialization unwind. Remove those
5717 * devices and proceed with the remaining.
5719 if (dev->reg_state == NETREG_UNINITIALIZED) {
5720 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
5724 list_del(&dev->unreg_list);
5727 dev->dismantle = true;
5728 BUG_ON(dev->reg_state != NETREG_REGISTERED);
5731 /* If device is running, close it first. */
5732 dev_close_many(head);
5734 list_for_each_entry(dev, head, unreg_list) {
5735 /* And unlink it from device chain. */
5736 unlist_netdevice(dev);
5738 dev->reg_state = NETREG_UNREGISTERING;
5743 list_for_each_entry(dev, head, unreg_list) {
5744 /* Shutdown queueing discipline. */
5748 /* Notify protocols, that we are about to destroy
5749 this device. They should clean all the things.
5751 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
5753 if (!dev->rtnl_link_ops ||
5754 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5755 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
5758 * Flush the unicast and multicast chains
5763 if (dev->netdev_ops->ndo_uninit)
5764 dev->netdev_ops->ndo_uninit(dev);
5766 /* Notifier chain MUST detach us all upper devices. */
5767 WARN_ON(netdev_has_any_upper_dev(dev));
5769 /* Remove entries from kobject tree */
5770 netdev_unregister_kobject(dev);
5772 /* Remove XPS queueing entries */
5773 netif_reset_xps_queues_gt(dev, 0);
5779 list_for_each_entry(dev, head, unreg_list)
5783 static void rollback_registered(struct net_device *dev)
5787 list_add(&dev->unreg_list, &single);
5788 rollback_registered_many(&single);
5792 static netdev_features_t netdev_fix_features(struct net_device *dev,
5793 netdev_features_t features)
5795 /* Fix illegal checksum combinations */
5796 if ((features & NETIF_F_HW_CSUM) &&
5797 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5798 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
5799 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5802 /* Fix illegal SG+CSUM combinations. */
5803 if ((features & NETIF_F_SG) &&
5804 !(features & NETIF_F_ALL_CSUM)) {
5806 "Dropping NETIF_F_SG since no checksum feature.\n");
5807 features &= ~NETIF_F_SG;
5810 /* TSO requires that SG is present as well. */
5811 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
5812 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
5813 features &= ~NETIF_F_ALL_TSO;
5816 /* TSO ECN requires that TSO is present as well. */
5817 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
5818 features &= ~NETIF_F_TSO_ECN;
5820 /* Software GSO depends on SG. */
5821 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
5822 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
5823 features &= ~NETIF_F_GSO;
5826 /* UFO needs SG and checksumming */
5827 if (features & NETIF_F_UFO) {
5828 /* maybe split UFO into V4 and V6? */
5829 if (!((features & NETIF_F_GEN_CSUM) ||
5830 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
5831 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5833 "Dropping NETIF_F_UFO since no checksum offload features.\n");
5834 features &= ~NETIF_F_UFO;
5837 if (!(features & NETIF_F_SG)) {
5839 "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
5840 features &= ~NETIF_F_UFO;
5847 int __netdev_update_features(struct net_device *dev)
5849 netdev_features_t features;
5854 features = netdev_get_wanted_features(dev);
5856 if (dev->netdev_ops->ndo_fix_features)
5857 features = dev->netdev_ops->ndo_fix_features(dev, features);
5859 /* driver might be less strict about feature dependencies */
5860 features = netdev_fix_features(dev, features);
5862 if (dev->features == features)
5865 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
5866 &dev->features, &features);
5868 if (dev->netdev_ops->ndo_set_features)
5869 err = dev->netdev_ops->ndo_set_features(dev, features);
5871 if (unlikely(err < 0)) {
5873 "set_features() failed (%d); wanted %pNF, left %pNF\n",
5874 err, &features, &dev->features);
5879 dev->features = features;
5885 * netdev_update_features - recalculate device features
5886 * @dev: the device to check
5888 * Recalculate dev->features set and send notifications if it
5889 * has changed. Should be called after driver or hardware dependent
5890 * conditions might have changed that influence the features.
5892 void netdev_update_features(struct net_device *dev)
5894 if (__netdev_update_features(dev))
5895 netdev_features_change(dev);
5897 EXPORT_SYMBOL(netdev_update_features);
5900 * netdev_change_features - recalculate device features
5901 * @dev: the device to check
5903 * Recalculate dev->features set and send notifications even
5904 * if they have not changed. Should be called instead of
5905 * netdev_update_features() if also dev->vlan_features might
5906 * have changed to allow the changes to be propagated to stacked
5909 void netdev_change_features(struct net_device *dev)
5911 __netdev_update_features(dev);
5912 netdev_features_change(dev);
5914 EXPORT_SYMBOL(netdev_change_features);
5917 * netif_stacked_transfer_operstate - transfer operstate
5918 * @rootdev: the root or lower level device to transfer state from
5919 * @dev: the device to transfer operstate to
5921 * Transfer operational state from root to device. This is normally
5922 * called when a stacking relationship exists between the root
5923 * device and the device(a leaf device).
5925 void netif_stacked_transfer_operstate(const struct net_device *rootdev,
5926 struct net_device *dev)
5928 if (rootdev->operstate == IF_OPER_DORMANT)
5929 netif_dormant_on(dev);
5931 netif_dormant_off(dev);
5933 if (netif_carrier_ok(rootdev)) {
5934 if (!netif_carrier_ok(dev))
5935 netif_carrier_on(dev);
5937 if (netif_carrier_ok(dev))
5938 netif_carrier_off(dev);
5941 EXPORT_SYMBOL(netif_stacked_transfer_operstate);
5944 static int netif_alloc_rx_queues(struct net_device *dev)
5946 unsigned int i, count = dev->num_rx_queues;
5947 struct netdev_rx_queue *rx;
5951 rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
5953 pr_err("netdev: Unable to allocate %u rx queues\n", count);
5958 for (i = 0; i < count; i++)
5964 static void netdev_init_one_queue(struct net_device *dev,
5965 struct netdev_queue *queue, void *_unused)
5967 /* Initialize queue lock */
5968 spin_lock_init(&queue->_xmit_lock);
5969 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
5970 queue->xmit_lock_owner = -1;
5971 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
5974 dql_init(&queue->dql, HZ);
5978 static int netif_alloc_netdev_queues(struct net_device *dev)
5980 unsigned int count = dev->num_tx_queues;
5981 struct netdev_queue *tx;
5985 tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL);
5987 pr_err("netdev: Unable to allocate %u tx queues\n", count);
5992 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
5993 spin_lock_init(&dev->tx_global_lock);
5999 * register_netdevice - register a network device
6000 * @dev: device to register
6002 * Take a completed network device structure and add it to the kernel
6003 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
6004 * chain. 0 is returned on success. A negative errno code is returned
6005 * on a failure to set up the device, or if the name is a duplicate.
6007 * Callers must hold the rtnl semaphore. You may want
6008 * register_netdev() instead of this.
6011 * The locking appears insufficient to guarantee two parallel registers
6012 * will not get the same name.
6015 int register_netdevice(struct net_device *dev)
6018 struct net *net = dev_net(dev);
6020 BUG_ON(dev_boot_phase);
6025 /* When net_device's are persistent, this will be fatal. */
6026 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
6029 spin_lock_init(&dev->addr_list_lock);
6030 netdev_set_addr_lockdep_class(dev);
6034 ret = dev_get_valid_name(net, dev, dev->name);
6038 /* Init, if this function is available */
6039 if (dev->netdev_ops->ndo_init) {
6040 ret = dev->netdev_ops->ndo_init(dev);
6050 dev->ifindex = dev_new_index(net);
6051 else if (__dev_get_by_index(net, dev->ifindex))
6054 if (dev->iflink == -1)
6055 dev->iflink = dev->ifindex;
6057 /* Transfer changeable features to wanted_features and enable
6058 * software offloads (GSO and GRO).
6060 dev->hw_features |= NETIF_F_SOFT_FEATURES;
6061 dev->features |= NETIF_F_SOFT_FEATURES;
6062 dev->wanted_features = dev->features & dev->hw_features;
6064 /* Turn on no cache copy if HW is doing checksum */
6065 if (!(dev->flags & IFF_LOOPBACK)) {
6066 dev->hw_features |= NETIF_F_NOCACHE_COPY;
6067 if (dev->features & NETIF_F_ALL_CSUM) {
6068 dev->wanted_features |= NETIF_F_NOCACHE_COPY;
6069 dev->features |= NETIF_F_NOCACHE_COPY;
6073 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
6075 dev->vlan_features |= NETIF_F_HIGHDMA;
6077 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
6078 ret = notifier_to_errno(ret);
6082 ret = netdev_register_kobject(dev);
6085 dev->reg_state = NETREG_REGISTERED;
6087 __netdev_update_features(dev);
6090 * Default initial state at registry is that the
6091 * device is present.
6094 set_bit(__LINK_STATE_PRESENT, &dev->state);
6096 linkwatch_init_dev(dev);
6098 dev_init_scheduler(dev);
6100 list_netdevice(dev);
6101 add_device_randomness(dev->dev_addr, dev->addr_len);
6103 /* If the device has permanent device address, driver should
6104 * set dev_addr and also addr_assign_type should be set to
6105 * NET_ADDR_PERM (default value).
6107 if (dev->addr_assign_type == NET_ADDR_PERM)
6108 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6110 /* Notify protocols, that a new device appeared. */
6111 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
6112 ret = notifier_to_errno(ret);
6114 rollback_registered(dev);
6115 dev->reg_state = NETREG_UNREGISTERED;
6118 * Prevent userspace races by waiting until the network
6119 * device is fully setup before sending notifications.
6121 if (!dev->rtnl_link_ops ||
6122 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
6123 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
6129 if (dev->netdev_ops->ndo_uninit)
6130 dev->netdev_ops->ndo_uninit(dev);
6133 EXPORT_SYMBOL(register_netdevice);
6136 * init_dummy_netdev - init a dummy network device for NAPI
6137 * @dev: device to init
6139 * This takes a network device structure and initialize the minimum
6140 * amount of fields so it can be used to schedule NAPI polls without
6141 * registering a full blown interface. This is to be used by drivers
6142 * that need to tie several hardware interfaces to a single NAPI
6143 * poll scheduler due to HW limitations.
6145 int init_dummy_netdev(struct net_device *dev)
6147 /* Clear everything. Note we don't initialize spinlocks
6148 * are they aren't supposed to be taken by any of the
6149 * NAPI code and this dummy netdev is supposed to be
6150 * only ever used for NAPI polls
6152 memset(dev, 0, sizeof(struct net_device));
6154 /* make sure we BUG if trying to hit standard
6155 * register/unregister code path
6157 dev->reg_state = NETREG_DUMMY;
6159 /* NAPI wants this */
6160 INIT_LIST_HEAD(&dev->napi_list);
6162 /* a dummy interface is started by default */
6163 set_bit(__LINK_STATE_PRESENT, &dev->state);
6164 set_bit(__LINK_STATE_START, &dev->state);
6166 /* Note : We dont allocate pcpu_refcnt for dummy devices,
6167 * because users of this 'device' dont need to change
6173 EXPORT_SYMBOL_GPL(init_dummy_netdev);
6177 * register_netdev - register a network device
6178 * @dev: device to register
6180 * Take a completed network device structure and add it to the kernel
6181 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
6182 * chain. 0 is returned on success. A negative errno code is returned
6183 * on a failure to set up the device, or if the name is a duplicate.
6185 * This is a wrapper around register_netdevice that takes the rtnl semaphore
6186 * and expands the device name if you passed a format string to
6189 int register_netdev(struct net_device *dev)
6194 err = register_netdevice(dev);
6198 EXPORT_SYMBOL(register_netdev);
6200 int netdev_refcnt_read(const struct net_device *dev)
6204 for_each_possible_cpu(i)
6205 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
6208 EXPORT_SYMBOL(netdev_refcnt_read);
6211 * netdev_wait_allrefs - wait until all references are gone.
6212 * @dev: target net_device
6214 * This is called when unregistering network devices.
6216 * Any protocol or device that holds a reference should register
6217 * for netdevice notification, and cleanup and put back the
6218 * reference if they receive an UNREGISTER event.
6219 * We can get stuck here if buggy protocols don't correctly
6222 static void netdev_wait_allrefs(struct net_device *dev)
6224 unsigned long rebroadcast_time, warning_time;
6227 linkwatch_forget_dev(dev);
6229 rebroadcast_time = warning_time = jiffies;
6230 refcnt = netdev_refcnt_read(dev);
6232 while (refcnt != 0) {
6233 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6236 /* Rebroadcast unregister notification */
6237 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6243 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
6244 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
6246 /* We must not have linkwatch events
6247 * pending on unregister. If this
6248 * happens, we simply run the queue
6249 * unscheduled, resulting in a noop
6252 linkwatch_run_queue();
6257 rebroadcast_time = jiffies;
6262 refcnt = netdev_refcnt_read(dev);
6264 if (time_after(jiffies, warning_time + 10 * HZ)) {
6265 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
6267 warning_time = jiffies;
6276 * register_netdevice(x1);
6277 * register_netdevice(x2);
6279 * unregister_netdevice(y1);
6280 * unregister_netdevice(y2);
6286 * We are invoked by rtnl_unlock().
6287 * This allows us to deal with problems:
6288 * 1) We can delete sysfs objects which invoke hotplug
6289 * without deadlocking with linkwatch via keventd.
6290 * 2) Since we run with the RTNL semaphore not held, we can sleep
6291 * safely in order to wait for the netdev refcnt to drop to zero.
6293 * We must not return until all unregister events added during
6294 * the interval the lock was held have been completed.
6296 void netdev_run_todo(void)
6298 struct list_head list;
6300 /* Snapshot list, allow later requests */
6301 list_replace_init(&net_todo_list, &list);
6306 /* Wait for rcu callbacks to finish before next phase */
6307 if (!list_empty(&list))
6310 while (!list_empty(&list)) {
6311 struct net_device *dev
6312 = list_first_entry(&list, struct net_device, todo_list);
6313 list_del(&dev->todo_list);
6316 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
6319 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
6320 pr_err("network todo '%s' but state %d\n",
6321 dev->name, dev->reg_state);
6326 dev->reg_state = NETREG_UNREGISTERED;
6328 on_each_cpu(flush_backlog, dev, 1);
6330 netdev_wait_allrefs(dev);
6333 BUG_ON(netdev_refcnt_read(dev));
6334 WARN_ON(rcu_access_pointer(dev->ip_ptr));
6335 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
6336 WARN_ON(dev->dn_ptr);
6338 if (dev->destructor)
6339 dev->destructor(dev);
6341 /* Free network device */
6342 kobject_put(&dev->dev.kobj);
6346 /* Convert net_device_stats to rtnl_link_stats64. They have the same
6347 * fields in the same order, with only the type differing.
6349 void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
6350 const struct net_device_stats *netdev_stats)
6352 #if BITS_PER_LONG == 64
6353 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
6354 memcpy(stats64, netdev_stats, sizeof(*stats64));
6356 size_t i, n = sizeof(*stats64) / sizeof(u64);
6357 const unsigned long *src = (const unsigned long *)netdev_stats;
6358 u64 *dst = (u64 *)stats64;
6360 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
6361 sizeof(*stats64) / sizeof(u64));
6362 for (i = 0; i < n; i++)
6366 EXPORT_SYMBOL(netdev_stats_to_stats64);
6369 * dev_get_stats - get network device statistics
6370 * @dev: device to get statistics from
6371 * @storage: place to store stats
6373 * Get network statistics from device. Return @storage.
6374 * The device driver may provide its own method by setting
6375 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
6376 * otherwise the internal statistics structure is used.
6378 struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
6379 struct rtnl_link_stats64 *storage)
6381 const struct net_device_ops *ops = dev->netdev_ops;
6383 if (ops->ndo_get_stats64) {
6384 memset(storage, 0, sizeof(*storage));
6385 ops->ndo_get_stats64(dev, storage);
6386 } else if (ops->ndo_get_stats) {
6387 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
6389 netdev_stats_to_stats64(storage, &dev->stats);
6391 storage->rx_dropped += atomic_long_read(&dev->rx_dropped);
6394 EXPORT_SYMBOL(dev_get_stats);
6396 struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
6398 struct netdev_queue *queue = dev_ingress_queue(dev);
6400 #ifdef CONFIG_NET_CLS_ACT
6403 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
6406 netdev_init_one_queue(dev, queue, NULL);
6407 queue->qdisc = &noop_qdisc;
6408 queue->qdisc_sleeping = &noop_qdisc;
6409 rcu_assign_pointer(dev->ingress_queue, queue);
6414 static const struct ethtool_ops default_ethtool_ops;
6416 void netdev_set_default_ethtool_ops(struct net_device *dev,
6417 const struct ethtool_ops *ops)
6419 if (dev->ethtool_ops == &default_ethtool_ops)
6420 dev->ethtool_ops = ops;
6422 EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
6425 * alloc_netdev_mqs - allocate network device
6426 * @sizeof_priv: size of private data to allocate space for
6427 * @name: device name format string
6428 * @setup: callback to initialize device
6429 * @txqs: the number of TX subqueues to allocate
6430 * @rxqs: the number of RX subqueues to allocate
6432 * Allocates a struct net_device with private data area for driver use
6433 * and performs basic initialization. Also allocates subquue structs
6434 * for each queue on the device.
6436 struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6437 void (*setup)(struct net_device *),
6438 unsigned int txqs, unsigned int rxqs)
6440 struct net_device *dev;
6442 struct net_device *p;
6444 BUG_ON(strlen(name) >= sizeof(dev->name));
6447 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
6453 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
6458 alloc_size = sizeof(struct net_device);
6460 /* ensure 32-byte alignment of private area */
6461 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
6462 alloc_size += sizeof_priv;
6464 /* ensure 32-byte alignment of whole construct */
6465 alloc_size += NETDEV_ALIGN - 1;
6467 p = kzalloc(alloc_size, GFP_KERNEL);
6469 pr_err("alloc_netdev: Unable to allocate device\n");
6473 dev = PTR_ALIGN(p, NETDEV_ALIGN);
6474 dev->padded = (char *)dev - (char *)p;
6476 dev->pcpu_refcnt = alloc_percpu(int);
6477 if (!dev->pcpu_refcnt)
6480 if (dev_addr_init(dev))
6486 dev_net_set(dev, &init_net);
6488 dev->gso_max_size = GSO_MAX_SIZE;
6489 dev->gso_max_segs = GSO_MAX_SEGS;
6491 INIT_LIST_HEAD(&dev->napi_list);
6492 INIT_LIST_HEAD(&dev->unreg_list);
6493 INIT_LIST_HEAD(&dev->link_watch_list);
6494 INIT_LIST_HEAD(&dev->upper_dev_list);
6495 dev->priv_flags = IFF_XMIT_DST_RELEASE;
6498 dev->num_tx_queues = txqs;
6499 dev->real_num_tx_queues = txqs;
6500 if (netif_alloc_netdev_queues(dev))
6504 dev->num_rx_queues = rxqs;
6505 dev->real_num_rx_queues = rxqs;
6506 if (netif_alloc_rx_queues(dev))
6510 strcpy(dev->name, name);
6511 dev->group = INIT_NETDEV_GROUP;
6512 if (!dev->ethtool_ops)
6513 dev->ethtool_ops = &default_ethtool_ops;
6521 free_percpu(dev->pcpu_refcnt);
6531 EXPORT_SYMBOL(alloc_netdev_mqs);
6534 * free_netdev - free network device
6537 * This function does the last stage of destroying an allocated device
6538 * interface. The reference to the device object is released.
6539 * If this is the last reference then it will be freed.
6541 void free_netdev(struct net_device *dev)
6543 struct napi_struct *p, *n;
6545 release_net(dev_net(dev));
6552 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
6554 /* Flush device addresses */
6555 dev_addr_flush(dev);
6557 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
6560 free_percpu(dev->pcpu_refcnt);
6561 dev->pcpu_refcnt = NULL;
6563 /* Compatibility with error handling in drivers */
6564 if (dev->reg_state == NETREG_UNINITIALIZED) {
6565 kfree((char *)dev - dev->padded);
6569 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
6570 dev->reg_state = NETREG_RELEASED;
6572 /* will free via device release */
6573 put_device(&dev->dev);
6575 EXPORT_SYMBOL(free_netdev);
6578 * synchronize_net - Synchronize with packet receive processing
6580 * Wait for packets currently being received to be done.
6581 * Does not block later packets from starting.
6583 void synchronize_net(void)
6586 if (rtnl_is_locked())
6587 synchronize_rcu_expedited();
6591 EXPORT_SYMBOL(synchronize_net);
6594 * unregister_netdevice_queue - remove device from the kernel
6598 * This function shuts down a device interface and removes it
6599 * from the kernel tables.
6600 * If head not NULL, device is queued to be unregistered later.
6602 * Callers must hold the rtnl semaphore. You may want
6603 * unregister_netdev() instead of this.
6606 void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
6611 list_move_tail(&dev->unreg_list, head);
6613 rollback_registered(dev);
6614 /* Finish processing unregister after unlock */
6618 EXPORT_SYMBOL(unregister_netdevice_queue);
6621 * unregister_netdevice_many - unregister many devices
6622 * @head: list of devices
6624 void unregister_netdevice_many(struct list_head *head)
6626 struct net_device *dev;
6628 if (!list_empty(head)) {
6629 rollback_registered_many(head);
6630 list_for_each_entry(dev, head, unreg_list)
6634 EXPORT_SYMBOL(unregister_netdevice_many);
6637 * unregister_netdev - remove device from the kernel
6640 * This function shuts down a device interface and removes it
6641 * from the kernel tables.
6643 * This is just a wrapper for unregister_netdevice that takes
6644 * the rtnl semaphore. In general you want to use this and not
6645 * unregister_netdevice.
6647 void unregister_netdev(struct net_device *dev)
6650 unregister_netdevice(dev);
6653 EXPORT_SYMBOL(unregister_netdev);
6656 * dev_change_net_namespace - move device to different nethost namespace
6658 * @net: network namespace
6659 * @pat: If not NULL name pattern to try if the current device name
6660 * is already taken in the destination network namespace.
6662 * This function shuts down a device interface and moves it
6663 * to a new network namespace. On success 0 is returned, on
6664 * a failure a netagive errno code is returned.
6666 * Callers must hold the rtnl semaphore.
6669 int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
6675 /* Don't allow namespace local devices to be moved. */
6677 if (dev->features & NETIF_F_NETNS_LOCAL)
6680 /* Ensure the device has been registrered */
6681 if (dev->reg_state != NETREG_REGISTERED)
6684 /* Get out if there is nothing todo */
6686 if (net_eq(dev_net(dev), net))
6689 /* Pick the destination device name, and ensure
6690 * we can use it in the destination network namespace.
6693 if (__dev_get_by_name(net, dev->name)) {
6694 /* We get here if we can't use the current device name */
6697 if (dev_get_valid_name(net, dev, pat) < 0)
6702 * And now a mini version of register_netdevice unregister_netdevice.
6705 /* If device is running close it first. */
6708 /* And unlink it from device chain */
6710 unlist_netdevice(dev);
6714 /* Shutdown queueing discipline. */
6717 /* Notify protocols, that we are about to destroy
6718 this device. They should clean all the things.
6720 Note that dev->reg_state stays at NETREG_REGISTERED.
6721 This is wanted because this way 8021q and macvlan know
6722 the device is just moving and can keep their slaves up.
6724 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6726 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
6727 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
6730 * Flush the unicast and multicast chains
6735 /* Send a netdev-removed uevent to the old namespace */
6736 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
6738 /* Actually switch the network namespace */
6739 dev_net_set(dev, net);
6741 /* If there is an ifindex conflict assign a new one */
6742 if (__dev_get_by_index(net, dev->ifindex)) {
6743 int iflink = (dev->iflink == dev->ifindex);
6744 dev->ifindex = dev_new_index(net);
6746 dev->iflink = dev->ifindex;
6749 /* Send a netdev-add uevent to the new namespace */
6750 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
6752 /* Fixup kobjects */
6753 err = device_rename(&dev->dev, dev->name);
6756 /* Add the device back in the hashes */
6757 list_netdevice(dev);
6759 /* Notify protocols, that a new device appeared. */
6760 call_netdevice_notifiers(NETDEV_REGISTER, dev);
6763 * Prevent userspace races by waiting until the network
6764 * device is fully setup before sending notifications.
6766 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
6773 EXPORT_SYMBOL_GPL(dev_change_net_namespace);
6775 static int dev_cpu_callback(struct notifier_block *nfb,
6776 unsigned long action,
6779 struct sk_buff **list_skb;
6780 struct sk_buff *skb;
6781 unsigned int cpu, oldcpu = (unsigned long)ocpu;
6782 struct softnet_data *sd, *oldsd;
6784 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
6787 local_irq_disable();
6788 cpu = smp_processor_id();
6789 sd = &per_cpu(softnet_data, cpu);
6790 oldsd = &per_cpu(softnet_data, oldcpu);
6792 /* Find end of our completion_queue. */
6793 list_skb = &sd->completion_queue;
6795 list_skb = &(*list_skb)->next;
6796 /* Append completion queue from offline CPU. */
6797 *list_skb = oldsd->completion_queue;
6798 oldsd->completion_queue = NULL;
6800 /* Append output queue from offline CPU. */
6801 if (oldsd->output_queue) {
6802 *sd->output_queue_tailp = oldsd->output_queue;
6803 sd->output_queue_tailp = oldsd->output_queue_tailp;
6804 oldsd->output_queue = NULL;
6805 oldsd->output_queue_tailp = &oldsd->output_queue;
6807 /* Append NAPI poll list from offline CPU. */
6808 if (!list_empty(&oldsd->poll_list)) {
6809 list_splice_init(&oldsd->poll_list, &sd->poll_list);
6810 raise_softirq_irqoff(NET_RX_SOFTIRQ);
6813 raise_softirq_irqoff(NET_TX_SOFTIRQ);
6816 /* Process offline CPU's input_pkt_queue */
6817 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
6819 input_queue_head_incr(oldsd);
6821 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
6823 input_queue_head_incr(oldsd);
6831 * netdev_increment_features - increment feature set by one
6832 * @all: current feature set
6833 * @one: new feature set
6834 * @mask: mask feature set
6836 * Computes a new feature set after adding a device with feature set
6837 * @one to the master device with current feature set @all. Will not
6838 * enable anything that is off in @mask. Returns the new feature set.
6840 netdev_features_t netdev_increment_features(netdev_features_t all,
6841 netdev_features_t one, netdev_features_t mask)
6843 if (mask & NETIF_F_GEN_CSUM)
6844 mask |= NETIF_F_ALL_CSUM;
6845 mask |= NETIF_F_VLAN_CHALLENGED;
6847 all |= one & (NETIF_F_ONE_FOR_ALL|NETIF_F_ALL_CSUM) & mask;
6848 all &= one | ~NETIF_F_ALL_FOR_ALL;
6850 /* If one device supports hw checksumming, set for all. */
6851 if (all & NETIF_F_GEN_CSUM)
6852 all &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_GEN_CSUM);
6856 EXPORT_SYMBOL(netdev_increment_features);
6858 static struct hlist_head *netdev_create_hash(void)
6861 struct hlist_head *hash;
6863 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
6865 for (i = 0; i < NETDEV_HASHENTRIES; i++)
6866 INIT_HLIST_HEAD(&hash[i]);
6871 /* Initialize per network namespace state */
6872 static int __net_init netdev_init(struct net *net)
6874 if (net != &init_net)
6875 INIT_LIST_HEAD(&net->dev_base_head);
6877 net->dev_name_head = netdev_create_hash();
6878 if (net->dev_name_head == NULL)
6881 net->dev_index_head = netdev_create_hash();
6882 if (net->dev_index_head == NULL)
6888 kfree(net->dev_name_head);
6894 * netdev_drivername - network driver for the device
6895 * @dev: network device
6897 * Determine network driver for device.
6899 const char *netdev_drivername(const struct net_device *dev)
6901 const struct device_driver *driver;
6902 const struct device *parent;
6903 const char *empty = "";
6905 parent = dev->dev.parent;
6909 driver = parent->driver;
6910 if (driver && driver->name)
6911 return driver->name;
6915 static int __netdev_printk(const char *level, const struct net_device *dev,
6916 struct va_format *vaf)
6920 if (dev && dev->dev.parent) {
6921 r = dev_printk_emit(level[1] - '0',
6924 dev_driver_string(dev->dev.parent),
6925 dev_name(dev->dev.parent),
6926 netdev_name(dev), vaf);
6928 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
6930 r = printk("%s(NULL net_device): %pV", level, vaf);
6936 int netdev_printk(const char *level, const struct net_device *dev,
6937 const char *format, ...)
6939 struct va_format vaf;
6943 va_start(args, format);
6948 r = __netdev_printk(level, dev, &vaf);
6954 EXPORT_SYMBOL(netdev_printk);
6956 #define define_netdev_printk_level(func, level) \
6957 int func(const struct net_device *dev, const char *fmt, ...) \
6960 struct va_format vaf; \
6963 va_start(args, fmt); \
6968 r = __netdev_printk(level, dev, &vaf); \
6974 EXPORT_SYMBOL(func);
6976 define_netdev_printk_level(netdev_emerg, KERN_EMERG);
6977 define_netdev_printk_level(netdev_alert, KERN_ALERT);
6978 define_netdev_printk_level(netdev_crit, KERN_CRIT);
6979 define_netdev_printk_level(netdev_err, KERN_ERR);
6980 define_netdev_printk_level(netdev_warn, KERN_WARNING);
6981 define_netdev_printk_level(netdev_notice, KERN_NOTICE);
6982 define_netdev_printk_level(netdev_info, KERN_INFO);
6984 static void __net_exit netdev_exit(struct net *net)
6986 kfree(net->dev_name_head);
6987 kfree(net->dev_index_head);
6990 static struct pernet_operations __net_initdata netdev_net_ops = {
6991 .init = netdev_init,
6992 .exit = netdev_exit,
6995 static void __net_exit default_device_exit(struct net *net)
6997 struct net_device *dev, *aux;
6999 * Push all migratable network devices back to the
7000 * initial network namespace
7003 for_each_netdev_safe(net, dev, aux) {
7005 char fb_name[IFNAMSIZ];
7007 /* Ignore unmoveable devices (i.e. loopback) */
7008 if (dev->features & NETIF_F_NETNS_LOCAL)
7011 /* Leave virtual devices for the generic cleanup */
7012 if (dev->rtnl_link_ops)
7015 /* Push remaining network devices to init_net */
7016 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
7017 err = dev_change_net_namespace(dev, &init_net, fb_name);
7019 pr_emerg("%s: failed to move %s to init_net: %d\n",
7020 __func__, dev->name, err);
7027 static void __net_exit default_device_exit_batch(struct list_head *net_list)
7029 /* At exit all network devices most be removed from a network
7030 * namespace. Do this in the reverse order of registration.
7031 * Do this across as many network namespaces as possible to
7032 * improve batching efficiency.
7034 struct net_device *dev;
7036 LIST_HEAD(dev_kill_list);
7039 list_for_each_entry(net, net_list, exit_list) {
7040 for_each_netdev_reverse(net, dev) {
7041 if (dev->rtnl_link_ops)
7042 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
7044 unregister_netdevice_queue(dev, &dev_kill_list);
7047 unregister_netdevice_many(&dev_kill_list);
7048 list_del(&dev_kill_list);
7052 static struct pernet_operations __net_initdata default_device_ops = {
7053 .exit = default_device_exit,
7054 .exit_batch = default_device_exit_batch,
7058 * Initialize the DEV module. At boot time this walks the device list and
7059 * unhooks any devices that fail to initialise (normally hardware not
7060 * present) and leaves us with a valid list of present and active devices.
7065 * This is called single threaded during boot, so no need
7066 * to take the rtnl semaphore.
7068 static int __init net_dev_init(void)
7070 int i, rc = -ENOMEM;
7072 BUG_ON(!dev_boot_phase);
7074 if (dev_proc_init())
7077 if (netdev_kobject_init())
7080 INIT_LIST_HEAD(&ptype_all);
7081 for (i = 0; i < PTYPE_HASH_SIZE; i++)
7082 INIT_LIST_HEAD(&ptype_base[i]);
7084 INIT_LIST_HEAD(&offload_base);
7086 if (register_pernet_subsys(&netdev_net_ops))
7090 * Initialise the packet receive queues.
7093 for_each_possible_cpu(i) {
7094 struct softnet_data *sd = &per_cpu(softnet_data, i);
7096 memset(sd, 0, sizeof(*sd));
7097 skb_queue_head_init(&sd->input_pkt_queue);
7098 skb_queue_head_init(&sd->process_queue);
7099 sd->completion_queue = NULL;
7100 INIT_LIST_HEAD(&sd->poll_list);
7101 sd->output_queue = NULL;
7102 sd->output_queue_tailp = &sd->output_queue;
7104 sd->csd.func = rps_trigger_softirq;
7110 sd->backlog.poll = process_backlog;
7111 sd->backlog.weight = weight_p;
7112 sd->backlog.gro_list = NULL;
7113 sd->backlog.gro_count = 0;
7118 /* The loopback device is special if any other network devices
7119 * is present in a network namespace the loopback device must
7120 * be present. Since we now dynamically allocate and free the
7121 * loopback device ensure this invariant is maintained by
7122 * keeping the loopback device as the first device on the
7123 * list of network devices. Ensuring the loopback devices
7124 * is the first device that appears and the last network device
7127 if (register_pernet_device(&loopback_net_ops))
7130 if (register_pernet_device(&default_device_ops))
7133 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
7134 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
7136 hotcpu_notifier(dev_cpu_callback, 0);
7144 subsys_initcall(net_dev_init);