1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NET3 Protocol independent device support routines.
5 * Derived from the non IP parts of dev.c 1.0.19
7 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Florian la Roche <rzsfl@rz.uni-sb.de>
12 * Alan Cox <gw4pts@gw4pts.ampr.org>
13 * David Hinds <dahinds@users.sourceforge.net>
14 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
15 * Adam Sulmicki <adam@cfar.umd.edu>
16 * Pekka Riikonen <priikone@poesidon.pspt.fi>
19 * D.J. Barrow : Fixed bug where dev->refcnt gets set
20 * to 2 if register_netdev gets called
21 * before net_dev_init & also removed a
22 * few lines of code in the process.
23 * Alan Cox : device private ioctl copies fields back.
24 * Alan Cox : Transmit queue code does relevant
25 * stunts to keep the queue safe.
26 * Alan Cox : Fixed double lock.
27 * Alan Cox : Fixed promisc NULL pointer trap
28 * ???????? : Support the full private ioctl range
29 * Alan Cox : Moved ioctl permission check into
31 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
32 * Alan Cox : 100 backlog just doesn't cut it when
33 * you start doing multicast video 8)
34 * Alan Cox : Rewrote net_bh and list manager.
35 * Alan Cox : Fix ETH_P_ALL echoback lengths.
36 * Alan Cox : Took out transmit every packet pass
37 * Saved a few bytes in the ioctl handler
38 * Alan Cox : Network driver sets packet type before
39 * calling netif_rx. Saves a function
41 * Alan Cox : Hashed net_bh()
42 * Richard Kooijman: Timestamp fixes.
43 * Alan Cox : Wrong field in SIOCGIFDSTADDR
44 * Alan Cox : Device lock protection.
45 * Alan Cox : Fixed nasty side effect of device close
47 * Rudi Cilibrasi : Pass the right thing to
49 * Dave Miller : 32bit quantity for the device lock to
50 * make it work out on a Sparc.
51 * Bjorn Ekwall : Added KERNELD hack.
52 * Alan Cox : Cleaned up the backlog initialise.
53 * Craig Metz : SIOCGIFCONF fix if space for under
55 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
56 * is no device open function.
57 * Andi Kleen : Fix error reporting for SIOCGIFCONF
58 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
59 * Cyrus Durgin : Cleaned for KMOD
60 * Adam Sulmicki : Bug Fix : Network Device Unload
61 * A network device unload needs to purge
63 * Paul Rusty Russell : SIOCSIFNAME
64 * Pekka Riikonen : Netdev boot-time settings code
65 * Andrew Morton : Make unregister_netdevice wait
66 * indefinitely on dev->refcnt
67 * J Hadi Salim : - Backlog queue sampling
68 * - netif_rx() feedback
71 #include <linux/uaccess.h>
72 #include <linux/bitops.h>
73 #include <linux/capability.h>
74 #include <linux/cpu.h>
75 #include <linux/types.h>
76 #include <linux/kernel.h>
77 #include <linux/hash.h>
78 #include <linux/slab.h>
79 #include <linux/sched.h>
80 #include <linux/sched/mm.h>
81 #include <linux/mutex.h>
82 #include <linux/string.h>
84 #include <linux/socket.h>
85 #include <linux/sockios.h>
86 #include <linux/errno.h>
87 #include <linux/interrupt.h>
88 #include <linux/if_ether.h>
89 #include <linux/netdevice.h>
90 #include <linux/etherdevice.h>
91 #include <linux/ethtool.h>
92 #include <linux/skbuff.h>
93 #include <linux/bpf.h>
94 #include <linux/bpf_trace.h>
95 #include <net/net_namespace.h>
97 #include <net/busy_poll.h>
98 #include <linux/rtnetlink.h>
99 #include <linux/stat.h>
101 #include <net/dst_metadata.h>
102 #include <net/pkt_sched.h>
103 #include <net/pkt_cls.h>
104 #include <net/checksum.h>
105 #include <net/xfrm.h>
106 #include <linux/highmem.h>
107 #include <linux/init.h>
108 #include <linux/module.h>
109 #include <linux/netpoll.h>
110 #include <linux/rcupdate.h>
111 #include <linux/delay.h>
112 #include <net/iw_handler.h>
113 #include <asm/current.h>
114 #include <linux/audit.h>
115 #include <linux/dmaengine.h>
116 #include <linux/err.h>
117 #include <linux/ctype.h>
118 #include <linux/if_arp.h>
119 #include <linux/if_vlan.h>
120 #include <linux/ip.h>
122 #include <net/mpls.h>
123 #include <linux/ipv6.h>
124 #include <linux/in.h>
125 #include <linux/jhash.h>
126 #include <linux/random.h>
127 #include <trace/events/napi.h>
128 #include <trace/events/net.h>
129 #include <trace/events/skb.h>
130 #include <linux/inetdevice.h>
131 #include <linux/cpu_rmap.h>
132 #include <linux/static_key.h>
133 #include <linux/hashtable.h>
134 #include <linux/vmalloc.h>
135 #include <linux/if_macvlan.h>
136 #include <linux/errqueue.h>
137 #include <linux/hrtimer.h>
138 #include <linux/netfilter_ingress.h>
139 #include <linux/crash_dump.h>
140 #include <linux/sctp.h>
141 #include <net/udp_tunnel.h>
142 #include <linux/net_namespace.h>
143 #include <linux/indirect_call_wrapper.h>
144 #include <net/devlink.h>
146 #include "net-sysfs.h"
148 #define MAX_GRO_SKBS 8
149 #define MAX_NEST_DEV 8
151 /* This should be increased if a protocol with a bigger head is added. */
152 #define GRO_MAX_HEAD (MAX_HEADER + 128)
154 static DEFINE_SPINLOCK(ptype_lock
);
155 static DEFINE_SPINLOCK(offload_lock
);
156 struct list_head ptype_base
[PTYPE_HASH_SIZE
] __read_mostly
;
157 struct list_head ptype_all __read_mostly
; /* Taps */
158 static struct list_head offload_base __read_mostly
;
160 static int netif_rx_internal(struct sk_buff
*skb
);
161 static int call_netdevice_notifiers_info(unsigned long val
,
162 struct netdev_notifier_info
*info
);
163 static int call_netdevice_notifiers_extack(unsigned long val
,
164 struct net_device
*dev
,
165 struct netlink_ext_ack
*extack
);
166 static struct napi_struct
*napi_by_id(unsigned int napi_id
);
169 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
172 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
174 * Writers must hold the rtnl semaphore while they loop through the
175 * dev_base_head list, and hold dev_base_lock for writing when they do the
176 * actual updates. This allows pure readers to access the list even
177 * while a writer is preparing to update it.
179 * To put it another way, dev_base_lock is held for writing only to
180 * protect against pure readers; the rtnl semaphore provides the
181 * protection against other writers.
183 * See, for example usages, register_netdevice() and
184 * unregister_netdevice(), which must be called with the rtnl
187 DEFINE_RWLOCK(dev_base_lock
);
188 EXPORT_SYMBOL(dev_base_lock
);
190 static DEFINE_MUTEX(ifalias_mutex
);
192 /* protects napi_hash addition/deletion and napi_gen_id */
193 static DEFINE_SPINLOCK(napi_hash_lock
);
195 static unsigned int napi_gen_id
= NR_CPUS
;
196 static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash
, 8);
198 static seqcount_t devnet_rename_seq
;
200 static inline void dev_base_seq_inc(struct net
*net
)
202 while (++net
->dev_base_seq
== 0)
206 static inline struct hlist_head
*dev_name_hash(struct net
*net
, const char *name
)
208 unsigned int hash
= full_name_hash(net
, name
, strnlen(name
, IFNAMSIZ
));
210 return &net
->dev_name_head
[hash_32(hash
, NETDEV_HASHBITS
)];
213 static inline struct hlist_head
*dev_index_hash(struct net
*net
, int ifindex
)
215 return &net
->dev_index_head
[ifindex
& (NETDEV_HASHENTRIES
- 1)];
218 static inline void rps_lock(struct softnet_data
*sd
)
221 spin_lock(&sd
->input_pkt_queue
.lock
);
225 static inline void rps_unlock(struct softnet_data
*sd
)
228 spin_unlock(&sd
->input_pkt_queue
.lock
);
232 static struct netdev_name_node
*netdev_name_node_alloc(struct net_device
*dev
,
235 struct netdev_name_node
*name_node
;
237 name_node
= kmalloc(sizeof(*name_node
), GFP_KERNEL
);
240 INIT_HLIST_NODE(&name_node
->hlist
);
241 name_node
->dev
= dev
;
242 name_node
->name
= name
;
246 static struct netdev_name_node
*
247 netdev_name_node_head_alloc(struct net_device
*dev
)
249 struct netdev_name_node
*name_node
;
251 name_node
= netdev_name_node_alloc(dev
, dev
->name
);
254 INIT_LIST_HEAD(&name_node
->list
);
258 static void netdev_name_node_free(struct netdev_name_node
*name_node
)
263 static void netdev_name_node_add(struct net
*net
,
264 struct netdev_name_node
*name_node
)
266 hlist_add_head_rcu(&name_node
->hlist
,
267 dev_name_hash(net
, name_node
->name
));
270 static void netdev_name_node_del(struct netdev_name_node
*name_node
)
272 hlist_del_rcu(&name_node
->hlist
);
275 static struct netdev_name_node
*netdev_name_node_lookup(struct net
*net
,
278 struct hlist_head
*head
= dev_name_hash(net
, name
);
279 struct netdev_name_node
*name_node
;
281 hlist_for_each_entry(name_node
, head
, hlist
)
282 if (!strcmp(name_node
->name
, name
))
287 static struct netdev_name_node
*netdev_name_node_lookup_rcu(struct net
*net
,
290 struct hlist_head
*head
= dev_name_hash(net
, name
);
291 struct netdev_name_node
*name_node
;
293 hlist_for_each_entry_rcu(name_node
, head
, hlist
)
294 if (!strcmp(name_node
->name
, name
))
299 int netdev_name_node_alt_create(struct net_device
*dev
, const char *name
)
301 struct netdev_name_node
*name_node
;
302 struct net
*net
= dev_net(dev
);
304 name_node
= netdev_name_node_lookup(net
, name
);
307 name_node
= netdev_name_node_alloc(dev
, name
);
310 netdev_name_node_add(net
, name_node
);
311 /* The node that holds dev->name acts as a head of per-device list. */
312 list_add_tail(&name_node
->list
, &dev
->name_node
->list
);
316 EXPORT_SYMBOL(netdev_name_node_alt_create
);
318 static void __netdev_name_node_alt_destroy(struct netdev_name_node
*name_node
)
320 list_del(&name_node
->list
);
321 netdev_name_node_del(name_node
);
322 kfree(name_node
->name
);
323 netdev_name_node_free(name_node
);
326 int netdev_name_node_alt_destroy(struct net_device
*dev
, const char *name
)
328 struct netdev_name_node
*name_node
;
329 struct net
*net
= dev_net(dev
);
331 name_node
= netdev_name_node_lookup(net
, name
);
334 __netdev_name_node_alt_destroy(name_node
);
338 EXPORT_SYMBOL(netdev_name_node_alt_destroy
);
340 static void netdev_name_node_alt_flush(struct net_device
*dev
)
342 struct netdev_name_node
*name_node
, *tmp
;
344 list_for_each_entry_safe(name_node
, tmp
, &dev
->name_node
->list
, list
)
345 __netdev_name_node_alt_destroy(name_node
);
348 /* Device list insertion */
349 static void list_netdevice(struct net_device
*dev
)
351 struct net
*net
= dev_net(dev
);
355 write_lock_bh(&dev_base_lock
);
356 list_add_tail_rcu(&dev
->dev_list
, &net
->dev_base_head
);
357 netdev_name_node_add(net
, dev
->name_node
);
358 hlist_add_head_rcu(&dev
->index_hlist
,
359 dev_index_hash(net
, dev
->ifindex
));
360 write_unlock_bh(&dev_base_lock
);
362 dev_base_seq_inc(net
);
365 /* Device list removal
366 * caller must respect a RCU grace period before freeing/reusing dev
368 static void unlist_netdevice(struct net_device
*dev
)
372 /* Unlink dev from the device chain */
373 write_lock_bh(&dev_base_lock
);
374 list_del_rcu(&dev
->dev_list
);
375 netdev_name_node_del(dev
->name_node
);
376 hlist_del_rcu(&dev
->index_hlist
);
377 write_unlock_bh(&dev_base_lock
);
379 dev_base_seq_inc(dev_net(dev
));
386 static RAW_NOTIFIER_HEAD(netdev_chain
);
389 * Device drivers call our routines to queue packets here. We empty the
390 * queue in the local softnet handler.
393 DEFINE_PER_CPU_ALIGNED(struct softnet_data
, softnet_data
);
394 EXPORT_PER_CPU_SYMBOL(softnet_data
);
396 /*******************************************************************************
398 * Protocol management and registration routines
400 *******************************************************************************/
404 * Add a protocol ID to the list. Now that the input handler is
405 * smarter we can dispense with all the messy stuff that used to be
408 * BEWARE!!! Protocol handlers, mangling input packets,
409 * MUST BE last in hash buckets and checking protocol handlers
410 * MUST start from promiscuous ptype_all chain in net_bh.
411 * It is true now, do not change it.
412 * Explanation follows: if protocol handler, mangling packet, will
413 * be the first on list, it is not able to sense, that packet
414 * is cloned and should be copied-on-write, so that it will
415 * change it and subsequent readers will get broken packet.
419 static inline struct list_head
*ptype_head(const struct packet_type
*pt
)
421 if (pt
->type
== htons(ETH_P_ALL
))
422 return pt
->dev
? &pt
->dev
->ptype_all
: &ptype_all
;
424 return pt
->dev
? &pt
->dev
->ptype_specific
:
425 &ptype_base
[ntohs(pt
->type
) & PTYPE_HASH_MASK
];
429 * dev_add_pack - add packet handler
430 * @pt: packet type declaration
432 * Add a protocol handler to the networking stack. The passed &packet_type
433 * is linked into kernel lists and may not be freed until it has been
434 * removed from the kernel lists.
436 * This call does not sleep therefore it can not
437 * guarantee all CPU's that are in middle of receiving packets
438 * will see the new packet type (until the next received packet).
441 void dev_add_pack(struct packet_type
*pt
)
443 struct list_head
*head
= ptype_head(pt
);
445 spin_lock(&ptype_lock
);
446 list_add_rcu(&pt
->list
, head
);
447 spin_unlock(&ptype_lock
);
449 EXPORT_SYMBOL(dev_add_pack
);
452 * __dev_remove_pack - remove packet handler
453 * @pt: packet type declaration
455 * Remove a protocol handler that was previously added to the kernel
456 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
457 * from the kernel lists and can be freed or reused once this function
460 * The packet type might still be in use by receivers
461 * and must not be freed until after all the CPU's have gone
462 * through a quiescent state.
464 void __dev_remove_pack(struct packet_type
*pt
)
466 struct list_head
*head
= ptype_head(pt
);
467 struct packet_type
*pt1
;
469 spin_lock(&ptype_lock
);
471 list_for_each_entry(pt1
, head
, list
) {
473 list_del_rcu(&pt
->list
);
478 pr_warn("dev_remove_pack: %p not found\n", pt
);
480 spin_unlock(&ptype_lock
);
482 EXPORT_SYMBOL(__dev_remove_pack
);
485 * dev_remove_pack - remove packet handler
486 * @pt: packet type declaration
488 * Remove a protocol handler that was previously added to the kernel
489 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
490 * from the kernel lists and can be freed or reused once this function
493 * This call sleeps to guarantee that no CPU is looking at the packet
496 void dev_remove_pack(struct packet_type
*pt
)
498 __dev_remove_pack(pt
);
502 EXPORT_SYMBOL(dev_remove_pack
);
506 * dev_add_offload - register offload handlers
507 * @po: protocol offload declaration
509 * Add protocol offload handlers to the networking stack. The passed
510 * &proto_offload is linked into kernel lists and may not be freed until
511 * it has been removed from the kernel lists.
513 * This call does not sleep therefore it can not
514 * guarantee all CPU's that are in middle of receiving packets
515 * will see the new offload handlers (until the next received packet).
517 void dev_add_offload(struct packet_offload
*po
)
519 struct packet_offload
*elem
;
521 spin_lock(&offload_lock
);
522 list_for_each_entry(elem
, &offload_base
, list
) {
523 if (po
->priority
< elem
->priority
)
526 list_add_rcu(&po
->list
, elem
->list
.prev
);
527 spin_unlock(&offload_lock
);
529 EXPORT_SYMBOL(dev_add_offload
);
532 * __dev_remove_offload - remove offload handler
533 * @po: packet offload declaration
535 * Remove a protocol offload handler that was previously added to the
536 * kernel offload handlers by dev_add_offload(). The passed &offload_type
537 * is removed from the kernel lists and can be freed or reused once this
540 * The packet type might still be in use by receivers
541 * and must not be freed until after all the CPU's have gone
542 * through a quiescent state.
544 static void __dev_remove_offload(struct packet_offload
*po
)
546 struct list_head
*head
= &offload_base
;
547 struct packet_offload
*po1
;
549 spin_lock(&offload_lock
);
551 list_for_each_entry(po1
, head
, list
) {
553 list_del_rcu(&po
->list
);
558 pr_warn("dev_remove_offload: %p not found\n", po
);
560 spin_unlock(&offload_lock
);
564 * dev_remove_offload - remove packet offload handler
565 * @po: packet offload declaration
567 * Remove a packet offload handler that was previously added to the kernel
568 * offload handlers by dev_add_offload(). The passed &offload_type is
569 * removed from the kernel lists and can be freed or reused once this
572 * This call sleeps to guarantee that no CPU is looking at the packet
575 void dev_remove_offload(struct packet_offload
*po
)
577 __dev_remove_offload(po
);
581 EXPORT_SYMBOL(dev_remove_offload
);
583 /******************************************************************************
585 * Device Boot-time Settings Routines
587 ******************************************************************************/
589 /* Boot time configuration table */
590 static struct netdev_boot_setup dev_boot_setup
[NETDEV_BOOT_SETUP_MAX
];
593 * netdev_boot_setup_add - add new setup entry
594 * @name: name of the device
595 * @map: configured settings for the device
597 * Adds new setup entry to the dev_boot_setup list. The function
598 * returns 0 on error and 1 on success. This is a generic routine to
601 static int netdev_boot_setup_add(char *name
, struct ifmap
*map
)
603 struct netdev_boot_setup
*s
;
607 for (i
= 0; i
< NETDEV_BOOT_SETUP_MAX
; i
++) {
608 if (s
[i
].name
[0] == '\0' || s
[i
].name
[0] == ' ') {
609 memset(s
[i
].name
, 0, sizeof(s
[i
].name
));
610 strlcpy(s
[i
].name
, name
, IFNAMSIZ
);
611 memcpy(&s
[i
].map
, map
, sizeof(s
[i
].map
));
616 return i
>= NETDEV_BOOT_SETUP_MAX
? 0 : 1;
620 * netdev_boot_setup_check - check boot time settings
621 * @dev: the netdevice
623 * Check boot time settings for the device.
624 * The found settings are set for the device to be used
625 * later in the device probing.
626 * Returns 0 if no settings found, 1 if they are.
628 int netdev_boot_setup_check(struct net_device
*dev
)
630 struct netdev_boot_setup
*s
= dev_boot_setup
;
633 for (i
= 0; i
< NETDEV_BOOT_SETUP_MAX
; i
++) {
634 if (s
[i
].name
[0] != '\0' && s
[i
].name
[0] != ' ' &&
635 !strcmp(dev
->name
, s
[i
].name
)) {
636 dev
->irq
= s
[i
].map
.irq
;
637 dev
->base_addr
= s
[i
].map
.base_addr
;
638 dev
->mem_start
= s
[i
].map
.mem_start
;
639 dev
->mem_end
= s
[i
].map
.mem_end
;
645 EXPORT_SYMBOL(netdev_boot_setup_check
);
649 * netdev_boot_base - get address from boot time settings
650 * @prefix: prefix for network device
651 * @unit: id for network device
653 * Check boot time settings for the base address of device.
654 * The found settings are set for the device to be used
655 * later in the device probing.
656 * Returns 0 if no settings found.
658 unsigned long netdev_boot_base(const char *prefix
, int unit
)
660 const struct netdev_boot_setup
*s
= dev_boot_setup
;
664 sprintf(name
, "%s%d", prefix
, unit
);
667 * If device already registered then return base of 1
668 * to indicate not to probe for this interface
670 if (__dev_get_by_name(&init_net
, name
))
673 for (i
= 0; i
< NETDEV_BOOT_SETUP_MAX
; i
++)
674 if (!strcmp(name
, s
[i
].name
))
675 return s
[i
].map
.base_addr
;
680 * Saves at boot time configured settings for any netdevice.
682 int __init
netdev_boot_setup(char *str
)
687 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
692 memset(&map
, 0, sizeof(map
));
696 map
.base_addr
= ints
[2];
698 map
.mem_start
= ints
[3];
700 map
.mem_end
= ints
[4];
702 /* Add new entry to the list */
703 return netdev_boot_setup_add(str
, &map
);
706 __setup("netdev=", netdev_boot_setup
);
708 /*******************************************************************************
710 * Device Interface Subroutines
712 *******************************************************************************/
715 * dev_get_iflink - get 'iflink' value of a interface
716 * @dev: targeted interface
718 * Indicates the ifindex the interface is linked to.
719 * Physical interfaces have the same 'ifindex' and 'iflink' values.
722 int dev_get_iflink(const struct net_device
*dev
)
724 if (dev
->netdev_ops
&& dev
->netdev_ops
->ndo_get_iflink
)
725 return dev
->netdev_ops
->ndo_get_iflink(dev
);
729 EXPORT_SYMBOL(dev_get_iflink
);
732 * dev_fill_metadata_dst - Retrieve tunnel egress information.
733 * @dev: targeted interface
736 * For better visibility of tunnel traffic OVS needs to retrieve
737 * egress tunnel information for a packet. Following API allows
738 * user to get this info.
740 int dev_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
742 struct ip_tunnel_info
*info
;
744 if (!dev
->netdev_ops
|| !dev
->netdev_ops
->ndo_fill_metadata_dst
)
747 info
= skb_tunnel_info_unclone(skb
);
750 if (unlikely(!(info
->mode
& IP_TUNNEL_INFO_TX
)))
753 return dev
->netdev_ops
->ndo_fill_metadata_dst(dev
, skb
);
755 EXPORT_SYMBOL_GPL(dev_fill_metadata_dst
);
758 * __dev_get_by_name - find a device by its name
759 * @net: the applicable net namespace
760 * @name: name to find
762 * Find an interface by name. Must be called under RTNL semaphore
763 * or @dev_base_lock. If the name is found a pointer to the device
764 * is returned. If the name is not found then %NULL is returned. The
765 * reference counters are not incremented so the caller must be
766 * careful with locks.
769 struct net_device
*__dev_get_by_name(struct net
*net
, const char *name
)
771 struct netdev_name_node
*node_name
;
773 node_name
= netdev_name_node_lookup(net
, name
);
774 return node_name
? node_name
->dev
: NULL
;
776 EXPORT_SYMBOL(__dev_get_by_name
);
779 * dev_get_by_name_rcu - find a device by its name
780 * @net: the applicable net namespace
781 * @name: name to find
783 * Find an interface by name.
784 * If the name is found a pointer to the device is returned.
785 * If the name is not found then %NULL is returned.
786 * The reference counters are not incremented so the caller must be
787 * careful with locks. The caller must hold RCU lock.
790 struct net_device
*dev_get_by_name_rcu(struct net
*net
, const char *name
)
792 struct netdev_name_node
*node_name
;
794 node_name
= netdev_name_node_lookup_rcu(net
, name
);
795 return node_name
? node_name
->dev
: NULL
;
797 EXPORT_SYMBOL(dev_get_by_name_rcu
);
800 * dev_get_by_name - find a device by its name
801 * @net: the applicable net namespace
802 * @name: name to find
804 * Find an interface by name. This can be called from any
805 * context and does its own locking. The returned handle has
806 * the usage count incremented and the caller must use dev_put() to
807 * release it when it is no longer needed. %NULL is returned if no
808 * matching device is found.
811 struct net_device
*dev_get_by_name(struct net
*net
, const char *name
)
813 struct net_device
*dev
;
816 dev
= dev_get_by_name_rcu(net
, name
);
822 EXPORT_SYMBOL(dev_get_by_name
);
825 * __dev_get_by_index - find a device by its ifindex
826 * @net: the applicable net namespace
827 * @ifindex: index of device
829 * Search for an interface by index. Returns %NULL if the device
830 * is not found or a pointer to the device. The device has not
831 * had its reference counter increased so the caller must be careful
832 * about locking. The caller must hold either the RTNL semaphore
836 struct net_device
*__dev_get_by_index(struct net
*net
, int ifindex
)
838 struct net_device
*dev
;
839 struct hlist_head
*head
= dev_index_hash(net
, ifindex
);
841 hlist_for_each_entry(dev
, head
, index_hlist
)
842 if (dev
->ifindex
== ifindex
)
847 EXPORT_SYMBOL(__dev_get_by_index
);
850 * dev_get_by_index_rcu - find a device by its ifindex
851 * @net: the applicable net namespace
852 * @ifindex: index of device
854 * Search for an interface by index. Returns %NULL if the device
855 * is not found or a pointer to the device. The device has not
856 * had its reference counter increased so the caller must be careful
857 * about locking. The caller must hold RCU lock.
860 struct net_device
*dev_get_by_index_rcu(struct net
*net
, int ifindex
)
862 struct net_device
*dev
;
863 struct hlist_head
*head
= dev_index_hash(net
, ifindex
);
865 hlist_for_each_entry_rcu(dev
, head
, index_hlist
)
866 if (dev
->ifindex
== ifindex
)
871 EXPORT_SYMBOL(dev_get_by_index_rcu
);
875 * dev_get_by_index - find a device by its ifindex
876 * @net: the applicable net namespace
877 * @ifindex: index of device
879 * Search for an interface by index. Returns NULL if the device
880 * is not found or a pointer to the device. The device returned has
881 * had a reference added and the pointer is safe until the user calls
882 * dev_put to indicate they have finished with it.
885 struct net_device
*dev_get_by_index(struct net
*net
, int ifindex
)
887 struct net_device
*dev
;
890 dev
= dev_get_by_index_rcu(net
, ifindex
);
896 EXPORT_SYMBOL(dev_get_by_index
);
899 * dev_get_by_napi_id - find a device by napi_id
900 * @napi_id: ID of the NAPI struct
902 * Search for an interface by NAPI ID. Returns %NULL if the device
903 * is not found or a pointer to the device. The device has not had
904 * its reference counter increased so the caller must be careful
905 * about locking. The caller must hold RCU lock.
908 struct net_device
*dev_get_by_napi_id(unsigned int napi_id
)
910 struct napi_struct
*napi
;
912 WARN_ON_ONCE(!rcu_read_lock_held());
914 if (napi_id
< MIN_NAPI_ID
)
917 napi
= napi_by_id(napi_id
);
919 return napi
? napi
->dev
: NULL
;
921 EXPORT_SYMBOL(dev_get_by_napi_id
);
924 * netdev_get_name - get a netdevice name, knowing its ifindex.
925 * @net: network namespace
926 * @name: a pointer to the buffer where the name will be stored.
927 * @ifindex: the ifindex of the interface to get the name from.
929 * The use of raw_seqcount_begin() and cond_resched() before
930 * retrying is required as we want to give the writers a chance
931 * to complete when CONFIG_PREEMPTION is not set.
933 int netdev_get_name(struct net
*net
, char *name
, int ifindex
)
935 struct net_device
*dev
;
939 seq
= raw_seqcount_begin(&devnet_rename_seq
);
941 dev
= dev_get_by_index_rcu(net
, ifindex
);
947 strcpy(name
, dev
->name
);
949 if (read_seqcount_retry(&devnet_rename_seq
, seq
)) {
958 * dev_getbyhwaddr_rcu - find a device by its hardware address
959 * @net: the applicable net namespace
960 * @type: media type of device
961 * @ha: hardware address
963 * Search for an interface by MAC address. Returns NULL if the device
964 * is not found or a pointer to the device.
965 * The caller must hold RCU or RTNL.
966 * The returned device has not had its ref count increased
967 * and the caller must therefore be careful about locking
971 struct net_device
*dev_getbyhwaddr_rcu(struct net
*net
, unsigned short type
,
974 struct net_device
*dev
;
976 for_each_netdev_rcu(net
, dev
)
977 if (dev
->type
== type
&&
978 !memcmp(dev
->dev_addr
, ha
, dev
->addr_len
))
983 EXPORT_SYMBOL(dev_getbyhwaddr_rcu
);
985 struct net_device
*__dev_getfirstbyhwtype(struct net
*net
, unsigned short type
)
987 struct net_device
*dev
;
990 for_each_netdev(net
, dev
)
991 if (dev
->type
== type
)
996 EXPORT_SYMBOL(__dev_getfirstbyhwtype
);
998 struct net_device
*dev_getfirstbyhwtype(struct net
*net
, unsigned short type
)
1000 struct net_device
*dev
, *ret
= NULL
;
1003 for_each_netdev_rcu(net
, dev
)
1004 if (dev
->type
== type
) {
1012 EXPORT_SYMBOL(dev_getfirstbyhwtype
);
1015 * __dev_get_by_flags - find any device with given flags
1016 * @net: the applicable net namespace
1017 * @if_flags: IFF_* values
1018 * @mask: bitmask of bits in if_flags to check
1020 * Search for any interface with the given flags. Returns NULL if a device
1021 * is not found or a pointer to the device. Must be called inside
1022 * rtnl_lock(), and result refcount is unchanged.
1025 struct net_device
*__dev_get_by_flags(struct net
*net
, unsigned short if_flags
,
1026 unsigned short mask
)
1028 struct net_device
*dev
, *ret
;
1033 for_each_netdev(net
, dev
) {
1034 if (((dev
->flags
^ if_flags
) & mask
) == 0) {
1041 EXPORT_SYMBOL(__dev_get_by_flags
);
1044 * dev_valid_name - check if name is okay for network device
1045 * @name: name string
1047 * Network device names need to be valid file names to
1048 * to allow sysfs to work. We also disallow any kind of
1051 bool dev_valid_name(const char *name
)
1055 if (strnlen(name
, IFNAMSIZ
) == IFNAMSIZ
)
1057 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
1061 if (*name
== '/' || *name
== ':' || isspace(*name
))
1067 EXPORT_SYMBOL(dev_valid_name
);
1070 * __dev_alloc_name - allocate a name for a device
1071 * @net: network namespace to allocate the device name in
1072 * @name: name format string
1073 * @buf: scratch buffer and result name string
1075 * Passed a format string - eg "lt%d" it will try and find a suitable
1076 * id. It scans list of devices to build up a free map, then chooses
1077 * the first empty slot. The caller must hold the dev_base or rtnl lock
1078 * while allocating the name and adding the device in order to avoid
1080 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1081 * Returns the number of the unit assigned or a negative errno code.
1084 static int __dev_alloc_name(struct net
*net
, const char *name
, char *buf
)
1088 const int max_netdevices
= 8*PAGE_SIZE
;
1089 unsigned long *inuse
;
1090 struct net_device
*d
;
1092 if (!dev_valid_name(name
))
1095 p
= strchr(name
, '%');
1098 * Verify the string as this thing may have come from
1099 * the user. There must be either one "%d" and no other "%"
1102 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
1105 /* Use one page as a bit array of possible slots */
1106 inuse
= (unsigned long *) get_zeroed_page(GFP_ATOMIC
);
1110 for_each_netdev(net
, d
) {
1111 if (!sscanf(d
->name
, name
, &i
))
1113 if (i
< 0 || i
>= max_netdevices
)
1116 /* avoid cases where sscanf is not exact inverse of printf */
1117 snprintf(buf
, IFNAMSIZ
, name
, i
);
1118 if (!strncmp(buf
, d
->name
, IFNAMSIZ
))
1122 i
= find_first_zero_bit(inuse
, max_netdevices
);
1123 free_page((unsigned long) inuse
);
1126 snprintf(buf
, IFNAMSIZ
, name
, i
);
1127 if (!__dev_get_by_name(net
, buf
))
1130 /* It is possible to run out of possible slots
1131 * when the name is long and there isn't enough space left
1132 * for the digits, or if all bits are used.
1137 static int dev_alloc_name_ns(struct net
*net
,
1138 struct net_device
*dev
,
1145 ret
= __dev_alloc_name(net
, name
, buf
);
1147 strlcpy(dev
->name
, buf
, IFNAMSIZ
);
1152 * dev_alloc_name - allocate a name for a device
1154 * @name: name format string
1156 * Passed a format string - eg "lt%d" it will try and find a suitable
1157 * id. It scans list of devices to build up a free map, then chooses
1158 * the first empty slot. The caller must hold the dev_base or rtnl lock
1159 * while allocating the name and adding the device in order to avoid
1161 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1162 * Returns the number of the unit assigned or a negative errno code.
1165 int dev_alloc_name(struct net_device
*dev
, const char *name
)
1167 return dev_alloc_name_ns(dev_net(dev
), dev
, name
);
1169 EXPORT_SYMBOL(dev_alloc_name
);
1171 static int dev_get_valid_name(struct net
*net
, struct net_device
*dev
,
1176 if (!dev_valid_name(name
))
1179 if (strchr(name
, '%'))
1180 return dev_alloc_name_ns(net
, dev
, name
);
1181 else if (__dev_get_by_name(net
, name
))
1183 else if (dev
->name
!= name
)
1184 strlcpy(dev
->name
, name
, IFNAMSIZ
);
1190 * dev_change_name - change name of a device
1192 * @newname: name (or format string) must be at least IFNAMSIZ
1194 * Change name of a device, can pass format strings "eth%d".
1197 int dev_change_name(struct net_device
*dev
, const char *newname
)
1199 unsigned char old_assign_type
;
1200 char oldname
[IFNAMSIZ
];
1206 BUG_ON(!dev_net(dev
));
1210 /* Some auto-enslaved devices e.g. failover slaves are
1211 * special, as userspace might rename the device after
1212 * the interface had been brought up and running since
1213 * the point kernel initiated auto-enslavement. Allow
1214 * live name change even when these slave devices are
1217 * Typically, users of these auto-enslaving devices
1218 * don't actually care about slave name change, as
1219 * they are supposed to operate on master interface
1222 if (dev
->flags
& IFF_UP
&&
1223 likely(!(dev
->priv_flags
& IFF_LIVE_RENAME_OK
)))
1226 write_seqcount_begin(&devnet_rename_seq
);
1228 if (strncmp(newname
, dev
->name
, IFNAMSIZ
) == 0) {
1229 write_seqcount_end(&devnet_rename_seq
);
1233 memcpy(oldname
, dev
->name
, IFNAMSIZ
);
1235 err
= dev_get_valid_name(net
, dev
, newname
);
1237 write_seqcount_end(&devnet_rename_seq
);
1241 if (oldname
[0] && !strchr(oldname
, '%'))
1242 netdev_info(dev
, "renamed from %s\n", oldname
);
1244 old_assign_type
= dev
->name_assign_type
;
1245 dev
->name_assign_type
= NET_NAME_RENAMED
;
1248 ret
= device_rename(&dev
->dev
, dev
->name
);
1250 memcpy(dev
->name
, oldname
, IFNAMSIZ
);
1251 dev
->name_assign_type
= old_assign_type
;
1252 write_seqcount_end(&devnet_rename_seq
);
1256 write_seqcount_end(&devnet_rename_seq
);
1258 netdev_adjacent_rename_links(dev
, oldname
);
1260 write_lock_bh(&dev_base_lock
);
1261 netdev_name_node_del(dev
->name_node
);
1262 write_unlock_bh(&dev_base_lock
);
1266 write_lock_bh(&dev_base_lock
);
1267 netdev_name_node_add(net
, dev
->name_node
);
1268 write_unlock_bh(&dev_base_lock
);
1270 ret
= call_netdevice_notifiers(NETDEV_CHANGENAME
, dev
);
1271 ret
= notifier_to_errno(ret
);
1274 /* err >= 0 after dev_alloc_name() or stores the first errno */
1277 write_seqcount_begin(&devnet_rename_seq
);
1278 memcpy(dev
->name
, oldname
, IFNAMSIZ
);
1279 memcpy(oldname
, newname
, IFNAMSIZ
);
1280 dev
->name_assign_type
= old_assign_type
;
1281 old_assign_type
= NET_NAME_RENAMED
;
1284 pr_err("%s: name change rollback failed: %d\n",
1293 * dev_set_alias - change ifalias of a device
1295 * @alias: name up to IFALIASZ
1296 * @len: limit of bytes to copy from info
1298 * Set ifalias for a device,
1300 int dev_set_alias(struct net_device
*dev
, const char *alias
, size_t len
)
1302 struct dev_ifalias
*new_alias
= NULL
;
1304 if (len
>= IFALIASZ
)
1308 new_alias
= kmalloc(sizeof(*new_alias
) + len
+ 1, GFP_KERNEL
);
1312 memcpy(new_alias
->ifalias
, alias
, len
);
1313 new_alias
->ifalias
[len
] = 0;
1316 mutex_lock(&ifalias_mutex
);
1317 new_alias
= rcu_replace_pointer(dev
->ifalias
, new_alias
,
1318 mutex_is_locked(&ifalias_mutex
));
1319 mutex_unlock(&ifalias_mutex
);
1322 kfree_rcu(new_alias
, rcuhead
);
1326 EXPORT_SYMBOL(dev_set_alias
);
1329 * dev_get_alias - get ifalias of a device
1331 * @name: buffer to store name of ifalias
1332 * @len: size of buffer
1334 * get ifalias for a device. Caller must make sure dev cannot go
1335 * away, e.g. rcu read lock or own a reference count to device.
1337 int dev_get_alias(const struct net_device
*dev
, char *name
, size_t len
)
1339 const struct dev_ifalias
*alias
;
1343 alias
= rcu_dereference(dev
->ifalias
);
1345 ret
= snprintf(name
, len
, "%s", alias
->ifalias
);
1352 * netdev_features_change - device changes features
1353 * @dev: device to cause notification
1355 * Called to indicate a device has changed features.
1357 void netdev_features_change(struct net_device
*dev
)
1359 call_netdevice_notifiers(NETDEV_FEAT_CHANGE
, dev
);
1361 EXPORT_SYMBOL(netdev_features_change
);
1364 * netdev_state_change - device changes state
1365 * @dev: device to cause notification
1367 * Called to indicate a device has changed state. This function calls
1368 * the notifier chains for netdev_chain and sends a NEWLINK message
1369 * to the routing socket.
1371 void netdev_state_change(struct net_device
*dev
)
1373 if (dev
->flags
& IFF_UP
) {
1374 struct netdev_notifier_change_info change_info
= {
1378 call_netdevice_notifiers_info(NETDEV_CHANGE
,
1380 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0, GFP_KERNEL
);
1383 EXPORT_SYMBOL(netdev_state_change
);
1386 * netdev_notify_peers - notify network peers about existence of @dev
1387 * @dev: network device
1389 * Generate traffic such that interested network peers are aware of
1390 * @dev, such as by generating a gratuitous ARP. This may be used when
1391 * a device wants to inform the rest of the network about some sort of
1392 * reconfiguration such as a failover event or virtual machine
1395 void netdev_notify_peers(struct net_device
*dev
)
1398 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS
, dev
);
1399 call_netdevice_notifiers(NETDEV_RESEND_IGMP
, dev
);
1402 EXPORT_SYMBOL(netdev_notify_peers
);
1404 static int __dev_open(struct net_device
*dev
, struct netlink_ext_ack
*extack
)
1406 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1411 if (!netif_device_present(dev
))
1414 /* Block netpoll from trying to do any rx path servicing.
1415 * If we don't do this there is a chance ndo_poll_controller
1416 * or ndo_poll may be running while we open the device
1418 netpoll_poll_disable(dev
);
1420 ret
= call_netdevice_notifiers_extack(NETDEV_PRE_UP
, dev
, extack
);
1421 ret
= notifier_to_errno(ret
);
1425 set_bit(__LINK_STATE_START
, &dev
->state
);
1427 if (ops
->ndo_validate_addr
)
1428 ret
= ops
->ndo_validate_addr(dev
);
1430 if (!ret
&& ops
->ndo_open
)
1431 ret
= ops
->ndo_open(dev
);
1433 netpoll_poll_enable(dev
);
1436 clear_bit(__LINK_STATE_START
, &dev
->state
);
1438 dev
->flags
|= IFF_UP
;
1439 dev_set_rx_mode(dev
);
1441 add_device_randomness(dev
->dev_addr
, dev
->addr_len
);
1448 * dev_open - prepare an interface for use.
1449 * @dev: device to open
1450 * @extack: netlink extended ack
1452 * Takes a device from down to up state. The device's private open
1453 * function is invoked and then the multicast lists are loaded. Finally
1454 * the device is moved into the up state and a %NETDEV_UP message is
1455 * sent to the netdev notifier chain.
1457 * Calling this function on an active interface is a nop. On a failure
1458 * a negative errno code is returned.
1460 int dev_open(struct net_device
*dev
, struct netlink_ext_ack
*extack
)
1464 if (dev
->flags
& IFF_UP
)
1467 ret
= __dev_open(dev
, extack
);
1471 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
, GFP_KERNEL
);
1472 call_netdevice_notifiers(NETDEV_UP
, dev
);
1476 EXPORT_SYMBOL(dev_open
);
1478 static void __dev_close_many(struct list_head
*head
)
1480 struct net_device
*dev
;
1485 list_for_each_entry(dev
, head
, close_list
) {
1486 /* Temporarily disable netpoll until the interface is down */
1487 netpoll_poll_disable(dev
);
1489 call_netdevice_notifiers(NETDEV_GOING_DOWN
, dev
);
1491 clear_bit(__LINK_STATE_START
, &dev
->state
);
1493 /* Synchronize to scheduled poll. We cannot touch poll list, it
1494 * can be even on different cpu. So just clear netif_running().
1496 * dev->stop() will invoke napi_disable() on all of it's
1497 * napi_struct instances on this device.
1499 smp_mb__after_atomic(); /* Commit netif_running(). */
1502 dev_deactivate_many(head
);
1504 list_for_each_entry(dev
, head
, close_list
) {
1505 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1508 * Call the device specific close. This cannot fail.
1509 * Only if device is UP
1511 * We allow it to be called even after a DETACH hot-plug
1517 dev
->flags
&= ~IFF_UP
;
1518 netpoll_poll_enable(dev
);
1522 static void __dev_close(struct net_device
*dev
)
1526 list_add(&dev
->close_list
, &single
);
1527 __dev_close_many(&single
);
1531 void dev_close_many(struct list_head
*head
, bool unlink
)
1533 struct net_device
*dev
, *tmp
;
1535 /* Remove the devices that don't need to be closed */
1536 list_for_each_entry_safe(dev
, tmp
, head
, close_list
)
1537 if (!(dev
->flags
& IFF_UP
))
1538 list_del_init(&dev
->close_list
);
1540 __dev_close_many(head
);
1542 list_for_each_entry_safe(dev
, tmp
, head
, close_list
) {
1543 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
, GFP_KERNEL
);
1544 call_netdevice_notifiers(NETDEV_DOWN
, dev
);
1546 list_del_init(&dev
->close_list
);
1549 EXPORT_SYMBOL(dev_close_many
);
1552 * dev_close - shutdown an interface.
1553 * @dev: device to shutdown
1555 * This function moves an active device into down state. A
1556 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1557 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1560 void dev_close(struct net_device
*dev
)
1562 if (dev
->flags
& IFF_UP
) {
1565 list_add(&dev
->close_list
, &single
);
1566 dev_close_many(&single
, true);
1570 EXPORT_SYMBOL(dev_close
);
1574 * dev_disable_lro - disable Large Receive Offload on a device
1577 * Disable Large Receive Offload (LRO) on a net device. Must be
1578 * called under RTNL. This is needed if received packets may be
1579 * forwarded to another interface.
1581 void dev_disable_lro(struct net_device
*dev
)
1583 struct net_device
*lower_dev
;
1584 struct list_head
*iter
;
1586 dev
->wanted_features
&= ~NETIF_F_LRO
;
1587 netdev_update_features(dev
);
1589 if (unlikely(dev
->features
& NETIF_F_LRO
))
1590 netdev_WARN(dev
, "failed to disable LRO!\n");
1592 netdev_for_each_lower_dev(dev
, lower_dev
, iter
)
1593 dev_disable_lro(lower_dev
);
1595 EXPORT_SYMBOL(dev_disable_lro
);
1598 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1601 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1602 * called under RTNL. This is needed if Generic XDP is installed on
1605 static void dev_disable_gro_hw(struct net_device
*dev
)
1607 dev
->wanted_features
&= ~NETIF_F_GRO_HW
;
1608 netdev_update_features(dev
);
1610 if (unlikely(dev
->features
& NETIF_F_GRO_HW
))
1611 netdev_WARN(dev
, "failed to disable GRO_HW!\n");
1614 const char *netdev_cmd_to_name(enum netdev_cmd cmd
)
1617 case NETDEV_##val: \
1618 return "NETDEV_" __stringify(val);
1620 N(UP
) N(DOWN
) N(REBOOT
) N(CHANGE
) N(REGISTER
) N(UNREGISTER
)
1621 N(CHANGEMTU
) N(CHANGEADDR
) N(GOING_DOWN
) N(CHANGENAME
) N(FEAT_CHANGE
)
1622 N(BONDING_FAILOVER
) N(PRE_UP
) N(PRE_TYPE_CHANGE
) N(POST_TYPE_CHANGE
)
1623 N(POST_INIT
) N(RELEASE
) N(NOTIFY_PEERS
) N(JOIN
) N(CHANGEUPPER
)
1624 N(RESEND_IGMP
) N(PRECHANGEMTU
) N(CHANGEINFODATA
) N(BONDING_INFO
)
1625 N(PRECHANGEUPPER
) N(CHANGELOWERSTATE
) N(UDP_TUNNEL_PUSH_INFO
)
1626 N(UDP_TUNNEL_DROP_INFO
) N(CHANGE_TX_QUEUE_LEN
)
1627 N(CVLAN_FILTER_PUSH_INFO
) N(CVLAN_FILTER_DROP_INFO
)
1628 N(SVLAN_FILTER_PUSH_INFO
) N(SVLAN_FILTER_DROP_INFO
)
1632 return "UNKNOWN_NETDEV_EVENT";
1634 EXPORT_SYMBOL_GPL(netdev_cmd_to_name
);
1636 static int call_netdevice_notifier(struct notifier_block
*nb
, unsigned long val
,
1637 struct net_device
*dev
)
1639 struct netdev_notifier_info info
= {
1643 return nb
->notifier_call(nb
, val
, &info
);
1646 static int call_netdevice_register_notifiers(struct notifier_block
*nb
,
1647 struct net_device
*dev
)
1651 err
= call_netdevice_notifier(nb
, NETDEV_REGISTER
, dev
);
1652 err
= notifier_to_errno(err
);
1656 if (!(dev
->flags
& IFF_UP
))
1659 call_netdevice_notifier(nb
, NETDEV_UP
, dev
);
1663 static void call_netdevice_unregister_notifiers(struct notifier_block
*nb
,
1664 struct net_device
*dev
)
1666 if (dev
->flags
& IFF_UP
) {
1667 call_netdevice_notifier(nb
, NETDEV_GOING_DOWN
,
1669 call_netdevice_notifier(nb
, NETDEV_DOWN
, dev
);
1671 call_netdevice_notifier(nb
, NETDEV_UNREGISTER
, dev
);
1674 static int call_netdevice_register_net_notifiers(struct notifier_block
*nb
,
1677 struct net_device
*dev
;
1680 for_each_netdev(net
, dev
) {
1681 err
= call_netdevice_register_notifiers(nb
, dev
);
1688 for_each_netdev_continue_reverse(net
, dev
)
1689 call_netdevice_unregister_notifiers(nb
, dev
);
1693 static void call_netdevice_unregister_net_notifiers(struct notifier_block
*nb
,
1696 struct net_device
*dev
;
1698 for_each_netdev(net
, dev
)
1699 call_netdevice_unregister_notifiers(nb
, dev
);
1702 static int dev_boot_phase
= 1;
1705 * register_netdevice_notifier - register a network notifier block
1708 * Register a notifier to be called when network device events occur.
1709 * The notifier passed is linked into the kernel structures and must
1710 * not be reused until it has been unregistered. A negative errno code
1711 * is returned on a failure.
1713 * When registered all registration and up events are replayed
1714 * to the new notifier to allow device to have a race free
1715 * view of the network device list.
1718 int register_netdevice_notifier(struct notifier_block
*nb
)
1723 /* Close race with setup_net() and cleanup_net() */
1724 down_write(&pernet_ops_rwsem
);
1726 err
= raw_notifier_chain_register(&netdev_chain
, nb
);
1732 err
= call_netdevice_register_net_notifiers(nb
, net
);
1739 up_write(&pernet_ops_rwsem
);
1743 for_each_net_continue_reverse(net
)
1744 call_netdevice_unregister_net_notifiers(nb
, net
);
1746 raw_notifier_chain_unregister(&netdev_chain
, nb
);
1749 EXPORT_SYMBOL(register_netdevice_notifier
);
1752 * unregister_netdevice_notifier - unregister a network notifier block
1755 * Unregister a notifier previously registered by
1756 * register_netdevice_notifier(). The notifier is unlinked into the
1757 * kernel structures and may then be reused. A negative errno code
1758 * is returned on a failure.
1760 * After unregistering unregister and down device events are synthesized
1761 * for all devices on the device list to the removed notifier to remove
1762 * the need for special case cleanup code.
1765 int unregister_netdevice_notifier(struct notifier_block
*nb
)
1770 /* Close race with setup_net() and cleanup_net() */
1771 down_write(&pernet_ops_rwsem
);
1773 err
= raw_notifier_chain_unregister(&netdev_chain
, nb
);
1778 call_netdevice_unregister_net_notifiers(nb
, net
);
1782 up_write(&pernet_ops_rwsem
);
1785 EXPORT_SYMBOL(unregister_netdevice_notifier
);
1787 static int __register_netdevice_notifier_net(struct net
*net
,
1788 struct notifier_block
*nb
,
1789 bool ignore_call_fail
)
1793 err
= raw_notifier_chain_register(&net
->netdev_chain
, nb
);
1799 err
= call_netdevice_register_net_notifiers(nb
, net
);
1800 if (err
&& !ignore_call_fail
)
1801 goto chain_unregister
;
1806 raw_notifier_chain_unregister(&net
->netdev_chain
, nb
);
1810 static int __unregister_netdevice_notifier_net(struct net
*net
,
1811 struct notifier_block
*nb
)
1815 err
= raw_notifier_chain_unregister(&net
->netdev_chain
, nb
);
1819 call_netdevice_unregister_net_notifiers(nb
, net
);
1824 * register_netdevice_notifier_net - register a per-netns network notifier block
1825 * @net: network namespace
1828 * Register a notifier to be called when network device events occur.
1829 * The notifier passed is linked into the kernel structures and must
1830 * not be reused until it has been unregistered. A negative errno code
1831 * is returned on a failure.
1833 * When registered all registration and up events are replayed
1834 * to the new notifier to allow device to have a race free
1835 * view of the network device list.
1838 int register_netdevice_notifier_net(struct net
*net
, struct notifier_block
*nb
)
1843 err
= __register_netdevice_notifier_net(net
, nb
, false);
1847 EXPORT_SYMBOL(register_netdevice_notifier_net
);
1850 * unregister_netdevice_notifier_net - unregister a per-netns
1851 * network notifier block
1852 * @net: network namespace
1855 * Unregister a notifier previously registered by
1856 * register_netdevice_notifier(). The notifier is unlinked into the
1857 * kernel structures and may then be reused. A negative errno code
1858 * is returned on a failure.
1860 * After unregistering unregister and down device events are synthesized
1861 * for all devices on the device list to the removed notifier to remove
1862 * the need for special case cleanup code.
1865 int unregister_netdevice_notifier_net(struct net
*net
,
1866 struct notifier_block
*nb
)
1871 err
= __unregister_netdevice_notifier_net(net
, nb
);
1875 EXPORT_SYMBOL(unregister_netdevice_notifier_net
);
1877 int register_netdevice_notifier_dev_net(struct net_device
*dev
,
1878 struct notifier_block
*nb
,
1879 struct netdev_net_notifier
*nn
)
1884 err
= __register_netdevice_notifier_net(dev_net(dev
), nb
, false);
1887 list_add(&nn
->list
, &dev
->net_notifier_list
);
1892 EXPORT_SYMBOL(register_netdevice_notifier_dev_net
);
1894 int unregister_netdevice_notifier_dev_net(struct net_device
*dev
,
1895 struct notifier_block
*nb
,
1896 struct netdev_net_notifier
*nn
)
1901 list_del(&nn
->list
);
1902 err
= __unregister_netdevice_notifier_net(dev_net(dev
), nb
);
1906 EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net
);
1908 static void move_netdevice_notifiers_dev_net(struct net_device
*dev
,
1911 struct netdev_net_notifier
*nn
;
1913 list_for_each_entry(nn
, &dev
->net_notifier_list
, list
) {
1914 __unregister_netdevice_notifier_net(dev_net(dev
), nn
->nb
);
1915 __register_netdevice_notifier_net(net
, nn
->nb
, true);
1920 * call_netdevice_notifiers_info - call all network notifier blocks
1921 * @val: value passed unmodified to notifier function
1922 * @info: notifier information data
1924 * Call all network notifier blocks. Parameters and return value
1925 * are as for raw_notifier_call_chain().
1928 static int call_netdevice_notifiers_info(unsigned long val
,
1929 struct netdev_notifier_info
*info
)
1931 struct net
*net
= dev_net(info
->dev
);
1936 /* Run per-netns notifier block chain first, then run the global one.
1937 * Hopefully, one day, the global one is going to be removed after
1938 * all notifier block registrators get converted to be per-netns.
1940 ret
= raw_notifier_call_chain(&net
->netdev_chain
, val
, info
);
1941 if (ret
& NOTIFY_STOP_MASK
)
1943 return raw_notifier_call_chain(&netdev_chain
, val
, info
);
1946 static int call_netdevice_notifiers_extack(unsigned long val
,
1947 struct net_device
*dev
,
1948 struct netlink_ext_ack
*extack
)
1950 struct netdev_notifier_info info
= {
1955 return call_netdevice_notifiers_info(val
, &info
);
1959 * call_netdevice_notifiers - call all network notifier blocks
1960 * @val: value passed unmodified to notifier function
1961 * @dev: net_device pointer passed unmodified to notifier function
1963 * Call all network notifier blocks. Parameters and return value
1964 * are as for raw_notifier_call_chain().
1967 int call_netdevice_notifiers(unsigned long val
, struct net_device
*dev
)
1969 return call_netdevice_notifiers_extack(val
, dev
, NULL
);
1971 EXPORT_SYMBOL(call_netdevice_notifiers
);
1974 * call_netdevice_notifiers_mtu - call all network notifier blocks
1975 * @val: value passed unmodified to notifier function
1976 * @dev: net_device pointer passed unmodified to notifier function
1977 * @arg: additional u32 argument passed to the notifier function
1979 * Call all network notifier blocks. Parameters and return value
1980 * are as for raw_notifier_call_chain().
1982 static int call_netdevice_notifiers_mtu(unsigned long val
,
1983 struct net_device
*dev
, u32 arg
)
1985 struct netdev_notifier_info_ext info
= {
1990 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext
, info
) != 0);
1992 return call_netdevice_notifiers_info(val
, &info
.info
);
1995 #ifdef CONFIG_NET_INGRESS
1996 static DEFINE_STATIC_KEY_FALSE(ingress_needed_key
);
1998 void net_inc_ingress_queue(void)
2000 static_branch_inc(&ingress_needed_key
);
2002 EXPORT_SYMBOL_GPL(net_inc_ingress_queue
);
2004 void net_dec_ingress_queue(void)
2006 static_branch_dec(&ingress_needed_key
);
2008 EXPORT_SYMBOL_GPL(net_dec_ingress_queue
);
2011 #ifdef CONFIG_NET_EGRESS
2012 static DEFINE_STATIC_KEY_FALSE(egress_needed_key
);
2014 void net_inc_egress_queue(void)
2016 static_branch_inc(&egress_needed_key
);
2018 EXPORT_SYMBOL_GPL(net_inc_egress_queue
);
2020 void net_dec_egress_queue(void)
2022 static_branch_dec(&egress_needed_key
);
2024 EXPORT_SYMBOL_GPL(net_dec_egress_queue
);
2027 static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key
);
2028 #ifdef CONFIG_JUMP_LABEL
2029 static atomic_t netstamp_needed_deferred
;
2030 static atomic_t netstamp_wanted
;
2031 static void netstamp_clear(struct work_struct
*work
)
2033 int deferred
= atomic_xchg(&netstamp_needed_deferred
, 0);
2036 wanted
= atomic_add_return(deferred
, &netstamp_wanted
);
2038 static_branch_enable(&netstamp_needed_key
);
2040 static_branch_disable(&netstamp_needed_key
);
2042 static DECLARE_WORK(netstamp_work
, netstamp_clear
);
2045 void net_enable_timestamp(void)
2047 #ifdef CONFIG_JUMP_LABEL
2051 wanted
= atomic_read(&netstamp_wanted
);
2054 if (atomic_cmpxchg(&netstamp_wanted
, wanted
, wanted
+ 1) == wanted
)
2057 atomic_inc(&netstamp_needed_deferred
);
2058 schedule_work(&netstamp_work
);
2060 static_branch_inc(&netstamp_needed_key
);
2063 EXPORT_SYMBOL(net_enable_timestamp
);
2065 void net_disable_timestamp(void)
2067 #ifdef CONFIG_JUMP_LABEL
2071 wanted
= atomic_read(&netstamp_wanted
);
2074 if (atomic_cmpxchg(&netstamp_wanted
, wanted
, wanted
- 1) == wanted
)
2077 atomic_dec(&netstamp_needed_deferred
);
2078 schedule_work(&netstamp_work
);
2080 static_branch_dec(&netstamp_needed_key
);
2083 EXPORT_SYMBOL(net_disable_timestamp
);
2085 static inline void net_timestamp_set(struct sk_buff
*skb
)
2088 if (static_branch_unlikely(&netstamp_needed_key
))
2089 __net_timestamp(skb
);
2092 #define net_timestamp_check(COND, SKB) \
2093 if (static_branch_unlikely(&netstamp_needed_key)) { \
2094 if ((COND) && !(SKB)->tstamp) \
2095 __net_timestamp(SKB); \
2098 bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
2102 if (!(dev
->flags
& IFF_UP
))
2105 len
= dev
->mtu
+ dev
->hard_header_len
+ VLAN_HLEN
;
2106 if (skb
->len
<= len
)
2109 /* if TSO is enabled, we don't care about the length as the packet
2110 * could be forwarded without being segmented before
2112 if (skb_is_gso(skb
))
2117 EXPORT_SYMBOL_GPL(is_skb_forwardable
);
2119 int __dev_forward_skb(struct net_device
*dev
, struct sk_buff
*skb
)
2121 int ret
= ____dev_forward_skb(dev
, skb
);
2124 skb
->protocol
= eth_type_trans(skb
, dev
);
2125 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
2130 EXPORT_SYMBOL_GPL(__dev_forward_skb
);
2133 * dev_forward_skb - loopback an skb to another netif
2135 * @dev: destination network device
2136 * @skb: buffer to forward
2139 * NET_RX_SUCCESS (no congestion)
2140 * NET_RX_DROP (packet was dropped, but freed)
2142 * dev_forward_skb can be used for injecting an skb from the
2143 * start_xmit function of one device into the receive queue
2144 * of another device.
2146 * The receiving device may be in another namespace, so
2147 * we have to clear all information in the skb that could
2148 * impact namespace isolation.
2150 int dev_forward_skb(struct net_device
*dev
, struct sk_buff
*skb
)
2152 return __dev_forward_skb(dev
, skb
) ?: netif_rx_internal(skb
);
2154 EXPORT_SYMBOL_GPL(dev_forward_skb
);
2156 static inline int deliver_skb(struct sk_buff
*skb
,
2157 struct packet_type
*pt_prev
,
2158 struct net_device
*orig_dev
)
2160 if (unlikely(skb_orphan_frags_rx(skb
, GFP_ATOMIC
)))
2162 refcount_inc(&skb
->users
);
2163 return pt_prev
->func(skb
, skb
->dev
, pt_prev
, orig_dev
);
2166 static inline void deliver_ptype_list_skb(struct sk_buff
*skb
,
2167 struct packet_type
**pt
,
2168 struct net_device
*orig_dev
,
2170 struct list_head
*ptype_list
)
2172 struct packet_type
*ptype
, *pt_prev
= *pt
;
2174 list_for_each_entry_rcu(ptype
, ptype_list
, list
) {
2175 if (ptype
->type
!= type
)
2178 deliver_skb(skb
, pt_prev
, orig_dev
);
2184 static inline bool skb_loop_sk(struct packet_type
*ptype
, struct sk_buff
*skb
)
2186 if (!ptype
->af_packet_priv
|| !skb
->sk
)
2189 if (ptype
->id_match
)
2190 return ptype
->id_match(ptype
, skb
->sk
);
2191 else if ((struct sock
*)ptype
->af_packet_priv
== skb
->sk
)
2198 * dev_nit_active - return true if any network interface taps are in use
2200 * @dev: network device to check for the presence of taps
2202 bool dev_nit_active(struct net_device
*dev
)
2204 return !list_empty(&ptype_all
) || !list_empty(&dev
->ptype_all
);
2206 EXPORT_SYMBOL_GPL(dev_nit_active
);
2209 * Support routine. Sends outgoing frames to any network
2210 * taps currently in use.
2213 void dev_queue_xmit_nit(struct sk_buff
*skb
, struct net_device
*dev
)
2215 struct packet_type
*ptype
;
2216 struct sk_buff
*skb2
= NULL
;
2217 struct packet_type
*pt_prev
= NULL
;
2218 struct list_head
*ptype_list
= &ptype_all
;
2222 list_for_each_entry_rcu(ptype
, ptype_list
, list
) {
2223 if (ptype
->ignore_outgoing
)
2226 /* Never send packets back to the socket
2227 * they originated from - MvS (miquels@drinkel.ow.org)
2229 if (skb_loop_sk(ptype
, skb
))
2233 deliver_skb(skb2
, pt_prev
, skb
->dev
);
2238 /* need to clone skb, done only once */
2239 skb2
= skb_clone(skb
, GFP_ATOMIC
);
2243 net_timestamp_set(skb2
);
2245 /* skb->nh should be correctly
2246 * set by sender, so that the second statement is
2247 * just protection against buggy protocols.
2249 skb_reset_mac_header(skb2
);
2251 if (skb_network_header(skb2
) < skb2
->data
||
2252 skb_network_header(skb2
) > skb_tail_pointer(skb2
)) {
2253 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2254 ntohs(skb2
->protocol
),
2256 skb_reset_network_header(skb2
);
2259 skb2
->transport_header
= skb2
->network_header
;
2260 skb2
->pkt_type
= PACKET_OUTGOING
;
2264 if (ptype_list
== &ptype_all
) {
2265 ptype_list
= &dev
->ptype_all
;
2270 if (!skb_orphan_frags_rx(skb2
, GFP_ATOMIC
))
2271 pt_prev
->func(skb2
, skb
->dev
, pt_prev
, skb
->dev
);
2277 EXPORT_SYMBOL_GPL(dev_queue_xmit_nit
);
2280 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
2281 * @dev: Network device
2282 * @txq: number of queues available
2284 * If real_num_tx_queues is changed the tc mappings may no longer be
2285 * valid. To resolve this verify the tc mapping remains valid and if
2286 * not NULL the mapping. With no priorities mapping to this
2287 * offset/count pair it will no longer be used. In the worst case TC0
2288 * is invalid nothing can be done so disable priority mappings. If is
2289 * expected that drivers will fix this mapping if they can before
2290 * calling netif_set_real_num_tx_queues.
2292 static void netif_setup_tc(struct net_device
*dev
, unsigned int txq
)
2295 struct netdev_tc_txq
*tc
= &dev
->tc_to_txq
[0];
2297 /* If TC0 is invalidated disable TC mapping */
2298 if (tc
->offset
+ tc
->count
> txq
) {
2299 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
2304 /* Invalidated prio to tc mappings set to TC0 */
2305 for (i
= 1; i
< TC_BITMASK
+ 1; i
++) {
2306 int q
= netdev_get_prio_tc_map(dev
, i
);
2308 tc
= &dev
->tc_to_txq
[q
];
2309 if (tc
->offset
+ tc
->count
> txq
) {
2310 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2312 netdev_set_prio_tc_map(dev
, i
, 0);
2317 int netdev_txq_to_tc(struct net_device
*dev
, unsigned int txq
)
2320 struct netdev_tc_txq
*tc
= &dev
->tc_to_txq
[0];
2323 /* walk through the TCs and see if it falls into any of them */
2324 for (i
= 0; i
< TC_MAX_QUEUE
; i
++, tc
++) {
2325 if ((txq
- tc
->offset
) < tc
->count
)
2329 /* didn't find it, just return -1 to indicate no match */
2335 EXPORT_SYMBOL(netdev_txq_to_tc
);
2338 struct static_key xps_needed __read_mostly
;
2339 EXPORT_SYMBOL(xps_needed
);
2340 struct static_key xps_rxqs_needed __read_mostly
;
2341 EXPORT_SYMBOL(xps_rxqs_needed
);
2342 static DEFINE_MUTEX(xps_map_mutex
);
2343 #define xmap_dereference(P) \
2344 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2346 static bool remove_xps_queue(struct xps_dev_maps
*dev_maps
,
2349 struct xps_map
*map
= NULL
;
2353 map
= xmap_dereference(dev_maps
->attr_map
[tci
]);
2357 for (pos
= map
->len
; pos
--;) {
2358 if (map
->queues
[pos
] != index
)
2362 map
->queues
[pos
] = map
->queues
[--map
->len
];
2366 RCU_INIT_POINTER(dev_maps
->attr_map
[tci
], NULL
);
2367 kfree_rcu(map
, rcu
);
2374 static bool remove_xps_queue_cpu(struct net_device
*dev
,
2375 struct xps_dev_maps
*dev_maps
,
2376 int cpu
, u16 offset
, u16 count
)
2378 int num_tc
= dev
->num_tc
? : 1;
2379 bool active
= false;
2382 for (tci
= cpu
* num_tc
; num_tc
--; tci
++) {
2385 for (i
= count
, j
= offset
; i
--; j
++) {
2386 if (!remove_xps_queue(dev_maps
, tci
, j
))
2396 static void reset_xps_maps(struct net_device
*dev
,
2397 struct xps_dev_maps
*dev_maps
,
2401 static_key_slow_dec_cpuslocked(&xps_rxqs_needed
);
2402 RCU_INIT_POINTER(dev
->xps_rxqs_map
, NULL
);
2404 RCU_INIT_POINTER(dev
->xps_cpus_map
, NULL
);
2406 static_key_slow_dec_cpuslocked(&xps_needed
);
2407 kfree_rcu(dev_maps
, rcu
);
2410 static void clean_xps_maps(struct net_device
*dev
, const unsigned long *mask
,
2411 struct xps_dev_maps
*dev_maps
, unsigned int nr_ids
,
2412 u16 offset
, u16 count
, bool is_rxqs_map
)
2414 bool active
= false;
2417 for (j
= -1; j
= netif_attrmask_next(j
, mask
, nr_ids
),
2419 active
|= remove_xps_queue_cpu(dev
, dev_maps
, j
, offset
,
2422 reset_xps_maps(dev
, dev_maps
, is_rxqs_map
);
2425 for (i
= offset
+ (count
- 1); count
--; i
--) {
2426 netdev_queue_numa_node_write(
2427 netdev_get_tx_queue(dev
, i
),
2433 static void netif_reset_xps_queues(struct net_device
*dev
, u16 offset
,
2436 const unsigned long *possible_mask
= NULL
;
2437 struct xps_dev_maps
*dev_maps
;
2438 unsigned int nr_ids
;
2440 if (!static_key_false(&xps_needed
))
2444 mutex_lock(&xps_map_mutex
);
2446 if (static_key_false(&xps_rxqs_needed
)) {
2447 dev_maps
= xmap_dereference(dev
->xps_rxqs_map
);
2449 nr_ids
= dev
->num_rx_queues
;
2450 clean_xps_maps(dev
, possible_mask
, dev_maps
, nr_ids
,
2451 offset
, count
, true);
2455 dev_maps
= xmap_dereference(dev
->xps_cpus_map
);
2459 if (num_possible_cpus() > 1)
2460 possible_mask
= cpumask_bits(cpu_possible_mask
);
2461 nr_ids
= nr_cpu_ids
;
2462 clean_xps_maps(dev
, possible_mask
, dev_maps
, nr_ids
, offset
, count
,
2466 mutex_unlock(&xps_map_mutex
);
2470 static void netif_reset_xps_queues_gt(struct net_device
*dev
, u16 index
)
2472 netif_reset_xps_queues(dev
, index
, dev
->num_tx_queues
- index
);
2475 static struct xps_map
*expand_xps_map(struct xps_map
*map
, int attr_index
,
2476 u16 index
, bool is_rxqs_map
)
2478 struct xps_map
*new_map
;
2479 int alloc_len
= XPS_MIN_MAP_ALLOC
;
2482 for (pos
= 0; map
&& pos
< map
->len
; pos
++) {
2483 if (map
->queues
[pos
] != index
)
2488 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
2490 if (pos
< map
->alloc_len
)
2493 alloc_len
= map
->alloc_len
* 2;
2496 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2500 new_map
= kzalloc(XPS_MAP_SIZE(alloc_len
), GFP_KERNEL
);
2502 new_map
= kzalloc_node(XPS_MAP_SIZE(alloc_len
), GFP_KERNEL
,
2503 cpu_to_node(attr_index
));
2507 for (i
= 0; i
< pos
; i
++)
2508 new_map
->queues
[i
] = map
->queues
[i
];
2509 new_map
->alloc_len
= alloc_len
;
2515 /* Must be called under cpus_read_lock */
2516 int __netif_set_xps_queue(struct net_device
*dev
, const unsigned long *mask
,
2517 u16 index
, bool is_rxqs_map
)
2519 const unsigned long *online_mask
= NULL
, *possible_mask
= NULL
;
2520 struct xps_dev_maps
*dev_maps
, *new_dev_maps
= NULL
;
2521 int i
, j
, tci
, numa_node_id
= -2;
2522 int maps_sz
, num_tc
= 1, tc
= 0;
2523 struct xps_map
*map
, *new_map
;
2524 bool active
= false;
2525 unsigned int nr_ids
;
2528 /* Do not allow XPS on subordinate device directly */
2529 num_tc
= dev
->num_tc
;
2533 /* If queue belongs to subordinate dev use its map */
2534 dev
= netdev_get_tx_queue(dev
, index
)->sb_dev
? : dev
;
2536 tc
= netdev_txq_to_tc(dev
, index
);
2541 mutex_lock(&xps_map_mutex
);
2543 maps_sz
= XPS_RXQ_DEV_MAPS_SIZE(num_tc
, dev
->num_rx_queues
);
2544 dev_maps
= xmap_dereference(dev
->xps_rxqs_map
);
2545 nr_ids
= dev
->num_rx_queues
;
2547 maps_sz
= XPS_CPU_DEV_MAPS_SIZE(num_tc
);
2548 if (num_possible_cpus() > 1) {
2549 online_mask
= cpumask_bits(cpu_online_mask
);
2550 possible_mask
= cpumask_bits(cpu_possible_mask
);
2552 dev_maps
= xmap_dereference(dev
->xps_cpus_map
);
2553 nr_ids
= nr_cpu_ids
;
2556 if (maps_sz
< L1_CACHE_BYTES
)
2557 maps_sz
= L1_CACHE_BYTES
;
2559 /* allocate memory for queue storage */
2560 for (j
= -1; j
= netif_attrmask_next_and(j
, online_mask
, mask
, nr_ids
),
2563 new_dev_maps
= kzalloc(maps_sz
, GFP_KERNEL
);
2564 if (!new_dev_maps
) {
2565 mutex_unlock(&xps_map_mutex
);
2569 tci
= j
* num_tc
+ tc
;
2570 map
= dev_maps
? xmap_dereference(dev_maps
->attr_map
[tci
]) :
2573 map
= expand_xps_map(map
, j
, index
, is_rxqs_map
);
2577 RCU_INIT_POINTER(new_dev_maps
->attr_map
[tci
], map
);
2581 goto out_no_new_maps
;
2584 /* Increment static keys at most once per type */
2585 static_key_slow_inc_cpuslocked(&xps_needed
);
2587 static_key_slow_inc_cpuslocked(&xps_rxqs_needed
);
2590 for (j
= -1; j
= netif_attrmask_next(j
, possible_mask
, nr_ids
),
2592 /* copy maps belonging to foreign traffic classes */
2593 for (i
= tc
, tci
= j
* num_tc
; dev_maps
&& i
--; tci
++) {
2594 /* fill in the new device map from the old device map */
2595 map
= xmap_dereference(dev_maps
->attr_map
[tci
]);
2596 RCU_INIT_POINTER(new_dev_maps
->attr_map
[tci
], map
);
2599 /* We need to explicitly update tci as prevous loop
2600 * could break out early if dev_maps is NULL.
2602 tci
= j
* num_tc
+ tc
;
2604 if (netif_attr_test_mask(j
, mask
, nr_ids
) &&
2605 netif_attr_test_online(j
, online_mask
, nr_ids
)) {
2606 /* add tx-queue to CPU/rx-queue maps */
2609 map
= xmap_dereference(new_dev_maps
->attr_map
[tci
]);
2610 while ((pos
< map
->len
) && (map
->queues
[pos
] != index
))
2613 if (pos
== map
->len
)
2614 map
->queues
[map
->len
++] = index
;
2617 if (numa_node_id
== -2)
2618 numa_node_id
= cpu_to_node(j
);
2619 else if (numa_node_id
!= cpu_to_node(j
))
2623 } else if (dev_maps
) {
2624 /* fill in the new device map from the old device map */
2625 map
= xmap_dereference(dev_maps
->attr_map
[tci
]);
2626 RCU_INIT_POINTER(new_dev_maps
->attr_map
[tci
], map
);
2629 /* copy maps belonging to foreign traffic classes */
2630 for (i
= num_tc
- tc
, tci
++; dev_maps
&& --i
; tci
++) {
2631 /* fill in the new device map from the old device map */
2632 map
= xmap_dereference(dev_maps
->attr_map
[tci
]);
2633 RCU_INIT_POINTER(new_dev_maps
->attr_map
[tci
], map
);
2638 rcu_assign_pointer(dev
->xps_rxqs_map
, new_dev_maps
);
2640 rcu_assign_pointer(dev
->xps_cpus_map
, new_dev_maps
);
2642 /* Cleanup old maps */
2644 goto out_no_old_maps
;
2646 for (j
= -1; j
= netif_attrmask_next(j
, possible_mask
, nr_ids
),
2648 for (i
= num_tc
, tci
= j
* num_tc
; i
--; tci
++) {
2649 new_map
= xmap_dereference(new_dev_maps
->attr_map
[tci
]);
2650 map
= xmap_dereference(dev_maps
->attr_map
[tci
]);
2651 if (map
&& map
!= new_map
)
2652 kfree_rcu(map
, rcu
);
2656 kfree_rcu(dev_maps
, rcu
);
2659 dev_maps
= new_dev_maps
;
2664 /* update Tx queue numa node */
2665 netdev_queue_numa_node_write(netdev_get_tx_queue(dev
, index
),
2666 (numa_node_id
>= 0) ?
2667 numa_node_id
: NUMA_NO_NODE
);
2673 /* removes tx-queue from unused CPUs/rx-queues */
2674 for (j
= -1; j
= netif_attrmask_next(j
, possible_mask
, nr_ids
),
2676 for (i
= tc
, tci
= j
* num_tc
; i
--; tci
++)
2677 active
|= remove_xps_queue(dev_maps
, tci
, index
);
2678 if (!netif_attr_test_mask(j
, mask
, nr_ids
) ||
2679 !netif_attr_test_online(j
, online_mask
, nr_ids
))
2680 active
|= remove_xps_queue(dev_maps
, tci
, index
);
2681 for (i
= num_tc
- tc
, tci
++; --i
; tci
++)
2682 active
|= remove_xps_queue(dev_maps
, tci
, index
);
2685 /* free map if not active */
2687 reset_xps_maps(dev
, dev_maps
, is_rxqs_map
);
2690 mutex_unlock(&xps_map_mutex
);
2694 /* remove any maps that we added */
2695 for (j
= -1; j
= netif_attrmask_next(j
, possible_mask
, nr_ids
),
2697 for (i
= num_tc
, tci
= j
* num_tc
; i
--; tci
++) {
2698 new_map
= xmap_dereference(new_dev_maps
->attr_map
[tci
]);
2700 xmap_dereference(dev_maps
->attr_map
[tci
]) :
2702 if (new_map
&& new_map
!= map
)
2707 mutex_unlock(&xps_map_mutex
);
2709 kfree(new_dev_maps
);
2712 EXPORT_SYMBOL_GPL(__netif_set_xps_queue
);
2714 int netif_set_xps_queue(struct net_device
*dev
, const struct cpumask
*mask
,
2720 ret
= __netif_set_xps_queue(dev
, cpumask_bits(mask
), index
, false);
2725 EXPORT_SYMBOL(netif_set_xps_queue
);
2728 static void netdev_unbind_all_sb_channels(struct net_device
*dev
)
2730 struct netdev_queue
*txq
= &dev
->_tx
[dev
->num_tx_queues
];
2732 /* Unbind any subordinate channels */
2733 while (txq
-- != &dev
->_tx
[0]) {
2735 netdev_unbind_sb_channel(dev
, txq
->sb_dev
);
2739 void netdev_reset_tc(struct net_device
*dev
)
2742 netif_reset_xps_queues_gt(dev
, 0);
2744 netdev_unbind_all_sb_channels(dev
);
2746 /* Reset TC configuration of device */
2748 memset(dev
->tc_to_txq
, 0, sizeof(dev
->tc_to_txq
));
2749 memset(dev
->prio_tc_map
, 0, sizeof(dev
->prio_tc_map
));
2751 EXPORT_SYMBOL(netdev_reset_tc
);
2753 int netdev_set_tc_queue(struct net_device
*dev
, u8 tc
, u16 count
, u16 offset
)
2755 if (tc
>= dev
->num_tc
)
2759 netif_reset_xps_queues(dev
, offset
, count
);
2761 dev
->tc_to_txq
[tc
].count
= count
;
2762 dev
->tc_to_txq
[tc
].offset
= offset
;
2765 EXPORT_SYMBOL(netdev_set_tc_queue
);
2767 int netdev_set_num_tc(struct net_device
*dev
, u8 num_tc
)
2769 if (num_tc
> TC_MAX_QUEUE
)
2773 netif_reset_xps_queues_gt(dev
, 0);
2775 netdev_unbind_all_sb_channels(dev
);
2777 dev
->num_tc
= num_tc
;
2780 EXPORT_SYMBOL(netdev_set_num_tc
);
2782 void netdev_unbind_sb_channel(struct net_device
*dev
,
2783 struct net_device
*sb_dev
)
2785 struct netdev_queue
*txq
= &dev
->_tx
[dev
->num_tx_queues
];
2788 netif_reset_xps_queues_gt(sb_dev
, 0);
2790 memset(sb_dev
->tc_to_txq
, 0, sizeof(sb_dev
->tc_to_txq
));
2791 memset(sb_dev
->prio_tc_map
, 0, sizeof(sb_dev
->prio_tc_map
));
2793 while (txq
-- != &dev
->_tx
[0]) {
2794 if (txq
->sb_dev
== sb_dev
)
2798 EXPORT_SYMBOL(netdev_unbind_sb_channel
);
2800 int netdev_bind_sb_channel_queue(struct net_device
*dev
,
2801 struct net_device
*sb_dev
,
2802 u8 tc
, u16 count
, u16 offset
)
2804 /* Make certain the sb_dev and dev are already configured */
2805 if (sb_dev
->num_tc
>= 0 || tc
>= dev
->num_tc
)
2808 /* We cannot hand out queues we don't have */
2809 if ((offset
+ count
) > dev
->real_num_tx_queues
)
2812 /* Record the mapping */
2813 sb_dev
->tc_to_txq
[tc
].count
= count
;
2814 sb_dev
->tc_to_txq
[tc
].offset
= offset
;
2816 /* Provide a way for Tx queue to find the tc_to_txq map or
2817 * XPS map for itself.
2820 netdev_get_tx_queue(dev
, count
+ offset
)->sb_dev
= sb_dev
;
2824 EXPORT_SYMBOL(netdev_bind_sb_channel_queue
);
2826 int netdev_set_sb_channel(struct net_device
*dev
, u16 channel
)
2828 /* Do not use a multiqueue device to represent a subordinate channel */
2829 if (netif_is_multiqueue(dev
))
2832 /* We allow channels 1 - 32767 to be used for subordinate channels.
2833 * Channel 0 is meant to be "native" mode and used only to represent
2834 * the main root device. We allow writing 0 to reset the device back
2835 * to normal mode after being used as a subordinate channel.
2837 if (channel
> S16_MAX
)
2840 dev
->num_tc
= -channel
;
2844 EXPORT_SYMBOL(netdev_set_sb_channel
);
2847 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2848 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
2850 int netif_set_real_num_tx_queues(struct net_device
*dev
, unsigned int txq
)
2855 disabling
= txq
< dev
->real_num_tx_queues
;
2857 if (txq
< 1 || txq
> dev
->num_tx_queues
)
2860 if (dev
->reg_state
== NETREG_REGISTERED
||
2861 dev
->reg_state
== NETREG_UNREGISTERING
) {
2864 rc
= netdev_queue_update_kobjects(dev
, dev
->real_num_tx_queues
,
2870 netif_setup_tc(dev
, txq
);
2872 dev
->real_num_tx_queues
= txq
;
2876 qdisc_reset_all_tx_gt(dev
, txq
);
2878 netif_reset_xps_queues_gt(dev
, txq
);
2882 dev
->real_num_tx_queues
= txq
;
2887 EXPORT_SYMBOL(netif_set_real_num_tx_queues
);
2891 * netif_set_real_num_rx_queues - set actual number of RX queues used
2892 * @dev: Network device
2893 * @rxq: Actual number of RX queues
2895 * This must be called either with the rtnl_lock held or before
2896 * registration of the net device. Returns 0 on success, or a
2897 * negative error code. If called before registration, it always
2900 int netif_set_real_num_rx_queues(struct net_device
*dev
, unsigned int rxq
)
2904 if (rxq
< 1 || rxq
> dev
->num_rx_queues
)
2907 if (dev
->reg_state
== NETREG_REGISTERED
) {
2910 rc
= net_rx_queue_update_kobjects(dev
, dev
->real_num_rx_queues
,
2916 dev
->real_num_rx_queues
= rxq
;
2919 EXPORT_SYMBOL(netif_set_real_num_rx_queues
);
2923 * netif_get_num_default_rss_queues - default number of RSS queues
2925 * This routine should set an upper limit on the number of RSS queues
2926 * used by default by multiqueue devices.
2928 int netif_get_num_default_rss_queues(void)
2930 return is_kdump_kernel() ?
2931 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES
, num_online_cpus());
2933 EXPORT_SYMBOL(netif_get_num_default_rss_queues
);
2935 static void __netif_reschedule(struct Qdisc
*q
)
2937 struct softnet_data
*sd
;
2938 unsigned long flags
;
2940 local_irq_save(flags
);
2941 sd
= this_cpu_ptr(&softnet_data
);
2942 q
->next_sched
= NULL
;
2943 *sd
->output_queue_tailp
= q
;
2944 sd
->output_queue_tailp
= &q
->next_sched
;
2945 raise_softirq_irqoff(NET_TX_SOFTIRQ
);
2946 local_irq_restore(flags
);
2949 void __netif_schedule(struct Qdisc
*q
)
2951 if (!test_and_set_bit(__QDISC_STATE_SCHED
, &q
->state
))
2952 __netif_reschedule(q
);
2954 EXPORT_SYMBOL(__netif_schedule
);
2956 struct dev_kfree_skb_cb
{
2957 enum skb_free_reason reason
;
2960 static struct dev_kfree_skb_cb
*get_kfree_skb_cb(const struct sk_buff
*skb
)
2962 return (struct dev_kfree_skb_cb
*)skb
->cb
;
2965 void netif_schedule_queue(struct netdev_queue
*txq
)
2968 if (!netif_xmit_stopped(txq
)) {
2969 struct Qdisc
*q
= rcu_dereference(txq
->qdisc
);
2971 __netif_schedule(q
);
2975 EXPORT_SYMBOL(netif_schedule_queue
);
2977 void netif_tx_wake_queue(struct netdev_queue
*dev_queue
)
2979 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF
, &dev_queue
->state
)) {
2983 q
= rcu_dereference(dev_queue
->qdisc
);
2984 __netif_schedule(q
);
2988 EXPORT_SYMBOL(netif_tx_wake_queue
);
2990 void __dev_kfree_skb_irq(struct sk_buff
*skb
, enum skb_free_reason reason
)
2992 unsigned long flags
;
2997 if (likely(refcount_read(&skb
->users
) == 1)) {
2999 refcount_set(&skb
->users
, 0);
3000 } else if (likely(!refcount_dec_and_test(&skb
->users
))) {
3003 get_kfree_skb_cb(skb
)->reason
= reason
;
3004 local_irq_save(flags
);
3005 skb
->next
= __this_cpu_read(softnet_data
.completion_queue
);
3006 __this_cpu_write(softnet_data
.completion_queue
, skb
);
3007 raise_softirq_irqoff(NET_TX_SOFTIRQ
);
3008 local_irq_restore(flags
);
3010 EXPORT_SYMBOL(__dev_kfree_skb_irq
);
3012 void __dev_kfree_skb_any(struct sk_buff
*skb
, enum skb_free_reason reason
)
3014 if (in_irq() || irqs_disabled())
3015 __dev_kfree_skb_irq(skb
, reason
);
3019 EXPORT_SYMBOL(__dev_kfree_skb_any
);
3023 * netif_device_detach - mark device as removed
3024 * @dev: network device
3026 * Mark device as removed from system and therefore no longer available.
3028 void netif_device_detach(struct net_device
*dev
)
3030 if (test_and_clear_bit(__LINK_STATE_PRESENT
, &dev
->state
) &&
3031 netif_running(dev
)) {
3032 netif_tx_stop_all_queues(dev
);
3035 EXPORT_SYMBOL(netif_device_detach
);
3038 * netif_device_attach - mark device as attached
3039 * @dev: network device
3041 * Mark device as attached from system and restart if needed.
3043 void netif_device_attach(struct net_device
*dev
)
3045 if (!test_and_set_bit(__LINK_STATE_PRESENT
, &dev
->state
) &&
3046 netif_running(dev
)) {
3047 netif_tx_wake_all_queues(dev
);
3048 __netdev_watchdog_up(dev
);
3051 EXPORT_SYMBOL(netif_device_attach
);
3054 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3055 * to be used as a distribution range.
3057 static u16
skb_tx_hash(const struct net_device
*dev
,
3058 const struct net_device
*sb_dev
,
3059 struct sk_buff
*skb
)
3063 u16 qcount
= dev
->real_num_tx_queues
;
3066 u8 tc
= netdev_get_prio_tc_map(dev
, skb
->priority
);
3068 qoffset
= sb_dev
->tc_to_txq
[tc
].offset
;
3069 qcount
= sb_dev
->tc_to_txq
[tc
].count
;
3072 if (skb_rx_queue_recorded(skb
)) {
3073 hash
= skb_get_rx_queue(skb
);
3074 while (unlikely(hash
>= qcount
))
3076 return hash
+ qoffset
;
3079 return (u16
) reciprocal_scale(skb_get_hash(skb
), qcount
) + qoffset
;
3082 static void skb_warn_bad_offload(const struct sk_buff
*skb
)
3084 static const netdev_features_t null_features
;
3085 struct net_device
*dev
= skb
->dev
;
3086 const char *name
= "";
3088 if (!net_ratelimit())
3092 if (dev
->dev
.parent
)
3093 name
= dev_driver_string(dev
->dev
.parent
);
3095 name
= netdev_name(dev
);
3097 skb_dump(KERN_WARNING
, skb
, false);
3098 WARN(1, "%s: caps=(%pNF, %pNF)\n",
3099 name
, dev
? &dev
->features
: &null_features
,
3100 skb
->sk
? &skb
->sk
->sk_route_caps
: &null_features
);
3104 * Invalidate hardware checksum when packet is to be mangled, and
3105 * complete checksum manually on outgoing path.
3107 int skb_checksum_help(struct sk_buff
*skb
)
3110 int ret
= 0, offset
;
3112 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
3113 goto out_set_summed
;
3115 if (unlikely(skb_shinfo(skb
)->gso_size
)) {
3116 skb_warn_bad_offload(skb
);
3120 /* Before computing a checksum, we should make sure no frag could
3121 * be modified by an external entity : checksum could be wrong.
3123 if (skb_has_shared_frag(skb
)) {
3124 ret
= __skb_linearize(skb
);
3129 offset
= skb_checksum_start_offset(skb
);
3130 BUG_ON(offset
>= skb_headlen(skb
));
3131 csum
= skb_checksum(skb
, offset
, skb
->len
- offset
, 0);
3133 offset
+= skb
->csum_offset
;
3134 BUG_ON(offset
+ sizeof(__sum16
) > skb_headlen(skb
));
3136 ret
= skb_ensure_writable(skb
, offset
+ sizeof(__sum16
));
3140 *(__sum16
*)(skb
->data
+ offset
) = csum_fold(csum
) ?: CSUM_MANGLED_0
;
3142 skb
->ip_summed
= CHECKSUM_NONE
;
3146 EXPORT_SYMBOL(skb_checksum_help
);
3148 int skb_crc32c_csum_help(struct sk_buff
*skb
)
3151 int ret
= 0, offset
, start
;
3153 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
3156 if (unlikely(skb_is_gso(skb
)))
3159 /* Before computing a checksum, we should make sure no frag could
3160 * be modified by an external entity : checksum could be wrong.
3162 if (unlikely(skb_has_shared_frag(skb
))) {
3163 ret
= __skb_linearize(skb
);
3167 start
= skb_checksum_start_offset(skb
);
3168 offset
= start
+ offsetof(struct sctphdr
, checksum
);
3169 if (WARN_ON_ONCE(offset
>= skb_headlen(skb
))) {
3174 ret
= skb_ensure_writable(skb
, offset
+ sizeof(__le32
));
3178 crc32c_csum
= cpu_to_le32(~__skb_checksum(skb
, start
,
3179 skb
->len
- start
, ~(__u32
)0,
3181 *(__le32
*)(skb
->data
+ offset
) = crc32c_csum
;
3182 skb
->ip_summed
= CHECKSUM_NONE
;
3183 skb
->csum_not_inet
= 0;
3188 __be16
skb_network_protocol(struct sk_buff
*skb
, int *depth
)
3190 __be16 type
= skb
->protocol
;
3192 /* Tunnel gso handlers can set protocol to ethernet. */
3193 if (type
== htons(ETH_P_TEB
)) {
3196 if (unlikely(!pskb_may_pull(skb
, sizeof(struct ethhdr
))))
3199 eth
= (struct ethhdr
*)skb
->data
;
3200 type
= eth
->h_proto
;
3203 return __vlan_get_protocol(skb
, type
, depth
);
3207 * skb_mac_gso_segment - mac layer segmentation handler.
3208 * @skb: buffer to segment
3209 * @features: features for the output path (see dev->features)
3211 struct sk_buff
*skb_mac_gso_segment(struct sk_buff
*skb
,
3212 netdev_features_t features
)
3214 struct sk_buff
*segs
= ERR_PTR(-EPROTONOSUPPORT
);
3215 struct packet_offload
*ptype
;
3216 int vlan_depth
= skb
->mac_len
;
3217 __be16 type
= skb_network_protocol(skb
, &vlan_depth
);
3219 if (unlikely(!type
))
3220 return ERR_PTR(-EINVAL
);
3222 __skb_pull(skb
, vlan_depth
);
3225 list_for_each_entry_rcu(ptype
, &offload_base
, list
) {
3226 if (ptype
->type
== type
&& ptype
->callbacks
.gso_segment
) {
3227 segs
= ptype
->callbacks
.gso_segment(skb
, features
);
3233 __skb_push(skb
, skb
->data
- skb_mac_header(skb
));
3237 EXPORT_SYMBOL(skb_mac_gso_segment
);
3240 /* openvswitch calls this on rx path, so we need a different check.
3242 static inline bool skb_needs_check(struct sk_buff
*skb
, bool tx_path
)
3245 return skb
->ip_summed
!= CHECKSUM_PARTIAL
&&
3246 skb
->ip_summed
!= CHECKSUM_UNNECESSARY
;
3248 return skb
->ip_summed
== CHECKSUM_NONE
;
3252 * __skb_gso_segment - Perform segmentation on skb.
3253 * @skb: buffer to segment
3254 * @features: features for the output path (see dev->features)
3255 * @tx_path: whether it is called in TX path
3257 * This function segments the given skb and returns a list of segments.
3259 * It may return NULL if the skb requires no segmentation. This is
3260 * only possible when GSO is used for verifying header integrity.
3262 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
3264 struct sk_buff
*__skb_gso_segment(struct sk_buff
*skb
,
3265 netdev_features_t features
, bool tx_path
)
3267 struct sk_buff
*segs
;
3269 if (unlikely(skb_needs_check(skb
, tx_path
))) {
3272 /* We're going to init ->check field in TCP or UDP header */
3273 err
= skb_cow_head(skb
, 0);
3275 return ERR_PTR(err
);
3278 /* Only report GSO partial support if it will enable us to
3279 * support segmentation on this frame without needing additional
3282 if (features
& NETIF_F_GSO_PARTIAL
) {
3283 netdev_features_t partial_features
= NETIF_F_GSO_ROBUST
;
3284 struct net_device
*dev
= skb
->dev
;
3286 partial_features
|= dev
->features
& dev
->gso_partial_features
;
3287 if (!skb_gso_ok(skb
, features
| partial_features
))
3288 features
&= ~NETIF_F_GSO_PARTIAL
;
3291 BUILD_BUG_ON(SKB_SGO_CB_OFFSET
+
3292 sizeof(*SKB_GSO_CB(skb
)) > sizeof(skb
->cb
));
3294 SKB_GSO_CB(skb
)->mac_offset
= skb_headroom(skb
);
3295 SKB_GSO_CB(skb
)->encap_level
= 0;
3297 skb_reset_mac_header(skb
);
3298 skb_reset_mac_len(skb
);
3300 segs
= skb_mac_gso_segment(skb
, features
);
3302 if (segs
!= skb
&& unlikely(skb_needs_check(skb
, tx_path
) && !IS_ERR(segs
)))
3303 skb_warn_bad_offload(skb
);
3307 EXPORT_SYMBOL(__skb_gso_segment
);
3309 /* Take action when hardware reception checksum errors are detected. */
3311 void netdev_rx_csum_fault(struct net_device
*dev
, struct sk_buff
*skb
)
3313 if (net_ratelimit()) {
3314 pr_err("%s: hw csum failure\n", dev
? dev
->name
: "<unknown>");
3315 skb_dump(KERN_ERR
, skb
, true);
3319 EXPORT_SYMBOL(netdev_rx_csum_fault
);
3322 /* XXX: check that highmem exists at all on the given machine. */
3323 static int illegal_highdma(struct net_device
*dev
, struct sk_buff
*skb
)
3325 #ifdef CONFIG_HIGHMEM
3328 if (!(dev
->features
& NETIF_F_HIGHDMA
)) {
3329 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
3330 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
3332 if (PageHighMem(skb_frag_page(frag
)))
3340 /* If MPLS offload request, verify we are testing hardware MPLS features
3341 * instead of standard features for the netdev.
3343 #if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3344 static netdev_features_t
net_mpls_features(struct sk_buff
*skb
,
3345 netdev_features_t features
,
3348 if (eth_p_mpls(type
))
3349 features
&= skb
->dev
->mpls_features
;
3354 static netdev_features_t
net_mpls_features(struct sk_buff
*skb
,
3355 netdev_features_t features
,
3362 static netdev_features_t
harmonize_features(struct sk_buff
*skb
,
3363 netdev_features_t features
)
3368 type
= skb_network_protocol(skb
, &tmp
);
3369 features
= net_mpls_features(skb
, features
, type
);
3371 if (skb
->ip_summed
!= CHECKSUM_NONE
&&
3372 !can_checksum_protocol(features
, type
)) {
3373 features
&= ~(NETIF_F_CSUM_MASK
| NETIF_F_GSO_MASK
);
3375 if (illegal_highdma(skb
->dev
, skb
))
3376 features
&= ~NETIF_F_SG
;
3381 netdev_features_t
passthru_features_check(struct sk_buff
*skb
,
3382 struct net_device
*dev
,
3383 netdev_features_t features
)
3387 EXPORT_SYMBOL(passthru_features_check
);
3389 static netdev_features_t
dflt_features_check(struct sk_buff
*skb
,
3390 struct net_device
*dev
,
3391 netdev_features_t features
)
3393 return vlan_features_check(skb
, features
);
3396 static netdev_features_t
gso_features_check(const struct sk_buff
*skb
,
3397 struct net_device
*dev
,
3398 netdev_features_t features
)
3400 u16 gso_segs
= skb_shinfo(skb
)->gso_segs
;
3402 if (gso_segs
> dev
->gso_max_segs
)
3403 return features
& ~NETIF_F_GSO_MASK
;
3405 /* Support for GSO partial features requires software
3406 * intervention before we can actually process the packets
3407 * so we need to strip support for any partial features now
3408 * and we can pull them back in after we have partially
3409 * segmented the frame.
3411 if (!(skb_shinfo(skb
)->gso_type
& SKB_GSO_PARTIAL
))
3412 features
&= ~dev
->gso_partial_features
;
3414 /* Make sure to clear the IPv4 ID mangling feature if the
3415 * IPv4 header has the potential to be fragmented.
3417 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
) {
3418 struct iphdr
*iph
= skb
->encapsulation
?
3419 inner_ip_hdr(skb
) : ip_hdr(skb
);
3421 if (!(iph
->frag_off
& htons(IP_DF
)))
3422 features
&= ~NETIF_F_TSO_MANGLEID
;
3428 netdev_features_t
netif_skb_features(struct sk_buff
*skb
)
3430 struct net_device
*dev
= skb
->dev
;
3431 netdev_features_t features
= dev
->features
;
3433 if (skb_is_gso(skb
))
3434 features
= gso_features_check(skb
, dev
, features
);
3436 /* If encapsulation offload request, verify we are testing
3437 * hardware encapsulation features instead of standard
3438 * features for the netdev
3440 if (skb
->encapsulation
)
3441 features
&= dev
->hw_enc_features
;
3443 if (skb_vlan_tagged(skb
))
3444 features
= netdev_intersect_features(features
,
3445 dev
->vlan_features
|
3446 NETIF_F_HW_VLAN_CTAG_TX
|
3447 NETIF_F_HW_VLAN_STAG_TX
);
3449 if (dev
->netdev_ops
->ndo_features_check
)
3450 features
&= dev
->netdev_ops
->ndo_features_check(skb
, dev
,
3453 features
&= dflt_features_check(skb
, dev
, features
);
3455 return harmonize_features(skb
, features
);
3457 EXPORT_SYMBOL(netif_skb_features
);
3459 static int xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
3460 struct netdev_queue
*txq
, bool more
)
3465 if (dev_nit_active(dev
))
3466 dev_queue_xmit_nit(skb
, dev
);
3469 trace_net_dev_start_xmit(skb
, dev
);
3470 rc
= netdev_start_xmit(skb
, dev
, txq
, more
);
3471 trace_net_dev_xmit(skb
, rc
, dev
, len
);
3476 struct sk_buff
*dev_hard_start_xmit(struct sk_buff
*first
, struct net_device
*dev
,
3477 struct netdev_queue
*txq
, int *ret
)
3479 struct sk_buff
*skb
= first
;
3480 int rc
= NETDEV_TX_OK
;
3483 struct sk_buff
*next
= skb
->next
;
3485 skb_mark_not_on_list(skb
);
3486 rc
= xmit_one(skb
, dev
, txq
, next
!= NULL
);
3487 if (unlikely(!dev_xmit_complete(rc
))) {
3493 if (netif_tx_queue_stopped(txq
) && skb
) {
3494 rc
= NETDEV_TX_BUSY
;
3504 static struct sk_buff
*validate_xmit_vlan(struct sk_buff
*skb
,
3505 netdev_features_t features
)
3507 if (skb_vlan_tag_present(skb
) &&
3508 !vlan_hw_offload_capable(features
, skb
->vlan_proto
))
3509 skb
= __vlan_hwaccel_push_inside(skb
);
3513 int skb_csum_hwoffload_help(struct sk_buff
*skb
,
3514 const netdev_features_t features
)
3516 if (unlikely(skb
->csum_not_inet
))
3517 return !!(features
& NETIF_F_SCTP_CRC
) ? 0 :
3518 skb_crc32c_csum_help(skb
);
3520 return !!(features
& NETIF_F_CSUM_MASK
) ? 0 : skb_checksum_help(skb
);
3522 EXPORT_SYMBOL(skb_csum_hwoffload_help
);
3524 static struct sk_buff
*validate_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
, bool *again
)
3526 netdev_features_t features
;
3528 features
= netif_skb_features(skb
);
3529 skb
= validate_xmit_vlan(skb
, features
);
3533 skb
= sk_validate_xmit_skb(skb
, dev
);
3537 if (netif_needs_gso(skb
, features
)) {
3538 struct sk_buff
*segs
;
3540 segs
= skb_gso_segment(skb
, features
);
3548 if (skb_needs_linearize(skb
, features
) &&
3549 __skb_linearize(skb
))
3552 /* If packet is not checksummed and device does not
3553 * support checksumming for this protocol, complete
3554 * checksumming here.
3556 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
3557 if (skb
->encapsulation
)
3558 skb_set_inner_transport_header(skb
,
3559 skb_checksum_start_offset(skb
));
3561 skb_set_transport_header(skb
,
3562 skb_checksum_start_offset(skb
));
3563 if (skb_csum_hwoffload_help(skb
, features
))
3568 skb
= validate_xmit_xfrm(skb
, features
, again
);
3575 atomic_long_inc(&dev
->tx_dropped
);
3579 struct sk_buff
*validate_xmit_skb_list(struct sk_buff
*skb
, struct net_device
*dev
, bool *again
)
3581 struct sk_buff
*next
, *head
= NULL
, *tail
;
3583 for (; skb
!= NULL
; skb
= next
) {
3585 skb_mark_not_on_list(skb
);
3587 /* in case skb wont be segmented, point to itself */
3590 skb
= validate_xmit_skb(skb
, dev
, again
);
3598 /* If skb was segmented, skb->prev points to
3599 * the last segment. If not, it still contains skb.
3605 EXPORT_SYMBOL_GPL(validate_xmit_skb_list
);
3607 static void qdisc_pkt_len_init(struct sk_buff
*skb
)
3609 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
3611 qdisc_skb_cb(skb
)->pkt_len
= skb
->len
;
3613 /* To get more precise estimation of bytes sent on wire,
3614 * we add to pkt_len the headers size of all segments
3616 if (shinfo
->gso_size
&& skb_transport_header_was_set(skb
)) {
3617 unsigned int hdr_len
;
3618 u16 gso_segs
= shinfo
->gso_segs
;
3620 /* mac layer + network layer */
3621 hdr_len
= skb_transport_header(skb
) - skb_mac_header(skb
);
3623 /* + transport layer */
3624 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
))) {
3625 const struct tcphdr
*th
;
3626 struct tcphdr _tcphdr
;
3628 th
= skb_header_pointer(skb
, skb_transport_offset(skb
),
3629 sizeof(_tcphdr
), &_tcphdr
);
3631 hdr_len
+= __tcp_hdrlen(th
);
3633 struct udphdr _udphdr
;
3635 if (skb_header_pointer(skb
, skb_transport_offset(skb
),
3636 sizeof(_udphdr
), &_udphdr
))
3637 hdr_len
+= sizeof(struct udphdr
);
3640 if (shinfo
->gso_type
& SKB_GSO_DODGY
)
3641 gso_segs
= DIV_ROUND_UP(skb
->len
- hdr_len
,
3644 qdisc_skb_cb(skb
)->pkt_len
+= (gso_segs
- 1) * hdr_len
;
3648 static inline int __dev_xmit_skb(struct sk_buff
*skb
, struct Qdisc
*q
,
3649 struct net_device
*dev
,
3650 struct netdev_queue
*txq
)
3652 spinlock_t
*root_lock
= qdisc_lock(q
);
3653 struct sk_buff
*to_free
= NULL
;
3657 qdisc_calculate_pkt_len(skb
, q
);
3659 if (q
->flags
& TCQ_F_NOLOCK
) {
3660 if ((q
->flags
& TCQ_F_CAN_BYPASS
) && READ_ONCE(q
->empty
) &&
3661 qdisc_run_begin(q
)) {
3662 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED
,
3664 __qdisc_drop(skb
, &to_free
);
3668 qdisc_bstats_cpu_update(q
, skb
);
3670 rc
= NET_XMIT_SUCCESS
;
3671 if (sch_direct_xmit(skb
, q
, dev
, txq
, NULL
, true))
3677 rc
= q
->enqueue(skb
, q
, &to_free
) & NET_XMIT_MASK
;
3681 if (unlikely(to_free
))
3682 kfree_skb_list(to_free
);
3687 * Heuristic to force contended enqueues to serialize on a
3688 * separate lock before trying to get qdisc main lock.
3689 * This permits qdisc->running owner to get the lock more
3690 * often and dequeue packets faster.
3692 contended
= qdisc_is_running(q
);
3693 if (unlikely(contended
))
3694 spin_lock(&q
->busylock
);
3696 spin_lock(root_lock
);
3697 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED
, &q
->state
))) {
3698 __qdisc_drop(skb
, &to_free
);
3700 } else if ((q
->flags
& TCQ_F_CAN_BYPASS
) && !qdisc_qlen(q
) &&
3701 qdisc_run_begin(q
)) {
3703 * This is a work-conserving queue; there are no old skbs
3704 * waiting to be sent out; and the qdisc is not running -
3705 * xmit the skb directly.
3708 qdisc_bstats_update(q
, skb
);
3710 if (sch_direct_xmit(skb
, q
, dev
, txq
, root_lock
, true)) {
3711 if (unlikely(contended
)) {
3712 spin_unlock(&q
->busylock
);
3719 rc
= NET_XMIT_SUCCESS
;
3721 rc
= q
->enqueue(skb
, q
, &to_free
) & NET_XMIT_MASK
;
3722 if (qdisc_run_begin(q
)) {
3723 if (unlikely(contended
)) {
3724 spin_unlock(&q
->busylock
);
3731 spin_unlock(root_lock
);
3732 if (unlikely(to_free
))
3733 kfree_skb_list(to_free
);
3734 if (unlikely(contended
))
3735 spin_unlock(&q
->busylock
);
3739 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
3740 static void skb_update_prio(struct sk_buff
*skb
)
3742 const struct netprio_map
*map
;
3743 const struct sock
*sk
;
3744 unsigned int prioidx
;
3748 map
= rcu_dereference_bh(skb
->dev
->priomap
);
3751 sk
= skb_to_full_sk(skb
);
3755 prioidx
= sock_cgroup_prioidx(&sk
->sk_cgrp_data
);
3757 if (prioidx
< map
->priomap_len
)
3758 skb
->priority
= map
->priomap
[prioidx
];
3761 #define skb_update_prio(skb)
3765 * dev_loopback_xmit - loop back @skb
3766 * @net: network namespace this loopback is happening in
3767 * @sk: sk needed to be a netfilter okfn
3768 * @skb: buffer to transmit
3770 int dev_loopback_xmit(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
3772 skb_reset_mac_header(skb
);
3773 __skb_pull(skb
, skb_network_offset(skb
));
3774 skb
->pkt_type
= PACKET_LOOPBACK
;
3775 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
3776 WARN_ON(!skb_dst(skb
));
3781 EXPORT_SYMBOL(dev_loopback_xmit
);
3783 #ifdef CONFIG_NET_EGRESS
3784 static struct sk_buff
*
3785 sch_handle_egress(struct sk_buff
*skb
, int *ret
, struct net_device
*dev
)
3787 struct mini_Qdisc
*miniq
= rcu_dereference_bh(dev
->miniq_egress
);
3788 struct tcf_result cl_res
;
3793 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
3794 mini_qdisc_bstats_cpu_update(miniq
, skb
);
3796 switch (tcf_classify(skb
, miniq
->filter_list
, &cl_res
, false)) {
3798 case TC_ACT_RECLASSIFY
:
3799 skb
->tc_index
= TC_H_MIN(cl_res
.classid
);
3802 mini_qdisc_qstats_cpu_drop(miniq
);
3803 *ret
= NET_XMIT_DROP
;
3809 *ret
= NET_XMIT_SUCCESS
;
3812 case TC_ACT_REDIRECT
:
3813 /* No need to push/pop skb's mac_header here on egress! */
3814 skb_do_redirect(skb
);
3815 *ret
= NET_XMIT_SUCCESS
;
3823 #endif /* CONFIG_NET_EGRESS */
3826 static int __get_xps_queue_idx(struct net_device
*dev
, struct sk_buff
*skb
,
3827 struct xps_dev_maps
*dev_maps
, unsigned int tci
)
3829 struct xps_map
*map
;
3830 int queue_index
= -1;
3834 tci
+= netdev_get_prio_tc_map(dev
, skb
->priority
);
3837 map
= rcu_dereference(dev_maps
->attr_map
[tci
]);
3840 queue_index
= map
->queues
[0];
3842 queue_index
= map
->queues
[reciprocal_scale(
3843 skb_get_hash(skb
), map
->len
)];
3844 if (unlikely(queue_index
>= dev
->real_num_tx_queues
))
3851 static int get_xps_queue(struct net_device
*dev
, struct net_device
*sb_dev
,
3852 struct sk_buff
*skb
)
3855 struct xps_dev_maps
*dev_maps
;
3856 struct sock
*sk
= skb
->sk
;
3857 int queue_index
= -1;
3859 if (!static_key_false(&xps_needed
))
3863 if (!static_key_false(&xps_rxqs_needed
))
3866 dev_maps
= rcu_dereference(sb_dev
->xps_rxqs_map
);
3868 int tci
= sk_rx_queue_get(sk
);
3870 if (tci
>= 0 && tci
< dev
->num_rx_queues
)
3871 queue_index
= __get_xps_queue_idx(dev
, skb
, dev_maps
,
3876 if (queue_index
< 0) {
3877 dev_maps
= rcu_dereference(sb_dev
->xps_cpus_map
);
3879 unsigned int tci
= skb
->sender_cpu
- 1;
3881 queue_index
= __get_xps_queue_idx(dev
, skb
, dev_maps
,
3893 u16
dev_pick_tx_zero(struct net_device
*dev
, struct sk_buff
*skb
,
3894 struct net_device
*sb_dev
)
3898 EXPORT_SYMBOL(dev_pick_tx_zero
);
3900 u16
dev_pick_tx_cpu_id(struct net_device
*dev
, struct sk_buff
*skb
,
3901 struct net_device
*sb_dev
)
3903 return (u16
)raw_smp_processor_id() % dev
->real_num_tx_queues
;
3905 EXPORT_SYMBOL(dev_pick_tx_cpu_id
);
3907 u16
netdev_pick_tx(struct net_device
*dev
, struct sk_buff
*skb
,
3908 struct net_device
*sb_dev
)
3910 struct sock
*sk
= skb
->sk
;
3911 int queue_index
= sk_tx_queue_get(sk
);
3913 sb_dev
= sb_dev
? : dev
;
3915 if (queue_index
< 0 || skb
->ooo_okay
||
3916 queue_index
>= dev
->real_num_tx_queues
) {
3917 int new_index
= get_xps_queue(dev
, sb_dev
, skb
);
3920 new_index
= skb_tx_hash(dev
, sb_dev
, skb
);
3922 if (queue_index
!= new_index
&& sk
&&
3924 rcu_access_pointer(sk
->sk_dst_cache
))
3925 sk_tx_queue_set(sk
, new_index
);
3927 queue_index
= new_index
;
3932 EXPORT_SYMBOL(netdev_pick_tx
);
3934 struct netdev_queue
*netdev_core_pick_tx(struct net_device
*dev
,
3935 struct sk_buff
*skb
,
3936 struct net_device
*sb_dev
)
3938 int queue_index
= 0;
3941 u32 sender_cpu
= skb
->sender_cpu
- 1;
3943 if (sender_cpu
>= (u32
)NR_CPUS
)
3944 skb
->sender_cpu
= raw_smp_processor_id() + 1;
3947 if (dev
->real_num_tx_queues
!= 1) {
3948 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3950 if (ops
->ndo_select_queue
)
3951 queue_index
= ops
->ndo_select_queue(dev
, skb
, sb_dev
);
3953 queue_index
= netdev_pick_tx(dev
, skb
, sb_dev
);
3955 queue_index
= netdev_cap_txqueue(dev
, queue_index
);
3958 skb_set_queue_mapping(skb
, queue_index
);
3959 return netdev_get_tx_queue(dev
, queue_index
);
3963 * __dev_queue_xmit - transmit a buffer
3964 * @skb: buffer to transmit
3965 * @sb_dev: suboordinate device used for L2 forwarding offload
3967 * Queue a buffer for transmission to a network device. The caller must
3968 * have set the device and priority and built the buffer before calling
3969 * this function. The function can be called from an interrupt.
3971 * A negative errno code is returned on a failure. A success does not
3972 * guarantee the frame will be transmitted as it may be dropped due
3973 * to congestion or traffic shaping.
3975 * -----------------------------------------------------------------------------------
3976 * I notice this method can also return errors from the queue disciplines,
3977 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3980 * Regardless of the return value, the skb is consumed, so it is currently
3981 * difficult to retry a send to this method. (You can bump the ref count
3982 * before sending to hold a reference for retry if you are careful.)
3984 * When calling this method, interrupts MUST be enabled. This is because
3985 * the BH enable code must have IRQs enabled so that it will not deadlock.
3988 static int __dev_queue_xmit(struct sk_buff
*skb
, struct net_device
*sb_dev
)
3990 struct net_device
*dev
= skb
->dev
;
3991 struct netdev_queue
*txq
;
3996 skb_reset_mac_header(skb
);
3998 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_SCHED_TSTAMP
))
3999 __skb_tstamp_tx(skb
, NULL
, skb
->sk
, SCM_TSTAMP_SCHED
);
4001 /* Disable soft irqs for various locks below. Also
4002 * stops preemption for RCU.
4006 skb_update_prio(skb
);
4008 qdisc_pkt_len_init(skb
);
4009 #ifdef CONFIG_NET_CLS_ACT
4010 skb
->tc_at_ingress
= 0;
4011 # ifdef CONFIG_NET_EGRESS
4012 if (static_branch_unlikely(&egress_needed_key
)) {
4013 skb
= sch_handle_egress(skb
, &rc
, dev
);
4019 /* If device/qdisc don't need skb->dst, release it right now while
4020 * its hot in this cpu cache.
4022 if (dev
->priv_flags
& IFF_XMIT_DST_RELEASE
)
4027 txq
= netdev_core_pick_tx(dev
, skb
, sb_dev
);
4028 q
= rcu_dereference_bh(txq
->qdisc
);
4030 trace_net_dev_queue(skb
);
4032 rc
= __dev_xmit_skb(skb
, q
, dev
, txq
);
4036 /* The device has no queue. Common case for software devices:
4037 * loopback, all the sorts of tunnels...
4039 * Really, it is unlikely that netif_tx_lock protection is necessary
4040 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4042 * However, it is possible, that they rely on protection
4045 * Check this and shot the lock. It is not prone from deadlocks.
4046 *Either shot noqueue qdisc, it is even simpler 8)
4048 if (dev
->flags
& IFF_UP
) {
4049 int cpu
= smp_processor_id(); /* ok because BHs are off */
4051 if (txq
->xmit_lock_owner
!= cpu
) {
4052 if (dev_xmit_recursion())
4053 goto recursion_alert
;
4055 skb
= validate_xmit_skb(skb
, dev
, &again
);
4059 HARD_TX_LOCK(dev
, txq
, cpu
);
4061 if (!netif_xmit_stopped(txq
)) {
4062 dev_xmit_recursion_inc();
4063 skb
= dev_hard_start_xmit(skb
, dev
, txq
, &rc
);
4064 dev_xmit_recursion_dec();
4065 if (dev_xmit_complete(rc
)) {
4066 HARD_TX_UNLOCK(dev
, txq
);
4070 HARD_TX_UNLOCK(dev
, txq
);
4071 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4074 /* Recursion is detected! It is possible,
4078 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4084 rcu_read_unlock_bh();
4086 atomic_long_inc(&dev
->tx_dropped
);
4087 kfree_skb_list(skb
);
4090 rcu_read_unlock_bh();
4094 int dev_queue_xmit(struct sk_buff
*skb
)
4096 return __dev_queue_xmit(skb
, NULL
);
4098 EXPORT_SYMBOL(dev_queue_xmit
);
4100 int dev_queue_xmit_accel(struct sk_buff
*skb
, struct net_device
*sb_dev
)
4102 return __dev_queue_xmit(skb
, sb_dev
);
4104 EXPORT_SYMBOL(dev_queue_xmit_accel
);
4106 int dev_direct_xmit(struct sk_buff
*skb
, u16 queue_id
)
4108 struct net_device
*dev
= skb
->dev
;
4109 struct sk_buff
*orig_skb
= skb
;
4110 struct netdev_queue
*txq
;
4111 int ret
= NETDEV_TX_BUSY
;
4114 if (unlikely(!netif_running(dev
) ||
4115 !netif_carrier_ok(dev
)))
4118 skb
= validate_xmit_skb_list(skb
, dev
, &again
);
4119 if (skb
!= orig_skb
)
4122 skb_set_queue_mapping(skb
, queue_id
);
4123 txq
= skb_get_tx_queue(dev
, skb
);
4127 HARD_TX_LOCK(dev
, txq
, smp_processor_id());
4128 if (!netif_xmit_frozen_or_drv_stopped(txq
))
4129 ret
= netdev_start_xmit(skb
, dev
, txq
, false);
4130 HARD_TX_UNLOCK(dev
, txq
);
4134 if (!dev_xmit_complete(ret
))
4139 atomic_long_inc(&dev
->tx_dropped
);
4140 kfree_skb_list(skb
);
4141 return NET_XMIT_DROP
;
4143 EXPORT_SYMBOL(dev_direct_xmit
);
4145 /*************************************************************************
4147 *************************************************************************/
4149 int netdev_max_backlog __read_mostly
= 1000;
4150 EXPORT_SYMBOL(netdev_max_backlog
);
4152 int netdev_tstamp_prequeue __read_mostly
= 1;
4153 int netdev_budget __read_mostly
= 300;
4154 unsigned int __read_mostly netdev_budget_usecs
= 2000;
4155 int weight_p __read_mostly
= 64; /* old backlog weight */
4156 int dev_weight_rx_bias __read_mostly
= 1; /* bias for backlog weight */
4157 int dev_weight_tx_bias __read_mostly
= 1; /* bias for output_queue quota */
4158 int dev_rx_weight __read_mostly
= 64;
4159 int dev_tx_weight __read_mostly
= 64;
4160 /* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4161 int gro_normal_batch __read_mostly
= 8;
4163 /* Called with irq disabled */
4164 static inline void ____napi_schedule(struct softnet_data
*sd
,
4165 struct napi_struct
*napi
)
4167 list_add_tail(&napi
->poll_list
, &sd
->poll_list
);
4168 __raise_softirq_irqoff(NET_RX_SOFTIRQ
);
4173 /* One global table that all flow-based protocols share. */
4174 struct rps_sock_flow_table __rcu
*rps_sock_flow_table __read_mostly
;
4175 EXPORT_SYMBOL(rps_sock_flow_table
);
4176 u32 rps_cpu_mask __read_mostly
;
4177 EXPORT_SYMBOL(rps_cpu_mask
);
4179 struct static_key_false rps_needed __read_mostly
;
4180 EXPORT_SYMBOL(rps_needed
);
4181 struct static_key_false rfs_needed __read_mostly
;
4182 EXPORT_SYMBOL(rfs_needed
);
4184 static struct rps_dev_flow
*
4185 set_rps_cpu(struct net_device
*dev
, struct sk_buff
*skb
,
4186 struct rps_dev_flow
*rflow
, u16 next_cpu
)
4188 if (next_cpu
< nr_cpu_ids
) {
4189 #ifdef CONFIG_RFS_ACCEL
4190 struct netdev_rx_queue
*rxqueue
;
4191 struct rps_dev_flow_table
*flow_table
;
4192 struct rps_dev_flow
*old_rflow
;
4197 /* Should we steer this flow to a different hardware queue? */
4198 if (!skb_rx_queue_recorded(skb
) || !dev
->rx_cpu_rmap
||
4199 !(dev
->features
& NETIF_F_NTUPLE
))
4201 rxq_index
= cpu_rmap_lookup_index(dev
->rx_cpu_rmap
, next_cpu
);
4202 if (rxq_index
== skb_get_rx_queue(skb
))
4205 rxqueue
= dev
->_rx
+ rxq_index
;
4206 flow_table
= rcu_dereference(rxqueue
->rps_flow_table
);
4209 flow_id
= skb_get_hash(skb
) & flow_table
->mask
;
4210 rc
= dev
->netdev_ops
->ndo_rx_flow_steer(dev
, skb
,
4211 rxq_index
, flow_id
);
4215 rflow
= &flow_table
->flows
[flow_id
];
4217 if (old_rflow
->filter
== rflow
->filter
)
4218 old_rflow
->filter
= RPS_NO_FILTER
;
4222 per_cpu(softnet_data
, next_cpu
).input_queue_head
;
4225 rflow
->cpu
= next_cpu
;
4230 * get_rps_cpu is called from netif_receive_skb and returns the target
4231 * CPU from the RPS map of the receiving queue for a given skb.
4232 * rcu_read_lock must be held on entry.
4234 static int get_rps_cpu(struct net_device
*dev
, struct sk_buff
*skb
,
4235 struct rps_dev_flow
**rflowp
)
4237 const struct rps_sock_flow_table
*sock_flow_table
;
4238 struct netdev_rx_queue
*rxqueue
= dev
->_rx
;
4239 struct rps_dev_flow_table
*flow_table
;
4240 struct rps_map
*map
;
4245 if (skb_rx_queue_recorded(skb
)) {
4246 u16 index
= skb_get_rx_queue(skb
);
4248 if (unlikely(index
>= dev
->real_num_rx_queues
)) {
4249 WARN_ONCE(dev
->real_num_rx_queues
> 1,
4250 "%s received packet on queue %u, but number "
4251 "of RX queues is %u\n",
4252 dev
->name
, index
, dev
->real_num_rx_queues
);
4258 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4260 flow_table
= rcu_dereference(rxqueue
->rps_flow_table
);
4261 map
= rcu_dereference(rxqueue
->rps_map
);
4262 if (!flow_table
&& !map
)
4265 skb_reset_network_header(skb
);
4266 hash
= skb_get_hash(skb
);
4270 sock_flow_table
= rcu_dereference(rps_sock_flow_table
);
4271 if (flow_table
&& sock_flow_table
) {
4272 struct rps_dev_flow
*rflow
;
4276 /* First check into global flow table if there is a match */
4277 ident
= sock_flow_table
->ents
[hash
& sock_flow_table
->mask
];
4278 if ((ident
^ hash
) & ~rps_cpu_mask
)
4281 next_cpu
= ident
& rps_cpu_mask
;
4283 /* OK, now we know there is a match,
4284 * we can look at the local (per receive queue) flow table
4286 rflow
= &flow_table
->flows
[hash
& flow_table
->mask
];
4290 * If the desired CPU (where last recvmsg was done) is
4291 * different from current CPU (one in the rx-queue flow
4292 * table entry), switch if one of the following holds:
4293 * - Current CPU is unset (>= nr_cpu_ids).
4294 * - Current CPU is offline.
4295 * - The current CPU's queue tail has advanced beyond the
4296 * last packet that was enqueued using this table entry.
4297 * This guarantees that all previous packets for the flow
4298 * have been dequeued, thus preserving in order delivery.
4300 if (unlikely(tcpu
!= next_cpu
) &&
4301 (tcpu
>= nr_cpu_ids
|| !cpu_online(tcpu
) ||
4302 ((int)(per_cpu(softnet_data
, tcpu
).input_queue_head
-
4303 rflow
->last_qtail
)) >= 0)) {
4305 rflow
= set_rps_cpu(dev
, skb
, rflow
, next_cpu
);
4308 if (tcpu
< nr_cpu_ids
&& cpu_online(tcpu
)) {
4318 tcpu
= map
->cpus
[reciprocal_scale(hash
, map
->len
)];
4319 if (cpu_online(tcpu
)) {
4329 #ifdef CONFIG_RFS_ACCEL
4332 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4333 * @dev: Device on which the filter was set
4334 * @rxq_index: RX queue index
4335 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4336 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4338 * Drivers that implement ndo_rx_flow_steer() should periodically call
4339 * this function for each installed filter and remove the filters for
4340 * which it returns %true.
4342 bool rps_may_expire_flow(struct net_device
*dev
, u16 rxq_index
,
4343 u32 flow_id
, u16 filter_id
)
4345 struct netdev_rx_queue
*rxqueue
= dev
->_rx
+ rxq_index
;
4346 struct rps_dev_flow_table
*flow_table
;
4347 struct rps_dev_flow
*rflow
;
4352 flow_table
= rcu_dereference(rxqueue
->rps_flow_table
);
4353 if (flow_table
&& flow_id
<= flow_table
->mask
) {
4354 rflow
= &flow_table
->flows
[flow_id
];
4355 cpu
= READ_ONCE(rflow
->cpu
);
4356 if (rflow
->filter
== filter_id
&& cpu
< nr_cpu_ids
&&
4357 ((int)(per_cpu(softnet_data
, cpu
).input_queue_head
-
4358 rflow
->last_qtail
) <
4359 (int)(10 * flow_table
->mask
)))
4365 EXPORT_SYMBOL(rps_may_expire_flow
);
4367 #endif /* CONFIG_RFS_ACCEL */
4369 /* Called from hardirq (IPI) context */
4370 static void rps_trigger_softirq(void *data
)
4372 struct softnet_data
*sd
= data
;
4374 ____napi_schedule(sd
, &sd
->backlog
);
4378 #endif /* CONFIG_RPS */
4381 * Check if this softnet_data structure is another cpu one
4382 * If yes, queue it to our IPI list and return 1
4385 static int rps_ipi_queued(struct softnet_data
*sd
)
4388 struct softnet_data
*mysd
= this_cpu_ptr(&softnet_data
);
4391 sd
->rps_ipi_next
= mysd
->rps_ipi_list
;
4392 mysd
->rps_ipi_list
= sd
;
4394 __raise_softirq_irqoff(NET_RX_SOFTIRQ
);
4397 #endif /* CONFIG_RPS */
4401 #ifdef CONFIG_NET_FLOW_LIMIT
4402 int netdev_flow_limit_table_len __read_mostly
= (1 << 12);
4405 static bool skb_flow_limit(struct sk_buff
*skb
, unsigned int qlen
)
4407 #ifdef CONFIG_NET_FLOW_LIMIT
4408 struct sd_flow_limit
*fl
;
4409 struct softnet_data
*sd
;
4410 unsigned int old_flow
, new_flow
;
4412 if (qlen
< (netdev_max_backlog
>> 1))
4415 sd
= this_cpu_ptr(&softnet_data
);
4418 fl
= rcu_dereference(sd
->flow_limit
);
4420 new_flow
= skb_get_hash(skb
) & (fl
->num_buckets
- 1);
4421 old_flow
= fl
->history
[fl
->history_head
];
4422 fl
->history
[fl
->history_head
] = new_flow
;
4425 fl
->history_head
&= FLOW_LIMIT_HISTORY
- 1;
4427 if (likely(fl
->buckets
[old_flow
]))
4428 fl
->buckets
[old_flow
]--;
4430 if (++fl
->buckets
[new_flow
] > (FLOW_LIMIT_HISTORY
>> 1)) {
4442 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4443 * queue (may be a remote CPU queue).
4445 static int enqueue_to_backlog(struct sk_buff
*skb
, int cpu
,
4446 unsigned int *qtail
)
4448 struct softnet_data
*sd
;
4449 unsigned long flags
;
4452 sd
= &per_cpu(softnet_data
, cpu
);
4454 local_irq_save(flags
);
4457 if (!netif_running(skb
->dev
))
4459 qlen
= skb_queue_len(&sd
->input_pkt_queue
);
4460 if (qlen
<= netdev_max_backlog
&& !skb_flow_limit(skb
, qlen
)) {
4463 __skb_queue_tail(&sd
->input_pkt_queue
, skb
);
4464 input_queue_tail_incr_save(sd
, qtail
);
4466 local_irq_restore(flags
);
4467 return NET_RX_SUCCESS
;
4470 /* Schedule NAPI for backlog device
4471 * We can use non atomic operation since we own the queue lock
4473 if (!__test_and_set_bit(NAPI_STATE_SCHED
, &sd
->backlog
.state
)) {
4474 if (!rps_ipi_queued(sd
))
4475 ____napi_schedule(sd
, &sd
->backlog
);
4484 local_irq_restore(flags
);
4486 atomic_long_inc(&skb
->dev
->rx_dropped
);
4491 static struct netdev_rx_queue
*netif_get_rxqueue(struct sk_buff
*skb
)
4493 struct net_device
*dev
= skb
->dev
;
4494 struct netdev_rx_queue
*rxqueue
;
4498 if (skb_rx_queue_recorded(skb
)) {
4499 u16 index
= skb_get_rx_queue(skb
);
4501 if (unlikely(index
>= dev
->real_num_rx_queues
)) {
4502 WARN_ONCE(dev
->real_num_rx_queues
> 1,
4503 "%s received packet on queue %u, but number "
4504 "of RX queues is %u\n",
4505 dev
->name
, index
, dev
->real_num_rx_queues
);
4507 return rxqueue
; /* Return first rxqueue */
4514 static u32
netif_receive_generic_xdp(struct sk_buff
*skb
,
4515 struct xdp_buff
*xdp
,
4516 struct bpf_prog
*xdp_prog
)
4518 struct netdev_rx_queue
*rxqueue
;
4519 void *orig_data
, *orig_data_end
;
4520 u32 metalen
, act
= XDP_DROP
;
4521 __be16 orig_eth_type
;
4527 /* Reinjected packets coming from act_mirred or similar should
4528 * not get XDP generic processing.
4530 if (skb_cloned(skb
) || skb_is_tc_redirected(skb
))
4533 /* XDP packets must be linear and must have sufficient headroom
4534 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4535 * native XDP provides, thus we need to do it here as well.
4537 if (skb_is_nonlinear(skb
) ||
4538 skb_headroom(skb
) < XDP_PACKET_HEADROOM
) {
4539 int hroom
= XDP_PACKET_HEADROOM
- skb_headroom(skb
);
4540 int troom
= skb
->tail
+ skb
->data_len
- skb
->end
;
4542 /* In case we have to go down the path and also linearize,
4543 * then lets do the pskb_expand_head() work just once here.
4545 if (pskb_expand_head(skb
,
4546 hroom
> 0 ? ALIGN(hroom
, NET_SKB_PAD
) : 0,
4547 troom
> 0 ? troom
+ 128 : 0, GFP_ATOMIC
))
4549 if (skb_linearize(skb
))
4553 /* The XDP program wants to see the packet starting at the MAC
4556 mac_len
= skb
->data
- skb_mac_header(skb
);
4557 hlen
= skb_headlen(skb
) + mac_len
;
4558 xdp
->data
= skb
->data
- mac_len
;
4559 xdp
->data_meta
= xdp
->data
;
4560 xdp
->data_end
= xdp
->data
+ hlen
;
4561 xdp
->data_hard_start
= skb
->data
- skb_headroom(skb
);
4562 orig_data_end
= xdp
->data_end
;
4563 orig_data
= xdp
->data
;
4564 eth
= (struct ethhdr
*)xdp
->data
;
4565 orig_bcast
= is_multicast_ether_addr_64bits(eth
->h_dest
);
4566 orig_eth_type
= eth
->h_proto
;
4568 rxqueue
= netif_get_rxqueue(skb
);
4569 xdp
->rxq
= &rxqueue
->xdp_rxq
;
4571 act
= bpf_prog_run_xdp(xdp_prog
, xdp
);
4573 /* check if bpf_xdp_adjust_head was used */
4574 off
= xdp
->data
- orig_data
;
4577 __skb_pull(skb
, off
);
4579 __skb_push(skb
, -off
);
4581 skb
->mac_header
+= off
;
4582 skb_reset_network_header(skb
);
4585 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4588 off
= orig_data_end
- xdp
->data_end
;
4590 skb_set_tail_pointer(skb
, xdp
->data_end
- xdp
->data
);
4595 /* check if XDP changed eth hdr such SKB needs update */
4596 eth
= (struct ethhdr
*)xdp
->data
;
4597 if ((orig_eth_type
!= eth
->h_proto
) ||
4598 (orig_bcast
!= is_multicast_ether_addr_64bits(eth
->h_dest
))) {
4599 __skb_push(skb
, ETH_HLEN
);
4600 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
4606 __skb_push(skb
, mac_len
);
4609 metalen
= xdp
->data
- xdp
->data_meta
;
4611 skb_metadata_set(skb
, metalen
);
4614 bpf_warn_invalid_xdp_action(act
);
4617 trace_xdp_exception(skb
->dev
, xdp_prog
, act
);
4628 /* When doing generic XDP we have to bypass the qdisc layer and the
4629 * network taps in order to match in-driver-XDP behavior.
4631 void generic_xdp_tx(struct sk_buff
*skb
, struct bpf_prog
*xdp_prog
)
4633 struct net_device
*dev
= skb
->dev
;
4634 struct netdev_queue
*txq
;
4635 bool free_skb
= true;
4638 txq
= netdev_core_pick_tx(dev
, skb
, NULL
);
4639 cpu
= smp_processor_id();
4640 HARD_TX_LOCK(dev
, txq
, cpu
);
4641 if (!netif_xmit_stopped(txq
)) {
4642 rc
= netdev_start_xmit(skb
, dev
, txq
, 0);
4643 if (dev_xmit_complete(rc
))
4646 HARD_TX_UNLOCK(dev
, txq
);
4648 trace_xdp_exception(dev
, xdp_prog
, XDP_TX
);
4652 EXPORT_SYMBOL_GPL(generic_xdp_tx
);
4654 static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key
);
4656 int do_xdp_generic(struct bpf_prog
*xdp_prog
, struct sk_buff
*skb
)
4659 struct xdp_buff xdp
;
4663 act
= netif_receive_generic_xdp(skb
, &xdp
, xdp_prog
);
4664 if (act
!= XDP_PASS
) {
4667 err
= xdp_do_generic_redirect(skb
->dev
, skb
,
4673 generic_xdp_tx(skb
, xdp_prog
);
4684 EXPORT_SYMBOL_GPL(do_xdp_generic
);
4686 static int netif_rx_internal(struct sk_buff
*skb
)
4690 net_timestamp_check(netdev_tstamp_prequeue
, skb
);
4692 trace_netif_rx(skb
);
4695 if (static_branch_unlikely(&rps_needed
)) {
4696 struct rps_dev_flow voidflow
, *rflow
= &voidflow
;
4702 cpu
= get_rps_cpu(skb
->dev
, skb
, &rflow
);
4704 cpu
= smp_processor_id();
4706 ret
= enqueue_to_backlog(skb
, cpu
, &rflow
->last_qtail
);
4715 ret
= enqueue_to_backlog(skb
, get_cpu(), &qtail
);
4722 * netif_rx - post buffer to the network code
4723 * @skb: buffer to post
4725 * This function receives a packet from a device driver and queues it for
4726 * the upper (protocol) levels to process. It always succeeds. The buffer
4727 * may be dropped during processing for congestion control or by the
4731 * NET_RX_SUCCESS (no congestion)
4732 * NET_RX_DROP (packet was dropped)
4736 int netif_rx(struct sk_buff
*skb
)
4740 trace_netif_rx_entry(skb
);
4742 ret
= netif_rx_internal(skb
);
4743 trace_netif_rx_exit(ret
);
4747 EXPORT_SYMBOL(netif_rx
);
4749 int netif_rx_ni(struct sk_buff
*skb
)
4753 trace_netif_rx_ni_entry(skb
);
4756 err
= netif_rx_internal(skb
);
4757 if (local_softirq_pending())
4760 trace_netif_rx_ni_exit(err
);
4764 EXPORT_SYMBOL(netif_rx_ni
);
4766 static __latent_entropy
void net_tx_action(struct softirq_action
*h
)
4768 struct softnet_data
*sd
= this_cpu_ptr(&softnet_data
);
4770 if (sd
->completion_queue
) {
4771 struct sk_buff
*clist
;
4773 local_irq_disable();
4774 clist
= sd
->completion_queue
;
4775 sd
->completion_queue
= NULL
;
4779 struct sk_buff
*skb
= clist
;
4781 clist
= clist
->next
;
4783 WARN_ON(refcount_read(&skb
->users
));
4784 if (likely(get_kfree_skb_cb(skb
)->reason
== SKB_REASON_CONSUMED
))
4785 trace_consume_skb(skb
);
4787 trace_kfree_skb(skb
, net_tx_action
);
4789 if (skb
->fclone
!= SKB_FCLONE_UNAVAILABLE
)
4792 __kfree_skb_defer(skb
);
4795 __kfree_skb_flush();
4798 if (sd
->output_queue
) {
4801 local_irq_disable();
4802 head
= sd
->output_queue
;
4803 sd
->output_queue
= NULL
;
4804 sd
->output_queue_tailp
= &sd
->output_queue
;
4808 struct Qdisc
*q
= head
;
4809 spinlock_t
*root_lock
= NULL
;
4811 head
= head
->next_sched
;
4813 if (!(q
->flags
& TCQ_F_NOLOCK
)) {
4814 root_lock
= qdisc_lock(q
);
4815 spin_lock(root_lock
);
4817 /* We need to make sure head->next_sched is read
4818 * before clearing __QDISC_STATE_SCHED
4820 smp_mb__before_atomic();
4821 clear_bit(__QDISC_STATE_SCHED
, &q
->state
);
4824 spin_unlock(root_lock
);
4828 xfrm_dev_backlog(sd
);
4831 #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
4832 /* This hook is defined here for ATM LANE */
4833 int (*br_fdb_test_addr_hook
)(struct net_device
*dev
,
4834 unsigned char *addr
) __read_mostly
;
4835 EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook
);
4838 static inline struct sk_buff
*
4839 sch_handle_ingress(struct sk_buff
*skb
, struct packet_type
**pt_prev
, int *ret
,
4840 struct net_device
*orig_dev
)
4842 #ifdef CONFIG_NET_CLS_ACT
4843 struct mini_Qdisc
*miniq
= rcu_dereference_bh(skb
->dev
->miniq_ingress
);
4844 struct tcf_result cl_res
;
4846 /* If there's at least one ingress present somewhere (so
4847 * we get here via enabled static key), remaining devices
4848 * that are not configured with an ingress qdisc will bail
4855 *ret
= deliver_skb(skb
, *pt_prev
, orig_dev
);
4859 qdisc_skb_cb(skb
)->pkt_len
= skb
->len
;
4860 skb
->tc_at_ingress
= 1;
4861 mini_qdisc_bstats_cpu_update(miniq
, skb
);
4863 switch (tcf_classify(skb
, miniq
->filter_list
, &cl_res
, false)) {
4865 case TC_ACT_RECLASSIFY
:
4866 skb
->tc_index
= TC_H_MIN(cl_res
.classid
);
4869 mini_qdisc_qstats_cpu_drop(miniq
);
4877 case TC_ACT_REDIRECT
:
4878 /* skb_mac_header check was done by cls/act_bpf, so
4879 * we can safely push the L2 header back before
4880 * redirecting to another netdev
4882 __skb_push(skb
, skb
->mac_len
);
4883 skb_do_redirect(skb
);
4885 case TC_ACT_CONSUMED
:
4890 #endif /* CONFIG_NET_CLS_ACT */
4895 * netdev_is_rx_handler_busy - check if receive handler is registered
4896 * @dev: device to check
4898 * Check if a receive handler is already registered for a given device.
4899 * Return true if there one.
4901 * The caller must hold the rtnl_mutex.
4903 bool netdev_is_rx_handler_busy(struct net_device
*dev
)
4906 return dev
&& rtnl_dereference(dev
->rx_handler
);
4908 EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy
);
4911 * netdev_rx_handler_register - register receive handler
4912 * @dev: device to register a handler for
4913 * @rx_handler: receive handler to register
4914 * @rx_handler_data: data pointer that is used by rx handler
4916 * Register a receive handler for a device. This handler will then be
4917 * called from __netif_receive_skb. A negative errno code is returned
4920 * The caller must hold the rtnl_mutex.
4922 * For a general description of rx_handler, see enum rx_handler_result.
4924 int netdev_rx_handler_register(struct net_device
*dev
,
4925 rx_handler_func_t
*rx_handler
,
4926 void *rx_handler_data
)
4928 if (netdev_is_rx_handler_busy(dev
))
4931 if (dev
->priv_flags
& IFF_NO_RX_HANDLER
)
4934 /* Note: rx_handler_data must be set before rx_handler */
4935 rcu_assign_pointer(dev
->rx_handler_data
, rx_handler_data
);
4936 rcu_assign_pointer(dev
->rx_handler
, rx_handler
);
4940 EXPORT_SYMBOL_GPL(netdev_rx_handler_register
);
4943 * netdev_rx_handler_unregister - unregister receive handler
4944 * @dev: device to unregister a handler from
4946 * Unregister a receive handler from a device.
4948 * The caller must hold the rtnl_mutex.
4950 void netdev_rx_handler_unregister(struct net_device
*dev
)
4954 RCU_INIT_POINTER(dev
->rx_handler
, NULL
);
4955 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4956 * section has a guarantee to see a non NULL rx_handler_data
4960 RCU_INIT_POINTER(dev
->rx_handler_data
, NULL
);
4962 EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister
);
4965 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4966 * the special handling of PFMEMALLOC skbs.
4968 static bool skb_pfmemalloc_protocol(struct sk_buff
*skb
)
4970 switch (skb
->protocol
) {
4971 case htons(ETH_P_ARP
):
4972 case htons(ETH_P_IP
):
4973 case htons(ETH_P_IPV6
):
4974 case htons(ETH_P_8021Q
):
4975 case htons(ETH_P_8021AD
):
4982 static inline int nf_ingress(struct sk_buff
*skb
, struct packet_type
**pt_prev
,
4983 int *ret
, struct net_device
*orig_dev
)
4985 if (nf_hook_ingress_active(skb
)) {
4989 *ret
= deliver_skb(skb
, *pt_prev
, orig_dev
);
4994 ingress_retval
= nf_hook_ingress(skb
);
4996 return ingress_retval
;
5001 static int __netif_receive_skb_core(struct sk_buff
*skb
, bool pfmemalloc
,
5002 struct packet_type
**ppt_prev
)
5004 struct packet_type
*ptype
, *pt_prev
;
5005 rx_handler_func_t
*rx_handler
;
5006 struct net_device
*orig_dev
;
5007 bool deliver_exact
= false;
5008 int ret
= NET_RX_DROP
;
5011 net_timestamp_check(!netdev_tstamp_prequeue
, skb
);
5013 trace_netif_receive_skb(skb
);
5015 orig_dev
= skb
->dev
;
5017 skb_reset_network_header(skb
);
5018 if (!skb_transport_header_was_set(skb
))
5019 skb_reset_transport_header(skb
);
5020 skb_reset_mac_len(skb
);
5025 skb
->skb_iif
= skb
->dev
->ifindex
;
5027 __this_cpu_inc(softnet_data
.processed
);
5029 if (static_branch_unlikely(&generic_xdp_needed_key
)) {
5033 ret2
= do_xdp_generic(rcu_dereference(skb
->dev
->xdp_prog
), skb
);
5036 if (ret2
!= XDP_PASS
)
5038 skb_reset_mac_len(skb
);
5041 if (skb
->protocol
== cpu_to_be16(ETH_P_8021Q
) ||
5042 skb
->protocol
== cpu_to_be16(ETH_P_8021AD
)) {
5043 skb
= skb_vlan_untag(skb
);
5048 if (skb_skip_tc_classify(skb
))
5054 list_for_each_entry_rcu(ptype
, &ptype_all
, list
) {
5056 ret
= deliver_skb(skb
, pt_prev
, orig_dev
);
5060 list_for_each_entry_rcu(ptype
, &skb
->dev
->ptype_all
, list
) {
5062 ret
= deliver_skb(skb
, pt_prev
, orig_dev
);
5067 #ifdef CONFIG_NET_INGRESS
5068 if (static_branch_unlikely(&ingress_needed_key
)) {
5069 skb
= sch_handle_ingress(skb
, &pt_prev
, &ret
, orig_dev
);
5073 if (nf_ingress(skb
, &pt_prev
, &ret
, orig_dev
) < 0)
5079 if (pfmemalloc
&& !skb_pfmemalloc_protocol(skb
))
5082 if (skb_vlan_tag_present(skb
)) {
5084 ret
= deliver_skb(skb
, pt_prev
, orig_dev
);
5087 if (vlan_do_receive(&skb
))
5089 else if (unlikely(!skb
))
5093 rx_handler
= rcu_dereference(skb
->dev
->rx_handler
);
5096 ret
= deliver_skb(skb
, pt_prev
, orig_dev
);
5099 switch (rx_handler(&skb
)) {
5100 case RX_HANDLER_CONSUMED
:
5101 ret
= NET_RX_SUCCESS
;
5103 case RX_HANDLER_ANOTHER
:
5105 case RX_HANDLER_EXACT
:
5106 deliver_exact
= true;
5107 case RX_HANDLER_PASS
:
5114 if (unlikely(skb_vlan_tag_present(skb
))) {
5116 if (skb_vlan_tag_get_id(skb
)) {
5117 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5120 skb
->pkt_type
= PACKET_OTHERHOST
;
5121 } else if (skb
->protocol
== cpu_to_be16(ETH_P_8021Q
) ||
5122 skb
->protocol
== cpu_to_be16(ETH_P_8021AD
)) {
5123 /* Outer header is 802.1P with vlan 0, inner header is
5124 * 802.1Q or 802.1AD and vlan_do_receive() above could
5125 * not find vlan dev for vlan id 0.
5127 __vlan_hwaccel_clear_tag(skb
);
5128 skb
= skb_vlan_untag(skb
);
5131 if (vlan_do_receive(&skb
))
5132 /* After stripping off 802.1P header with vlan 0
5133 * vlan dev is found for inner header.
5136 else if (unlikely(!skb
))
5139 /* We have stripped outer 802.1P vlan 0 header.
5140 * But could not find vlan dev.
5141 * check again for vlan id to set OTHERHOST.
5145 /* Note: we might in the future use prio bits
5146 * and set skb->priority like in vlan_do_receive()
5147 * For the time being, just ignore Priority Code Point
5149 __vlan_hwaccel_clear_tag(skb
);
5152 type
= skb
->protocol
;
5154 /* deliver only exact match when indicated */
5155 if (likely(!deliver_exact
)) {
5156 deliver_ptype_list_skb(skb
, &pt_prev
, orig_dev
, type
,
5157 &ptype_base
[ntohs(type
) &
5161 deliver_ptype_list_skb(skb
, &pt_prev
, orig_dev
, type
,
5162 &orig_dev
->ptype_specific
);
5164 if (unlikely(skb
->dev
!= orig_dev
)) {
5165 deliver_ptype_list_skb(skb
, &pt_prev
, orig_dev
, type
,
5166 &skb
->dev
->ptype_specific
);
5170 if (unlikely(skb_orphan_frags_rx(skb
, GFP_ATOMIC
)))
5172 *ppt_prev
= pt_prev
;
5176 atomic_long_inc(&skb
->dev
->rx_dropped
);
5178 atomic_long_inc(&skb
->dev
->rx_nohandler
);
5180 /* Jamal, now you will not able to escape explaining
5181 * me how you were going to use this. :-)
5190 static int __netif_receive_skb_one_core(struct sk_buff
*skb
, bool pfmemalloc
)
5192 struct net_device
*orig_dev
= skb
->dev
;
5193 struct packet_type
*pt_prev
= NULL
;
5196 ret
= __netif_receive_skb_core(skb
, pfmemalloc
, &pt_prev
);
5198 ret
= INDIRECT_CALL_INET(pt_prev
->func
, ipv6_rcv
, ip_rcv
, skb
,
5199 skb
->dev
, pt_prev
, orig_dev
);
5204 * netif_receive_skb_core - special purpose version of netif_receive_skb
5205 * @skb: buffer to process
5207 * More direct receive version of netif_receive_skb(). It should
5208 * only be used by callers that have a need to skip RPS and Generic XDP.
5209 * Caller must also take care of handling if (page_is_)pfmemalloc.
5211 * This function may only be called from softirq context and interrupts
5212 * should be enabled.
5214 * Return values (usually ignored):
5215 * NET_RX_SUCCESS: no congestion
5216 * NET_RX_DROP: packet was dropped
5218 int netif_receive_skb_core(struct sk_buff
*skb
)
5223 ret
= __netif_receive_skb_one_core(skb
, false);
5228 EXPORT_SYMBOL(netif_receive_skb_core
);
5230 static inline void __netif_receive_skb_list_ptype(struct list_head
*head
,
5231 struct packet_type
*pt_prev
,
5232 struct net_device
*orig_dev
)
5234 struct sk_buff
*skb
, *next
;
5238 if (list_empty(head
))
5240 if (pt_prev
->list_func
!= NULL
)
5241 INDIRECT_CALL_INET(pt_prev
->list_func
, ipv6_list_rcv
,
5242 ip_list_rcv
, head
, pt_prev
, orig_dev
);
5244 list_for_each_entry_safe(skb
, next
, head
, list
) {
5245 skb_list_del_init(skb
);
5246 pt_prev
->func(skb
, skb
->dev
, pt_prev
, orig_dev
);
5250 static void __netif_receive_skb_list_core(struct list_head
*head
, bool pfmemalloc
)
5252 /* Fast-path assumptions:
5253 * - There is no RX handler.
5254 * - Only one packet_type matches.
5255 * If either of these fails, we will end up doing some per-packet
5256 * processing in-line, then handling the 'last ptype' for the whole
5257 * sublist. This can't cause out-of-order delivery to any single ptype,
5258 * because the 'last ptype' must be constant across the sublist, and all
5259 * other ptypes are handled per-packet.
5261 /* Current (common) ptype of sublist */
5262 struct packet_type
*pt_curr
= NULL
;
5263 /* Current (common) orig_dev of sublist */
5264 struct net_device
*od_curr
= NULL
;
5265 struct list_head sublist
;
5266 struct sk_buff
*skb
, *next
;
5268 INIT_LIST_HEAD(&sublist
);
5269 list_for_each_entry_safe(skb
, next
, head
, list
) {
5270 struct net_device
*orig_dev
= skb
->dev
;
5271 struct packet_type
*pt_prev
= NULL
;
5273 skb_list_del_init(skb
);
5274 __netif_receive_skb_core(skb
, pfmemalloc
, &pt_prev
);
5277 if (pt_curr
!= pt_prev
|| od_curr
!= orig_dev
) {
5278 /* dispatch old sublist */
5279 __netif_receive_skb_list_ptype(&sublist
, pt_curr
, od_curr
);
5280 /* start new sublist */
5281 INIT_LIST_HEAD(&sublist
);
5285 list_add_tail(&skb
->list
, &sublist
);
5288 /* dispatch final sublist */
5289 __netif_receive_skb_list_ptype(&sublist
, pt_curr
, od_curr
);
5292 static int __netif_receive_skb(struct sk_buff
*skb
)
5296 if (sk_memalloc_socks() && skb_pfmemalloc(skb
)) {
5297 unsigned int noreclaim_flag
;
5300 * PFMEMALLOC skbs are special, they should
5301 * - be delivered to SOCK_MEMALLOC sockets only
5302 * - stay away from userspace
5303 * - have bounded memory usage
5305 * Use PF_MEMALLOC as this saves us from propagating the allocation
5306 * context down to all allocation sites.
5308 noreclaim_flag
= memalloc_noreclaim_save();
5309 ret
= __netif_receive_skb_one_core(skb
, true);
5310 memalloc_noreclaim_restore(noreclaim_flag
);
5312 ret
= __netif_receive_skb_one_core(skb
, false);
5317 static void __netif_receive_skb_list(struct list_head
*head
)
5319 unsigned long noreclaim_flag
= 0;
5320 struct sk_buff
*skb
, *next
;
5321 bool pfmemalloc
= false; /* Is current sublist PF_MEMALLOC? */
5323 list_for_each_entry_safe(skb
, next
, head
, list
) {
5324 if ((sk_memalloc_socks() && skb_pfmemalloc(skb
)) != pfmemalloc
) {
5325 struct list_head sublist
;
5327 /* Handle the previous sublist */
5328 list_cut_before(&sublist
, head
, &skb
->list
);
5329 if (!list_empty(&sublist
))
5330 __netif_receive_skb_list_core(&sublist
, pfmemalloc
);
5331 pfmemalloc
= !pfmemalloc
;
5332 /* See comments in __netif_receive_skb */
5334 noreclaim_flag
= memalloc_noreclaim_save();
5336 memalloc_noreclaim_restore(noreclaim_flag
);
5339 /* Handle the remaining sublist */
5340 if (!list_empty(head
))
5341 __netif_receive_skb_list_core(head
, pfmemalloc
);
5342 /* Restore pflags */
5344 memalloc_noreclaim_restore(noreclaim_flag
);
5347 static int generic_xdp_install(struct net_device
*dev
, struct netdev_bpf
*xdp
)
5349 struct bpf_prog
*old
= rtnl_dereference(dev
->xdp_prog
);
5350 struct bpf_prog
*new = xdp
->prog
;
5353 switch (xdp
->command
) {
5354 case XDP_SETUP_PROG
:
5355 rcu_assign_pointer(dev
->xdp_prog
, new);
5360 static_branch_dec(&generic_xdp_needed_key
);
5361 } else if (new && !old
) {
5362 static_branch_inc(&generic_xdp_needed_key
);
5363 dev_disable_lro(dev
);
5364 dev_disable_gro_hw(dev
);
5368 case XDP_QUERY_PROG
:
5369 xdp
->prog_id
= old
? old
->aux
->id
: 0;
5380 static int netif_receive_skb_internal(struct sk_buff
*skb
)
5384 net_timestamp_check(netdev_tstamp_prequeue
, skb
);
5386 if (skb_defer_rx_timestamp(skb
))
5387 return NET_RX_SUCCESS
;
5391 if (static_branch_unlikely(&rps_needed
)) {
5392 struct rps_dev_flow voidflow
, *rflow
= &voidflow
;
5393 int cpu
= get_rps_cpu(skb
->dev
, skb
, &rflow
);
5396 ret
= enqueue_to_backlog(skb
, cpu
, &rflow
->last_qtail
);
5402 ret
= __netif_receive_skb(skb
);
5407 static void netif_receive_skb_list_internal(struct list_head
*head
)
5409 struct sk_buff
*skb
, *next
;
5410 struct list_head sublist
;
5412 INIT_LIST_HEAD(&sublist
);
5413 list_for_each_entry_safe(skb
, next
, head
, list
) {
5414 net_timestamp_check(netdev_tstamp_prequeue
, skb
);
5415 skb_list_del_init(skb
);
5416 if (!skb_defer_rx_timestamp(skb
))
5417 list_add_tail(&skb
->list
, &sublist
);
5419 list_splice_init(&sublist
, head
);
5423 if (static_branch_unlikely(&rps_needed
)) {
5424 list_for_each_entry_safe(skb
, next
, head
, list
) {
5425 struct rps_dev_flow voidflow
, *rflow
= &voidflow
;
5426 int cpu
= get_rps_cpu(skb
->dev
, skb
, &rflow
);
5429 /* Will be handled, remove from list */
5430 skb_list_del_init(skb
);
5431 enqueue_to_backlog(skb
, cpu
, &rflow
->last_qtail
);
5436 __netif_receive_skb_list(head
);
5441 * netif_receive_skb - process receive buffer from network
5442 * @skb: buffer to process
5444 * netif_receive_skb() is the main receive data processing function.
5445 * It always succeeds. The buffer may be dropped during processing
5446 * for congestion control or by the protocol layers.
5448 * This function may only be called from softirq context and interrupts
5449 * should be enabled.
5451 * Return values (usually ignored):
5452 * NET_RX_SUCCESS: no congestion
5453 * NET_RX_DROP: packet was dropped
5455 int netif_receive_skb(struct sk_buff
*skb
)
5459 trace_netif_receive_skb_entry(skb
);
5461 ret
= netif_receive_skb_internal(skb
);
5462 trace_netif_receive_skb_exit(ret
);
5466 EXPORT_SYMBOL(netif_receive_skb
);
5469 * netif_receive_skb_list - process many receive buffers from network
5470 * @head: list of skbs to process.
5472 * Since return value of netif_receive_skb() is normally ignored, and
5473 * wouldn't be meaningful for a list, this function returns void.
5475 * This function may only be called from softirq context and interrupts
5476 * should be enabled.
5478 void netif_receive_skb_list(struct list_head
*head
)
5480 struct sk_buff
*skb
;
5482 if (list_empty(head
))
5484 if (trace_netif_receive_skb_list_entry_enabled()) {
5485 list_for_each_entry(skb
, head
, list
)
5486 trace_netif_receive_skb_list_entry(skb
);
5488 netif_receive_skb_list_internal(head
);
5489 trace_netif_receive_skb_list_exit(0);
5491 EXPORT_SYMBOL(netif_receive_skb_list
);
5493 DEFINE_PER_CPU(struct work_struct
, flush_works
);
5495 /* Network device is going away, flush any packets still pending */
5496 static void flush_backlog(struct work_struct
*work
)
5498 struct sk_buff
*skb
, *tmp
;
5499 struct softnet_data
*sd
;
5502 sd
= this_cpu_ptr(&softnet_data
);
5504 local_irq_disable();
5506 skb_queue_walk_safe(&sd
->input_pkt_queue
, skb
, tmp
) {
5507 if (skb
->dev
->reg_state
== NETREG_UNREGISTERING
) {
5508 __skb_unlink(skb
, &sd
->input_pkt_queue
);
5510 input_queue_head_incr(sd
);
5516 skb_queue_walk_safe(&sd
->process_queue
, skb
, tmp
) {
5517 if (skb
->dev
->reg_state
== NETREG_UNREGISTERING
) {
5518 __skb_unlink(skb
, &sd
->process_queue
);
5520 input_queue_head_incr(sd
);
5526 static void flush_all_backlogs(void)
5532 for_each_online_cpu(cpu
)
5533 queue_work_on(cpu
, system_highpri_wq
,
5534 per_cpu_ptr(&flush_works
, cpu
));
5536 for_each_online_cpu(cpu
)
5537 flush_work(per_cpu_ptr(&flush_works
, cpu
));
5542 /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5543 static void gro_normal_list(struct napi_struct
*napi
)
5545 if (!napi
->rx_count
)
5547 netif_receive_skb_list_internal(&napi
->rx_list
);
5548 INIT_LIST_HEAD(&napi
->rx_list
);
5552 /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5553 * pass the whole batch up to the stack.
5555 static void gro_normal_one(struct napi_struct
*napi
, struct sk_buff
*skb
)
5557 list_add_tail(&skb
->list
, &napi
->rx_list
);
5558 if (++napi
->rx_count
>= gro_normal_batch
)
5559 gro_normal_list(napi
);
5562 INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff
*, int));
5563 INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff
*, int));
5564 static int napi_gro_complete(struct napi_struct
*napi
, struct sk_buff
*skb
)
5566 struct packet_offload
*ptype
;
5567 __be16 type
= skb
->protocol
;
5568 struct list_head
*head
= &offload_base
;
5571 BUILD_BUG_ON(sizeof(struct napi_gro_cb
) > sizeof(skb
->cb
));
5573 if (NAPI_GRO_CB(skb
)->count
== 1) {
5574 skb_shinfo(skb
)->gso_size
= 0;
5579 list_for_each_entry_rcu(ptype
, head
, list
) {
5580 if (ptype
->type
!= type
|| !ptype
->callbacks
.gro_complete
)
5583 err
= INDIRECT_CALL_INET(ptype
->callbacks
.gro_complete
,
5584 ipv6_gro_complete
, inet_gro_complete
,
5591 WARN_ON(&ptype
->list
== head
);
5593 return NET_RX_SUCCESS
;
5597 gro_normal_one(napi
, skb
);
5598 return NET_RX_SUCCESS
;
5601 static void __napi_gro_flush_chain(struct napi_struct
*napi
, u32 index
,
5604 struct list_head
*head
= &napi
->gro_hash
[index
].list
;
5605 struct sk_buff
*skb
, *p
;
5607 list_for_each_entry_safe_reverse(skb
, p
, head
, list
) {
5608 if (flush_old
&& NAPI_GRO_CB(skb
)->age
== jiffies
)
5610 skb_list_del_init(skb
);
5611 napi_gro_complete(napi
, skb
);
5612 napi
->gro_hash
[index
].count
--;
5615 if (!napi
->gro_hash
[index
].count
)
5616 __clear_bit(index
, &napi
->gro_bitmask
);
5619 /* napi->gro_hash[].list contains packets ordered by age.
5620 * youngest packets at the head of it.
5621 * Complete skbs in reverse order to reduce latencies.
5623 void napi_gro_flush(struct napi_struct
*napi
, bool flush_old
)
5625 unsigned long bitmask
= napi
->gro_bitmask
;
5626 unsigned int i
, base
= ~0U;
5628 while ((i
= ffs(bitmask
)) != 0) {
5631 __napi_gro_flush_chain(napi
, base
, flush_old
);
5634 EXPORT_SYMBOL(napi_gro_flush
);
5636 static struct list_head
*gro_list_prepare(struct napi_struct
*napi
,
5637 struct sk_buff
*skb
)
5639 unsigned int maclen
= skb
->dev
->hard_header_len
;
5640 u32 hash
= skb_get_hash_raw(skb
);
5641 struct list_head
*head
;
5644 head
= &napi
->gro_hash
[hash
& (GRO_HASH_BUCKETS
- 1)].list
;
5645 list_for_each_entry(p
, head
, list
) {
5646 unsigned long diffs
;
5648 NAPI_GRO_CB(p
)->flush
= 0;
5650 if (hash
!= skb_get_hash_raw(p
)) {
5651 NAPI_GRO_CB(p
)->same_flow
= 0;
5655 diffs
= (unsigned long)p
->dev
^ (unsigned long)skb
->dev
;
5656 diffs
|= skb_vlan_tag_present(p
) ^ skb_vlan_tag_present(skb
);
5657 if (skb_vlan_tag_present(p
))
5658 diffs
|= skb_vlan_tag_get(p
) ^ skb_vlan_tag_get(skb
);
5659 diffs
|= skb_metadata_dst_cmp(p
, skb
);
5660 diffs
|= skb_metadata_differs(p
, skb
);
5661 if (maclen
== ETH_HLEN
)
5662 diffs
|= compare_ether_header(skb_mac_header(p
),
5663 skb_mac_header(skb
));
5665 diffs
= memcmp(skb_mac_header(p
),
5666 skb_mac_header(skb
),
5668 NAPI_GRO_CB(p
)->same_flow
= !diffs
;
5674 static void skb_gro_reset_offset(struct sk_buff
*skb
)
5676 const struct skb_shared_info
*pinfo
= skb_shinfo(skb
);
5677 const skb_frag_t
*frag0
= &pinfo
->frags
[0];
5679 NAPI_GRO_CB(skb
)->data_offset
= 0;
5680 NAPI_GRO_CB(skb
)->frag0
= NULL
;
5681 NAPI_GRO_CB(skb
)->frag0_len
= 0;
5683 if (!skb_headlen(skb
) && pinfo
->nr_frags
&&
5684 !PageHighMem(skb_frag_page(frag0
))) {
5685 NAPI_GRO_CB(skb
)->frag0
= skb_frag_address(frag0
);
5686 NAPI_GRO_CB(skb
)->frag0_len
= min_t(unsigned int,
5687 skb_frag_size(frag0
),
5688 skb
->end
- skb
->tail
);
5692 static void gro_pull_from_frag0(struct sk_buff
*skb
, int grow
)
5694 struct skb_shared_info
*pinfo
= skb_shinfo(skb
);
5696 BUG_ON(skb
->end
- skb
->tail
< grow
);
5698 memcpy(skb_tail_pointer(skb
), NAPI_GRO_CB(skb
)->frag0
, grow
);
5700 skb
->data_len
-= grow
;
5703 skb_frag_off_add(&pinfo
->frags
[0], grow
);
5704 skb_frag_size_sub(&pinfo
->frags
[0], grow
);
5706 if (unlikely(!skb_frag_size(&pinfo
->frags
[0]))) {
5707 skb_frag_unref(skb
, 0);
5708 memmove(pinfo
->frags
, pinfo
->frags
+ 1,
5709 --pinfo
->nr_frags
* sizeof(pinfo
->frags
[0]));
5713 static void gro_flush_oldest(struct napi_struct
*napi
, struct list_head
*head
)
5715 struct sk_buff
*oldest
;
5717 oldest
= list_last_entry(head
, struct sk_buff
, list
);
5719 /* We are called with head length >= MAX_GRO_SKBS, so this is
5722 if (WARN_ON_ONCE(!oldest
))
5725 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5728 skb_list_del_init(oldest
);
5729 napi_gro_complete(napi
, oldest
);
5732 INDIRECT_CALLABLE_DECLARE(struct sk_buff
*inet_gro_receive(struct list_head
*,
5734 INDIRECT_CALLABLE_DECLARE(struct sk_buff
*ipv6_gro_receive(struct list_head
*,
5736 static enum gro_result
dev_gro_receive(struct napi_struct
*napi
, struct sk_buff
*skb
)
5738 u32 hash
= skb_get_hash_raw(skb
) & (GRO_HASH_BUCKETS
- 1);
5739 struct list_head
*head
= &offload_base
;
5740 struct packet_offload
*ptype
;
5741 __be16 type
= skb
->protocol
;
5742 struct list_head
*gro_head
;
5743 struct sk_buff
*pp
= NULL
;
5744 enum gro_result ret
;
5748 if (netif_elide_gro(skb
->dev
))
5751 gro_head
= gro_list_prepare(napi
, skb
);
5754 list_for_each_entry_rcu(ptype
, head
, list
) {
5755 if (ptype
->type
!= type
|| !ptype
->callbacks
.gro_receive
)
5758 skb_set_network_header(skb
, skb_gro_offset(skb
));
5759 skb_reset_mac_len(skb
);
5760 NAPI_GRO_CB(skb
)->same_flow
= 0;
5761 NAPI_GRO_CB(skb
)->flush
= skb_is_gso(skb
) || skb_has_frag_list(skb
);
5762 NAPI_GRO_CB(skb
)->free
= 0;
5763 NAPI_GRO_CB(skb
)->encap_mark
= 0;
5764 NAPI_GRO_CB(skb
)->recursion_counter
= 0;
5765 NAPI_GRO_CB(skb
)->is_fou
= 0;
5766 NAPI_GRO_CB(skb
)->is_atomic
= 1;
5767 NAPI_GRO_CB(skb
)->gro_remcsum_start
= 0;
5769 /* Setup for GRO checksum validation */
5770 switch (skb
->ip_summed
) {
5771 case CHECKSUM_COMPLETE
:
5772 NAPI_GRO_CB(skb
)->csum
= skb
->csum
;
5773 NAPI_GRO_CB(skb
)->csum_valid
= 1;
5774 NAPI_GRO_CB(skb
)->csum_cnt
= 0;
5776 case CHECKSUM_UNNECESSARY
:
5777 NAPI_GRO_CB(skb
)->csum_cnt
= skb
->csum_level
+ 1;
5778 NAPI_GRO_CB(skb
)->csum_valid
= 0;
5781 NAPI_GRO_CB(skb
)->csum_cnt
= 0;
5782 NAPI_GRO_CB(skb
)->csum_valid
= 0;
5785 pp
= INDIRECT_CALL_INET(ptype
->callbacks
.gro_receive
,
5786 ipv6_gro_receive
, inet_gro_receive
,
5792 if (&ptype
->list
== head
)
5795 if (PTR_ERR(pp
) == -EINPROGRESS
) {
5800 same_flow
= NAPI_GRO_CB(skb
)->same_flow
;
5801 ret
= NAPI_GRO_CB(skb
)->free
? GRO_MERGED_FREE
: GRO_MERGED
;
5804 skb_list_del_init(pp
);
5805 napi_gro_complete(napi
, pp
);
5806 napi
->gro_hash
[hash
].count
--;
5812 if (NAPI_GRO_CB(skb
)->flush
)
5815 if (unlikely(napi
->gro_hash
[hash
].count
>= MAX_GRO_SKBS
)) {
5816 gro_flush_oldest(napi
, gro_head
);
5818 napi
->gro_hash
[hash
].count
++;
5820 NAPI_GRO_CB(skb
)->count
= 1;
5821 NAPI_GRO_CB(skb
)->age
= jiffies
;
5822 NAPI_GRO_CB(skb
)->last
= skb
;
5823 skb_shinfo(skb
)->gso_size
= skb_gro_len(skb
);
5824 list_add(&skb
->list
, gro_head
);
5828 grow
= skb_gro_offset(skb
) - skb_headlen(skb
);
5830 gro_pull_from_frag0(skb
, grow
);
5832 if (napi
->gro_hash
[hash
].count
) {
5833 if (!test_bit(hash
, &napi
->gro_bitmask
))
5834 __set_bit(hash
, &napi
->gro_bitmask
);
5835 } else if (test_bit(hash
, &napi
->gro_bitmask
)) {
5836 __clear_bit(hash
, &napi
->gro_bitmask
);
5846 struct packet_offload
*gro_find_receive_by_type(__be16 type
)
5848 struct list_head
*offload_head
= &offload_base
;
5849 struct packet_offload
*ptype
;
5851 list_for_each_entry_rcu(ptype
, offload_head
, list
) {
5852 if (ptype
->type
!= type
|| !ptype
->callbacks
.gro_receive
)
5858 EXPORT_SYMBOL(gro_find_receive_by_type
);
5860 struct packet_offload
*gro_find_complete_by_type(__be16 type
)
5862 struct list_head
*offload_head
= &offload_base
;
5863 struct packet_offload
*ptype
;
5865 list_for_each_entry_rcu(ptype
, offload_head
, list
) {
5866 if (ptype
->type
!= type
|| !ptype
->callbacks
.gro_complete
)
5872 EXPORT_SYMBOL(gro_find_complete_by_type
);
5874 static void napi_skb_free_stolen_head(struct sk_buff
*skb
)
5878 kmem_cache_free(skbuff_head_cache
, skb
);
5881 static gro_result_t
napi_skb_finish(struct napi_struct
*napi
,
5882 struct sk_buff
*skb
,
5887 gro_normal_one(napi
, skb
);
5894 case GRO_MERGED_FREE
:
5895 if (NAPI_GRO_CB(skb
)->free
== NAPI_GRO_FREE_STOLEN_HEAD
)
5896 napi_skb_free_stolen_head(skb
);
5910 gro_result_t
napi_gro_receive(struct napi_struct
*napi
, struct sk_buff
*skb
)
5914 skb_mark_napi_id(skb
, napi
);
5915 trace_napi_gro_receive_entry(skb
);
5917 skb_gro_reset_offset(skb
);
5919 ret
= napi_skb_finish(napi
, skb
, dev_gro_receive(napi
, skb
));
5920 trace_napi_gro_receive_exit(ret
);
5924 EXPORT_SYMBOL(napi_gro_receive
);
5926 static void napi_reuse_skb(struct napi_struct
*napi
, struct sk_buff
*skb
)
5928 if (unlikely(skb
->pfmemalloc
)) {
5932 __skb_pull(skb
, skb_headlen(skb
));
5933 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5934 skb_reserve(skb
, NET_SKB_PAD
+ NET_IP_ALIGN
- skb_headroom(skb
));
5935 __vlan_hwaccel_clear_tag(skb
);
5936 skb
->dev
= napi
->dev
;
5939 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5940 skb
->pkt_type
= PACKET_HOST
;
5942 skb
->encapsulation
= 0;
5943 skb_shinfo(skb
)->gso_type
= 0;
5944 skb
->truesize
= SKB_TRUESIZE(skb_end_offset(skb
));
5950 struct sk_buff
*napi_get_frags(struct napi_struct
*napi
)
5952 struct sk_buff
*skb
= napi
->skb
;
5955 skb
= napi_alloc_skb(napi
, GRO_MAX_HEAD
);
5958 skb_mark_napi_id(skb
, napi
);
5963 EXPORT_SYMBOL(napi_get_frags
);
5965 static gro_result_t
napi_frags_finish(struct napi_struct
*napi
,
5966 struct sk_buff
*skb
,
5972 __skb_push(skb
, ETH_HLEN
);
5973 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
5974 if (ret
== GRO_NORMAL
)
5975 gro_normal_one(napi
, skb
);
5979 napi_reuse_skb(napi
, skb
);
5982 case GRO_MERGED_FREE
:
5983 if (NAPI_GRO_CB(skb
)->free
== NAPI_GRO_FREE_STOLEN_HEAD
)
5984 napi_skb_free_stolen_head(skb
);
5986 napi_reuse_skb(napi
, skb
);
5997 /* Upper GRO stack assumes network header starts at gro_offset=0
5998 * Drivers could call both napi_gro_frags() and napi_gro_receive()
5999 * We copy ethernet header into skb->data to have a common layout.
6001 static struct sk_buff
*napi_frags_skb(struct napi_struct
*napi
)
6003 struct sk_buff
*skb
= napi
->skb
;
6004 const struct ethhdr
*eth
;
6005 unsigned int hlen
= sizeof(*eth
);
6009 skb_reset_mac_header(skb
);
6010 skb_gro_reset_offset(skb
);
6012 if (unlikely(skb_gro_header_hard(skb
, hlen
))) {
6013 eth
= skb_gro_header_slow(skb
, hlen
, 0);
6014 if (unlikely(!eth
)) {
6015 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6016 __func__
, napi
->dev
->name
);
6017 napi_reuse_skb(napi
, skb
);
6021 eth
= (const struct ethhdr
*)skb
->data
;
6022 gro_pull_from_frag0(skb
, hlen
);
6023 NAPI_GRO_CB(skb
)->frag0
+= hlen
;
6024 NAPI_GRO_CB(skb
)->frag0_len
-= hlen
;
6026 __skb_pull(skb
, hlen
);
6029 * This works because the only protocols we care about don't require
6031 * We'll fix it up properly in napi_frags_finish()
6033 skb
->protocol
= eth
->h_proto
;
6038 gro_result_t
napi_gro_frags(struct napi_struct
*napi
)
6041 struct sk_buff
*skb
= napi_frags_skb(napi
);
6046 trace_napi_gro_frags_entry(skb
);
6048 ret
= napi_frags_finish(napi
, skb
, dev_gro_receive(napi
, skb
));
6049 trace_napi_gro_frags_exit(ret
);
6053 EXPORT_SYMBOL(napi_gro_frags
);
6055 /* Compute the checksum from gro_offset and return the folded value
6056 * after adding in any pseudo checksum.
6058 __sum16
__skb_gro_checksum_complete(struct sk_buff
*skb
)
6063 wsum
= skb_checksum(skb
, skb_gro_offset(skb
), skb_gro_len(skb
), 0);
6065 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6066 sum
= csum_fold(csum_add(NAPI_GRO_CB(skb
)->csum
, wsum
));
6067 /* See comments in __skb_checksum_complete(). */
6069 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
6070 !skb
->csum_complete_sw
)
6071 netdev_rx_csum_fault(skb
->dev
, skb
);
6074 NAPI_GRO_CB(skb
)->csum
= wsum
;
6075 NAPI_GRO_CB(skb
)->csum_valid
= 1;
6079 EXPORT_SYMBOL(__skb_gro_checksum_complete
);
6081 static void net_rps_send_ipi(struct softnet_data
*remsd
)
6085 struct softnet_data
*next
= remsd
->rps_ipi_next
;
6087 if (cpu_online(remsd
->cpu
))
6088 smp_call_function_single_async(remsd
->cpu
, &remsd
->csd
);
6095 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
6096 * Note: called with local irq disabled, but exits with local irq enabled.
6098 static void net_rps_action_and_irq_enable(struct softnet_data
*sd
)
6101 struct softnet_data
*remsd
= sd
->rps_ipi_list
;
6104 sd
->rps_ipi_list
= NULL
;
6108 /* Send pending IPI's to kick RPS processing on remote cpus. */
6109 net_rps_send_ipi(remsd
);
6115 static bool sd_has_rps_ipi_waiting(struct softnet_data
*sd
)
6118 return sd
->rps_ipi_list
!= NULL
;
6124 static int process_backlog(struct napi_struct
*napi
, int quota
)
6126 struct softnet_data
*sd
= container_of(napi
, struct softnet_data
, backlog
);
6130 /* Check if we have pending ipi, its better to send them now,
6131 * not waiting net_rx_action() end.
6133 if (sd_has_rps_ipi_waiting(sd
)) {
6134 local_irq_disable();
6135 net_rps_action_and_irq_enable(sd
);
6138 napi
->weight
= dev_rx_weight
;
6140 struct sk_buff
*skb
;
6142 while ((skb
= __skb_dequeue(&sd
->process_queue
))) {
6144 __netif_receive_skb(skb
);
6146 input_queue_head_incr(sd
);
6147 if (++work
>= quota
)
6152 local_irq_disable();
6154 if (skb_queue_empty(&sd
->input_pkt_queue
)) {
6156 * Inline a custom version of __napi_complete().
6157 * only current cpu owns and manipulates this napi,
6158 * and NAPI_STATE_SCHED is the only possible flag set
6160 * We can use a plain write instead of clear_bit(),
6161 * and we dont need an smp_mb() memory barrier.
6166 skb_queue_splice_tail_init(&sd
->input_pkt_queue
,
6167 &sd
->process_queue
);
6177 * __napi_schedule - schedule for receive
6178 * @n: entry to schedule
6180 * The entry's receive function will be scheduled to run.
6181 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
6183 void __napi_schedule(struct napi_struct
*n
)
6185 unsigned long flags
;
6187 local_irq_save(flags
);
6188 ____napi_schedule(this_cpu_ptr(&softnet_data
), n
);
6189 local_irq_restore(flags
);
6191 EXPORT_SYMBOL(__napi_schedule
);
6194 * napi_schedule_prep - check if napi can be scheduled
6197 * Test if NAPI routine is already running, and if not mark
6198 * it as running. This is used as a condition variable
6199 * insure only one NAPI poll instance runs. We also make
6200 * sure there is no pending NAPI disable.
6202 bool napi_schedule_prep(struct napi_struct
*n
)
6204 unsigned long val
, new;
6207 val
= READ_ONCE(n
->state
);
6208 if (unlikely(val
& NAPIF_STATE_DISABLE
))
6210 new = val
| NAPIF_STATE_SCHED
;
6212 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6213 * This was suggested by Alexander Duyck, as compiler
6214 * emits better code than :
6215 * if (val & NAPIF_STATE_SCHED)
6216 * new |= NAPIF_STATE_MISSED;
6218 new |= (val
& NAPIF_STATE_SCHED
) / NAPIF_STATE_SCHED
*
6220 } while (cmpxchg(&n
->state
, val
, new) != val
);
6222 return !(val
& NAPIF_STATE_SCHED
);
6224 EXPORT_SYMBOL(napi_schedule_prep
);
6227 * __napi_schedule_irqoff - schedule for receive
6228 * @n: entry to schedule
6230 * Variant of __napi_schedule() assuming hard irqs are masked
6232 void __napi_schedule_irqoff(struct napi_struct
*n
)
6234 ____napi_schedule(this_cpu_ptr(&softnet_data
), n
);
6236 EXPORT_SYMBOL(__napi_schedule_irqoff
);
6238 bool napi_complete_done(struct napi_struct
*n
, int work_done
)
6240 unsigned long flags
, val
, new;
6243 * 1) Don't let napi dequeue from the cpu poll list
6244 * just in case its running on a different cpu.
6245 * 2) If we are busy polling, do nothing here, we have
6246 * the guarantee we will be called later.
6248 if (unlikely(n
->state
& (NAPIF_STATE_NPSVC
|
6249 NAPIF_STATE_IN_BUSY_POLL
)))
6252 if (n
->gro_bitmask
) {
6253 unsigned long timeout
= 0;
6256 timeout
= n
->dev
->gro_flush_timeout
;
6258 /* When the NAPI instance uses a timeout and keeps postponing
6259 * it, we need to bound somehow the time packets are kept in
6262 napi_gro_flush(n
, !!timeout
);
6264 hrtimer_start(&n
->timer
, ns_to_ktime(timeout
),
6265 HRTIMER_MODE_REL_PINNED
);
6270 if (unlikely(!list_empty(&n
->poll_list
))) {
6271 /* If n->poll_list is not empty, we need to mask irqs */
6272 local_irq_save(flags
);
6273 list_del_init(&n
->poll_list
);
6274 local_irq_restore(flags
);
6278 val
= READ_ONCE(n
->state
);
6280 WARN_ON_ONCE(!(val
& NAPIF_STATE_SCHED
));
6282 new = val
& ~(NAPIF_STATE_MISSED
| NAPIF_STATE_SCHED
);
6284 /* If STATE_MISSED was set, leave STATE_SCHED set,
6285 * because we will call napi->poll() one more time.
6286 * This C code was suggested by Alexander Duyck to help gcc.
6288 new |= (val
& NAPIF_STATE_MISSED
) / NAPIF_STATE_MISSED
*
6290 } while (cmpxchg(&n
->state
, val
, new) != val
);
6292 if (unlikely(val
& NAPIF_STATE_MISSED
)) {
6299 EXPORT_SYMBOL(napi_complete_done
);
6301 /* must be called under rcu_read_lock(), as we dont take a reference */
6302 static struct napi_struct
*napi_by_id(unsigned int napi_id
)
6304 unsigned int hash
= napi_id
% HASH_SIZE(napi_hash
);
6305 struct napi_struct
*napi
;
6307 hlist_for_each_entry_rcu(napi
, &napi_hash
[hash
], napi_hash_node
)
6308 if (napi
->napi_id
== napi_id
)
6314 #if defined(CONFIG_NET_RX_BUSY_POLL)
6316 #define BUSY_POLL_BUDGET 8
6318 static void busy_poll_stop(struct napi_struct
*napi
, void *have_poll_lock
)
6322 /* Busy polling means there is a high chance device driver hard irq
6323 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6324 * set in napi_schedule_prep().
6325 * Since we are about to call napi->poll() once more, we can safely
6326 * clear NAPI_STATE_MISSED.
6328 * Note: x86 could use a single "lock and ..." instruction
6329 * to perform these two clear_bit()
6331 clear_bit(NAPI_STATE_MISSED
, &napi
->state
);
6332 clear_bit(NAPI_STATE_IN_BUSY_POLL
, &napi
->state
);
6336 /* All we really want here is to re-enable device interrupts.
6337 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6339 rc
= napi
->poll(napi
, BUSY_POLL_BUDGET
);
6340 /* We can't gro_normal_list() here, because napi->poll() might have
6341 * rearmed the napi (napi_complete_done()) in which case it could
6342 * already be running on another CPU.
6344 trace_napi_poll(napi
, rc
, BUSY_POLL_BUDGET
);
6345 netpoll_poll_unlock(have_poll_lock
);
6346 if (rc
== BUSY_POLL_BUDGET
) {
6347 /* As the whole budget was spent, we still own the napi so can
6348 * safely handle the rx_list.
6350 gro_normal_list(napi
);
6351 __napi_schedule(napi
);
6356 void napi_busy_loop(unsigned int napi_id
,
6357 bool (*loop_end
)(void *, unsigned long),
6360 unsigned long start_time
= loop_end
? busy_loop_current_time() : 0;
6361 int (*napi_poll
)(struct napi_struct
*napi
, int budget
);
6362 void *have_poll_lock
= NULL
;
6363 struct napi_struct
*napi
;
6370 napi
= napi_by_id(napi_id
);
6380 unsigned long val
= READ_ONCE(napi
->state
);
6382 /* If multiple threads are competing for this napi,
6383 * we avoid dirtying napi->state as much as we can.
6385 if (val
& (NAPIF_STATE_DISABLE
| NAPIF_STATE_SCHED
|
6386 NAPIF_STATE_IN_BUSY_POLL
))
6388 if (cmpxchg(&napi
->state
, val
,
6389 val
| NAPIF_STATE_IN_BUSY_POLL
|
6390 NAPIF_STATE_SCHED
) != val
)
6392 have_poll_lock
= netpoll_poll_lock(napi
);
6393 napi_poll
= napi
->poll
;
6395 work
= napi_poll(napi
, BUSY_POLL_BUDGET
);
6396 trace_napi_poll(napi
, work
, BUSY_POLL_BUDGET
);
6397 gro_normal_list(napi
);
6400 __NET_ADD_STATS(dev_net(napi
->dev
),
6401 LINUX_MIB_BUSYPOLLRXPACKETS
, work
);
6404 if (!loop_end
|| loop_end(loop_end_arg
, start_time
))
6407 if (unlikely(need_resched())) {
6409 busy_poll_stop(napi
, have_poll_lock
);
6413 if (loop_end(loop_end_arg
, start_time
))
6420 busy_poll_stop(napi
, have_poll_lock
);
6425 EXPORT_SYMBOL(napi_busy_loop
);
6427 #endif /* CONFIG_NET_RX_BUSY_POLL */
6429 static void napi_hash_add(struct napi_struct
*napi
)
6431 if (test_bit(NAPI_STATE_NO_BUSY_POLL
, &napi
->state
) ||
6432 test_and_set_bit(NAPI_STATE_HASHED
, &napi
->state
))
6435 spin_lock(&napi_hash_lock
);
6437 /* 0..NR_CPUS range is reserved for sender_cpu use */
6439 if (unlikely(++napi_gen_id
< MIN_NAPI_ID
))
6440 napi_gen_id
= MIN_NAPI_ID
;
6441 } while (napi_by_id(napi_gen_id
));
6442 napi
->napi_id
= napi_gen_id
;
6444 hlist_add_head_rcu(&napi
->napi_hash_node
,
6445 &napi_hash
[napi
->napi_id
% HASH_SIZE(napi_hash
)]);
6447 spin_unlock(&napi_hash_lock
);
6450 /* Warning : caller is responsible to make sure rcu grace period
6451 * is respected before freeing memory containing @napi
6453 bool napi_hash_del(struct napi_struct
*napi
)
6455 bool rcu_sync_needed
= false;
6457 spin_lock(&napi_hash_lock
);
6459 if (test_and_clear_bit(NAPI_STATE_HASHED
, &napi
->state
)) {
6460 rcu_sync_needed
= true;
6461 hlist_del_rcu(&napi
->napi_hash_node
);
6463 spin_unlock(&napi_hash_lock
);
6464 return rcu_sync_needed
;
6466 EXPORT_SYMBOL_GPL(napi_hash_del
);
6468 static enum hrtimer_restart
napi_watchdog(struct hrtimer
*timer
)
6470 struct napi_struct
*napi
;
6472 napi
= container_of(timer
, struct napi_struct
, timer
);
6474 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6475 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6477 if (napi
->gro_bitmask
&& !napi_disable_pending(napi
) &&
6478 !test_and_set_bit(NAPI_STATE_SCHED
, &napi
->state
))
6479 __napi_schedule_irqoff(napi
);
6481 return HRTIMER_NORESTART
;
6484 static void init_gro_hash(struct napi_struct
*napi
)
6488 for (i
= 0; i
< GRO_HASH_BUCKETS
; i
++) {
6489 INIT_LIST_HEAD(&napi
->gro_hash
[i
].list
);
6490 napi
->gro_hash
[i
].count
= 0;
6492 napi
->gro_bitmask
= 0;
6495 void netif_napi_add(struct net_device
*dev
, struct napi_struct
*napi
,
6496 int (*poll
)(struct napi_struct
*, int), int weight
)
6498 INIT_LIST_HEAD(&napi
->poll_list
);
6499 hrtimer_init(&napi
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_PINNED
);
6500 napi
->timer
.function
= napi_watchdog
;
6501 init_gro_hash(napi
);
6503 INIT_LIST_HEAD(&napi
->rx_list
);
6506 if (weight
> NAPI_POLL_WEIGHT
)
6507 netdev_err_once(dev
, "%s() called with weight %d\n", __func__
,
6509 napi
->weight
= weight
;
6510 list_add(&napi
->dev_list
, &dev
->napi_list
);
6512 #ifdef CONFIG_NETPOLL
6513 napi
->poll_owner
= -1;
6515 set_bit(NAPI_STATE_SCHED
, &napi
->state
);
6516 napi_hash_add(napi
);
6518 EXPORT_SYMBOL(netif_napi_add
);
6520 void napi_disable(struct napi_struct
*n
)
6523 set_bit(NAPI_STATE_DISABLE
, &n
->state
);
6525 while (test_and_set_bit(NAPI_STATE_SCHED
, &n
->state
))
6527 while (test_and_set_bit(NAPI_STATE_NPSVC
, &n
->state
))
6530 hrtimer_cancel(&n
->timer
);
6532 clear_bit(NAPI_STATE_DISABLE
, &n
->state
);
6534 EXPORT_SYMBOL(napi_disable
);
6536 static void flush_gro_hash(struct napi_struct
*napi
)
6540 for (i
= 0; i
< GRO_HASH_BUCKETS
; i
++) {
6541 struct sk_buff
*skb
, *n
;
6543 list_for_each_entry_safe(skb
, n
, &napi
->gro_hash
[i
].list
, list
)
6545 napi
->gro_hash
[i
].count
= 0;
6549 /* Must be called in process context */
6550 void netif_napi_del(struct napi_struct
*napi
)
6553 if (napi_hash_del(napi
))
6555 list_del_init(&napi
->dev_list
);
6556 napi_free_frags(napi
);
6558 flush_gro_hash(napi
);
6559 napi
->gro_bitmask
= 0;
6561 EXPORT_SYMBOL(netif_napi_del
);
6563 static int napi_poll(struct napi_struct
*n
, struct list_head
*repoll
)
6568 list_del_init(&n
->poll_list
);
6570 have
= netpoll_poll_lock(n
);
6574 /* This NAPI_STATE_SCHED test is for avoiding a race
6575 * with netpoll's poll_napi(). Only the entity which
6576 * obtains the lock and sees NAPI_STATE_SCHED set will
6577 * actually make the ->poll() call. Therefore we avoid
6578 * accidentally calling ->poll() when NAPI is not scheduled.
6581 if (test_bit(NAPI_STATE_SCHED
, &n
->state
)) {
6582 work
= n
->poll(n
, weight
);
6583 trace_napi_poll(n
, work
, weight
);
6586 WARN_ON_ONCE(work
> weight
);
6588 if (likely(work
< weight
))
6591 /* Drivers must not modify the NAPI state if they
6592 * consume the entire weight. In such cases this code
6593 * still "owns" the NAPI instance and therefore can
6594 * move the instance around on the list at-will.
6596 if (unlikely(napi_disable_pending(n
))) {
6601 if (n
->gro_bitmask
) {
6602 /* flush too old packets
6603 * If HZ < 1000, flush all packets.
6605 napi_gro_flush(n
, HZ
>= 1000);
6610 /* Some drivers may have called napi_schedule
6611 * prior to exhausting their budget.
6613 if (unlikely(!list_empty(&n
->poll_list
))) {
6614 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6615 n
->dev
? n
->dev
->name
: "backlog");
6619 list_add_tail(&n
->poll_list
, repoll
);
6622 netpoll_poll_unlock(have
);
6627 static __latent_entropy
void net_rx_action(struct softirq_action
*h
)
6629 struct softnet_data
*sd
= this_cpu_ptr(&softnet_data
);
6630 unsigned long time_limit
= jiffies
+
6631 usecs_to_jiffies(netdev_budget_usecs
);
6632 int budget
= netdev_budget
;
6636 local_irq_disable();
6637 list_splice_init(&sd
->poll_list
, &list
);
6641 struct napi_struct
*n
;
6643 if (list_empty(&list
)) {
6644 if (!sd_has_rps_ipi_waiting(sd
) && list_empty(&repoll
))
6649 n
= list_first_entry(&list
, struct napi_struct
, poll_list
);
6650 budget
-= napi_poll(n
, &repoll
);
6652 /* If softirq window is exhausted then punt.
6653 * Allow this to run for 2 jiffies since which will allow
6654 * an average latency of 1.5/HZ.
6656 if (unlikely(budget
<= 0 ||
6657 time_after_eq(jiffies
, time_limit
))) {
6663 local_irq_disable();
6665 list_splice_tail_init(&sd
->poll_list
, &list
);
6666 list_splice_tail(&repoll
, &list
);
6667 list_splice(&list
, &sd
->poll_list
);
6668 if (!list_empty(&sd
->poll_list
))
6669 __raise_softirq_irqoff(NET_RX_SOFTIRQ
);
6671 net_rps_action_and_irq_enable(sd
);
6673 __kfree_skb_flush();
6676 struct netdev_adjacent
{
6677 struct net_device
*dev
;
6679 /* upper master flag, there can only be one master device per list */
6682 /* lookup ignore flag */
6685 /* counter for the number of times this device was added to us */
6688 /* private field for the users */
6691 struct list_head list
;
6692 struct rcu_head rcu
;
6695 static struct netdev_adjacent
*__netdev_find_adj(struct net_device
*adj_dev
,
6696 struct list_head
*adj_list
)
6698 struct netdev_adjacent
*adj
;
6700 list_for_each_entry(adj
, adj_list
, list
) {
6701 if (adj
->dev
== adj_dev
)
6707 static int ____netdev_has_upper_dev(struct net_device
*upper_dev
, void *data
)
6709 struct net_device
*dev
= data
;
6711 return upper_dev
== dev
;
6715 * netdev_has_upper_dev - Check if device is linked to an upper device
6717 * @upper_dev: upper device to check
6719 * Find out if a device is linked to specified upper device and return true
6720 * in case it is. Note that this checks only immediate upper device,
6721 * not through a complete stack of devices. The caller must hold the RTNL lock.
6723 bool netdev_has_upper_dev(struct net_device
*dev
,
6724 struct net_device
*upper_dev
)
6728 return netdev_walk_all_upper_dev_rcu(dev
, ____netdev_has_upper_dev
,
6731 EXPORT_SYMBOL(netdev_has_upper_dev
);
6734 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6736 * @upper_dev: upper device to check
6738 * Find out if a device is linked to specified upper device and return true
6739 * in case it is. Note that this checks the entire upper device chain.
6740 * The caller must hold rcu lock.
6743 bool netdev_has_upper_dev_all_rcu(struct net_device
*dev
,
6744 struct net_device
*upper_dev
)
6746 return !!netdev_walk_all_upper_dev_rcu(dev
, ____netdev_has_upper_dev
,
6749 EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu
);
6752 * netdev_has_any_upper_dev - Check if device is linked to some device
6755 * Find out if a device is linked to an upper device and return true in case
6756 * it is. The caller must hold the RTNL lock.
6758 bool netdev_has_any_upper_dev(struct net_device
*dev
)
6762 return !list_empty(&dev
->adj_list
.upper
);
6764 EXPORT_SYMBOL(netdev_has_any_upper_dev
);
6767 * netdev_master_upper_dev_get - Get master upper device
6770 * Find a master upper device and return pointer to it or NULL in case
6771 * it's not there. The caller must hold the RTNL lock.
6773 struct net_device
*netdev_master_upper_dev_get(struct net_device
*dev
)
6775 struct netdev_adjacent
*upper
;
6779 if (list_empty(&dev
->adj_list
.upper
))
6782 upper
= list_first_entry(&dev
->adj_list
.upper
,
6783 struct netdev_adjacent
, list
);
6784 if (likely(upper
->master
))
6788 EXPORT_SYMBOL(netdev_master_upper_dev_get
);
6790 static struct net_device
*__netdev_master_upper_dev_get(struct net_device
*dev
)
6792 struct netdev_adjacent
*upper
;
6796 if (list_empty(&dev
->adj_list
.upper
))
6799 upper
= list_first_entry(&dev
->adj_list
.upper
,
6800 struct netdev_adjacent
, list
);
6801 if (likely(upper
->master
) && !upper
->ignore
)
6807 * netdev_has_any_lower_dev - Check if device is linked to some device
6810 * Find out if a device is linked to a lower device and return true in case
6811 * it is. The caller must hold the RTNL lock.
6813 static bool netdev_has_any_lower_dev(struct net_device
*dev
)
6817 return !list_empty(&dev
->adj_list
.lower
);
6820 void *netdev_adjacent_get_private(struct list_head
*adj_list
)
6822 struct netdev_adjacent
*adj
;
6824 adj
= list_entry(adj_list
, struct netdev_adjacent
, list
);
6826 return adj
->private;
6828 EXPORT_SYMBOL(netdev_adjacent_get_private
);
6831 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6833 * @iter: list_head ** of the current position
6835 * Gets the next device from the dev's upper list, starting from iter
6836 * position. The caller must hold RCU read lock.
6838 struct net_device
*netdev_upper_get_next_dev_rcu(struct net_device
*dev
,
6839 struct list_head
**iter
)
6841 struct netdev_adjacent
*upper
;
6843 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6845 upper
= list_entry_rcu((*iter
)->next
, struct netdev_adjacent
, list
);
6847 if (&upper
->list
== &dev
->adj_list
.upper
)
6850 *iter
= &upper
->list
;
6854 EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu
);
6856 static struct net_device
*__netdev_next_upper_dev(struct net_device
*dev
,
6857 struct list_head
**iter
,
6860 struct netdev_adjacent
*upper
;
6862 upper
= list_entry((*iter
)->next
, struct netdev_adjacent
, list
);
6864 if (&upper
->list
== &dev
->adj_list
.upper
)
6867 *iter
= &upper
->list
;
6868 *ignore
= upper
->ignore
;
6873 static struct net_device
*netdev_next_upper_dev_rcu(struct net_device
*dev
,
6874 struct list_head
**iter
)
6876 struct netdev_adjacent
*upper
;
6878 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6880 upper
= list_entry_rcu((*iter
)->next
, struct netdev_adjacent
, list
);
6882 if (&upper
->list
== &dev
->adj_list
.upper
)
6885 *iter
= &upper
->list
;
6890 static int __netdev_walk_all_upper_dev(struct net_device
*dev
,
6891 int (*fn
)(struct net_device
*dev
,
6895 struct net_device
*udev
, *next
, *now
, *dev_stack
[MAX_NEST_DEV
+ 1];
6896 struct list_head
*niter
, *iter
, *iter_stack
[MAX_NEST_DEV
+ 1];
6901 iter
= &dev
->adj_list
.upper
;
6905 ret
= fn(now
, data
);
6912 udev
= __netdev_next_upper_dev(now
, &iter
, &ignore
);
6919 niter
= &udev
->adj_list
.upper
;
6920 dev_stack
[cur
] = now
;
6921 iter_stack
[cur
++] = iter
;
6928 next
= dev_stack
[--cur
];
6929 niter
= iter_stack
[cur
];
6939 int netdev_walk_all_upper_dev_rcu(struct net_device
*dev
,
6940 int (*fn
)(struct net_device
*dev
,
6944 struct net_device
*udev
, *next
, *now
, *dev_stack
[MAX_NEST_DEV
+ 1];
6945 struct list_head
*niter
, *iter
, *iter_stack
[MAX_NEST_DEV
+ 1];
6949 iter
= &dev
->adj_list
.upper
;
6953 ret
= fn(now
, data
);
6960 udev
= netdev_next_upper_dev_rcu(now
, &iter
);
6965 niter
= &udev
->adj_list
.upper
;
6966 dev_stack
[cur
] = now
;
6967 iter_stack
[cur
++] = iter
;
6974 next
= dev_stack
[--cur
];
6975 niter
= iter_stack
[cur
];
6984 EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu
);
6986 static bool __netdev_has_upper_dev(struct net_device
*dev
,
6987 struct net_device
*upper_dev
)
6991 return __netdev_walk_all_upper_dev(dev
, ____netdev_has_upper_dev
,
6996 * netdev_lower_get_next_private - Get the next ->private from the
6997 * lower neighbour list
6999 * @iter: list_head ** of the current position
7001 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7002 * list, starting from iter position. The caller must hold either hold the
7003 * RTNL lock or its own locking that guarantees that the neighbour lower
7004 * list will remain unchanged.
7006 void *netdev_lower_get_next_private(struct net_device
*dev
,
7007 struct list_head
**iter
)
7009 struct netdev_adjacent
*lower
;
7011 lower
= list_entry(*iter
, struct netdev_adjacent
, list
);
7013 if (&lower
->list
== &dev
->adj_list
.lower
)
7016 *iter
= lower
->list
.next
;
7018 return lower
->private;
7020 EXPORT_SYMBOL(netdev_lower_get_next_private
);
7023 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7024 * lower neighbour list, RCU
7027 * @iter: list_head ** of the current position
7029 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7030 * list, starting from iter position. The caller must hold RCU read lock.
7032 void *netdev_lower_get_next_private_rcu(struct net_device
*dev
,
7033 struct list_head
**iter
)
7035 struct netdev_adjacent
*lower
;
7037 WARN_ON_ONCE(!rcu_read_lock_held());
7039 lower
= list_entry_rcu((*iter
)->next
, struct netdev_adjacent
, list
);
7041 if (&lower
->list
== &dev
->adj_list
.lower
)
7044 *iter
= &lower
->list
;
7046 return lower
->private;
7048 EXPORT_SYMBOL(netdev_lower_get_next_private_rcu
);
7051 * netdev_lower_get_next - Get the next device from the lower neighbour
7054 * @iter: list_head ** of the current position
7056 * Gets the next netdev_adjacent from the dev's lower neighbour
7057 * list, starting from iter position. The caller must hold RTNL lock or
7058 * its own locking that guarantees that the neighbour lower
7059 * list will remain unchanged.
7061 void *netdev_lower_get_next(struct net_device
*dev
, struct list_head
**iter
)
7063 struct netdev_adjacent
*lower
;
7065 lower
= list_entry(*iter
, struct netdev_adjacent
, list
);
7067 if (&lower
->list
== &dev
->adj_list
.lower
)
7070 *iter
= lower
->list
.next
;
7074 EXPORT_SYMBOL(netdev_lower_get_next
);
7076 static struct net_device
*netdev_next_lower_dev(struct net_device
*dev
,
7077 struct list_head
**iter
)
7079 struct netdev_adjacent
*lower
;
7081 lower
= list_entry((*iter
)->next
, struct netdev_adjacent
, list
);
7083 if (&lower
->list
== &dev
->adj_list
.lower
)
7086 *iter
= &lower
->list
;
7091 static struct net_device
*__netdev_next_lower_dev(struct net_device
*dev
,
7092 struct list_head
**iter
,
7095 struct netdev_adjacent
*lower
;
7097 lower
= list_entry((*iter
)->next
, struct netdev_adjacent
, list
);
7099 if (&lower
->list
== &dev
->adj_list
.lower
)
7102 *iter
= &lower
->list
;
7103 *ignore
= lower
->ignore
;
7108 int netdev_walk_all_lower_dev(struct net_device
*dev
,
7109 int (*fn
)(struct net_device
*dev
,
7113 struct net_device
*ldev
, *next
, *now
, *dev_stack
[MAX_NEST_DEV
+ 1];
7114 struct list_head
*niter
, *iter
, *iter_stack
[MAX_NEST_DEV
+ 1];
7118 iter
= &dev
->adj_list
.lower
;
7122 ret
= fn(now
, data
);
7129 ldev
= netdev_next_lower_dev(now
, &iter
);
7134 niter
= &ldev
->adj_list
.lower
;
7135 dev_stack
[cur
] = now
;
7136 iter_stack
[cur
++] = iter
;
7143 next
= dev_stack
[--cur
];
7144 niter
= iter_stack
[cur
];
7153 EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev
);
7155 static int __netdev_walk_all_lower_dev(struct net_device
*dev
,
7156 int (*fn
)(struct net_device
*dev
,
7160 struct net_device
*ldev
, *next
, *now
, *dev_stack
[MAX_NEST_DEV
+ 1];
7161 struct list_head
*niter
, *iter
, *iter_stack
[MAX_NEST_DEV
+ 1];
7166 iter
= &dev
->adj_list
.lower
;
7170 ret
= fn(now
, data
);
7177 ldev
= __netdev_next_lower_dev(now
, &iter
, &ignore
);
7184 niter
= &ldev
->adj_list
.lower
;
7185 dev_stack
[cur
] = now
;
7186 iter_stack
[cur
++] = iter
;
7193 next
= dev_stack
[--cur
];
7194 niter
= iter_stack
[cur
];
7204 static struct net_device
*netdev_next_lower_dev_rcu(struct net_device
*dev
,
7205 struct list_head
**iter
)
7207 struct netdev_adjacent
*lower
;
7209 lower
= list_entry_rcu((*iter
)->next
, struct netdev_adjacent
, list
);
7210 if (&lower
->list
== &dev
->adj_list
.lower
)
7213 *iter
= &lower
->list
;
7218 static u8
__netdev_upper_depth(struct net_device
*dev
)
7220 struct net_device
*udev
;
7221 struct list_head
*iter
;
7225 for (iter
= &dev
->adj_list
.upper
,
7226 udev
= __netdev_next_upper_dev(dev
, &iter
, &ignore
);
7228 udev
= __netdev_next_upper_dev(dev
, &iter
, &ignore
)) {
7231 if (max_depth
< udev
->upper_level
)
7232 max_depth
= udev
->upper_level
;
7238 static u8
__netdev_lower_depth(struct net_device
*dev
)
7240 struct net_device
*ldev
;
7241 struct list_head
*iter
;
7245 for (iter
= &dev
->adj_list
.lower
,
7246 ldev
= __netdev_next_lower_dev(dev
, &iter
, &ignore
);
7248 ldev
= __netdev_next_lower_dev(dev
, &iter
, &ignore
)) {
7251 if (max_depth
< ldev
->lower_level
)
7252 max_depth
= ldev
->lower_level
;
7258 static int __netdev_update_upper_level(struct net_device
*dev
, void *data
)
7260 dev
->upper_level
= __netdev_upper_depth(dev
) + 1;
7264 static int __netdev_update_lower_level(struct net_device
*dev
, void *data
)
7266 dev
->lower_level
= __netdev_lower_depth(dev
) + 1;
7270 int netdev_walk_all_lower_dev_rcu(struct net_device
*dev
,
7271 int (*fn
)(struct net_device
*dev
,
7275 struct net_device
*ldev
, *next
, *now
, *dev_stack
[MAX_NEST_DEV
+ 1];
7276 struct list_head
*niter
, *iter
, *iter_stack
[MAX_NEST_DEV
+ 1];
7280 iter
= &dev
->adj_list
.lower
;
7284 ret
= fn(now
, data
);
7291 ldev
= netdev_next_lower_dev_rcu(now
, &iter
);
7296 niter
= &ldev
->adj_list
.lower
;
7297 dev_stack
[cur
] = now
;
7298 iter_stack
[cur
++] = iter
;
7305 next
= dev_stack
[--cur
];
7306 niter
= iter_stack
[cur
];
7315 EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu
);
7318 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7319 * lower neighbour list, RCU
7323 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7324 * list. The caller must hold RCU read lock.
7326 void *netdev_lower_get_first_private_rcu(struct net_device
*dev
)
7328 struct netdev_adjacent
*lower
;
7330 lower
= list_first_or_null_rcu(&dev
->adj_list
.lower
,
7331 struct netdev_adjacent
, list
);
7333 return lower
->private;
7336 EXPORT_SYMBOL(netdev_lower_get_first_private_rcu
);
7339 * netdev_master_upper_dev_get_rcu - Get master upper device
7342 * Find a master upper device and return pointer to it or NULL in case
7343 * it's not there. The caller must hold the RCU read lock.
7345 struct net_device
*netdev_master_upper_dev_get_rcu(struct net_device
*dev
)
7347 struct netdev_adjacent
*upper
;
7349 upper
= list_first_or_null_rcu(&dev
->adj_list
.upper
,
7350 struct netdev_adjacent
, list
);
7351 if (upper
&& likely(upper
->master
))
7355 EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu
);
7357 static int netdev_adjacent_sysfs_add(struct net_device
*dev
,
7358 struct net_device
*adj_dev
,
7359 struct list_head
*dev_list
)
7361 char linkname
[IFNAMSIZ
+7];
7363 sprintf(linkname
, dev_list
== &dev
->adj_list
.upper
?
7364 "upper_%s" : "lower_%s", adj_dev
->name
);
7365 return sysfs_create_link(&(dev
->dev
.kobj
), &(adj_dev
->dev
.kobj
),
7368 static void netdev_adjacent_sysfs_del(struct net_device
*dev
,
7370 struct list_head
*dev_list
)
7372 char linkname
[IFNAMSIZ
+7];
7374 sprintf(linkname
, dev_list
== &dev
->adj_list
.upper
?
7375 "upper_%s" : "lower_%s", name
);
7376 sysfs_remove_link(&(dev
->dev
.kobj
), linkname
);
7379 static inline bool netdev_adjacent_is_neigh_list(struct net_device
*dev
,
7380 struct net_device
*adj_dev
,
7381 struct list_head
*dev_list
)
7383 return (dev_list
== &dev
->adj_list
.upper
||
7384 dev_list
== &dev
->adj_list
.lower
) &&
7385 net_eq(dev_net(dev
), dev_net(adj_dev
));
7388 static int __netdev_adjacent_dev_insert(struct net_device
*dev
,
7389 struct net_device
*adj_dev
,
7390 struct list_head
*dev_list
,
7391 void *private, bool master
)
7393 struct netdev_adjacent
*adj
;
7396 adj
= __netdev_find_adj(adj_dev
, dev_list
);
7400 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7401 dev
->name
, adj_dev
->name
, adj
->ref_nr
);
7406 adj
= kmalloc(sizeof(*adj
), GFP_KERNEL
);
7411 adj
->master
= master
;
7413 adj
->private = private;
7414 adj
->ignore
= false;
7417 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7418 dev
->name
, adj_dev
->name
, adj
->ref_nr
, adj_dev
->name
);
7420 if (netdev_adjacent_is_neigh_list(dev
, adj_dev
, dev_list
)) {
7421 ret
= netdev_adjacent_sysfs_add(dev
, adj_dev
, dev_list
);
7426 /* Ensure that master link is always the first item in list. */
7428 ret
= sysfs_create_link(&(dev
->dev
.kobj
),
7429 &(adj_dev
->dev
.kobj
), "master");
7431 goto remove_symlinks
;
7433 list_add_rcu(&adj
->list
, dev_list
);
7435 list_add_tail_rcu(&adj
->list
, dev_list
);
7441 if (netdev_adjacent_is_neigh_list(dev
, adj_dev
, dev_list
))
7442 netdev_adjacent_sysfs_del(dev
, adj_dev
->name
, dev_list
);
7450 static void __netdev_adjacent_dev_remove(struct net_device
*dev
,
7451 struct net_device
*adj_dev
,
7453 struct list_head
*dev_list
)
7455 struct netdev_adjacent
*adj
;
7457 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7458 dev
->name
, adj_dev
->name
, ref_nr
);
7460 adj
= __netdev_find_adj(adj_dev
, dev_list
);
7463 pr_err("Adjacency does not exist for device %s from %s\n",
7464 dev
->name
, adj_dev
->name
);
7469 if (adj
->ref_nr
> ref_nr
) {
7470 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7471 dev
->name
, adj_dev
->name
, ref_nr
,
7472 adj
->ref_nr
- ref_nr
);
7473 adj
->ref_nr
-= ref_nr
;
7478 sysfs_remove_link(&(dev
->dev
.kobj
), "master");
7480 if (netdev_adjacent_is_neigh_list(dev
, adj_dev
, dev_list
))
7481 netdev_adjacent_sysfs_del(dev
, adj_dev
->name
, dev_list
);
7483 list_del_rcu(&adj
->list
);
7484 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
7485 adj_dev
->name
, dev
->name
, adj_dev
->name
);
7487 kfree_rcu(adj
, rcu
);
7490 static int __netdev_adjacent_dev_link_lists(struct net_device
*dev
,
7491 struct net_device
*upper_dev
,
7492 struct list_head
*up_list
,
7493 struct list_head
*down_list
,
7494 void *private, bool master
)
7498 ret
= __netdev_adjacent_dev_insert(dev
, upper_dev
, up_list
,
7503 ret
= __netdev_adjacent_dev_insert(upper_dev
, dev
, down_list
,
7506 __netdev_adjacent_dev_remove(dev
, upper_dev
, 1, up_list
);
7513 static void __netdev_adjacent_dev_unlink_lists(struct net_device
*dev
,
7514 struct net_device
*upper_dev
,
7516 struct list_head
*up_list
,
7517 struct list_head
*down_list
)
7519 __netdev_adjacent_dev_remove(dev
, upper_dev
, ref_nr
, up_list
);
7520 __netdev_adjacent_dev_remove(upper_dev
, dev
, ref_nr
, down_list
);
7523 static int __netdev_adjacent_dev_link_neighbour(struct net_device
*dev
,
7524 struct net_device
*upper_dev
,
7525 void *private, bool master
)
7527 return __netdev_adjacent_dev_link_lists(dev
, upper_dev
,
7528 &dev
->adj_list
.upper
,
7529 &upper_dev
->adj_list
.lower
,
7533 static void __netdev_adjacent_dev_unlink_neighbour(struct net_device
*dev
,
7534 struct net_device
*upper_dev
)
7536 __netdev_adjacent_dev_unlink_lists(dev
, upper_dev
, 1,
7537 &dev
->adj_list
.upper
,
7538 &upper_dev
->adj_list
.lower
);
7541 static int __netdev_upper_dev_link(struct net_device
*dev
,
7542 struct net_device
*upper_dev
, bool master
,
7543 void *upper_priv
, void *upper_info
,
7544 struct netlink_ext_ack
*extack
)
7546 struct netdev_notifier_changeupper_info changeupper_info
= {
7551 .upper_dev
= upper_dev
,
7554 .upper_info
= upper_info
,
7556 struct net_device
*master_dev
;
7561 if (dev
== upper_dev
)
7564 /* To prevent loops, check if dev is not upper device to upper_dev. */
7565 if (__netdev_has_upper_dev(upper_dev
, dev
))
7568 if ((dev
->lower_level
+ upper_dev
->upper_level
) > MAX_NEST_DEV
)
7572 if (__netdev_has_upper_dev(dev
, upper_dev
))
7575 master_dev
= __netdev_master_upper_dev_get(dev
);
7577 return master_dev
== upper_dev
? -EEXIST
: -EBUSY
;
7580 ret
= call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER
,
7581 &changeupper_info
.info
);
7582 ret
= notifier_to_errno(ret
);
7586 ret
= __netdev_adjacent_dev_link_neighbour(dev
, upper_dev
, upper_priv
,
7591 ret
= call_netdevice_notifiers_info(NETDEV_CHANGEUPPER
,
7592 &changeupper_info
.info
);
7593 ret
= notifier_to_errno(ret
);
7597 __netdev_update_upper_level(dev
, NULL
);
7598 __netdev_walk_all_lower_dev(dev
, __netdev_update_upper_level
, NULL
);
7600 __netdev_update_lower_level(upper_dev
, NULL
);
7601 __netdev_walk_all_upper_dev(upper_dev
, __netdev_update_lower_level
,
7607 __netdev_adjacent_dev_unlink_neighbour(dev
, upper_dev
);
7613 * netdev_upper_dev_link - Add a link to the upper device
7615 * @upper_dev: new upper device
7616 * @extack: netlink extended ack
7618 * Adds a link to device which is upper to this one. The caller must hold
7619 * the RTNL lock. On a failure a negative errno code is returned.
7620 * On success the reference counts are adjusted and the function
7623 int netdev_upper_dev_link(struct net_device
*dev
,
7624 struct net_device
*upper_dev
,
7625 struct netlink_ext_ack
*extack
)
7627 return __netdev_upper_dev_link(dev
, upper_dev
, false,
7628 NULL
, NULL
, extack
);
7630 EXPORT_SYMBOL(netdev_upper_dev_link
);
7633 * netdev_master_upper_dev_link - Add a master link to the upper device
7635 * @upper_dev: new upper device
7636 * @upper_priv: upper device private
7637 * @upper_info: upper info to be passed down via notifier
7638 * @extack: netlink extended ack
7640 * Adds a link to device which is upper to this one. In this case, only
7641 * one master upper device can be linked, although other non-master devices
7642 * might be linked as well. The caller must hold the RTNL lock.
7643 * On a failure a negative errno code is returned. On success the reference
7644 * counts are adjusted and the function returns zero.
7646 int netdev_master_upper_dev_link(struct net_device
*dev
,
7647 struct net_device
*upper_dev
,
7648 void *upper_priv
, void *upper_info
,
7649 struct netlink_ext_ack
*extack
)
7651 return __netdev_upper_dev_link(dev
, upper_dev
, true,
7652 upper_priv
, upper_info
, extack
);
7654 EXPORT_SYMBOL(netdev_master_upper_dev_link
);
7657 * netdev_upper_dev_unlink - Removes a link to upper device
7659 * @upper_dev: new upper device
7661 * Removes a link to device which is upper to this one. The caller must hold
7664 void netdev_upper_dev_unlink(struct net_device
*dev
,
7665 struct net_device
*upper_dev
)
7667 struct netdev_notifier_changeupper_info changeupper_info
= {
7671 .upper_dev
= upper_dev
,
7677 changeupper_info
.master
= netdev_master_upper_dev_get(dev
) == upper_dev
;
7679 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER
,
7680 &changeupper_info
.info
);
7682 __netdev_adjacent_dev_unlink_neighbour(dev
, upper_dev
);
7684 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER
,
7685 &changeupper_info
.info
);
7687 __netdev_update_upper_level(dev
, NULL
);
7688 __netdev_walk_all_lower_dev(dev
, __netdev_update_upper_level
, NULL
);
7690 __netdev_update_lower_level(upper_dev
, NULL
);
7691 __netdev_walk_all_upper_dev(upper_dev
, __netdev_update_lower_level
,
7694 EXPORT_SYMBOL(netdev_upper_dev_unlink
);
7696 static void __netdev_adjacent_dev_set(struct net_device
*upper_dev
,
7697 struct net_device
*lower_dev
,
7700 struct netdev_adjacent
*adj
;
7702 adj
= __netdev_find_adj(lower_dev
, &upper_dev
->adj_list
.lower
);
7706 adj
= __netdev_find_adj(upper_dev
, &lower_dev
->adj_list
.upper
);
7711 static void netdev_adjacent_dev_disable(struct net_device
*upper_dev
,
7712 struct net_device
*lower_dev
)
7714 __netdev_adjacent_dev_set(upper_dev
, lower_dev
, true);
7717 static void netdev_adjacent_dev_enable(struct net_device
*upper_dev
,
7718 struct net_device
*lower_dev
)
7720 __netdev_adjacent_dev_set(upper_dev
, lower_dev
, false);
7723 int netdev_adjacent_change_prepare(struct net_device
*old_dev
,
7724 struct net_device
*new_dev
,
7725 struct net_device
*dev
,
7726 struct netlink_ext_ack
*extack
)
7733 if (old_dev
&& new_dev
!= old_dev
)
7734 netdev_adjacent_dev_disable(dev
, old_dev
);
7736 err
= netdev_upper_dev_link(new_dev
, dev
, extack
);
7738 if (old_dev
&& new_dev
!= old_dev
)
7739 netdev_adjacent_dev_enable(dev
, old_dev
);
7745 EXPORT_SYMBOL(netdev_adjacent_change_prepare
);
7747 void netdev_adjacent_change_commit(struct net_device
*old_dev
,
7748 struct net_device
*new_dev
,
7749 struct net_device
*dev
)
7751 if (!new_dev
|| !old_dev
)
7754 if (new_dev
== old_dev
)
7757 netdev_adjacent_dev_enable(dev
, old_dev
);
7758 netdev_upper_dev_unlink(old_dev
, dev
);
7760 EXPORT_SYMBOL(netdev_adjacent_change_commit
);
7762 void netdev_adjacent_change_abort(struct net_device
*old_dev
,
7763 struct net_device
*new_dev
,
7764 struct net_device
*dev
)
7769 if (old_dev
&& new_dev
!= old_dev
)
7770 netdev_adjacent_dev_enable(dev
, old_dev
);
7772 netdev_upper_dev_unlink(new_dev
, dev
);
7774 EXPORT_SYMBOL(netdev_adjacent_change_abort
);
7777 * netdev_bonding_info_change - Dispatch event about slave change
7779 * @bonding_info: info to dispatch
7781 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7782 * The caller must hold the RTNL lock.
7784 void netdev_bonding_info_change(struct net_device
*dev
,
7785 struct netdev_bonding_info
*bonding_info
)
7787 struct netdev_notifier_bonding_info info
= {
7791 memcpy(&info
.bonding_info
, bonding_info
,
7792 sizeof(struct netdev_bonding_info
));
7793 call_netdevice_notifiers_info(NETDEV_BONDING_INFO
,
7796 EXPORT_SYMBOL(netdev_bonding_info_change
);
7798 static void netdev_adjacent_add_links(struct net_device
*dev
)
7800 struct netdev_adjacent
*iter
;
7802 struct net
*net
= dev_net(dev
);
7804 list_for_each_entry(iter
, &dev
->adj_list
.upper
, list
) {
7805 if (!net_eq(net
, dev_net(iter
->dev
)))
7807 netdev_adjacent_sysfs_add(iter
->dev
, dev
,
7808 &iter
->dev
->adj_list
.lower
);
7809 netdev_adjacent_sysfs_add(dev
, iter
->dev
,
7810 &dev
->adj_list
.upper
);
7813 list_for_each_entry(iter
, &dev
->adj_list
.lower
, list
) {
7814 if (!net_eq(net
, dev_net(iter
->dev
)))
7816 netdev_adjacent_sysfs_add(iter
->dev
, dev
,
7817 &iter
->dev
->adj_list
.upper
);
7818 netdev_adjacent_sysfs_add(dev
, iter
->dev
,
7819 &dev
->adj_list
.lower
);
7823 static void netdev_adjacent_del_links(struct net_device
*dev
)
7825 struct netdev_adjacent
*iter
;
7827 struct net
*net
= dev_net(dev
);
7829 list_for_each_entry(iter
, &dev
->adj_list
.upper
, list
) {
7830 if (!net_eq(net
, dev_net(iter
->dev
)))
7832 netdev_adjacent_sysfs_del(iter
->dev
, dev
->name
,
7833 &iter
->dev
->adj_list
.lower
);
7834 netdev_adjacent_sysfs_del(dev
, iter
->dev
->name
,
7835 &dev
->adj_list
.upper
);
7838 list_for_each_entry(iter
, &dev
->adj_list
.lower
, list
) {
7839 if (!net_eq(net
, dev_net(iter
->dev
)))
7841 netdev_adjacent_sysfs_del(iter
->dev
, dev
->name
,
7842 &iter
->dev
->adj_list
.upper
);
7843 netdev_adjacent_sysfs_del(dev
, iter
->dev
->name
,
7844 &dev
->adj_list
.lower
);
7848 void netdev_adjacent_rename_links(struct net_device
*dev
, char *oldname
)
7850 struct netdev_adjacent
*iter
;
7852 struct net
*net
= dev_net(dev
);
7854 list_for_each_entry(iter
, &dev
->adj_list
.upper
, list
) {
7855 if (!net_eq(net
, dev_net(iter
->dev
)))
7857 netdev_adjacent_sysfs_del(iter
->dev
, oldname
,
7858 &iter
->dev
->adj_list
.lower
);
7859 netdev_adjacent_sysfs_add(iter
->dev
, dev
,
7860 &iter
->dev
->adj_list
.lower
);
7863 list_for_each_entry(iter
, &dev
->adj_list
.lower
, list
) {
7864 if (!net_eq(net
, dev_net(iter
->dev
)))
7866 netdev_adjacent_sysfs_del(iter
->dev
, oldname
,
7867 &iter
->dev
->adj_list
.upper
);
7868 netdev_adjacent_sysfs_add(iter
->dev
, dev
,
7869 &iter
->dev
->adj_list
.upper
);
7873 void *netdev_lower_dev_get_private(struct net_device
*dev
,
7874 struct net_device
*lower_dev
)
7876 struct netdev_adjacent
*lower
;
7880 lower
= __netdev_find_adj(lower_dev
, &dev
->adj_list
.lower
);
7884 return lower
->private;
7886 EXPORT_SYMBOL(netdev_lower_dev_get_private
);
7890 * netdev_lower_change - Dispatch event about lower device state change
7891 * @lower_dev: device
7892 * @lower_state_info: state to dispatch
7894 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7895 * The caller must hold the RTNL lock.
7897 void netdev_lower_state_changed(struct net_device
*lower_dev
,
7898 void *lower_state_info
)
7900 struct netdev_notifier_changelowerstate_info changelowerstate_info
= {
7901 .info
.dev
= lower_dev
,
7905 changelowerstate_info
.lower_state_info
= lower_state_info
;
7906 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE
,
7907 &changelowerstate_info
.info
);
7909 EXPORT_SYMBOL(netdev_lower_state_changed
);
7911 static void dev_change_rx_flags(struct net_device
*dev
, int flags
)
7913 const struct net_device_ops
*ops
= dev
->netdev_ops
;
7915 if (ops
->ndo_change_rx_flags
)
7916 ops
->ndo_change_rx_flags(dev
, flags
);
7919 static int __dev_set_promiscuity(struct net_device
*dev
, int inc
, bool notify
)
7921 unsigned int old_flags
= dev
->flags
;
7927 dev
->flags
|= IFF_PROMISC
;
7928 dev
->promiscuity
+= inc
;
7929 if (dev
->promiscuity
== 0) {
7932 * If inc causes overflow, untouch promisc and return error.
7935 dev
->flags
&= ~IFF_PROMISC
;
7937 dev
->promiscuity
-= inc
;
7938 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7943 if (dev
->flags
!= old_flags
) {
7944 pr_info("device %s %s promiscuous mode\n",
7946 dev
->flags
& IFF_PROMISC
? "entered" : "left");
7947 if (audit_enabled
) {
7948 current_uid_gid(&uid
, &gid
);
7949 audit_log(audit_context(), GFP_ATOMIC
,
7950 AUDIT_ANOM_PROMISCUOUS
,
7951 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7952 dev
->name
, (dev
->flags
& IFF_PROMISC
),
7953 (old_flags
& IFF_PROMISC
),
7954 from_kuid(&init_user_ns
, audit_get_loginuid(current
)),
7955 from_kuid(&init_user_ns
, uid
),
7956 from_kgid(&init_user_ns
, gid
),
7957 audit_get_sessionid(current
));
7960 dev_change_rx_flags(dev
, IFF_PROMISC
);
7963 __dev_notify_flags(dev
, old_flags
, IFF_PROMISC
);
7968 * dev_set_promiscuity - update promiscuity count on a device
7972 * Add or remove promiscuity from a device. While the count in the device
7973 * remains above zero the interface remains promiscuous. Once it hits zero
7974 * the device reverts back to normal filtering operation. A negative inc
7975 * value is used to drop promiscuity on the device.
7976 * Return 0 if successful or a negative errno code on error.
7978 int dev_set_promiscuity(struct net_device
*dev
, int inc
)
7980 unsigned int old_flags
= dev
->flags
;
7983 err
= __dev_set_promiscuity(dev
, inc
, true);
7986 if (dev
->flags
!= old_flags
)
7987 dev_set_rx_mode(dev
);
7990 EXPORT_SYMBOL(dev_set_promiscuity
);
7992 static int __dev_set_allmulti(struct net_device
*dev
, int inc
, bool notify
)
7994 unsigned int old_flags
= dev
->flags
, old_gflags
= dev
->gflags
;
7998 dev
->flags
|= IFF_ALLMULTI
;
7999 dev
->allmulti
+= inc
;
8000 if (dev
->allmulti
== 0) {
8003 * If inc causes overflow, untouch allmulti and return error.
8006 dev
->flags
&= ~IFF_ALLMULTI
;
8008 dev
->allmulti
-= inc
;
8009 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8014 if (dev
->flags
^ old_flags
) {
8015 dev_change_rx_flags(dev
, IFF_ALLMULTI
);
8016 dev_set_rx_mode(dev
);
8018 __dev_notify_flags(dev
, old_flags
,
8019 dev
->gflags
^ old_gflags
);
8025 * dev_set_allmulti - update allmulti count on a device
8029 * Add or remove reception of all multicast frames to a device. While the
8030 * count in the device remains above zero the interface remains listening
8031 * to all interfaces. Once it hits zero the device reverts back to normal
8032 * filtering operation. A negative @inc value is used to drop the counter
8033 * when releasing a resource needing all multicasts.
8034 * Return 0 if successful or a negative errno code on error.
8037 int dev_set_allmulti(struct net_device
*dev
, int inc
)
8039 return __dev_set_allmulti(dev
, inc
, true);
8041 EXPORT_SYMBOL(dev_set_allmulti
);
8044 * Upload unicast and multicast address lists to device and
8045 * configure RX filtering. When the device doesn't support unicast
8046 * filtering it is put in promiscuous mode while unicast addresses
8049 void __dev_set_rx_mode(struct net_device
*dev
)
8051 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8053 /* dev_open will call this function so the list will stay sane. */
8054 if (!(dev
->flags
&IFF_UP
))
8057 if (!netif_device_present(dev
))
8060 if (!(dev
->priv_flags
& IFF_UNICAST_FLT
)) {
8061 /* Unicast addresses changes may only happen under the rtnl,
8062 * therefore calling __dev_set_promiscuity here is safe.
8064 if (!netdev_uc_empty(dev
) && !dev
->uc_promisc
) {
8065 __dev_set_promiscuity(dev
, 1, false);
8066 dev
->uc_promisc
= true;
8067 } else if (netdev_uc_empty(dev
) && dev
->uc_promisc
) {
8068 __dev_set_promiscuity(dev
, -1, false);
8069 dev
->uc_promisc
= false;
8073 if (ops
->ndo_set_rx_mode
)
8074 ops
->ndo_set_rx_mode(dev
);
8077 void dev_set_rx_mode(struct net_device
*dev
)
8079 netif_addr_lock_bh(dev
);
8080 __dev_set_rx_mode(dev
);
8081 netif_addr_unlock_bh(dev
);
8085 * dev_get_flags - get flags reported to userspace
8088 * Get the combination of flag bits exported through APIs to userspace.
8090 unsigned int dev_get_flags(const struct net_device
*dev
)
8094 flags
= (dev
->flags
& ~(IFF_PROMISC
|
8099 (dev
->gflags
& (IFF_PROMISC
|
8102 if (netif_running(dev
)) {
8103 if (netif_oper_up(dev
))
8104 flags
|= IFF_RUNNING
;
8105 if (netif_carrier_ok(dev
))
8106 flags
|= IFF_LOWER_UP
;
8107 if (netif_dormant(dev
))
8108 flags
|= IFF_DORMANT
;
8113 EXPORT_SYMBOL(dev_get_flags
);
8115 int __dev_change_flags(struct net_device
*dev
, unsigned int flags
,
8116 struct netlink_ext_ack
*extack
)
8118 unsigned int old_flags
= dev
->flags
;
8124 * Set the flags on our device.
8127 dev
->flags
= (flags
& (IFF_DEBUG
| IFF_NOTRAILERS
| IFF_NOARP
|
8128 IFF_DYNAMIC
| IFF_MULTICAST
| IFF_PORTSEL
|
8130 (dev
->flags
& (IFF_UP
| IFF_VOLATILE
| IFF_PROMISC
|
8134 * Load in the correct multicast list now the flags have changed.
8137 if ((old_flags
^ flags
) & IFF_MULTICAST
)
8138 dev_change_rx_flags(dev
, IFF_MULTICAST
);
8140 dev_set_rx_mode(dev
);
8143 * Have we downed the interface. We handle IFF_UP ourselves
8144 * according to user attempts to set it, rather than blindly
8149 if ((old_flags
^ flags
) & IFF_UP
) {
8150 if (old_flags
& IFF_UP
)
8153 ret
= __dev_open(dev
, extack
);
8156 if ((flags
^ dev
->gflags
) & IFF_PROMISC
) {
8157 int inc
= (flags
& IFF_PROMISC
) ? 1 : -1;
8158 unsigned int old_flags
= dev
->flags
;
8160 dev
->gflags
^= IFF_PROMISC
;
8162 if (__dev_set_promiscuity(dev
, inc
, false) >= 0)
8163 if (dev
->flags
!= old_flags
)
8164 dev_set_rx_mode(dev
);
8167 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
8168 * is important. Some (broken) drivers set IFF_PROMISC, when
8169 * IFF_ALLMULTI is requested not asking us and not reporting.
8171 if ((flags
^ dev
->gflags
) & IFF_ALLMULTI
) {
8172 int inc
= (flags
& IFF_ALLMULTI
) ? 1 : -1;
8174 dev
->gflags
^= IFF_ALLMULTI
;
8175 __dev_set_allmulti(dev
, inc
, false);
8181 void __dev_notify_flags(struct net_device
*dev
, unsigned int old_flags
,
8182 unsigned int gchanges
)
8184 unsigned int changes
= dev
->flags
^ old_flags
;
8187 rtmsg_ifinfo(RTM_NEWLINK
, dev
, gchanges
, GFP_ATOMIC
);
8189 if (changes
& IFF_UP
) {
8190 if (dev
->flags
& IFF_UP
)
8191 call_netdevice_notifiers(NETDEV_UP
, dev
);
8193 call_netdevice_notifiers(NETDEV_DOWN
, dev
);
8196 if (dev
->flags
& IFF_UP
&&
8197 (changes
& ~(IFF_UP
| IFF_PROMISC
| IFF_ALLMULTI
| IFF_VOLATILE
))) {
8198 struct netdev_notifier_change_info change_info
= {
8202 .flags_changed
= changes
,
8205 call_netdevice_notifiers_info(NETDEV_CHANGE
, &change_info
.info
);
8210 * dev_change_flags - change device settings
8212 * @flags: device state flags
8213 * @extack: netlink extended ack
8215 * Change settings on device based state flags. The flags are
8216 * in the userspace exported format.
8218 int dev_change_flags(struct net_device
*dev
, unsigned int flags
,
8219 struct netlink_ext_ack
*extack
)
8222 unsigned int changes
, old_flags
= dev
->flags
, old_gflags
= dev
->gflags
;
8224 ret
= __dev_change_flags(dev
, flags
, extack
);
8228 changes
= (old_flags
^ dev
->flags
) | (old_gflags
^ dev
->gflags
);
8229 __dev_notify_flags(dev
, old_flags
, changes
);
8232 EXPORT_SYMBOL(dev_change_flags
);
8234 int __dev_set_mtu(struct net_device
*dev
, int new_mtu
)
8236 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8238 if (ops
->ndo_change_mtu
)
8239 return ops
->ndo_change_mtu(dev
, new_mtu
);
8241 /* Pairs with all the lockless reads of dev->mtu in the stack */
8242 WRITE_ONCE(dev
->mtu
, new_mtu
);
8245 EXPORT_SYMBOL(__dev_set_mtu
);
8247 int dev_validate_mtu(struct net_device
*dev
, int new_mtu
,
8248 struct netlink_ext_ack
*extack
)
8250 /* MTU must be positive, and in range */
8251 if (new_mtu
< 0 || new_mtu
< dev
->min_mtu
) {
8252 NL_SET_ERR_MSG(extack
, "mtu less than device minimum");
8256 if (dev
->max_mtu
> 0 && new_mtu
> dev
->max_mtu
) {
8257 NL_SET_ERR_MSG(extack
, "mtu greater than device maximum");
8264 * dev_set_mtu_ext - Change maximum transfer unit
8266 * @new_mtu: new transfer unit
8267 * @extack: netlink extended ack
8269 * Change the maximum transfer size of the network device.
8271 int dev_set_mtu_ext(struct net_device
*dev
, int new_mtu
,
8272 struct netlink_ext_ack
*extack
)
8276 if (new_mtu
== dev
->mtu
)
8279 err
= dev_validate_mtu(dev
, new_mtu
, extack
);
8283 if (!netif_device_present(dev
))
8286 err
= call_netdevice_notifiers(NETDEV_PRECHANGEMTU
, dev
);
8287 err
= notifier_to_errno(err
);
8291 orig_mtu
= dev
->mtu
;
8292 err
= __dev_set_mtu(dev
, new_mtu
);
8295 err
= call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU
, dev
,
8297 err
= notifier_to_errno(err
);
8299 /* setting mtu back and notifying everyone again,
8300 * so that they have a chance to revert changes.
8302 __dev_set_mtu(dev
, orig_mtu
);
8303 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU
, dev
,
8310 int dev_set_mtu(struct net_device
*dev
, int new_mtu
)
8312 struct netlink_ext_ack extack
;
8315 memset(&extack
, 0, sizeof(extack
));
8316 err
= dev_set_mtu_ext(dev
, new_mtu
, &extack
);
8317 if (err
&& extack
._msg
)
8318 net_err_ratelimited("%s: %s\n", dev
->name
, extack
._msg
);
8321 EXPORT_SYMBOL(dev_set_mtu
);
8324 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8326 * @new_len: new tx queue length
8328 int dev_change_tx_queue_len(struct net_device
*dev
, unsigned long new_len
)
8330 unsigned int orig_len
= dev
->tx_queue_len
;
8333 if (new_len
!= (unsigned int)new_len
)
8336 if (new_len
!= orig_len
) {
8337 dev
->tx_queue_len
= new_len
;
8338 res
= call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN
, dev
);
8339 res
= notifier_to_errno(res
);
8342 res
= dev_qdisc_change_tx_queue_len(dev
);
8350 netdev_err(dev
, "refused to change device tx_queue_len\n");
8351 dev
->tx_queue_len
= orig_len
;
8356 * dev_set_group - Change group this device belongs to
8358 * @new_group: group this device should belong to
8360 void dev_set_group(struct net_device
*dev
, int new_group
)
8362 dev
->group
= new_group
;
8364 EXPORT_SYMBOL(dev_set_group
);
8367 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8369 * @addr: new address
8370 * @extack: netlink extended ack
8372 int dev_pre_changeaddr_notify(struct net_device
*dev
, const char *addr
,
8373 struct netlink_ext_ack
*extack
)
8375 struct netdev_notifier_pre_changeaddr_info info
= {
8377 .info
.extack
= extack
,
8382 rc
= call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR
, &info
.info
);
8383 return notifier_to_errno(rc
);
8385 EXPORT_SYMBOL(dev_pre_changeaddr_notify
);
8388 * dev_set_mac_address - Change Media Access Control Address
8391 * @extack: netlink extended ack
8393 * Change the hardware (MAC) address of the device
8395 int dev_set_mac_address(struct net_device
*dev
, struct sockaddr
*sa
,
8396 struct netlink_ext_ack
*extack
)
8398 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8401 if (!ops
->ndo_set_mac_address
)
8403 if (sa
->sa_family
!= dev
->type
)
8405 if (!netif_device_present(dev
))
8407 err
= dev_pre_changeaddr_notify(dev
, sa
->sa_data
, extack
);
8410 err
= ops
->ndo_set_mac_address(dev
, sa
);
8413 dev
->addr_assign_type
= NET_ADDR_SET
;
8414 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
8415 add_device_randomness(dev
->dev_addr
, dev
->addr_len
);
8418 EXPORT_SYMBOL(dev_set_mac_address
);
8421 * dev_change_carrier - Change device carrier
8423 * @new_carrier: new value
8425 * Change device carrier
8427 int dev_change_carrier(struct net_device
*dev
, bool new_carrier
)
8429 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8431 if (!ops
->ndo_change_carrier
)
8433 if (!netif_device_present(dev
))
8435 return ops
->ndo_change_carrier(dev
, new_carrier
);
8437 EXPORT_SYMBOL(dev_change_carrier
);
8440 * dev_get_phys_port_id - Get device physical port ID
8444 * Get device physical port ID
8446 int dev_get_phys_port_id(struct net_device
*dev
,
8447 struct netdev_phys_item_id
*ppid
)
8449 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8451 if (!ops
->ndo_get_phys_port_id
)
8453 return ops
->ndo_get_phys_port_id(dev
, ppid
);
8455 EXPORT_SYMBOL(dev_get_phys_port_id
);
8458 * dev_get_phys_port_name - Get device physical port name
8461 * @len: limit of bytes to copy to name
8463 * Get device physical port name
8465 int dev_get_phys_port_name(struct net_device
*dev
,
8466 char *name
, size_t len
)
8468 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8471 if (ops
->ndo_get_phys_port_name
) {
8472 err
= ops
->ndo_get_phys_port_name(dev
, name
, len
);
8473 if (err
!= -EOPNOTSUPP
)
8476 return devlink_compat_phys_port_name_get(dev
, name
, len
);
8478 EXPORT_SYMBOL(dev_get_phys_port_name
);
8481 * dev_get_port_parent_id - Get the device's port parent identifier
8482 * @dev: network device
8483 * @ppid: pointer to a storage for the port's parent identifier
8484 * @recurse: allow/disallow recursion to lower devices
8486 * Get the devices's port parent identifier
8488 int dev_get_port_parent_id(struct net_device
*dev
,
8489 struct netdev_phys_item_id
*ppid
,
8492 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8493 struct netdev_phys_item_id first
= { };
8494 struct net_device
*lower_dev
;
8495 struct list_head
*iter
;
8498 if (ops
->ndo_get_port_parent_id
) {
8499 err
= ops
->ndo_get_port_parent_id(dev
, ppid
);
8500 if (err
!= -EOPNOTSUPP
)
8504 err
= devlink_compat_switch_id_get(dev
, ppid
);
8505 if (!err
|| err
!= -EOPNOTSUPP
)
8511 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
8512 err
= dev_get_port_parent_id(lower_dev
, ppid
, recurse
);
8517 else if (memcmp(&first
, ppid
, sizeof(*ppid
)))
8523 EXPORT_SYMBOL(dev_get_port_parent_id
);
8526 * netdev_port_same_parent_id - Indicate if two network devices have
8527 * the same port parent identifier
8528 * @a: first network device
8529 * @b: second network device
8531 bool netdev_port_same_parent_id(struct net_device
*a
, struct net_device
*b
)
8533 struct netdev_phys_item_id a_id
= { };
8534 struct netdev_phys_item_id b_id
= { };
8536 if (dev_get_port_parent_id(a
, &a_id
, true) ||
8537 dev_get_port_parent_id(b
, &b_id
, true))
8540 return netdev_phys_item_id_same(&a_id
, &b_id
);
8542 EXPORT_SYMBOL(netdev_port_same_parent_id
);
8545 * dev_change_proto_down - update protocol port state information
8547 * @proto_down: new value
8549 * This info can be used by switch drivers to set the phys state of the
8552 int dev_change_proto_down(struct net_device
*dev
, bool proto_down
)
8554 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8556 if (!ops
->ndo_change_proto_down
)
8558 if (!netif_device_present(dev
))
8560 return ops
->ndo_change_proto_down(dev
, proto_down
);
8562 EXPORT_SYMBOL(dev_change_proto_down
);
8565 * dev_change_proto_down_generic - generic implementation for
8566 * ndo_change_proto_down that sets carrier according to
8570 * @proto_down: new value
8572 int dev_change_proto_down_generic(struct net_device
*dev
, bool proto_down
)
8575 netif_carrier_off(dev
);
8577 netif_carrier_on(dev
);
8578 dev
->proto_down
= proto_down
;
8581 EXPORT_SYMBOL(dev_change_proto_down_generic
);
8583 u32
__dev_xdp_query(struct net_device
*dev
, bpf_op_t bpf_op
,
8584 enum bpf_netdev_command cmd
)
8586 struct netdev_bpf xdp
;
8591 memset(&xdp
, 0, sizeof(xdp
));
8594 /* Query must always succeed. */
8595 WARN_ON(bpf_op(dev
, &xdp
) < 0 && cmd
== XDP_QUERY_PROG
);
8600 static int dev_xdp_install(struct net_device
*dev
, bpf_op_t bpf_op
,
8601 struct netlink_ext_ack
*extack
, u32 flags
,
8602 struct bpf_prog
*prog
)
8604 bool non_hw
= !(flags
& XDP_FLAGS_HW_MODE
);
8605 struct bpf_prog
*prev_prog
= NULL
;
8606 struct netdev_bpf xdp
;
8610 prev_prog
= bpf_prog_by_id(__dev_xdp_query(dev
, bpf_op
,
8612 if (IS_ERR(prev_prog
))
8616 memset(&xdp
, 0, sizeof(xdp
));
8617 if (flags
& XDP_FLAGS_HW_MODE
)
8618 xdp
.command
= XDP_SETUP_PROG_HW
;
8620 xdp
.command
= XDP_SETUP_PROG
;
8621 xdp
.extack
= extack
;
8625 err
= bpf_op(dev
, &xdp
);
8627 bpf_prog_change_xdp(prev_prog
, prog
);
8630 bpf_prog_put(prev_prog
);
8635 static void dev_xdp_uninstall(struct net_device
*dev
)
8637 struct netdev_bpf xdp
;
8640 /* Remove generic XDP */
8641 WARN_ON(dev_xdp_install(dev
, generic_xdp_install
, NULL
, 0, NULL
));
8643 /* Remove from the driver */
8644 ndo_bpf
= dev
->netdev_ops
->ndo_bpf
;
8648 memset(&xdp
, 0, sizeof(xdp
));
8649 xdp
.command
= XDP_QUERY_PROG
;
8650 WARN_ON(ndo_bpf(dev
, &xdp
));
8652 WARN_ON(dev_xdp_install(dev
, ndo_bpf
, NULL
, xdp
.prog_flags
,
8655 /* Remove HW offload */
8656 memset(&xdp
, 0, sizeof(xdp
));
8657 xdp
.command
= XDP_QUERY_PROG_HW
;
8658 if (!ndo_bpf(dev
, &xdp
) && xdp
.prog_id
)
8659 WARN_ON(dev_xdp_install(dev
, ndo_bpf
, NULL
, xdp
.prog_flags
,
8664 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
8666 * @extack: netlink extended ack
8667 * @fd: new program fd or negative value to clear
8668 * @flags: xdp-related flags
8670 * Set or clear a bpf program for a device
8672 int dev_change_xdp_fd(struct net_device
*dev
, struct netlink_ext_ack
*extack
,
8675 const struct net_device_ops
*ops
= dev
->netdev_ops
;
8676 enum bpf_netdev_command query
;
8677 struct bpf_prog
*prog
= NULL
;
8678 bpf_op_t bpf_op
, bpf_chk
;
8684 offload
= flags
& XDP_FLAGS_HW_MODE
;
8685 query
= offload
? XDP_QUERY_PROG_HW
: XDP_QUERY_PROG
;
8687 bpf_op
= bpf_chk
= ops
->ndo_bpf
;
8688 if (!bpf_op
&& (flags
& (XDP_FLAGS_DRV_MODE
| XDP_FLAGS_HW_MODE
))) {
8689 NL_SET_ERR_MSG(extack
, "underlying driver does not support XDP in native mode");
8692 if (!bpf_op
|| (flags
& XDP_FLAGS_SKB_MODE
))
8693 bpf_op
= generic_xdp_install
;
8694 if (bpf_op
== bpf_chk
)
8695 bpf_chk
= generic_xdp_install
;
8700 if (!offload
&& __dev_xdp_query(dev
, bpf_chk
, XDP_QUERY_PROG
)) {
8701 NL_SET_ERR_MSG(extack
, "native and generic XDP can't be active at the same time");
8705 prog_id
= __dev_xdp_query(dev
, bpf_op
, query
);
8706 if ((flags
& XDP_FLAGS_UPDATE_IF_NOEXIST
) && prog_id
) {
8707 NL_SET_ERR_MSG(extack
, "XDP program already attached");
8711 prog
= bpf_prog_get_type_dev(fd
, BPF_PROG_TYPE_XDP
,
8712 bpf_op
== ops
->ndo_bpf
);
8714 return PTR_ERR(prog
);
8716 if (!offload
&& bpf_prog_is_dev_bound(prog
->aux
)) {
8717 NL_SET_ERR_MSG(extack
, "using device-bound program without HW_MODE flag is not supported");
8722 /* prog->aux->id may be 0 for orphaned device-bound progs */
8723 if (prog
->aux
->id
&& prog
->aux
->id
== prog_id
) {
8728 if (!__dev_xdp_query(dev
, bpf_op
, query
))
8732 err
= dev_xdp_install(dev
, bpf_op
, extack
, flags
, prog
);
8733 if (err
< 0 && prog
)
8740 * dev_new_index - allocate an ifindex
8741 * @net: the applicable net namespace
8743 * Returns a suitable unique value for a new device interface
8744 * number. The caller must hold the rtnl semaphore or the
8745 * dev_base_lock to be sure it remains unique.
8747 static int dev_new_index(struct net
*net
)
8749 int ifindex
= net
->ifindex
;
8754 if (!__dev_get_by_index(net
, ifindex
))
8755 return net
->ifindex
= ifindex
;
8759 /* Delayed registration/unregisteration */
8760 static LIST_HEAD(net_todo_list
);
8761 DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq
);
8763 static void net_set_todo(struct net_device
*dev
)
8765 list_add_tail(&dev
->todo_list
, &net_todo_list
);
8766 dev_net(dev
)->dev_unreg_count
++;
8769 static void rollback_registered_many(struct list_head
*head
)
8771 struct net_device
*dev
, *tmp
;
8772 LIST_HEAD(close_head
);
8774 BUG_ON(dev_boot_phase
);
8777 list_for_each_entry_safe(dev
, tmp
, head
, unreg_list
) {
8778 /* Some devices call without registering
8779 * for initialization unwind. Remove those
8780 * devices and proceed with the remaining.
8782 if (dev
->reg_state
== NETREG_UNINITIALIZED
) {
8783 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
8787 list_del(&dev
->unreg_list
);
8790 dev
->dismantle
= true;
8791 BUG_ON(dev
->reg_state
!= NETREG_REGISTERED
);
8794 /* If device is running, close it first. */
8795 list_for_each_entry(dev
, head
, unreg_list
)
8796 list_add_tail(&dev
->close_list
, &close_head
);
8797 dev_close_many(&close_head
, true);
8799 list_for_each_entry(dev
, head
, unreg_list
) {
8800 /* And unlink it from device chain. */
8801 unlist_netdevice(dev
);
8803 dev
->reg_state
= NETREG_UNREGISTERING
;
8805 flush_all_backlogs();
8809 list_for_each_entry(dev
, head
, unreg_list
) {
8810 struct sk_buff
*skb
= NULL
;
8812 /* Shutdown queueing discipline. */
8815 dev_xdp_uninstall(dev
);
8817 /* Notify protocols, that we are about to destroy
8818 * this device. They should clean all the things.
8820 call_netdevice_notifiers(NETDEV_UNREGISTER
, dev
);
8822 if (!dev
->rtnl_link_ops
||
8823 dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
)
8824 skb
= rtmsg_ifinfo_build_skb(RTM_DELLINK
, dev
, ~0U, 0,
8825 GFP_KERNEL
, NULL
, 0);
8828 * Flush the unicast and multicast chains
8833 netdev_name_node_alt_flush(dev
);
8834 netdev_name_node_free(dev
->name_node
);
8836 if (dev
->netdev_ops
->ndo_uninit
)
8837 dev
->netdev_ops
->ndo_uninit(dev
);
8840 rtmsg_ifinfo_send(skb
, dev
, GFP_KERNEL
);
8842 /* Notifier chain MUST detach us all upper devices. */
8843 WARN_ON(netdev_has_any_upper_dev(dev
));
8844 WARN_ON(netdev_has_any_lower_dev(dev
));
8846 /* Remove entries from kobject tree */
8847 netdev_unregister_kobject(dev
);
8849 /* Remove XPS queueing entries */
8850 netif_reset_xps_queues_gt(dev
, 0);
8856 list_for_each_entry(dev
, head
, unreg_list
)
8860 static void rollback_registered(struct net_device
*dev
)
8864 list_add(&dev
->unreg_list
, &single
);
8865 rollback_registered_many(&single
);
8869 static netdev_features_t
netdev_sync_upper_features(struct net_device
*lower
,
8870 struct net_device
*upper
, netdev_features_t features
)
8872 netdev_features_t upper_disables
= NETIF_F_UPPER_DISABLES
;
8873 netdev_features_t feature
;
8876 for_each_netdev_feature(upper_disables
, feature_bit
) {
8877 feature
= __NETIF_F_BIT(feature_bit
);
8878 if (!(upper
->wanted_features
& feature
)
8879 && (features
& feature
)) {
8880 netdev_dbg(lower
, "Dropping feature %pNF, upper dev %s has it off.\n",
8881 &feature
, upper
->name
);
8882 features
&= ~feature
;
8889 static void netdev_sync_lower_features(struct net_device
*upper
,
8890 struct net_device
*lower
, netdev_features_t features
)
8892 netdev_features_t upper_disables
= NETIF_F_UPPER_DISABLES
;
8893 netdev_features_t feature
;
8896 for_each_netdev_feature(upper_disables
, feature_bit
) {
8897 feature
= __NETIF_F_BIT(feature_bit
);
8898 if (!(features
& feature
) && (lower
->features
& feature
)) {
8899 netdev_dbg(upper
, "Disabling feature %pNF on lower dev %s.\n",
8900 &feature
, lower
->name
);
8901 lower
->wanted_features
&= ~feature
;
8902 netdev_update_features(lower
);
8904 if (unlikely(lower
->features
& feature
))
8905 netdev_WARN(upper
, "failed to disable %pNF on %s!\n",
8906 &feature
, lower
->name
);
8911 static netdev_features_t
netdev_fix_features(struct net_device
*dev
,
8912 netdev_features_t features
)
8914 /* Fix illegal checksum combinations */
8915 if ((features
& NETIF_F_HW_CSUM
) &&
8916 (features
& (NETIF_F_IP_CSUM
|NETIF_F_IPV6_CSUM
))) {
8917 netdev_warn(dev
, "mixed HW and IP checksum settings.\n");
8918 features
&= ~(NETIF_F_IP_CSUM
|NETIF_F_IPV6_CSUM
);
8921 /* TSO requires that SG is present as well. */
8922 if ((features
& NETIF_F_ALL_TSO
) && !(features
& NETIF_F_SG
)) {
8923 netdev_dbg(dev
, "Dropping TSO features since no SG feature.\n");
8924 features
&= ~NETIF_F_ALL_TSO
;
8927 if ((features
& NETIF_F_TSO
) && !(features
& NETIF_F_HW_CSUM
) &&
8928 !(features
& NETIF_F_IP_CSUM
)) {
8929 netdev_dbg(dev
, "Dropping TSO features since no CSUM feature.\n");
8930 features
&= ~NETIF_F_TSO
;
8931 features
&= ~NETIF_F_TSO_ECN
;
8934 if ((features
& NETIF_F_TSO6
) && !(features
& NETIF_F_HW_CSUM
) &&
8935 !(features
& NETIF_F_IPV6_CSUM
)) {
8936 netdev_dbg(dev
, "Dropping TSO6 features since no CSUM feature.\n");
8937 features
&= ~NETIF_F_TSO6
;
8940 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8941 if ((features
& NETIF_F_TSO_MANGLEID
) && !(features
& NETIF_F_TSO
))
8942 features
&= ~NETIF_F_TSO_MANGLEID
;
8944 /* TSO ECN requires that TSO is present as well. */
8945 if ((features
& NETIF_F_ALL_TSO
) == NETIF_F_TSO_ECN
)
8946 features
&= ~NETIF_F_TSO_ECN
;
8948 /* Software GSO depends on SG. */
8949 if ((features
& NETIF_F_GSO
) && !(features
& NETIF_F_SG
)) {
8950 netdev_dbg(dev
, "Dropping NETIF_F_GSO since no SG feature.\n");
8951 features
&= ~NETIF_F_GSO
;
8954 /* GSO partial features require GSO partial be set */
8955 if ((features
& dev
->gso_partial_features
) &&
8956 !(features
& NETIF_F_GSO_PARTIAL
)) {
8958 "Dropping partially supported GSO features since no GSO partial.\n");
8959 features
&= ~dev
->gso_partial_features
;
8962 if (!(features
& NETIF_F_RXCSUM
)) {
8963 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8964 * successfully merged by hardware must also have the
8965 * checksum verified by hardware. If the user does not
8966 * want to enable RXCSUM, logically, we should disable GRO_HW.
8968 if (features
& NETIF_F_GRO_HW
) {
8969 netdev_dbg(dev
, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8970 features
&= ~NETIF_F_GRO_HW
;
8974 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8975 if (features
& NETIF_F_RXFCS
) {
8976 if (features
& NETIF_F_LRO
) {
8977 netdev_dbg(dev
, "Dropping LRO feature since RX-FCS is requested.\n");
8978 features
&= ~NETIF_F_LRO
;
8981 if (features
& NETIF_F_GRO_HW
) {
8982 netdev_dbg(dev
, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8983 features
&= ~NETIF_F_GRO_HW
;
8990 int __netdev_update_features(struct net_device
*dev
)
8992 struct net_device
*upper
, *lower
;
8993 netdev_features_t features
;
8994 struct list_head
*iter
;
8999 features
= netdev_get_wanted_features(dev
);
9001 if (dev
->netdev_ops
->ndo_fix_features
)
9002 features
= dev
->netdev_ops
->ndo_fix_features(dev
, features
);
9004 /* driver might be less strict about feature dependencies */
9005 features
= netdev_fix_features(dev
, features
);
9007 /* some features can't be enabled if they're off an an upper device */
9008 netdev_for_each_upper_dev_rcu(dev
, upper
, iter
)
9009 features
= netdev_sync_upper_features(dev
, upper
, features
);
9011 if (dev
->features
== features
)
9014 netdev_dbg(dev
, "Features changed: %pNF -> %pNF\n",
9015 &dev
->features
, &features
);
9017 if (dev
->netdev_ops
->ndo_set_features
)
9018 err
= dev
->netdev_ops
->ndo_set_features(dev
, features
);
9022 if (unlikely(err
< 0)) {
9024 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9025 err
, &features
, &dev
->features
);
9026 /* return non-0 since some features might have changed and
9027 * it's better to fire a spurious notification than miss it
9033 /* some features must be disabled on lower devices when disabled
9034 * on an upper device (think: bonding master or bridge)
9036 netdev_for_each_lower_dev(dev
, lower
, iter
)
9037 netdev_sync_lower_features(dev
, lower
, features
);
9040 netdev_features_t diff
= features
^ dev
->features
;
9042 if (diff
& NETIF_F_RX_UDP_TUNNEL_PORT
) {
9043 /* udp_tunnel_{get,drop}_rx_info both need
9044 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9045 * device, or they won't do anything.
9046 * Thus we need to update dev->features
9047 * *before* calling udp_tunnel_get_rx_info,
9048 * but *after* calling udp_tunnel_drop_rx_info.
9050 if (features
& NETIF_F_RX_UDP_TUNNEL_PORT
) {
9051 dev
->features
= features
;
9052 udp_tunnel_get_rx_info(dev
);
9054 udp_tunnel_drop_rx_info(dev
);
9058 if (diff
& NETIF_F_HW_VLAN_CTAG_FILTER
) {
9059 if (features
& NETIF_F_HW_VLAN_CTAG_FILTER
) {
9060 dev
->features
= features
;
9061 err
|= vlan_get_rx_ctag_filter_info(dev
);
9063 vlan_drop_rx_ctag_filter_info(dev
);
9067 if (diff
& NETIF_F_HW_VLAN_STAG_FILTER
) {
9068 if (features
& NETIF_F_HW_VLAN_STAG_FILTER
) {
9069 dev
->features
= features
;
9070 err
|= vlan_get_rx_stag_filter_info(dev
);
9072 vlan_drop_rx_stag_filter_info(dev
);
9076 dev
->features
= features
;
9079 return err
< 0 ? 0 : 1;
9083 * netdev_update_features - recalculate device features
9084 * @dev: the device to check
9086 * Recalculate dev->features set and send notifications if it
9087 * has changed. Should be called after driver or hardware dependent
9088 * conditions might have changed that influence the features.
9090 void netdev_update_features(struct net_device
*dev
)
9092 if (__netdev_update_features(dev
))
9093 netdev_features_change(dev
);
9095 EXPORT_SYMBOL(netdev_update_features
);
9098 * netdev_change_features - recalculate device features
9099 * @dev: the device to check
9101 * Recalculate dev->features set and send notifications even
9102 * if they have not changed. Should be called instead of
9103 * netdev_update_features() if also dev->vlan_features might
9104 * have changed to allow the changes to be propagated to stacked
9107 void netdev_change_features(struct net_device
*dev
)
9109 __netdev_update_features(dev
);
9110 netdev_features_change(dev
);
9112 EXPORT_SYMBOL(netdev_change_features
);
9115 * netif_stacked_transfer_operstate - transfer operstate
9116 * @rootdev: the root or lower level device to transfer state from
9117 * @dev: the device to transfer operstate to
9119 * Transfer operational state from root to device. This is normally
9120 * called when a stacking relationship exists between the root
9121 * device and the device(a leaf device).
9123 void netif_stacked_transfer_operstate(const struct net_device
*rootdev
,
9124 struct net_device
*dev
)
9126 if (rootdev
->operstate
== IF_OPER_DORMANT
)
9127 netif_dormant_on(dev
);
9129 netif_dormant_off(dev
);
9131 if (netif_carrier_ok(rootdev
))
9132 netif_carrier_on(dev
);
9134 netif_carrier_off(dev
);
9136 EXPORT_SYMBOL(netif_stacked_transfer_operstate
);
9138 static int netif_alloc_rx_queues(struct net_device
*dev
)
9140 unsigned int i
, count
= dev
->num_rx_queues
;
9141 struct netdev_rx_queue
*rx
;
9142 size_t sz
= count
* sizeof(*rx
);
9147 rx
= kvzalloc(sz
, GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
9153 for (i
= 0; i
< count
; i
++) {
9156 /* XDP RX-queue setup */
9157 err
= xdp_rxq_info_reg(&rx
[i
].xdp_rxq
, dev
, i
);
9164 /* Rollback successful reg's and free other resources */
9166 xdp_rxq_info_unreg(&rx
[i
].xdp_rxq
);
9172 static void netif_free_rx_queues(struct net_device
*dev
)
9174 unsigned int i
, count
= dev
->num_rx_queues
;
9176 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9180 for (i
= 0; i
< count
; i
++)
9181 xdp_rxq_info_unreg(&dev
->_rx
[i
].xdp_rxq
);
9186 static void netdev_init_one_queue(struct net_device
*dev
,
9187 struct netdev_queue
*queue
, void *_unused
)
9189 /* Initialize queue lock */
9190 spin_lock_init(&queue
->_xmit_lock
);
9191 lockdep_set_class(&queue
->_xmit_lock
, &dev
->qdisc_xmit_lock_key
);
9192 queue
->xmit_lock_owner
= -1;
9193 netdev_queue_numa_node_write(queue
, NUMA_NO_NODE
);
9196 dql_init(&queue
->dql
, HZ
);
9200 static void netif_free_tx_queues(struct net_device
*dev
)
9205 static int netif_alloc_netdev_queues(struct net_device
*dev
)
9207 unsigned int count
= dev
->num_tx_queues
;
9208 struct netdev_queue
*tx
;
9209 size_t sz
= count
* sizeof(*tx
);
9211 if (count
< 1 || count
> 0xffff)
9214 tx
= kvzalloc(sz
, GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
9220 netdev_for_each_tx_queue(dev
, netdev_init_one_queue
, NULL
);
9221 spin_lock_init(&dev
->tx_global_lock
);
9226 void netif_tx_stop_all_queues(struct net_device
*dev
)
9230 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
9231 struct netdev_queue
*txq
= netdev_get_tx_queue(dev
, i
);
9233 netif_tx_stop_queue(txq
);
9236 EXPORT_SYMBOL(netif_tx_stop_all_queues
);
9238 static void netdev_register_lockdep_key(struct net_device
*dev
)
9240 lockdep_register_key(&dev
->qdisc_tx_busylock_key
);
9241 lockdep_register_key(&dev
->qdisc_running_key
);
9242 lockdep_register_key(&dev
->qdisc_xmit_lock_key
);
9243 lockdep_register_key(&dev
->addr_list_lock_key
);
9246 static void netdev_unregister_lockdep_key(struct net_device
*dev
)
9248 lockdep_unregister_key(&dev
->qdisc_tx_busylock_key
);
9249 lockdep_unregister_key(&dev
->qdisc_running_key
);
9250 lockdep_unregister_key(&dev
->qdisc_xmit_lock_key
);
9251 lockdep_unregister_key(&dev
->addr_list_lock_key
);
9254 void netdev_update_lockdep_key(struct net_device
*dev
)
9256 lockdep_unregister_key(&dev
->addr_list_lock_key
);
9257 lockdep_register_key(&dev
->addr_list_lock_key
);
9259 lockdep_set_class(&dev
->addr_list_lock
, &dev
->addr_list_lock_key
);
9261 EXPORT_SYMBOL(netdev_update_lockdep_key
);
9264 * register_netdevice - register a network device
9265 * @dev: device to register
9267 * Take a completed network device structure and add it to the kernel
9268 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9269 * chain. 0 is returned on success. A negative errno code is returned
9270 * on a failure to set up the device, or if the name is a duplicate.
9272 * Callers must hold the rtnl semaphore. You may want
9273 * register_netdev() instead of this.
9276 * The locking appears insufficient to guarantee two parallel registers
9277 * will not get the same name.
9280 int register_netdevice(struct net_device
*dev
)
9283 struct net
*net
= dev_net(dev
);
9285 BUILD_BUG_ON(sizeof(netdev_features_t
) * BITS_PER_BYTE
<
9286 NETDEV_FEATURE_COUNT
);
9287 BUG_ON(dev_boot_phase
);
9292 /* When net_device's are persistent, this will be fatal. */
9293 BUG_ON(dev
->reg_state
!= NETREG_UNINITIALIZED
);
9296 spin_lock_init(&dev
->addr_list_lock
);
9297 lockdep_set_class(&dev
->addr_list_lock
, &dev
->addr_list_lock_key
);
9299 ret
= dev_get_valid_name(net
, dev
, dev
->name
);
9304 dev
->name_node
= netdev_name_node_head_alloc(dev
);
9305 if (!dev
->name_node
)
9308 /* Init, if this function is available */
9309 if (dev
->netdev_ops
->ndo_init
) {
9310 ret
= dev
->netdev_ops
->ndo_init(dev
);
9318 if (((dev
->hw_features
| dev
->features
) &
9319 NETIF_F_HW_VLAN_CTAG_FILTER
) &&
9320 (!dev
->netdev_ops
->ndo_vlan_rx_add_vid
||
9321 !dev
->netdev_ops
->ndo_vlan_rx_kill_vid
)) {
9322 netdev_WARN(dev
, "Buggy VLAN acceleration in driver!\n");
9329 dev
->ifindex
= dev_new_index(net
);
9330 else if (__dev_get_by_index(net
, dev
->ifindex
))
9333 /* Transfer changeable features to wanted_features and enable
9334 * software offloads (GSO and GRO).
9336 dev
->hw_features
|= (NETIF_F_SOFT_FEATURES
| NETIF_F_SOFT_FEATURES_OFF
);
9337 dev
->features
|= NETIF_F_SOFT_FEATURES
;
9339 if (dev
->netdev_ops
->ndo_udp_tunnel_add
) {
9340 dev
->features
|= NETIF_F_RX_UDP_TUNNEL_PORT
;
9341 dev
->hw_features
|= NETIF_F_RX_UDP_TUNNEL_PORT
;
9344 dev
->wanted_features
= dev
->features
& dev
->hw_features
;
9346 if (!(dev
->flags
& IFF_LOOPBACK
))
9347 dev
->hw_features
|= NETIF_F_NOCACHE_COPY
;
9349 /* If IPv4 TCP segmentation offload is supported we should also
9350 * allow the device to enable segmenting the frame with the option
9351 * of ignoring a static IP ID value. This doesn't enable the
9352 * feature itself but allows the user to enable it later.
9354 if (dev
->hw_features
& NETIF_F_TSO
)
9355 dev
->hw_features
|= NETIF_F_TSO_MANGLEID
;
9356 if (dev
->vlan_features
& NETIF_F_TSO
)
9357 dev
->vlan_features
|= NETIF_F_TSO_MANGLEID
;
9358 if (dev
->mpls_features
& NETIF_F_TSO
)
9359 dev
->mpls_features
|= NETIF_F_TSO_MANGLEID
;
9360 if (dev
->hw_enc_features
& NETIF_F_TSO
)
9361 dev
->hw_enc_features
|= NETIF_F_TSO_MANGLEID
;
9363 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
9365 dev
->vlan_features
|= NETIF_F_HIGHDMA
;
9367 /* Make NETIF_F_SG inheritable to tunnel devices.
9369 dev
->hw_enc_features
|= NETIF_F_SG
| NETIF_F_GSO_PARTIAL
;
9371 /* Make NETIF_F_SG inheritable to MPLS.
9373 dev
->mpls_features
|= NETIF_F_SG
;
9375 ret
= call_netdevice_notifiers(NETDEV_POST_INIT
, dev
);
9376 ret
= notifier_to_errno(ret
);
9380 ret
= netdev_register_kobject(dev
);
9382 dev
->reg_state
= NETREG_UNREGISTERED
;
9385 dev
->reg_state
= NETREG_REGISTERED
;
9387 __netdev_update_features(dev
);
9390 * Default initial state at registry is that the
9391 * device is present.
9394 set_bit(__LINK_STATE_PRESENT
, &dev
->state
);
9396 linkwatch_init_dev(dev
);
9398 dev_init_scheduler(dev
);
9400 list_netdevice(dev
);
9401 add_device_randomness(dev
->dev_addr
, dev
->addr_len
);
9403 /* If the device has permanent device address, driver should
9404 * set dev_addr and also addr_assign_type should be set to
9405 * NET_ADDR_PERM (default value).
9407 if (dev
->addr_assign_type
== NET_ADDR_PERM
)
9408 memcpy(dev
->perm_addr
, dev
->dev_addr
, dev
->addr_len
);
9410 /* Notify protocols, that a new device appeared. */
9411 ret
= call_netdevice_notifiers(NETDEV_REGISTER
, dev
);
9412 ret
= notifier_to_errno(ret
);
9414 rollback_registered(dev
);
9417 dev
->reg_state
= NETREG_UNREGISTERED
;
9420 * Prevent userspace races by waiting until the network
9421 * device is fully setup before sending notifications.
9423 if (!dev
->rtnl_link_ops
||
9424 dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
)
9425 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U, GFP_KERNEL
);
9431 if (dev
->netdev_ops
->ndo_uninit
)
9432 dev
->netdev_ops
->ndo_uninit(dev
);
9433 if (dev
->priv_destructor
)
9434 dev
->priv_destructor(dev
);
9436 netdev_name_node_free(dev
->name_node
);
9439 EXPORT_SYMBOL(register_netdevice
);
9442 * init_dummy_netdev - init a dummy network device for NAPI
9443 * @dev: device to init
9445 * This takes a network device structure and initialize the minimum
9446 * amount of fields so it can be used to schedule NAPI polls without
9447 * registering a full blown interface. This is to be used by drivers
9448 * that need to tie several hardware interfaces to a single NAPI
9449 * poll scheduler due to HW limitations.
9451 int init_dummy_netdev(struct net_device
*dev
)
9453 /* Clear everything. Note we don't initialize spinlocks
9454 * are they aren't supposed to be taken by any of the
9455 * NAPI code and this dummy netdev is supposed to be
9456 * only ever used for NAPI polls
9458 memset(dev
, 0, sizeof(struct net_device
));
9460 /* make sure we BUG if trying to hit standard
9461 * register/unregister code path
9463 dev
->reg_state
= NETREG_DUMMY
;
9465 /* NAPI wants this */
9466 INIT_LIST_HEAD(&dev
->napi_list
);
9468 /* a dummy interface is started by default */
9469 set_bit(__LINK_STATE_PRESENT
, &dev
->state
);
9470 set_bit(__LINK_STATE_START
, &dev
->state
);
9472 /* napi_busy_loop stats accounting wants this */
9473 dev_net_set(dev
, &init_net
);
9475 /* Note : We dont allocate pcpu_refcnt for dummy devices,
9476 * because users of this 'device' dont need to change
9482 EXPORT_SYMBOL_GPL(init_dummy_netdev
);
9486 * register_netdev - register a network device
9487 * @dev: device to register
9489 * Take a completed network device structure and add it to the kernel
9490 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9491 * chain. 0 is returned on success. A negative errno code is returned
9492 * on a failure to set up the device, or if the name is a duplicate.
9494 * This is a wrapper around register_netdevice that takes the rtnl semaphore
9495 * and expands the device name if you passed a format string to
9498 int register_netdev(struct net_device
*dev
)
9502 if (rtnl_lock_killable())
9504 err
= register_netdevice(dev
);
9508 EXPORT_SYMBOL(register_netdev
);
9510 int netdev_refcnt_read(const struct net_device
*dev
)
9514 for_each_possible_cpu(i
)
9515 refcnt
+= *per_cpu_ptr(dev
->pcpu_refcnt
, i
);
9518 EXPORT_SYMBOL(netdev_refcnt_read
);
9521 * netdev_wait_allrefs - wait until all references are gone.
9522 * @dev: target net_device
9524 * This is called when unregistering network devices.
9526 * Any protocol or device that holds a reference should register
9527 * for netdevice notification, and cleanup and put back the
9528 * reference if they receive an UNREGISTER event.
9529 * We can get stuck here if buggy protocols don't correctly
9532 static void netdev_wait_allrefs(struct net_device
*dev
)
9534 unsigned long rebroadcast_time
, warning_time
;
9537 linkwatch_forget_dev(dev
);
9539 rebroadcast_time
= warning_time
= jiffies
;
9540 refcnt
= netdev_refcnt_read(dev
);
9542 while (refcnt
!= 0) {
9543 if (time_after(jiffies
, rebroadcast_time
+ 1 * HZ
)) {
9546 /* Rebroadcast unregister notification */
9547 call_netdevice_notifiers(NETDEV_UNREGISTER
, dev
);
9553 if (test_bit(__LINK_STATE_LINKWATCH_PENDING
,
9555 /* We must not have linkwatch events
9556 * pending on unregister. If this
9557 * happens, we simply run the queue
9558 * unscheduled, resulting in a noop
9561 linkwatch_run_queue();
9566 rebroadcast_time
= jiffies
;
9571 refcnt
= netdev_refcnt_read(dev
);
9573 if (refcnt
&& time_after(jiffies
, warning_time
+ 10 * HZ
)) {
9574 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9576 warning_time
= jiffies
;
9585 * register_netdevice(x1);
9586 * register_netdevice(x2);
9588 * unregister_netdevice(y1);
9589 * unregister_netdevice(y2);
9595 * We are invoked by rtnl_unlock().
9596 * This allows us to deal with problems:
9597 * 1) We can delete sysfs objects which invoke hotplug
9598 * without deadlocking with linkwatch via keventd.
9599 * 2) Since we run with the RTNL semaphore not held, we can sleep
9600 * safely in order to wait for the netdev refcnt to drop to zero.
9602 * We must not return until all unregister events added during
9603 * the interval the lock was held have been completed.
9605 void netdev_run_todo(void)
9607 struct list_head list
;
9609 /* Snapshot list, allow later requests */
9610 list_replace_init(&net_todo_list
, &list
);
9615 /* Wait for rcu callbacks to finish before next phase */
9616 if (!list_empty(&list
))
9619 while (!list_empty(&list
)) {
9620 struct net_device
*dev
9621 = list_first_entry(&list
, struct net_device
, todo_list
);
9622 list_del(&dev
->todo_list
);
9624 if (unlikely(dev
->reg_state
!= NETREG_UNREGISTERING
)) {
9625 pr_err("network todo '%s' but state %d\n",
9626 dev
->name
, dev
->reg_state
);
9631 dev
->reg_state
= NETREG_UNREGISTERED
;
9633 netdev_wait_allrefs(dev
);
9636 BUG_ON(netdev_refcnt_read(dev
));
9637 BUG_ON(!list_empty(&dev
->ptype_all
));
9638 BUG_ON(!list_empty(&dev
->ptype_specific
));
9639 WARN_ON(rcu_access_pointer(dev
->ip_ptr
));
9640 WARN_ON(rcu_access_pointer(dev
->ip6_ptr
));
9641 #if IS_ENABLED(CONFIG_DECNET)
9642 WARN_ON(dev
->dn_ptr
);
9644 if (dev
->priv_destructor
)
9645 dev
->priv_destructor(dev
);
9646 if (dev
->needs_free_netdev
)
9649 /* Report a network device has been unregistered */
9651 dev_net(dev
)->dev_unreg_count
--;
9653 wake_up(&netdev_unregistering_wq
);
9655 /* Free network device */
9656 kobject_put(&dev
->dev
.kobj
);
9660 /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
9661 * all the same fields in the same order as net_device_stats, with only
9662 * the type differing, but rtnl_link_stats64 may have additional fields
9663 * at the end for newer counters.
9665 void netdev_stats_to_stats64(struct rtnl_link_stats64
*stats64
,
9666 const struct net_device_stats
*netdev_stats
)
9668 #if BITS_PER_LONG == 64
9669 BUILD_BUG_ON(sizeof(*stats64
) < sizeof(*netdev_stats
));
9670 memcpy(stats64
, netdev_stats
, sizeof(*netdev_stats
));
9671 /* zero out counters that only exist in rtnl_link_stats64 */
9672 memset((char *)stats64
+ sizeof(*netdev_stats
), 0,
9673 sizeof(*stats64
) - sizeof(*netdev_stats
));
9675 size_t i
, n
= sizeof(*netdev_stats
) / sizeof(unsigned long);
9676 const unsigned long *src
= (const unsigned long *)netdev_stats
;
9677 u64
*dst
= (u64
*)stats64
;
9679 BUILD_BUG_ON(n
> sizeof(*stats64
) / sizeof(u64
));
9680 for (i
= 0; i
< n
; i
++)
9682 /* zero out counters that only exist in rtnl_link_stats64 */
9683 memset((char *)stats64
+ n
* sizeof(u64
), 0,
9684 sizeof(*stats64
) - n
* sizeof(u64
));
9687 EXPORT_SYMBOL(netdev_stats_to_stats64
);
9690 * dev_get_stats - get network device statistics
9691 * @dev: device to get statistics from
9692 * @storage: place to store stats
9694 * Get network statistics from device. Return @storage.
9695 * The device driver may provide its own method by setting
9696 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
9697 * otherwise the internal statistics structure is used.
9699 struct rtnl_link_stats64
*dev_get_stats(struct net_device
*dev
,
9700 struct rtnl_link_stats64
*storage
)
9702 const struct net_device_ops
*ops
= dev
->netdev_ops
;
9704 if (ops
->ndo_get_stats64
) {
9705 memset(storage
, 0, sizeof(*storage
));
9706 ops
->ndo_get_stats64(dev
, storage
);
9707 } else if (ops
->ndo_get_stats
) {
9708 netdev_stats_to_stats64(storage
, ops
->ndo_get_stats(dev
));
9710 netdev_stats_to_stats64(storage
, &dev
->stats
);
9712 storage
->rx_dropped
+= (unsigned long)atomic_long_read(&dev
->rx_dropped
);
9713 storage
->tx_dropped
+= (unsigned long)atomic_long_read(&dev
->tx_dropped
);
9714 storage
->rx_nohandler
+= (unsigned long)atomic_long_read(&dev
->rx_nohandler
);
9717 EXPORT_SYMBOL(dev_get_stats
);
9719 struct netdev_queue
*dev_ingress_queue_create(struct net_device
*dev
)
9721 struct netdev_queue
*queue
= dev_ingress_queue(dev
);
9723 #ifdef CONFIG_NET_CLS_ACT
9726 queue
= kzalloc(sizeof(*queue
), GFP_KERNEL
);
9729 netdev_init_one_queue(dev
, queue
, NULL
);
9730 RCU_INIT_POINTER(queue
->qdisc
, &noop_qdisc
);
9731 queue
->qdisc_sleeping
= &noop_qdisc
;
9732 rcu_assign_pointer(dev
->ingress_queue
, queue
);
9737 static const struct ethtool_ops default_ethtool_ops
;
9739 void netdev_set_default_ethtool_ops(struct net_device
*dev
,
9740 const struct ethtool_ops
*ops
)
9742 if (dev
->ethtool_ops
== &default_ethtool_ops
)
9743 dev
->ethtool_ops
= ops
;
9745 EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops
);
9747 void netdev_freemem(struct net_device
*dev
)
9749 char *addr
= (char *)dev
- dev
->padded
;
9755 * alloc_netdev_mqs - allocate network device
9756 * @sizeof_priv: size of private data to allocate space for
9757 * @name: device name format string
9758 * @name_assign_type: origin of device name
9759 * @setup: callback to initialize device
9760 * @txqs: the number of TX subqueues to allocate
9761 * @rxqs: the number of RX subqueues to allocate
9763 * Allocates a struct net_device with private data area for driver use
9764 * and performs basic initialization. Also allocates subqueue structs
9765 * for each queue on the device.
9767 struct net_device
*alloc_netdev_mqs(int sizeof_priv
, const char *name
,
9768 unsigned char name_assign_type
,
9769 void (*setup
)(struct net_device
*),
9770 unsigned int txqs
, unsigned int rxqs
)
9772 struct net_device
*dev
;
9773 unsigned int alloc_size
;
9774 struct net_device
*p
;
9776 BUG_ON(strlen(name
) >= sizeof(dev
->name
));
9779 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
9784 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
9788 alloc_size
= sizeof(struct net_device
);
9790 /* ensure 32-byte alignment of private area */
9791 alloc_size
= ALIGN(alloc_size
, NETDEV_ALIGN
);
9792 alloc_size
+= sizeof_priv
;
9794 /* ensure 32-byte alignment of whole construct */
9795 alloc_size
+= NETDEV_ALIGN
- 1;
9797 p
= kvzalloc(alloc_size
, GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
9801 dev
= PTR_ALIGN(p
, NETDEV_ALIGN
);
9802 dev
->padded
= (char *)dev
- (char *)p
;
9804 dev
->pcpu_refcnt
= alloc_percpu(int);
9805 if (!dev
->pcpu_refcnt
)
9808 if (dev_addr_init(dev
))
9814 dev_net_set(dev
, &init_net
);
9816 netdev_register_lockdep_key(dev
);
9818 dev
->gso_max_size
= GSO_MAX_SIZE
;
9819 dev
->gso_max_segs
= GSO_MAX_SEGS
;
9820 dev
->upper_level
= 1;
9821 dev
->lower_level
= 1;
9823 INIT_LIST_HEAD(&dev
->napi_list
);
9824 INIT_LIST_HEAD(&dev
->unreg_list
);
9825 INIT_LIST_HEAD(&dev
->close_list
);
9826 INIT_LIST_HEAD(&dev
->link_watch_list
);
9827 INIT_LIST_HEAD(&dev
->adj_list
.upper
);
9828 INIT_LIST_HEAD(&dev
->adj_list
.lower
);
9829 INIT_LIST_HEAD(&dev
->ptype_all
);
9830 INIT_LIST_HEAD(&dev
->ptype_specific
);
9831 INIT_LIST_HEAD(&dev
->net_notifier_list
);
9832 #ifdef CONFIG_NET_SCHED
9833 hash_init(dev
->qdisc_hash
);
9835 dev
->priv_flags
= IFF_XMIT_DST_RELEASE
| IFF_XMIT_DST_RELEASE_PERM
;
9838 if (!dev
->tx_queue_len
) {
9839 dev
->priv_flags
|= IFF_NO_QUEUE
;
9840 dev
->tx_queue_len
= DEFAULT_TX_QUEUE_LEN
;
9843 dev
->num_tx_queues
= txqs
;
9844 dev
->real_num_tx_queues
= txqs
;
9845 if (netif_alloc_netdev_queues(dev
))
9848 dev
->num_rx_queues
= rxqs
;
9849 dev
->real_num_rx_queues
= rxqs
;
9850 if (netif_alloc_rx_queues(dev
))
9853 strcpy(dev
->name
, name
);
9854 dev
->name_assign_type
= name_assign_type
;
9855 dev
->group
= INIT_NETDEV_GROUP
;
9856 if (!dev
->ethtool_ops
)
9857 dev
->ethtool_ops
= &default_ethtool_ops
;
9859 nf_hook_ingress_init(dev
);
9868 free_percpu(dev
->pcpu_refcnt
);
9870 netdev_freemem(dev
);
9873 EXPORT_SYMBOL(alloc_netdev_mqs
);
9876 * free_netdev - free network device
9879 * This function does the last stage of destroying an allocated device
9880 * interface. The reference to the device object is released. If this
9881 * is the last reference then it will be freed.Must be called in process
9884 void free_netdev(struct net_device
*dev
)
9886 struct napi_struct
*p
, *n
;
9889 netif_free_tx_queues(dev
);
9890 netif_free_rx_queues(dev
);
9892 kfree(rcu_dereference_protected(dev
->ingress_queue
, 1));
9894 /* Flush device addresses */
9895 dev_addr_flush(dev
);
9897 list_for_each_entry_safe(p
, n
, &dev
->napi_list
, dev_list
)
9900 free_percpu(dev
->pcpu_refcnt
);
9901 dev
->pcpu_refcnt
= NULL
;
9902 free_percpu(dev
->xdp_bulkq
);
9903 dev
->xdp_bulkq
= NULL
;
9905 netdev_unregister_lockdep_key(dev
);
9907 /* Compatibility with error handling in drivers */
9908 if (dev
->reg_state
== NETREG_UNINITIALIZED
) {
9909 netdev_freemem(dev
);
9913 BUG_ON(dev
->reg_state
!= NETREG_UNREGISTERED
);
9914 dev
->reg_state
= NETREG_RELEASED
;
9916 /* will free via device release */
9917 put_device(&dev
->dev
);
9919 EXPORT_SYMBOL(free_netdev
);
9922 * synchronize_net - Synchronize with packet receive processing
9924 * Wait for packets currently being received to be done.
9925 * Does not block later packets from starting.
9927 void synchronize_net(void)
9930 if (rtnl_is_locked())
9931 synchronize_rcu_expedited();
9935 EXPORT_SYMBOL(synchronize_net
);
9938 * unregister_netdevice_queue - remove device from the kernel
9942 * This function shuts down a device interface and removes it
9943 * from the kernel tables.
9944 * If head not NULL, device is queued to be unregistered later.
9946 * Callers must hold the rtnl semaphore. You may want
9947 * unregister_netdev() instead of this.
9950 void unregister_netdevice_queue(struct net_device
*dev
, struct list_head
*head
)
9955 list_move_tail(&dev
->unreg_list
, head
);
9957 rollback_registered(dev
);
9958 /* Finish processing unregister after unlock */
9962 EXPORT_SYMBOL(unregister_netdevice_queue
);
9965 * unregister_netdevice_many - unregister many devices
9966 * @head: list of devices
9968 * Note: As most callers use a stack allocated list_head,
9969 * we force a list_del() to make sure stack wont be corrupted later.
9971 void unregister_netdevice_many(struct list_head
*head
)
9973 struct net_device
*dev
;
9975 if (!list_empty(head
)) {
9976 rollback_registered_many(head
);
9977 list_for_each_entry(dev
, head
, unreg_list
)
9982 EXPORT_SYMBOL(unregister_netdevice_many
);
9985 * unregister_netdev - remove device from the kernel
9988 * This function shuts down a device interface and removes it
9989 * from the kernel tables.
9991 * This is just a wrapper for unregister_netdevice that takes
9992 * the rtnl semaphore. In general you want to use this and not
9993 * unregister_netdevice.
9995 void unregister_netdev(struct net_device
*dev
)
9998 unregister_netdevice(dev
);
10001 EXPORT_SYMBOL(unregister_netdev
);
10004 * dev_change_net_namespace - move device to different nethost namespace
10006 * @net: network namespace
10007 * @pat: If not NULL name pattern to try if the current device name
10008 * is already taken in the destination network namespace.
10010 * This function shuts down a device interface and moves it
10011 * to a new network namespace. On success 0 is returned, on
10012 * a failure a netagive errno code is returned.
10014 * Callers must hold the rtnl semaphore.
10017 int dev_change_net_namespace(struct net_device
*dev
, struct net
*net
, const char *pat
)
10019 int err
, new_nsid
, new_ifindex
;
10023 /* Don't allow namespace local devices to be moved. */
10025 if (dev
->features
& NETIF_F_NETNS_LOCAL
)
10028 /* Ensure the device has been registrered */
10029 if (dev
->reg_state
!= NETREG_REGISTERED
)
10032 /* Get out if there is nothing todo */
10034 if (net_eq(dev_net(dev
), net
))
10037 /* Pick the destination device name, and ensure
10038 * we can use it in the destination network namespace.
10041 if (__dev_get_by_name(net
, dev
->name
)) {
10042 /* We get here if we can't use the current device name */
10045 err
= dev_get_valid_name(net
, dev
, pat
);
10051 * And now a mini version of register_netdevice unregister_netdevice.
10054 /* If device is running close it first. */
10057 /* And unlink it from device chain */
10058 unlist_netdevice(dev
);
10062 /* Shutdown queueing discipline. */
10065 /* Notify protocols, that we are about to destroy
10066 * this device. They should clean all the things.
10068 * Note that dev->reg_state stays at NETREG_REGISTERED.
10069 * This is wanted because this way 8021q and macvlan know
10070 * the device is just moving and can keep their slaves up.
10072 call_netdevice_notifiers(NETDEV_UNREGISTER
, dev
);
10075 new_nsid
= peernet2id_alloc(dev_net(dev
), net
, GFP_KERNEL
);
10076 /* If there is an ifindex conflict assign a new one */
10077 if (__dev_get_by_index(net
, dev
->ifindex
))
10078 new_ifindex
= dev_new_index(net
);
10080 new_ifindex
= dev
->ifindex
;
10082 rtmsg_ifinfo_newnet(RTM_DELLINK
, dev
, ~0U, GFP_KERNEL
, &new_nsid
,
10086 * Flush the unicast and multicast chains
10091 /* Send a netdev-removed uevent to the old namespace */
10092 kobject_uevent(&dev
->dev
.kobj
, KOBJ_REMOVE
);
10093 netdev_adjacent_del_links(dev
);
10095 /* Move per-net netdevice notifiers that are following the netdevice */
10096 move_netdevice_notifiers_dev_net(dev
, net
);
10098 /* Actually switch the network namespace */
10099 dev_net_set(dev
, net
);
10100 dev
->ifindex
= new_ifindex
;
10102 /* Send a netdev-add uevent to the new namespace */
10103 kobject_uevent(&dev
->dev
.kobj
, KOBJ_ADD
);
10104 netdev_adjacent_add_links(dev
);
10106 /* Fixup kobjects */
10107 err
= device_rename(&dev
->dev
, dev
->name
);
10110 /* Add the device back in the hashes */
10111 list_netdevice(dev
);
10113 /* Notify protocols, that a new device appeared. */
10114 call_netdevice_notifiers(NETDEV_REGISTER
, dev
);
10117 * Prevent userspace races by waiting until the network
10118 * device is fully setup before sending notifications.
10120 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U, GFP_KERNEL
);
10127 EXPORT_SYMBOL_GPL(dev_change_net_namespace
);
10129 static int dev_cpu_dead(unsigned int oldcpu
)
10131 struct sk_buff
**list_skb
;
10132 struct sk_buff
*skb
;
10134 struct softnet_data
*sd
, *oldsd
, *remsd
= NULL
;
10136 local_irq_disable();
10137 cpu
= smp_processor_id();
10138 sd
= &per_cpu(softnet_data
, cpu
);
10139 oldsd
= &per_cpu(softnet_data
, oldcpu
);
10141 /* Find end of our completion_queue. */
10142 list_skb
= &sd
->completion_queue
;
10144 list_skb
= &(*list_skb
)->next
;
10145 /* Append completion queue from offline CPU. */
10146 *list_skb
= oldsd
->completion_queue
;
10147 oldsd
->completion_queue
= NULL
;
10149 /* Append output queue from offline CPU. */
10150 if (oldsd
->output_queue
) {
10151 *sd
->output_queue_tailp
= oldsd
->output_queue
;
10152 sd
->output_queue_tailp
= oldsd
->output_queue_tailp
;
10153 oldsd
->output_queue
= NULL
;
10154 oldsd
->output_queue_tailp
= &oldsd
->output_queue
;
10156 /* Append NAPI poll list from offline CPU, with one exception :
10157 * process_backlog() must be called by cpu owning percpu backlog.
10158 * We properly handle process_queue & input_pkt_queue later.
10160 while (!list_empty(&oldsd
->poll_list
)) {
10161 struct napi_struct
*napi
= list_first_entry(&oldsd
->poll_list
,
10162 struct napi_struct
,
10165 list_del_init(&napi
->poll_list
);
10166 if (napi
->poll
== process_backlog
)
10169 ____napi_schedule(sd
, napi
);
10172 raise_softirq_irqoff(NET_TX_SOFTIRQ
);
10173 local_irq_enable();
10176 remsd
= oldsd
->rps_ipi_list
;
10177 oldsd
->rps_ipi_list
= NULL
;
10179 /* send out pending IPI's on offline CPU */
10180 net_rps_send_ipi(remsd
);
10182 /* Process offline CPU's input_pkt_queue */
10183 while ((skb
= __skb_dequeue(&oldsd
->process_queue
))) {
10185 input_queue_head_incr(oldsd
);
10187 while ((skb
= skb_dequeue(&oldsd
->input_pkt_queue
))) {
10189 input_queue_head_incr(oldsd
);
10196 * netdev_increment_features - increment feature set by one
10197 * @all: current feature set
10198 * @one: new feature set
10199 * @mask: mask feature set
10201 * Computes a new feature set after adding a device with feature set
10202 * @one to the master device with current feature set @all. Will not
10203 * enable anything that is off in @mask. Returns the new feature set.
10205 netdev_features_t
netdev_increment_features(netdev_features_t all
,
10206 netdev_features_t one
, netdev_features_t mask
)
10208 if (mask
& NETIF_F_HW_CSUM
)
10209 mask
|= NETIF_F_CSUM_MASK
;
10210 mask
|= NETIF_F_VLAN_CHALLENGED
;
10212 all
|= one
& (NETIF_F_ONE_FOR_ALL
| NETIF_F_CSUM_MASK
) & mask
;
10213 all
&= one
| ~NETIF_F_ALL_FOR_ALL
;
10215 /* If one device supports hw checksumming, set for all. */
10216 if (all
& NETIF_F_HW_CSUM
)
10217 all
&= ~(NETIF_F_CSUM_MASK
& ~NETIF_F_HW_CSUM
);
10221 EXPORT_SYMBOL(netdev_increment_features
);
10223 static struct hlist_head
* __net_init
netdev_create_hash(void)
10226 struct hlist_head
*hash
;
10228 hash
= kmalloc_array(NETDEV_HASHENTRIES
, sizeof(*hash
), GFP_KERNEL
);
10230 for (i
= 0; i
< NETDEV_HASHENTRIES
; i
++)
10231 INIT_HLIST_HEAD(&hash
[i
]);
10236 /* Initialize per network namespace state */
10237 static int __net_init
netdev_init(struct net
*net
)
10239 BUILD_BUG_ON(GRO_HASH_BUCKETS
>
10240 8 * sizeof_field(struct napi_struct
, gro_bitmask
));
10242 if (net
!= &init_net
)
10243 INIT_LIST_HEAD(&net
->dev_base_head
);
10245 net
->dev_name_head
= netdev_create_hash();
10246 if (net
->dev_name_head
== NULL
)
10249 net
->dev_index_head
= netdev_create_hash();
10250 if (net
->dev_index_head
== NULL
)
10253 RAW_INIT_NOTIFIER_HEAD(&net
->netdev_chain
);
10258 kfree(net
->dev_name_head
);
10264 * netdev_drivername - network driver for the device
10265 * @dev: network device
10267 * Determine network driver for device.
10269 const char *netdev_drivername(const struct net_device
*dev
)
10271 const struct device_driver
*driver
;
10272 const struct device
*parent
;
10273 const char *empty
= "";
10275 parent
= dev
->dev
.parent
;
10279 driver
= parent
->driver
;
10280 if (driver
&& driver
->name
)
10281 return driver
->name
;
10285 static void __netdev_printk(const char *level
, const struct net_device
*dev
,
10286 struct va_format
*vaf
)
10288 if (dev
&& dev
->dev
.parent
) {
10289 dev_printk_emit(level
[1] - '0',
10292 dev_driver_string(dev
->dev
.parent
),
10293 dev_name(dev
->dev
.parent
),
10294 netdev_name(dev
), netdev_reg_state(dev
),
10297 printk("%s%s%s: %pV",
10298 level
, netdev_name(dev
), netdev_reg_state(dev
), vaf
);
10300 printk("%s(NULL net_device): %pV", level
, vaf
);
10304 void netdev_printk(const char *level
, const struct net_device
*dev
,
10305 const char *format
, ...)
10307 struct va_format vaf
;
10310 va_start(args
, format
);
10315 __netdev_printk(level
, dev
, &vaf
);
10319 EXPORT_SYMBOL(netdev_printk
);
10321 #define define_netdev_printk_level(func, level) \
10322 void func(const struct net_device *dev, const char *fmt, ...) \
10324 struct va_format vaf; \
10327 va_start(args, fmt); \
10332 __netdev_printk(level, dev, &vaf); \
10336 EXPORT_SYMBOL(func);
10338 define_netdev_printk_level(netdev_emerg
, KERN_EMERG
);
10339 define_netdev_printk_level(netdev_alert
, KERN_ALERT
);
10340 define_netdev_printk_level(netdev_crit
, KERN_CRIT
);
10341 define_netdev_printk_level(netdev_err
, KERN_ERR
);
10342 define_netdev_printk_level(netdev_warn
, KERN_WARNING
);
10343 define_netdev_printk_level(netdev_notice
, KERN_NOTICE
);
10344 define_netdev_printk_level(netdev_info
, KERN_INFO
);
10346 static void __net_exit
netdev_exit(struct net
*net
)
10348 kfree(net
->dev_name_head
);
10349 kfree(net
->dev_index_head
);
10350 if (net
!= &init_net
)
10351 WARN_ON_ONCE(!list_empty(&net
->dev_base_head
));
10354 static struct pernet_operations __net_initdata netdev_net_ops
= {
10355 .init
= netdev_init
,
10356 .exit
= netdev_exit
,
10359 static void __net_exit
default_device_exit(struct net
*net
)
10361 struct net_device
*dev
, *aux
;
10363 * Push all migratable network devices back to the
10364 * initial network namespace
10367 for_each_netdev_safe(net
, dev
, aux
) {
10369 char fb_name
[IFNAMSIZ
];
10371 /* Ignore unmoveable devices (i.e. loopback) */
10372 if (dev
->features
& NETIF_F_NETNS_LOCAL
)
10375 /* Leave virtual devices for the generic cleanup */
10376 if (dev
->rtnl_link_ops
)
10379 /* Push remaining network devices to init_net */
10380 snprintf(fb_name
, IFNAMSIZ
, "dev%d", dev
->ifindex
);
10381 if (__dev_get_by_name(&init_net
, fb_name
))
10382 snprintf(fb_name
, IFNAMSIZ
, "dev%%d");
10383 err
= dev_change_net_namespace(dev
, &init_net
, fb_name
);
10385 pr_emerg("%s: failed to move %s to init_net: %d\n",
10386 __func__
, dev
->name
, err
);
10393 static void __net_exit
rtnl_lock_unregistering(struct list_head
*net_list
)
10395 /* Return with the rtnl_lock held when there are no network
10396 * devices unregistering in any network namespace in net_list.
10399 bool unregistering
;
10400 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
10402 add_wait_queue(&netdev_unregistering_wq
, &wait
);
10404 unregistering
= false;
10406 list_for_each_entry(net
, net_list
, exit_list
) {
10407 if (net
->dev_unreg_count
> 0) {
10408 unregistering
= true;
10412 if (!unregistering
)
10416 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
10418 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
10421 static void __net_exit
default_device_exit_batch(struct list_head
*net_list
)
10423 /* At exit all network devices most be removed from a network
10424 * namespace. Do this in the reverse order of registration.
10425 * Do this across as many network namespaces as possible to
10426 * improve batching efficiency.
10428 struct net_device
*dev
;
10430 LIST_HEAD(dev_kill_list
);
10432 /* To prevent network device cleanup code from dereferencing
10433 * loopback devices or network devices that have been freed
10434 * wait here for all pending unregistrations to complete,
10435 * before unregistring the loopback device and allowing the
10436 * network namespace be freed.
10438 * The netdev todo list containing all network devices
10439 * unregistrations that happen in default_device_exit_batch
10440 * will run in the rtnl_unlock() at the end of
10441 * default_device_exit_batch.
10443 rtnl_lock_unregistering(net_list
);
10444 list_for_each_entry(net
, net_list
, exit_list
) {
10445 for_each_netdev_reverse(net
, dev
) {
10446 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->dellink
)
10447 dev
->rtnl_link_ops
->dellink(dev
, &dev_kill_list
);
10449 unregister_netdevice_queue(dev
, &dev_kill_list
);
10452 unregister_netdevice_many(&dev_kill_list
);
10456 static struct pernet_operations __net_initdata default_device_ops
= {
10457 .exit
= default_device_exit
,
10458 .exit_batch
= default_device_exit_batch
,
10462 * Initialize the DEV module. At boot time this walks the device list and
10463 * unhooks any devices that fail to initialise (normally hardware not
10464 * present) and leaves us with a valid list of present and active devices.
10469 * This is called single threaded during boot, so no need
10470 * to take the rtnl semaphore.
10472 static int __init
net_dev_init(void)
10474 int i
, rc
= -ENOMEM
;
10476 BUG_ON(!dev_boot_phase
);
10478 if (dev_proc_init())
10481 if (netdev_kobject_init())
10484 INIT_LIST_HEAD(&ptype_all
);
10485 for (i
= 0; i
< PTYPE_HASH_SIZE
; i
++)
10486 INIT_LIST_HEAD(&ptype_base
[i
]);
10488 INIT_LIST_HEAD(&offload_base
);
10490 if (register_pernet_subsys(&netdev_net_ops
))
10494 * Initialise the packet receive queues.
10497 for_each_possible_cpu(i
) {
10498 struct work_struct
*flush
= per_cpu_ptr(&flush_works
, i
);
10499 struct softnet_data
*sd
= &per_cpu(softnet_data
, i
);
10501 INIT_WORK(flush
, flush_backlog
);
10503 skb_queue_head_init(&sd
->input_pkt_queue
);
10504 skb_queue_head_init(&sd
->process_queue
);
10505 #ifdef CONFIG_XFRM_OFFLOAD
10506 skb_queue_head_init(&sd
->xfrm_backlog
);
10508 INIT_LIST_HEAD(&sd
->poll_list
);
10509 sd
->output_queue_tailp
= &sd
->output_queue
;
10511 sd
->csd
.func
= rps_trigger_softirq
;
10516 init_gro_hash(&sd
->backlog
);
10517 sd
->backlog
.poll
= process_backlog
;
10518 sd
->backlog
.weight
= weight_p
;
10521 dev_boot_phase
= 0;
10523 /* The loopback device is special if any other network devices
10524 * is present in a network namespace the loopback device must
10525 * be present. Since we now dynamically allocate and free the
10526 * loopback device ensure this invariant is maintained by
10527 * keeping the loopback device as the first device on the
10528 * list of network devices. Ensuring the loopback devices
10529 * is the first device that appears and the last network device
10532 if (register_pernet_device(&loopback_net_ops
))
10535 if (register_pernet_device(&default_device_ops
))
10538 open_softirq(NET_TX_SOFTIRQ
, net_tx_action
);
10539 open_softirq(NET_RX_SOFTIRQ
, net_rx_action
);
10541 rc
= cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD
, "net/dev:dead",
10542 NULL
, dev_cpu_dead
);
10549 subsys_initcall(net_dev_init
);